summaryrefslogtreecommitdiffstats
path: root/python.d/python-modules-installer.sh
blob: cda3c6662f9807a1fba5df6f0b7e3e52c89e893d (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env bash

umask 022

dir="/usr/local/libexec/netdata/python.d"
target="${dir}/python_modules"
pv="$(python -V 2>&1)"

# parse parameters
while [ ! -z "${1}" ]
do
    case "${1}" in
    -p|--python)
        pv="Python ${2}"
        shift 2
        ;;

    -d|--dir)
        dir="${2}"
        target="${dir}/python_modules"
        echo >&2 "Will install python modules in: '${target}'"
        shift 2
        ;;

    -s|--system)
        target=
        echo >&2 "Will install python modules system-wide"
        shift
        ;;

    -h|--help)
        echo "${0} [--dir netdata-python.d-path] [--system]"
        echo "Please make sure you have installed packages: python-pip (or python3-pip) python-dev libyaml-dev libmysqlclient-dev"
        exit 0
        ;;

    *)
        echo >&2 "Cannot understand parameter: ${1}"
        exit 1
        ;;
    esac
done


if [ ! -z "${target}" -a ! -d "${target}" ]
then
    echo >&2 "Cannot find directory: '${target}'"
    exit 1
fi

if [[ "${pv}" =~ ^Python\ 2.* ]]
then
    pv=2
    pip="$(which pip2 2>/dev/null)"
elif [[ "${pv}" =~ ^Python\ 3.* ]]
then
    pv=3
    pip="$(which pip3 2>/dev/null)"
else
    echo >&2 "Cannot detect python version. Is python installed?"
    exit 1
fi

[ -z "${pip}" ] && pip="$(which pip 2>/dev/null)"
if [ -z "${pip}" ]
then
    echo >&2 "pip command is required to install python v${pv} modules."
    [ "${pv}" = "2" ] && echo >&2 "Please install python-pip."
    [ "${pv}" = "3" ] && echo >&2 "Please install python3-pip."
    exit 1
fi

echo >&2 "Working for python version ${pv} (pip command: '${pip}')"
echo >&2 "Installing netdata python modules in: '${target}'"

run() {
    printf "Running command:\n# "
    printf "%q " "${@}"
    printf "\n"
    "${@}"
}

# try to install all the python modules given as parameters
# until the first that succeeds
failed=""
installed=""
errors=0
pip_install() {
    local ret x msg="${1}"
    shift

    echo >&2
    echo >&2
    echo >&2 "Installing one of: ${*}"

    for x in "${@}"
    do
        echo >&2
        echo >&2 "attempting to install: ${x}"
        if [ ! -z "${target}" ]
        then
            run "${pip}" install --target "${target}" "${x}"
            ret=$?
        else
            run "${pip}" install "${x}"
            ret=$?
        fi
        [ ${ret} -eq 0 ] && break
        echo >&2 "failed to install: ${x}. ${msg}"
    done

    if [ ${ret} -ne 0 ]
    then
        echo >&2
        echo >&2
        echo >&2 "FAILED: could not install any of: ${*}. ${msg}"
        echo >&2
        echo >&2
        errors=$(( errors + 1 ))
        failed="${failed}|${*}"
    else
        echo >&2
        echo >&2
        echo >&2 "SUCCESS: we have: ${x}"
        echo >&2
        echo >&2
        installed="${installed} ${x}"
    fi
    return ${ret}
}

if [ "${pv}" = "2" ]
then
    pip_install "is libyaml-dev and python-dev installed?" pyyaml
    pip_install "is libmysqlclient-dev and python-dev installed?" mysqlclient mysql-python pymysql
else
    pip_install "is libyaml-dev and python-dev installed?" pyyaml
    pip_install "is libmysqlclient-dev and python-dev installed?" mysql-python mysqlclient pymysql
fi

echo >&2
echo >&2
if [ ${errors} -ne 0 ]
then
    echo >&2 "Failed to install ${errors} modules: ${failed}"
    if [ ! -z "${target}" ]
    then
		echo >&2
		echo >&2 "If you are getting errors during cleanup from pip, there is a known bug"
		echo >&2 "in certain versions of pip that prevents installing packages local to an"
		echo >&2 "application. To install them system-wide please run:"
		echo >&2 "$0 --system"
	fi
    exit 1
else
    echo >&2 "All done. We have: ${installed}"
    exit 0
fi