File: /home/imgbuild-common.sh
#!/bin/bash
shell_identity=$(ps hco comm $$)
if [ "$shell_identity" != bash ]; then
echo "ERROR: $0 must be used with bash, not $shell_identity"
pstree -aA $$
exit 1
fi
do_until_x_retries() {
try=1
max_tries=$1
action=$2
while ! $action; do
if (( try > max_tries )); then
return 1
fi
echo "Try #$try (max $max_tries) of $action failed, retrying after 10s..."
sleep 10
((try++))
done
}
has_yum() {
which yum >/dev/null 2>&1
}
has_dnf() {
which dnf >/dev/null 2>&1
}
has_apt() {
which apt-get >/dev/null 2>&1
}
# Install a single package with whichever package system is present.
#
# If given two arguments, the 1st is for use on any "Enterprise Linux"
# distro and the 2nd is for use on Ubuntu.
#
# If given one argument, the same package name is used regardless of distro.
install_package() {
local el_package=$1
local ubuntu_package=${2:-$el_package}
if has_apt; then
function runAptGetInstall()
{
DEBIAN_FRONTEND='noninteractive' apt-get -y -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold' install $ubuntu_package
}
do_until_x_retries 5 runAptGetInstall
elif has_dnf; then
do_until_x_retries 5 "dnf -y install $el_package"
elif has_yum; then
do_until_x_retries 5 "yum -y install $el_package"
fi
}
try_remove_package() {
local el_package=$1
local ubuntu_package=$2
if [[ "$ubuntu_package" ]] && has_apt; then
apt-get -y -q -q remove $ubuntu_package ||:
elif [[ "$el_package" ]] && has_dnf; then
dnf -y remove $el_package ||:
elif [[ "$el_package" ]] && has_yum; then
yum -y remove $el_package ||:
fi
}
os_release_pretty_name() {
awk '-F[="]*' '/PRETTY_NAME/ { print $2; exit; }' /etc/os-release
}
os_release_name_contains() {
grep -Eiqs "^(PRETTY_)?NAME=.*$1" /etc/os-release >/dev/null 2>&1
}
os_id_like() {
grep -Eiqs "^ID_LIKE=.*$1" /etc/os-release >/dev/null 2>&1
}
os_major_version_at_least() {
# This only checks the first number before any dot which may exist in the version string
awk -v MINIMUM=$1 '-F[=".]*' '/^VERSION_ID/ { if ( $2 >= MINIMUM ) exit; exit 1; }' /etc/os-release > /dev/null 2>&1
}
os_major_version_is() {
# This only checks the first number before any dot which may exist in the version string
awk -v WANT=$1 '-F[=".]*' '/^VERSION_ID/ { if ( $2 == WANT ) exit; exit 1; }' /etc/os-release > /dev/null 2>&1
}
is_almalinux() {
os_release_name_contains 'AlmaLinux'
}
is_centos() {
os_release_name_contains 'CentOS'
}
is_ubuntu() {
os_release_name_contains 'Ubuntu'
}
is_rocky_linux() {
os_release_name_contains 'Rocky'
}
is_rhel_based() {
os_id_like 'rhel'
}
dmi_system_manufacturer() {
dmidecode --string system-manufacturer
}
dmi_system_manufacturer_contains() {
dmi_system_manufacturer | grep -iqs "$1" >/dev/null 2>&1
}
is_digitalocean() {
dmi_system_manufacturer_contains "DigitalOcean"
}
is_azure() {
result=$(systemctl list-units --plain --no-legend --no-pager --all 'waagent*' 'walinuxagent*')
if [ ! -z "$result" ]; then return 0; fi
return 1
}
waagent_masked() {
result=$(systemctl list-units --plain --no-legend --no-pager --all --state=masked 'waagent*' 'walinuxagent*')
if [ ! -z "$result" ]; then return 0; fi
return 1
}
wait_for_waagent_provision() {
if waagent_masked; then return 0; fi
is_azure && for try in `seq 1 50`; do
if [ -f '/var/lib/waagent/provisioned' ]; then
return 0
fi
diag "waiting for waagent to finish provision ($try/50) ..."
sleep 5
done
return 1
}
has_cloud_init() {
which cloud-init >/dev/null 2>&1
}
wait_for_cloud_init_provision() {
if ! has_cloud_init; then return 0; fi
cloud-init status --wait
}
is_networkmanager_enabled() {
systemctl is-enabled NetworkManager.service > /dev/null 2>&1
}
is_systemd_networkd_enabled() {
systemctl is-enabled systemd-networkd.service > /dev/null 2>&1
}
disable_networkmanager_service() {
local EVAL_AFTER_REMOVED=$1
if is_networkmanager_enabled && os_major_version_is 8; then
systemctl stop NetworkManager.service || true
systemctl disable NetworkManager.service || true
systemctl mask NetworkManager.service || true
fi
if is_rhel_based && os_major_version_is 8; then
try_remove_package 'NetworkManager*'
eval $EVAL_AFTER_REMOVED
fi
}
enable_legacy_network_service() {
local EVAL_BEFORE_ENABLE=$1
if is_rhel_based && os_major_version_is 8 && ! is_systemd_networkd_enabled; then
install_package 'network-scripts'
eval $EVAL_BEFORE_ENABLE
systemctl daemon-reload
systemctl enable network
head -n500 /etc/sysconfig/network-scripts/ifcfg-* ||:
systemctl start network
fi
}
set_up_install_on_boot() {
local SSH_USER=$1
local CPANEL_VERSION=$2
local CPANEL_MIRROR=$3
SCRIPTS_DIR="/tmp"
if test -f "/root/tmp/system_update.sh"; then
SCRIPTS_DIR="/root/tmp"
fi
bash $SCRIPTS_DIR/system_update.sh
if [ ! -z "${CPANEL_VERSION}" ]; then
echo "CPANEL=${CPANEL_VERSION}" > /etc/cpupdate.conf;
fi
if [ ! -z "${CPANEL_MIRROR}" ]; then
echo "HTTPUPDATE=${CPANEL_MIRROR}" > /etc/cpsources.conf;
fi
SSH_USER=$SSH_USER bash $SCRIPTS_DIR/set_up_install_on_boot.sh
}
go_build() {
local SOURCE=$1
local DEST=$2
if [[ -f $SOURCE ]]; then
if has_apt; then
snap install --classic go
PATH="${PATH}:/snap/bin"
elif has_dnf; then
install_package golang
fi
if go version >/dev/null 2>&1; then
go build -o $DEST $SOURCE
fi
fi
}
user_homedir() {
getent passwd "$1" | cut -d: -f 6
}
diag() {
echo "[$0]" $*
}
get_shell_ext() {
var=".sh"
if is_ubuntu; then
var=""
fi
echo $var
}
install_sudo_user_bash_profile() {
user=$1
destination=$2
if [[ $user != "root" ]]; then
install -v -m 644 -o $user -g $user /var/cpanel/bash_profile.sudo_user $destination
fi
}