[experimental] peerjs : cleanup

This commit is contained in:
Gitouche 2021-05-05 23:03:22 +02:00
parent 9adbb1da5f
commit 832c3c1940
9 changed files with 120 additions and 26 deletions

View file

@ -28,3 +28,19 @@ ensure_pwd_is_scriptdir () {
ensure_systemd_as_user_dir_exists () { ensure_systemd_as_user_dir_exists () {
mkdir -p ${HOME}/.config/systemd/user/ mkdir -p ${HOME}/.config/systemd/user/
} }
check_systemd_unit_exists () {
systemctl --user cat -- ${1} &> /dev/null
}
check_systemd_unit_running () {
systemctl --user is-active --quiet service ${1}
}
check_container_exists () {
podman container exists ${1}
}
check_container_running () {
[[ $("podman container inspect -f '{{.State.Status}}' ${1}") == "running" ]] || exit 1
}

15
podman-peerjs/00_status.sh Executable file
View file

@ -0,0 +1,15 @@
#!/usr/bin/env bash
ABSDIR="$( dirname "$(readlink -f -- "$0")" )"
source ${ABSDIR}/../functions.sh
source ${ABSDIR}/vars.sh
ensure_pwd_is_scriptdir
ensure_not_root
echo "Checking podman images..."
podman images ${image_name}
echo "Checking container status..."
podman ps -a | grep ${container_name}
echo "Checking systemd unit status..."
systemctl --user status ${service_name}

View file

@ -7,10 +7,9 @@ source ${ABSDIR}/vars.sh
ensure_pwd_is_scriptdir ensure_pwd_is_scriptdir
ensure_not_root ensure_not_root
if podman container exists ${container_name}; then check_container_exists ${container_name} && {
echo container ${container_name} alredy exists, please remove it first. echo container ${container_name} alredy exists, please remove it first.
exit 1 exit 1
fi }
podman run --name ${container_name} --user nobody -p ${listen_if}:${listen_port}:9000 -d ${upstream_image}:${upstream_version} podman run --name ${container_name} --user nobody -p ${listen_if}:${listen_port}:9000 -d ${upstream_image}:${upstream_version}

View file

@ -7,13 +7,13 @@ source ${ABSDIR}/vars.sh
ensure_pwd_is_scriptdir ensure_pwd_is_scriptdir
ensure_not_root ensure_not_root
if ! podman container exists ${container_name}; then check_container_exists ${container_name} || {
echo container ${container_name} must exist in order to add it to systemd echo container ${container_name} must exist in order to add it to systemd
exit 1 exit 1
fi }
ensure_systemd_as_user_dir_exists ensure_systemd_as_user_dir_exists
podman generate systemd --name ${container_name} > ~/.config/systemd/user/${service_name} podman generate systemd --name ${container_name} > ${HOME}/.config/systemd/user/${service_name}
podman stop ${container_name} podman stop ${container_name}
systemctl --user enable ${service_name} systemctl --user enable ${service_name}

View file

@ -7,11 +7,33 @@ source ${ABSDIR}/vars.sh
ensure_pwd_is_scriptdir ensure_pwd_is_scriptdir
ensure_not_root ensure_not_root
if systemctl --user is-active --quiet service ${service_name}; then # FAIL if container does not exists.
check_container_exists ${container_name} || {
echo Container ${container_name} does not exists.
echo Please create it first with install script.
exit 1
}
# FAIL if systemd unit does not exists.
check_systemd_unit_exists ${service_name} || {
echo Systemd unit for this container does not exists.
echo Please create if first with enable script.
exit 1
}
# FAIL if systemd unit is running.
check_systemd_unit_running ${service_name} && {
echo Service ${service_name} is already running. echo Service ${service_name} is already running.
exit 1 exit 1
fi }
# Add check if running, ask to stop it first, then exit. # FAIL if container is already running - without systemd control.
check_container_running ${container_name} && {
echo Container ${container_name} is already running, but not controlled by systemd.
echo Stop it first and rerun this script.
exit 1
}
# OK
echo "Starting container through systemd"
systemctl --user start ${service_name} systemctl --user start ${service_name}

View file

@ -7,11 +7,28 @@ source ${ABSDIR}/vars.sh
ensure_pwd_is_scriptdir ensure_pwd_is_scriptdir
ensure_not_root ensure_not_root
if ! systemctl --user is-active --quiet service ${service_name}; then check_container_exists ${container_name} || {
echo Service ${service_name} is already stopped. echo Container ${container_name} does not exists.
exit 1 exit 1
fi }
if check_systemd_unit_exists ${service_name}; then
# WARN if systemd unit not running, but continue.
check_systemd_unit_running ${service_name} || {
echo Service ${service_name} is not in running state.
systemctl --user status ${service_name}
}
# Stopping with systemd
systemctl --user stop ${service_name}
exit 0
}
# Check if running through podman (no systemd), stop with podman stop , then exit # Check if running through podman (no systemd), stop with podman stop , then exit
if check_container_running ${container_name}; then
systemctl --user stop ${service_name} echo Container found running without systemd unit, stopping it now.
podman stop ${container_name}
exit 0
else
echo Container ${container_name} is not running
exit 1
fi

View file

@ -7,10 +7,14 @@ source ${ABSDIR}/vars.sh
ensure_pwd_is_scriptdir ensure_pwd_is_scriptdir
ensure_not_root ensure_not_root
# check if active in systemd, then check_container_exists ${container_name} || {
systemctl --user stop ${service_name} echo Container ${container_name} does not exists
# check if running in podman, then exit 1
podman stop ${container_name} }
# always check_systemd_unit_exists ${service_name} || {
systemctl --user disable ${service_name} echo "Systemd unit ${service_name} does not exists"
exit 1
}
systemctl --user --now disable ${service_name}

View file

@ -7,12 +7,24 @@ source ${ABSDIR}/vars.sh
ensure_pwd_is_scriptdir ensure_pwd_is_scriptdir
ensure_not_root ensure_not_root
if ! podman container exists ${container_name}; then # FAIL if container does not exists
echo container ${container_name} does not exists. check_container_exists ${container_name} || {
echo Container ${container_name} does not exists.
exit 1 exit 1
fi }
# add check if running, ask to stop first # FAIL if systemd unit is running
# add check if systemctl exists, ask to disable first check_systemd_unit_running ${service_name} && {
echo Systemd unit ${service_name} is running. Stop it first.
exit 1
}
# FAIL if container is running
check_container_running ${container_name} && {
echo Container ${container_name} is running. Stop it first.
exit 1
}
rm -f ${HOME}/.config/systemd/user/${service_name}
systemctl --user daemon-reload
podman rm ${container_name} podman rm ${container_name}

View file

@ -7,5 +7,14 @@ source ${ABSDIR}/vars.sh
ensure_pwd_is_scriptdir ensure_pwd_is_scriptdir
ensure_not_root ensure_not_root
podman rm ${container_name} check_systemd_unit_exists ${service_name} && {
echo "Systemd unit ${service_name} exists. Please destroy first."
exit 1
}
check_container_exists ${container_name} && {
echo "Container ${container_name} exists. Please destroy first."
exit 1
}
podman rmi "$(podman images -a -q -- ${upstream_image})" podman rmi "$(podman images -a -q -- ${upstream_image})"