1 # no shebang necessary - this is a library to be sourced
3 # -----------------------------------------------------------------------------
4 # checking the availability of commands
7 which "${1}" 2>/dev/null || \
8 command -v "${1}" 2>/dev/null
12 which_cmd "${1}" >/dev/null 2>&1 && return 0
17 # -----------------------------------------------------------------------------
46 # Is stderr on the terminal? If not, then fail
51 if [ $(( $(tput colors 2>/dev/null) )) -ge 8 ]
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)"
87 echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
90 # -----------------------------------------------------------------------------
94 l2=" |.-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-" \
95 l3=" | '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' " \
96 l4=" +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->" \
98 netdata="netdata" start end msg="${*}" chartcolor="${TPUT_DIM}"
100 [ ${#msg} -lt ${#netdata} ] && msg="${msg}${sp:0:$(( ${#netdata} - ${#msg}))}"
101 [ ${#msg} -gt $(( ${#l2} - 20 )) ] && msg="${msg:0:$(( ${#l2} - 23 ))}..."
103 start="$(( ${#l2} / 2 - 4 ))"
104 [ $(( start + ${#msg} + 4 )) -gt ${#l2} ] && start=$((${#l2} - ${#msg} - 4))
105 end=$(( ${start} + ${#msg} + 4 ))
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}"
115 # -----------------------------------------------------------------------------
116 # portable service command
118 service_cmd="$(which_cmd service)"
119 systemctl_cmd="$(which_cmd systemctl)"
121 local cmd="${1}" action="${2}"
123 if [ ! -z "${service_cmd}" ]
125 run "${service_cmd}" "${cmd}" "${action}"
127 elif [ ! -z "${systemctl_cmd}" ]
129 run "${systemctl_cmd}" "${action}" "${cmd}"
135 # -----------------------------------------------------------------------------
138 printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
142 printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
145 run_logfile="/dev/null"
147 local user="${USER}" dir="$(basename "${PWD}")" info info_console
149 if [ "${UID}" = "0" ]
151 info="[root ${dir}]# "
152 info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
154 info="[${user} ${dir}]$ "
155 info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
158 printf >> "${run_logfile}" "${info}"
159 printf >> "${run_logfile}" "%q " "${@}"
160 printf >> "${run_logfile}" " ... "
162 printf >&2 "${info_console}${TPUT_BOLD}${TPUT_YELLOW}"
163 printf >&2 "%q " "${@}"
164 printf >&2 "${TPUT_RESET}\n"
172 printf >> "${run_logfile}" "FAILED with exit code ${ret}\n"
175 printf >> "${run_logfile}" "OK\n"
181 portable_add_user() {
182 local username="${1}"
184 getent passwd "${username}" > /dev/null 2>&1
185 [ $? -eq 0 ] && echo >&2 "User '${username}' already exists." && return 0
187 echo >&2 "Adding ${username} user account ..."
189 local nologin="$(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false')"
194 run useradd -r -g "${username}" -c "${username}" -s "${nologin}" -d / "${username}" && return 0
200 run pw useradd "${username}" -d / -g "${username}" -s "${nologin}" && return 0
206 run adduser -D -G "${username}" "${username}" && return 0
209 echo >&2 "Failed to add ${username} user account !"
214 portable_add_group() {
215 local groupname="${1}"
217 getent group "${groupname}" > /dev/null 2>&1
218 [ $? -eq 0 ] && echo >&2 "Group '${groupname}' already exists." && return 0
220 echo >&2 "Adding ${groupname} user group ..."
223 if check_cmd groupadd
225 run groupadd -r "${groupname}" && return 0
231 run pw groupadd "${groupname}" && return 0
235 if check_cmd addgroup
237 run addgroup "${groupname}" && return 0
240 echo >&2 "Failed to add ${groupname} user group !"
244 portable_add_user_to_group() {
245 local groupname="${1}" username="${2}"
247 getent group "${groupname}" > /dev/null 2>&1
248 [ $? -ne 0 ] && echo >&2 "Group '${groupname}' does not exist." && return 1
250 # find the user is already in the group
251 local users=$(getent group "${groupname}" | cut -d ':' -f 4)
252 if [[ ",${users}," =~ ,${username}, ]]
254 # username is already there
255 echo >&2 "User '${username}' is already in group '${groupname}'."
258 # username is not in group
259 echo >&2 "Adding ${username} user to the ${groupname} group ..."
264 run usermod -a -G "${groupname}" "${username}" && return 0
270 run pw groupmod "${groupname}" -m "${username}" && return 0
274 if check_cmd addgroup
276 run addgroup "${username}" "${groupname}" && return 0
279 echo >&2 "Failed to add user ${username} to group ${groupname} !"
285 # man systemd-detect-virt
286 local cmd=$(which_cmd systemd-detect-virt)
287 if [ ! -z "${cmd}" -a -x "${cmd}" ]
289 "${cmd}" --container >/dev/null 2>&1 && return 0
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; } )
296 [ ${pid} -ne 1 ] && return 0
298 # lxc sets environment variable 'container'
299 [ ! -z "${container}" ] && return 0
301 # docker creates /.dockerenv
302 # http://stackoverflow.com/a/25518345
303 [ -f "/.dockerenv" ] && return 0
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" ]
309 "/bin/running-in-container" >/dev/null 2>&1 && return 0
316 local pids p myns ns systemctl
318 # if the directory /etc/systemd/system does not exit, it is not systemd
319 [ ! -d /etc/systemd/system ] && return 1
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
325 # if pid 1 is systemd, it is systemd
326 [ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0
328 # if systemd is not running, it is not systemd
329 pids=$(pidof systemd 2>/dev/null)
330 [ -z "${pids}" ] && return 1
332 # check if the running systemd processes are not in our namespace
333 myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
336 ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
338 # if pid of systemd is in our namespace, it is systemd
339 [ ! -z "${myns}" && "${myns}" = "${ns}" ] && return 0
342 # else, it is not systemd