]> arthur.barton.de Git - netdata.git/blob - python.d/python-modules-installer.sh.in
Merge pull request #1744 from l2isbad/web_log_plugin
[netdata.git] / python.d / python-modules-installer.sh.in
1 #!/usr/bin/env 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         echo "Please make sure you have installed packages: python-pip (or python3-pip) python-dev libyaml-dev libmysqlclient-dev"
34         exit 0
35         ;;
36
37     *)
38         echo >&2 "Cannot understand parameter: ${1}"
39         exit 1
40         ;;
41     esac
42 done
43
44
45 if [ ! -z "${target}" -a ! -d "${target}" ]
46 then
47     echo >&2 "Cannot find directory: '${target}'"
48     exit 1
49 fi
50
51 if [[ "${pv}" =~ ^Python\ 2.* ]]
52 then
53     pv=2
54     pip="$(which pip2 2>/dev/null)"
55 elif [[ "${pv}" =~ ^Python\ 3.* ]]
56 then
57     pv=3
58     pip="$(which pip3 2>/dev/null)"
59 else
60     echo >&2 "Cannot detect python version. Is python installed?"
61     exit 1
62 fi
63
64 [ -z "${pip}" ] && pip="$(which pip 2>/dev/null)"
65 if [ -z "${pip}" ]
66 then
67     echo >&2 "pip command is required to install python v${pv} modules."
68     [ "${pv}" = "2" ] && echo >&2 "Please install python-pip."
69     [ "${pv}" = "3" ] && echo >&2 "Please install python3-pip."
70     exit 1
71 fi
72
73 echo >&2 "Working for python version ${pv} (pip command: '${pip}')"
74 echo >&2 "Installing netdata python modules in: '${target}'"
75
76 run() {
77     printf "Running command:\n# "
78     printf "%q " "${@}"
79     printf "\n"
80     "${@}"
81 }
82
83 # try to install all the python modules given as parameters
84 # until the first that succeeds
85 failed=""
86 installed=""
87 errors=0
88 pip_install() {
89     local ret x msg="${1}"
90     shift
91
92     echo >&2
93     echo >&2
94     echo >&2 "Installing one of: ${*}"
95
96     for x in "${@}"
97     do
98         echo >&2
99         echo >&2 "attempting to install: ${x}"
100         if [ ! -z "${target}" ]
101         then
102             run "${pip}" install --target "${target}" "${x}"
103             ret=$?
104         else
105             run "${pip}" install "${x}"
106             ret=$?
107         fi
108         [ ${ret} -eq 0 ] && break
109         echo >&2 "failed to install: ${x}. ${msg}"
110     done
111
112     if [ ${ret} -ne 0 ]
113     then
114         echo >&2
115         echo >&2
116         echo >&2 "FAILED: could not install any of: ${*}. ${msg}"
117         echo >&2
118         echo >&2
119         errors=$(( errors + 1 ))
120         failed="${failed}|${*}"
121     else
122         echo >&2
123         echo >&2
124         echo >&2 "SUCCESS: we have: ${x}"
125         echo >&2
126         echo >&2
127         installed="${installed} ${x}"
128     fi
129     return ${ret}
130 }
131
132 if [ "${pv}" = "2" ]
133 then
134     pip_install "is libyaml-dev and python-dev installed?" pyyaml
135     pip_install "is libmysqlclient-dev and python-dev installed?" mysqlclient mysql-python pymysql
136 else
137     pip_install "is libyaml-dev and python-dev installed?" pyyaml
138     pip_install "is libmysqlclient-dev and python-dev installed?" mysql-python mysqlclient pymysql
139 fi
140
141 echo >&2
142 echo >&2
143 if [ ${errors} -ne 0 ]
144 then
145     echo >&2 "Failed to install ${errors} modules: ${failed}"
146     if [ ! -z "${target}" ]
147     then
148                 echo >&2
149                 echo >&2 "If you are getting errors during cleanup from pip, there is a known bug"
150                 echo >&2 "in certain versions of pip that prevents installing packages local to an"
151                 echo >&2 "application. To install them system-wide please run:"
152                 echo >&2 "$0 --system"
153         fi
154     exit 1
155 else
156     echo >&2 "All done. We have: ${installed}"
157     exit 0
158 fi