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
25 if [ $(( $(tput colors 2>/dev/null) )) -ge 8 ]
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)"
61 echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
64 # -----------------------------------------------------------------------------
68 l2=" |.-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-" \
69 l3=" | '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' " \
70 l4=" +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->" \
72 netdata="netdata" start end msg="${*}" chartcolor="${TPUT_DIM}"
74 [ ${#msg} -lt ${#netdata} ] && msg="${msg}${sp:0:$(( ${#netdata} - ${#msg}))}"
75 [ ${#msg} -gt $(( ${#l2} - 20 )) ] && msg="${msg:0:$(( ${#l2} - 23 ))}..."
77 start="$(( ${#l2} / 2 - 4 ))"
78 [ $(( start + ${#msg} + 4 )) -gt ${#l2} ] && start=$((${#l2} - ${#msg} - 4))
79 end=$(( ${start} + ${#msg} + 4 ))
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}"
89 # -----------------------------------------------------------------------------
90 # portable service command
92 service_cmd="$(which_cmd service)"
93 systemctl_cmd="$(which_cmd systemctl)"
95 local cmd="${1}" action="${2}"
97 if [ ! -z "${service_cmd}" ]
99 run "${service_cmd}" "${cmd}" "${action}"
101 elif [ ! -z "${systemctl_cmd}" ]
103 run "${systemctl_cmd}" "${action}" "${cmd}"
109 # -----------------------------------------------------------------------------
112 printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
116 printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
119 run_logfile="/dev/null"
121 local user="${USER}" dir="$(basename "${PWD}")" info info_console
123 if [ "${UID}" = "0" ]
125 info="[root ${dir}]# "
126 info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
128 info="[${user} ${dir}]$ "
129 info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
132 printf >> "${run_logfile}" "${info}"
133 printf >> "${run_logfile}" "%q " "${@}"
134 printf >> "${run_logfile}" " ... "
136 printf >&2 "${info_console}${TPUT_BOLD}${TPUT_YELLOW}"
137 printf >&2 "%q " "${@}"
138 printf >&2 "${TPUT_RESET}\n"
146 printf >> "${run_logfile}" "FAILED with exit code ${ret}\n"
149 printf >> "${run_logfile}" "OK\n"
155 portable_add_user() {
156 local username="${1}"
158 getent passwd "${username}" > /dev/null 2>&1
159 [ $? -eq 0 ] && echo >&2 "User '${username}' already exists." && return 0
161 echo >&2 "Adding ${username} user account ..."
163 local nologin="$(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false')"
168 run useradd -r -g "${username}" -c "${username}" -s "${nologin}" -d / "${username}" && return 0
174 run pw useradd "${username}" -d / -g "${username}" -s "${nologin}" && return 0
180 run adduser -D -G "${username}" "${username}" && return 0
183 echo >&2 "Failed to add ${username} user account !"
188 portable_add_group() {
189 local groupname="${1}"
191 getent group "${groupname}" > /dev/null 2>&1
192 [ $? -eq 0 ] && echo >&2 "Group '${groupname}' already exists." && return 0
194 echo >&2 "Adding ${groupname} user group ..."
197 if check_cmd groupadd
199 run groupadd -r "${groupname}" && return 0
205 run pw groupadd "${groupname}" && return 0
209 if check_cmd addgroup
211 run addgroup "${groupname}" && return 0
214 echo >&2 "Failed to add ${groupname} user group !"
218 portable_add_user_to_group() {
219 local groupname="${1}" username="${2}"
221 getent group "${groupname}" > /dev/null 2>&1
222 [ $? -ne 0 ] && echo >&2 "Group '${groupname}' does not exist." && return 1
224 # find the user is already in the group
225 local users=$(getent group "${groupname}" | cut -d ':' -f 4)
226 if [[ ",${users}," =~ ,${username}, ]]
228 # username is already there
229 echo >&2 "User '${username}' is already in group '${groupname}'."
232 # username is not in group
233 echo >&2 "Adding ${username} user to the ${groupname} group ..."
238 run usermod -a -G "${groupname}" "${username}" && return 0
244 run pw groupmod "${groupname}" -m "${username}" && return 0
248 if check_cmd addgroup
250 run addgroup "${username}" "${groupname}" && return 0
253 echo >&2 "Failed to add user ${username} to group ${groupname} !"
259 # man systemd-detect-virt
260 local cmd=$(which_cmd systemd-detect-virt)
261 if [ ! -z "${cmd}" -a -x "${cmd}" ]
263 "${cmd}" --container >/dev/null 2>&1 && return 0
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 | head -n 1 | { IFS='(),#:' read name pid th threads; echo $pid; } )
269 local p=$(( pid + 0 ))
270 [ ${pid} -ne 1 ] && return 0
272 # lxc sets environment variable 'container'
273 [ ! -z "${container}" ] && return 0
275 # docker creates /.dockerenv
276 # http://stackoverflow.com/a/25518345
277 [ -f "/.dockerenv" ] && return 0
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" ]
283 "/bin/running-in-container" >/dev/null 2>&1 && return 0
290 local pids p myns ns systemctl
292 # if the directory /etc/systemd/system does not exit, it is not systemd
293 [ ! -d /etc/systemd/system ] && return 1
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
299 # if pid 1 is systemd, it is systemd
300 [ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0
302 # if systemd is not running, it is not systemd
303 pids=$(pidof systemd 2>/dev/null)
304 [ -z "${pids}" ] && return 1
306 # check if the running systemd processes are not in our namespace
307 myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
310 ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
312 # if pid of systemd is in our namespace, it is systemd
313 [ ! -z "${myns}" && "${myns}" = "${ns}" ] && return 0
316 # else, it is not systemd