functions.sh : declare checks and ensure
This commit is contained in:
parent
6e31334d74
commit
4fe1c5b94e
111
functions.sh
111
functions.sh
|
@ -2,20 +2,32 @@
|
||||||
|
|
||||||
SCRIPTNAME="$( basename -- "$0" )"
|
SCRIPTNAME="$( basename -- "$0" )"
|
||||||
|
|
||||||
|
# check : current user is root?
|
||||||
|
check_root () {
|
||||||
|
[[ $EUID -eq 0 ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
# ok if root
|
||||||
ensure_root () {
|
ensure_root () {
|
||||||
if [[ $EUID -ne 0 ]]; then
|
if ! check_root; then
|
||||||
echo "This script must be run as root"
|
echo "ERROR : This script must be run as root"
|
||||||
exit 1
|
exit 1
|
||||||
|
else
|
||||||
|
return 0
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ok if non root
|
||||||
ensure_not_root () {
|
ensure_not_root () {
|
||||||
if [[ $EUID -eq 0 ]]; then
|
if check_root; then
|
||||||
echo "This script must not be run as root"
|
echo "ERROR : This script must not be *run* as root"
|
||||||
exit 1
|
exit 1
|
||||||
|
else
|
||||||
|
return 0
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ok if running script from current directory
|
||||||
ensure_pwd_is_scriptdir () {
|
ensure_pwd_is_scriptdir () {
|
||||||
if [[ $PWD != $ABSDIR ]]; then
|
if [[ $PWD != $ABSDIR ]]; then
|
||||||
echo "Please cd in the script directory before running it :"
|
echo "Please cd in the script directory before running it :"
|
||||||
|
@ -25,22 +37,107 @@ ensure_pwd_is_scriptdir () {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# make sure systemd directory exists
|
||||||
ensure_systemd_as_user_dir_exists () {
|
ensure_systemd_as_user_dir_exists () {
|
||||||
mkdir -p ${HOME}/.config/systemd/user/
|
[[ -d ${HOME}/.config/systemd/user/ ]] || mkdir -p ${HOME}/.config/systemd/user/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# check: systemd unit exists
|
||||||
check_systemd_unit_exists () {
|
check_systemd_unit_exists () {
|
||||||
systemctl --user cat -- ${1} &> /dev/null
|
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} already exists!"
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# check: systemd unit running
|
||||||
check_systemd_unit_running () {
|
check_systemd_unit_running () {
|
||||||
systemctl --user is-active --quiet service ${1}
|
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?
|
||||||
check_container_exists () {
|
check_container_exists () {
|
||||||
podman container exists ${1}
|
podman container exists ${1}
|
||||||
}
|
}
|
||||||
|
|
||||||
check_container_running () {
|
# ok if container exists
|
||||||
[[ "podman container inspect -f '{{.State.Status}}' ${1}" != "running" ]]
|
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} already exists"
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# check: container ${1} is running?
|
||||||
|
check_container_running () {
|
||||||
|
[[ "podman container inspect -f '{{.State.Status}}' ${1}" == "running" ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
# ok if container exists
|
||||||
|
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 does not exists
|
||||||
|
ensure_container_not_running () {
|
||||||
|
if check_container_running ${1}; then
|
||||||
|
echo "ERROR : container ${1} is running"
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue