]> arthur.barton.de Git - netdata.git/blob - installer/functions.sh
ab-debian 0.20170327.01-0ab1, upstream v1.6.0-42-gaa6b96fc
[netdata.git] / installer / functions.sh
1 # no shebang necessary - this is a library to be sourced
2
3 # -----------------------------------------------------------------------------
4 # checking the availability of commands
5
6 which_cmd() {
7     which "${1}" 2>/dev/null || \
8         command -v "${1}" 2>/dev/null
9 }
10
11 check_cmd() {
12     which_cmd "${1}" >/dev/null 2>&1 && return 0
13     return 1
14 }
15
16
17 # -----------------------------------------------------------------------------
18
19 setup_terminal() {
20     TPUT_RESET=""
21     TPUT_BLACK=""
22     TPUT_RED=""
23     TPUT_GREEN=""
24     TPUT_YELLOW=""
25     TPUT_BLUE=""
26     TPUT_PURPLE=""
27     TPUT_CYAN=""
28     TPUT_WHITE=""
29     TPUT_BGBLACK=""
30     TPUT_BGRED=""
31     TPUT_BGGREEN=""
32     TPUT_BGYELLOW=""
33     TPUT_BGBLUE=""
34     TPUT_BGPURPLE=""
35     TPUT_BGCYAN=""
36     TPUT_BGWHITE=""
37     TPUT_BOLD=""
38     TPUT_DIM=""
39     TPUT_UNDERLINED=""
40     TPUT_BLINK=""
41     TPUT_INVERTED=""
42     TPUT_STANDOUT=""
43     TPUT_BELL=""
44     TPUT_CLEAR=""
45
46     # Is stderr on the terminal? If not, then fail
47     test -t 2 || return 1
48
49     if check_cmd tput
50     then
51         if [ $(( $(tput colors 2>/dev/null) )) -ge 8 ]
52         then
53             # Enable colors
54             TPUT_RESET="$(tput sgr 0)"
55             TPUT_BLACK="$(tput setaf 0)"
56             TPUT_RED="$(tput setaf 1)"
57             TPUT_GREEN="$(tput setaf 2)"
58             TPUT_YELLOW="$(tput setaf 3)"
59             TPUT_BLUE="$(tput setaf 4)"
60             TPUT_PURPLE="$(tput setaf 5)"
61             TPUT_CYAN="$(tput setaf 6)"
62             TPUT_WHITE="$(tput setaf 7)"
63             TPUT_BGBLACK="$(tput setab 0)"
64             TPUT_BGRED="$(tput setab 1)"
65             TPUT_BGGREEN="$(tput setab 2)"
66             TPUT_BGYELLOW="$(tput setab 3)"
67             TPUT_BGBLUE="$(tput setab 4)"
68             TPUT_BGPURPLE="$(tput setab 5)"
69             TPUT_BGCYAN="$(tput setab 6)"
70             TPUT_BGWHITE="$(tput setab 7)"
71             TPUT_BOLD="$(tput bold)"
72             TPUT_DIM="$(tput dim)"
73             TPUT_UNDERLINED="$(tput smul)"
74             TPUT_BLINK="$(tput blink)"
75             TPUT_INVERTED="$(tput rev)"
76             TPUT_STANDOUT="$(tput smso)"
77             TPUT_BELL="$(tput bel)"
78             TPUT_CLEAR="$(tput clear)"
79         fi
80     fi
81
82     return 0
83 }
84 setup_terminal
85
86 progress() {
87     echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
88 }
89
90 # -----------------------------------------------------------------------------
91
92 netdata_banner() {
93     local   l1="  ^"                                                                            \
94             l2="  |.-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-"  \
95             l3="  |   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'  "  \
96             l4="  +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->" \
97             sp="                                                                              " \
98             netdata="netdata" start end msg="${*}" chartcolor="${TPUT_DIM}"
99
100     [ ${#msg} -lt ${#netdata} ] && msg="${msg}${sp:0:$(( ${#netdata} - ${#msg}))}"
101     [ ${#msg} -gt $(( ${#l2} - 20 )) ] && msg="${msg:0:$(( ${#l2} - 23 ))}..."
102
103     start="$(( ${#l2} / 2 - 4 ))"
104     [ $(( start + ${#msg} + 4 )) -gt ${#l2} ] && start=$((${#l2} - ${#msg} - 4))
105     end=$(( ${start} + ${#msg} + 4 ))
106
107     echo >&2
108     echo >&2 "${chartcolor}${l1}${TPUT_RESET}"
109     echo >&2 "${chartcolor}${l2:0:start}${sp:0:2}${TPUT_RESET}${TPUT_BOLD}${TPUT_GREEN}${netdata}${TPUT_RESET}${chartcolor}${sp:0:$((end - start - 2 - ${#netdata}))}${l2:end:$((${#l2} - end))}${TPUT_RESET}"
110     echo >&2 "${chartcolor}${l3:0:start}${sp:0:2}${TPUT_RESET}${TPUT_BOLD}${TPUT_CYAN}${msg}${TPUT_RESET}${chartcolor}${sp:0:2}${l3:end:$((${#l2} - end))}${TPUT_RESET}"
111     echo >&2 "${chartcolor}${l4}${TPUT_RESET}"
112     echo >&2
113 }
114
115 # -----------------------------------------------------------------------------
116 # portable service command
117
118 service_cmd="$(which_cmd service)"
119 systemctl_cmd="$(which_cmd systemctl)"
120 service() {
121     local cmd="${1}" action="${2}"
122
123     if [ ! -z "${service_cmd}" ]
124     then
125         run "${service_cmd}" "${cmd}" "${action}"
126         return $?
127     elif [ ! -z "${systemctl_cmd}" ]
128     then
129         run "${systemctl_cmd}" "${action}" "${cmd}"
130         return $?
131     fi
132     return 1
133 }
134
135 # -----------------------------------------------------------------------------
136
137 run_ok() {
138     printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
139 }
140
141 run_failed() {
142     printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
143 }
144
145 run_logfile="/dev/null"
146 run() {
147     local user="${USER}" dir="$(basename "${PWD}")" info info_console
148
149     if [ "${UID}" = "0" ]
150         then
151         info="[root ${dir}]# "
152         info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
153     else
154         info="[${user} ${dir}]$ "
155         info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
156     fi
157
158     printf >> "${run_logfile}" "${info}"
159     printf >> "${run_logfile}" "%q " "${@}"
160     printf >> "${run_logfile}" " ... "
161
162     printf >&2 "${info_console}${TPUT_BOLD}${TPUT_YELLOW}"
163     printf >&2 "%q " "${@}"
164     printf >&2 "${TPUT_RESET}\n"
165
166     "${@}"
167
168     local ret=$?
169     if [ ${ret} -ne 0 ]
170         then
171         run_failed
172         printf >> "${run_logfile}" "FAILED with exit code ${ret}\n"
173     else
174         run_ok
175         printf >> "${run_logfile}" "OK\n"
176     fi
177
178     return ${ret}
179 }
180
181 portable_add_user() {
182     local username="${1}"
183
184     getent passwd "${username}" > /dev/null 2>&1
185     [ $? -eq 0 ] && echo >&2 "User '${username}' already exists." && return 0
186
187     echo >&2 "Adding ${username} user account ..."
188
189     local nologin="$(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false')"
190
191     # Linux
192     if check_cmd useradd
193     then
194         run useradd -r -g "${username}" -c "${username}" -s "${nologin}" -d / "${username}" && return 0
195     fi
196
197     # FreeBSD
198     if check_cmd pw
199     then
200         run pw useradd "${username}" -d / -g "${username}" -s "${nologin}" && return 0
201     fi
202
203     # BusyBox
204     if check_cmd adduser
205     then
206         run adduser -D -G "${username}" "${username}" && return 0
207     fi
208
209     echo >&2 "Failed to add ${username} user account !"
210
211     return 1
212 }
213
214 portable_add_group() {
215     local groupname="${1}"
216
217     getent group "${groupname}" > /dev/null 2>&1
218     [ $? -eq 0 ] && echo >&2 "Group '${groupname}' already exists." && return 0
219
220     echo >&2 "Adding ${groupname} user group ..."
221
222     # Linux
223     if check_cmd groupadd
224     then
225         run groupadd -r "${groupname}" && return 0
226     fi
227
228     # FreeBSD
229     if check_cmd pw
230     then
231         run pw groupadd "${groupname}" && return 0
232     fi
233
234     # BusyBox
235     if check_cmd addgroup
236     then
237         run addgroup "${groupname}" && return 0
238     fi
239
240     echo >&2 "Failed to add ${groupname} user group !"
241     return 1
242 }
243
244 portable_add_user_to_group() {
245     local groupname="${1}" username="${2}"
246
247     getent group "${groupname}" > /dev/null 2>&1
248     [ $? -ne 0 ] && echo >&2 "Group '${groupname}' does not exist." && return 1
249
250     # find the user is already in the group
251     local users=$(getent group "${groupname}" | cut -d ':' -f 4)
252     if [[ ",${users}," =~ ,${username}, ]]
253         then
254         # username is already there
255         echo >&2 "User '${username}' is already in group '${groupname}'."
256         return 0
257     else
258         # username is not in group
259         echo >&2 "Adding ${username} user to the ${groupname} group ..."
260
261         # Linux
262         if check_cmd usermod
263         then
264             run usermod -a -G "${groupname}" "${username}" && return 0
265         fi
266
267         # FreeBSD
268         if check_cmd pw
269         then
270             run pw groupmod "${groupname}" -m "${username}" && return 0
271         fi
272
273         # BusyBox
274         if check_cmd addgroup
275         then
276             run addgroup "${username}" "${groupname}" && return 0
277         fi
278
279         echo >&2 "Failed to add user ${username} to group ${groupname} !"
280         return 1
281     fi
282 }
283
284 iscontainer() {
285     # man systemd-detect-virt
286     local cmd=$(which_cmd systemd-detect-virt)
287     if [ ! -z "${cmd}" -a -x "${cmd}" ]
288         then
289         "${cmd}" --container >/dev/null 2>&1 && return 0
290     fi
291
292     # /proc/1/sched exposes the host's pid of our init !
293     # http://stackoverflow.com/a/37016302
294     local pid=$( cat /proc/1/sched 2>/dev/null | head -n 1 | { IFS='(),#:' read name pid th threads; echo $pid; } )
295     pid=$(( pid + 0 ))
296     [ ${pid} -ne 1 ] && return 0
297
298     # lxc sets environment variable 'container'
299     [ ! -z "${container}" ] && return 0
300
301     # docker creates /.dockerenv
302     # http://stackoverflow.com/a/25518345
303     [ -f "/.dockerenv" ] && return 0
304
305     # ubuntu and debian supply /bin/running-in-container
306     # https://www.apt-browse.org/browse/ubuntu/trusty/main/i386/upstart/1.12.1-0ubuntu4/file/bin/running-in-container
307     if [ -x "/bin/running-in-container" ]
308         then
309         "/bin/running-in-container" >/dev/null 2>&1 && return 0
310     fi
311
312     return 1
313 }
314
315 issystemd() {
316     local pids p myns ns systemctl
317
318     # if the directory /etc/systemd/system does not exit, it is not systemd
319     [ ! -d /etc/systemd/system ] && return 1
320
321     # if there is no systemctl command, it is not systemd
322     systemctl=$(which systemctl 2>/dev/null || command -v systemctl 2>/dev/null)
323     [ -z "${systemctl}" -o ! -x "${systemctl}" ] && return 1
324
325     # if pid 1 is systemd, it is systemd
326     [ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0
327
328     # if systemd is not running, it is not systemd
329     pids=$(pidof systemd 2>/dev/null)
330     [ -z "${pids}" ] && return 1
331
332     # check if the running systemd processes are not in our namespace
333     myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
334     for p in ${pids}
335     do
336         ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
337
338         # if pid of systemd is in our namespace, it is systemd
339         [ ! -z "${myns}" && "${myns}" = "${ns}" ] && return 0
340     done
341
342     # else, it is not systemd
343     return 1
344 }