]> arthur.barton.de Git - netdata.git/blob - python.d/python-modules-installer.sh.in
fixed typo
[netdata.git] / python.d / python-modules-installer.sh.in
1 #!/bin/bash
2
3 umask 022
4
5 dir="@pythondir_POST@"
6 target="${dir}/python_modules"
7 pv="$(python -V 2>&1)"
8
9 # parse parameters
10 while [ ! -z "${1}" ]
11 do
12     case "${1}" in
13     -p|--python)
14         pv="Python ${2}"
15         shift 2
16         ;;
17
18     -d|--dir)
19         dir="${2}"
20         target="${dir}/python_modules"
21         echo >&2 "Will install python modules in: '${target}'"
22         shift 2
23         ;;
24
25     -s|--system)
26         target=
27         echo >&2 "Will install python modules system-wide"
28         shift
29         ;;
30
31     -h|--help)
32         echo "${0} [--dir netdata-python.d-path] [--system]"
33         exit 0
34         ;;
35
36     *)
37         echo >&2 "Cannot understand parameter: ${1}"
38         exit 1
39         ;;
40     esac
41 done
42
43
44 if [ ! -z "${target}" -a ! -d "${target}" ]
45 then
46     echo >&2 "Cannot find directory: '${target}'"
47     exit 1
48 fi
49
50 if [[ "${pv}" =~ ^Python\ 2.* ]]
51 then
52     pv=2
53     pip="$(which pip2 2>/dev/null)"
54 elif [[ "${pv}" =~ ^Python\ 3.* ]]
55 then
56     pv=3
57     pip="$(which pip3 2>/dev/null)"
58 else
59     echo >&2 "Cannot detect python version"
60     exit 1
61 fi
62
63 [ -z "${pip}" ] && pip="$(which pip 2>/dev/null)"
64 if [ -z "${pip}" ]
65 then
66     echo >&2 "pip command is required to install python v${pv} modules"
67     exit 1
68 fi
69
70 echo >&2 "Working for python version ${pv} (pip command: '${pip}')"
71 echo >&2 "Installing netdata python modules in: '${target}'"
72
73 run() {
74     printf "Running command:\n# "
75     printf "%q " "${@}"
76     printf "\n"
77     "${@}"
78 }
79
80 # try to install all the python modules given as parameters
81 # until the first that succeeds
82 failed=""
83 installed=""
84 errors=0
85 pip_install() {
86     local ret x
87
88     echo >&2
89     echo >&2
90     echo >&2 "Installing one of: ${*}"
91
92     for x in "${@}"
93     do
94         echo >&2
95         echo >&2 "attempting to install: ${x}"
96         if [ ! -z "${target}" ]
97         then
98             run "${pip}" install --target "${target}" "${x}"
99             ret=$?
100         else
101             run "${pip}" install "${x}"
102             ret=$?
103         fi
104         [ ${ret} -eq 0 ] && break
105         echo >&2 "failed to install: ${x}"
106     done
107
108     if [ ${ret} -ne 0 ]
109     then
110         echo >&2 "ERROR: could not install any of: ${*}"
111         errors=$(( errors + 1 ))
112         failed="${failed}|${*}"
113     else
114         echo >&2 "SUCCESS: we have: ${x}"
115         installed="${installed} ${x}"
116     fi
117     return ${ret}
118 }
119
120 if [ "${pv}" = "2" ]
121 then
122     pip_install pyyaml yaml
123     pip_install mysqlclient mysql-python pymysql
124 else
125     pip_install yaml pyyaml
126     pip_install mysql-python mysqlclient pymysql
127 fi
128
129 echo >&2
130 echo >&2
131 if [ ${errors} -ne 0 ]
132 then
133     echo >&2 "Failed to install ${errors} modules: ${failed}"
134     exit 1
135 else
136     echo >&2 "All done. We have: ${installed}"
137     exit 0
138 fi