services-garbaye/functions.sh

205 lines
3.8 KiB
Bash
Raw Normal View History

2021-05-04 20:14:08 +00:00
#!/usr/bin/env bash
SCRIPTNAME="$( basename -- "$0" )"
# check : current user is root?
check_root () {
[[ $EUID -eq 0 ]]
}
# ok if root
2021-05-04 20:14:08 +00:00
ensure_root () {
if ! check_root; then
echo "ERROR : This script must be run as root"
exit 1
else
return 0
fi
2021-05-04 20:14:08 +00:00
}
# ok if non root
2021-05-04 20:14:08 +00:00
ensure_not_root () {
if check_root; then
echo "ERROR : This script must not be *run* as root"
exit 1
else
return 0
fi
2021-05-04 20:14:08 +00:00
}
# ok if running script from current directory
2021-05-04 20:14:08 +00:00
ensure_pwd_is_scriptdir () {
if [[ $PWD != $ABSDIR ]]; then
echo "Please cd in the script directory before running it :"
echo "cd ${ABSDIR}"
echo "./${SCRIPTNAME}"
exit 1
fi
}
# make sure systemd directory exists
2021-05-04 20:14:08 +00:00
ensure_systemd_as_user_dir_exists () {
[[ -d ${HOME}/.config/systemd/user/ ]] || mkdir -p ${HOME}/.config/systemd/user/
2021-05-04 20:14:08 +00:00
}
2021-05-05 21:03:22 +00:00
2021-06-04 17:36:50 +00:00
# sed or die trying
sed_in_place () {
grep -q "${1}" "${3}"
if [ $? -eq 0 ]; then
sed -i -e "s|${1}|${2}|g" ${3}
else
echo "Pattern ${1} not found in file ${3}, exiting."
exit 1
fi
}
# check: systemd unit exists
2021-05-05 21:03:22 +00:00
check_systemd_unit_exists () {
systemctl --user cat -- ${1} &> /dev/null
}
# ok if systemd unit file {1} exists
ensure_systemd_unit_exists () {
if ! check_systemd_unit_exists ${1}; then
echo "ERROR : systemd unit ${1} does not exists!"
exit 1
else
return 0
fi
}
# ok if systemd unit file {1} does not exists
ensure_systemd_unit_not_exists () {
if check_systemd_unit_exists ${1}; then
echo "ERROR : systemd unit ${1} exists!"
exit 1
else
return 0
fi
}
# check: systemd unit running
2021-05-05 21:03:22 +00:00
check_systemd_unit_running () {
systemctl --user is-active --quiet service ${1}
}
# ok if systemd unit {1} is running
ensure_systemd_unit_running () {
if ! check_systemd_unit_running ${1}; then
echo "ERROR : systemd unit ${1} is not running!"
exit 1
else
return 0
fi
}
# ok if systemd unit {1} is NOT running
ensure_systemd_unit_not_running () {
if check_systemd_unit_running ${1}; then
echo "ERROR : systemd unit ${1} is running!"
exit 1
else
return 0
fi
}
# check: container $1 exists?
2021-05-05 21:03:22 +00:00
check_container_exists () {
podman container exists ${1}
2021-05-05 21:03:22 +00:00
}
# ok if container exists
ensure_container_exists () {
if ! check_container_exists ${1}; then
echo "ERROR : container ${1} does not exists"
exit 1
else
return 0
fi
}
# ok if container does not exists
ensure_container_not_exists () {
if check_container_exists ${1}; then
echo "ERROR : container ${1} exists"
exit 1
else
return 0
fi
}
# check: container ${1} is running?
2021-05-05 21:03:22 +00:00
check_container_running () {
[[ "podman container inspect -f '{{.State.Status}}' ${1}" == "running" ]]
}
# ok if container is running
ensure_container_running () {
if ! check_container_running ${1}; then
echo "ERROR : container ${1} is not running"
exit 1
else
return 0
fi
}
# ok if container is not running
ensure_container_not_running () {
if check_container_running ${1}; then
echo "ERROR : container ${1} is running"
exit 1
else
return 0
fi
2021-05-05 21:03:22 +00:00
}
2021-06-04 13:33:24 +00:00
# check: pod $1 exists?
check_pod_exists () {
podman pod exists ${1}
}
# ok if pod exists
ensure_pod_exists () {
if ! check_pod_exists ${1}; then
echo "ERROR : pod ${1} does not exists"
exit 1
else
return 0
fi
}
# ok if pod does not exists
ensure_pod_not_exists () {
if check_pod_exists ${1}; then
echo "ERROR : pod ${1} exists"
exit 1
else
return 0
fi
}
# check: pod ${1} is running?
check_pod_running () {
[[ "podman pod inspect -f '{{.State}}' ${1}" == "Running" ]]
}
# ok if pod is running
ensure_pod_running () {
if ! check_pod_running ${1}; then
echo "ERROR : pod ${1} is not running"
exit 1
else
return 0
fi
}
# ok if pod is not running
ensure_pod_not_running () {
if check_pod_running ${1}; then
echo "ERROR : pod ${1} is running"
exit 1
else
return 0
fi
}