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 # -----------------------------------------------------------------------------
20 # Is stderr on the terminal? If not, then fail
23 if [ ! -z "$TPUT_CMD" ]
25 if [ $[$($TPUT_CMD colors 2>/dev/null)] -ge 8 ]
37 COLOR_BGBLACK="\e[40m"
39 COLOR_BGGREEN="\e[42m"
40 COLOR_BGYELLOW="\e[43m"
42 COLOR_BGPURPLE="\e[45m"
44 COLOR_BGWHITE="\e[47m"
47 COLOR_UNDERLINED="\e[4m"
49 COLOR_INVERTED="\e[7m"
55 TPUT_CMD="$(which_cmd tput)"
59 printf >&2 " --- ${COLOR_DIM}${COLOR_BOLD}${*}${COLOR_RESET} --- \n"
62 # -----------------------------------------------------------------------------
66 l2=" |.-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-" \
67 l3=" | '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' " \
68 l4=" +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->" \
70 netdata="netdata" start end msg="${*}" chartcolor="${COLOR_DIM}"
72 [ ${#msg} -lt ${#netdata} ] && msg="${msg}${sp:0:$(( ${#netdata} - ${#msg}))}"
73 [ ${#msg} -gt $(( ${#l2} - 20 )) ] && msg="${msg:0:$(( ${#l2} - 23 ))}..."
75 start="$(( ${#l2} / 2 - 4 ))"
76 [ $(( start + ${#msg} + 4 )) -gt ${#l2} ] && start=$((${#l2} - ${#msg} - 4))
77 end=$(( ${start} + ${#msg} + 4 ))
80 echo >&2 -e "${chartcolor}${l1}${COLOR_RESET}"
81 echo >&2 -e "${chartcolor}${l2:0:start}${sp:0:2}${COLOR_RESET}${COLOR_BOLD}${COLOR_GREEN}${netdata}${COLOR_RESET}${chartcolor}${sp:0:$((end - start - 2 - ${#netdata}))}${l2:end:$((${#l2} - end))}${COLOR_RESET}"
82 echo >&2 -e "${chartcolor}${l3:0:start}${sp:0:2}${COLOR_RESET}${COLOR_BOLD}${COLOR_CYAN}${msg}${COLOR_RESET}${chartcolor}${sp:0:2}${l3:end:$((${#l2} - end))}${COLOR_RESET}"
83 echo >&2 -e "${chartcolor}${l4}${COLOR_RESET}"
87 # -----------------------------------------------------------------------------
88 # portable service command
90 service_cmd="$(which_cmd service)"
91 systemctl_cmd="$(which_cmd systemctl)"
93 local cmd="${1}" action="${2}"
95 if [ ! -z "${service_cmd}" ]
97 run "${service_cmd}" "${cmd}" "${action}"
99 elif [ ! -z "${systemctl_cmd}" ]
101 run "${systemctl_cmd}" "${action}" "${cmd}"
107 # -----------------------------------------------------------------------------
109 run_logfile="/dev/null"
111 local user="${USER}" dir="$(basename "${PWD}")" info info_console
113 if [ "${UID}" = "0" ]
115 info="[root ${dir}]# "
116 info_console="[${COLOR_DIM}${dir}${COLOR_RESET}]# "
118 info="[${user} ${dir}]$ "
119 info_console="[${COLOR_DIM}${dir}${COLOR_RESET}]$ "
122 printf >> "${run_logfile}" "${info}"
123 printf >> "${run_logfile}" "%q " "${@}"
124 printf >> "${run_logfile}" " ... "
126 printf >&2 "${info_console}${COLOR_BOLD}${COLOR_YELLOW}"
127 printf >&2 "%q " "${@}"
128 printf >&2 "${COLOR_RESET}\n"
135 printf >&2 "${COLOR_BGRED}${COLOR_WHITE}${COLOR_BOLD} FAILED ${COLOR_RESET}\n\n"
136 printf >> "${run_logfile}" "FAILED with exit code ${ret}\n"
138 printf >&2 "${COLOR_BGGREEN}${COLOR_WHITE}${COLOR_BOLD} OK ${COLOR_RESET}\n\n"
139 printf >> "${run_logfile}" "OK\n"
145 portable_add_user() {
146 local username="${1}"
148 getent passwd "${username}" > /dev/null 2>&1
149 [ $? -eq 0 ] && return 0
151 echo >&2 "Adding ${username} user account ..."
153 local nologin="$(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false')"
158 run useradd -r -g "${username}" -c "${username}" -s "${nologin}" -d / "${username}" && return 0
164 run pw useradd "${username}" -d / -g "${username}" -s "${nologin}" && return 0
170 run adduser -D -G "${username}" "${username}" && return 0
173 echo >&2 "Failed to add ${username} user account !"
178 portable_add_group() {
179 local groupname="${1}"
181 getent group "${groupname}" > /dev/null 2>&1
182 [ $? -eq 0 ] && return 0
184 echo >&2 "Adding ${groupname} user group ..."
187 if check_cmd groupadd
189 run groupadd -r "${groupname}" && return 0
195 run pw groupadd "${groupname}" && return 0
199 if check_cmd addgroup
201 run addgroup "${groupname}" && return 0
204 echo >&2 "Failed to add ${groupname} user group !"
208 portable_add_user_to_group() {
209 local groupname="${1}" username="${2}"
211 getent group "${groupname}" > /dev/null 2>&1
212 [ $? -ne 0 ] && return 1
214 # find the user is already in the group
215 local users=$(getent group "${groupname}" | cut -d ':' -f 4)
216 if [[ ",${users}," =~ ,${username}, ]]
218 # username is already there
221 # username is not in group
222 echo >&2 "Adding ${username} user to the ${groupname} group ..."
227 run usermod -a -G "${groupname}" "${username}" && return 0
233 run pw groupmod "${groupname}" -m "${username}" && return 0
237 if check_cmd addgroup
239 run addgroup "${username}" "${groupname}" && return 0
242 echo >&2 "Failed to add user ${username} to group ${groupname} !"
248 # man systemd-detect-virt
249 local cmd=$(which_cmd systemd-detect-virt)
250 if [ ! -z "${cmd}" -a -x "${cmd}" ]
252 "${cmd}" --container >/dev/null 2>&1 && return 0
255 # /proc/1/sched exposes the host's pid of our init !
256 # http://stackoverflow.com/a/37016302
257 local pid=$( cat /proc/1/sched | head -n 1 | { IFS='(),#:' read name pid th threads; echo $pid; } )
258 local p=$(( pid + 0 ))
259 [ ${pid} -ne 1 ] && return 0
261 # lxc sets environment variable 'container'
262 [ ! -z "${container}" ] && return 0
264 # docker creates /.dockerenv
265 # http://stackoverflow.com/a/25518345
266 [ -f "/.dockerenv" ] && return 0
268 # ubuntu and debian supply /bin/running-in-container
269 # https://www.apt-browse.org/browse/ubuntu/trusty/main/i386/upstart/1.12.1-0ubuntu4/file/bin/running-in-container
270 if [ -x "/bin/running-in-container" ]
272 "/bin/running-in-container" >/dev/null 2>&1 && return 0
279 local pids p myns ns systemctl
281 # if the directory /etc/systemd/system does not exit, it is not systemd
282 [ ! -d /etc/systemd/system ] && return 1
284 # if there is no systemctl command, it is not systemd
285 systemctl=$(which systemctl 2>/dev/null || command -v systemctl 2>/dev/null)
286 [ -z "${systemctl}" -o ! -x "${systemctl}" ] && return 1
288 # if pid 1 is systemd, it is systemd
289 [ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0
291 # if systemd is not running, it is not systemd
292 pids=$(pidof systemd 2>/dev/null)
293 [ -z "${pids}" ] && return 1
295 # check if the running systemd processes are not in our namespace
296 myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
299 ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
301 # if pid of systemd is in our namespace, it is systemd
302 [ ! -z "${myns}" && "${myns}" = "${ns}" ] && return 0
305 # else, it is not systemd