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