forked from Azure/azure-linux-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension_shim.sh
executable file
·114 lines (97 loc) · 2.57 KB
/
extension_shim.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
# Keeping the default command
COMMAND=""
PYTHON=""
USAGE="$(basename "$0") [-h] [-i|--install] [-u|--uninstall] [-d|--disable] [-e|--enable] [-p|--update]
Program to find the installed python on the box and invoke a Python extension script.
where:
-h|--help show this help text
-i|--install install the extension
-u|--uninstall uninstall the extension
-d|--disable disable the extension
-e|--enable enable the extension
-p|--update update the extension
-c|--command command to run
example:
# Install usage
$ bash extension_shim.sh -i
python ./vmaccess.py -install
# Custom executable python file
$ bash extension_shim.sh -c ""hello.py"" -i
python hello.py -install
# Custom executable python file with arguments
$ bash extension_shim.sh -c ""hello.py --install""
python hello.py --install
"
function find_python(){
local python_exec_command=$1
# Check if there is python defined.
if command -v python >/dev/null 2>&1 ; then
eval ${python_exec_command}="python"
else
# Python was not found. Searching for Python3 now.
if command -v python3 >/dev/null 2>&1 ; then
eval ${python_exec_command}="python3"
fi
fi
}
# Transform long options to short ones for getopts support (getopts doesn't support long args)
for arg in "$@"; do
shift
case "$arg" in
"--help") set -- "$@" "-h" ;;
"--install") set -- "$@" "-i" ;;
"--update") set -- "$@" "-p" ;;
"--enable") set -- "$@" "-e" ;;
"--disable") set -- "$@" "-d" ;;
"--uninstall") set -- "$@" "-u" ;;
*) set -- "$@" "$arg"
esac
done
if [ -z "$arg" ]
then
echo "$USAGE" >&2
exit 1
fi
# Get the arguments
while getopts "iudephc:?" o; do
case "${o}" in
h|\?)
echo "$USAGE"
exit 0
;;
i)
operation="-install"
;;
u)
operation="-uninstall"
;;
d)
operation="-disable"
;;
e)
operation="-enable"
;;
p)
operation="-update"
;;
c)
COMMAND="$OPTARG"
;;
*)
echo "$USAGE" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
# If find_python is not able to find a python installed, $PYTHON will be null.
find_python PYTHON
if [ -z "$PYTHON" ]; then
echo "No Python interpreter found on the box" >&2
exit 51 # Not Supported
else
echo `${PYTHON} --version`
fi
${PYTHON} ${COMMAND} ${operation}
# DONE