From 832c3c194012024a1f9c428a1186436a724d4d15 Mon Sep 17 00:00:00 2001 From: Gitouche <26656-gitouche@users.noreply.framagit.org> Date: Wed, 5 May 2021 23:03:22 +0200 Subject: [PATCH] [experimental] peerjs : cleanup --- functions.sh | 16 ++++++++++++++++ podman-peerjs/00_status.sh | 15 +++++++++++++++ podman-peerjs/10_install.sh | 5 ++--- podman-peerjs/20_enable.sh | 6 +++--- podman-peerjs/30_start.sh | 28 +++++++++++++++++++++++++--- podman-peerjs/40_stop.sh | 27 ++++++++++++++++++++++----- podman-peerjs/70_disable.sh | 16 ++++++++++------ podman-peerjs/80_destroy.sh | 22 +++++++++++++++++----- podman-peerjs/90_prune.sh | 11 ++++++++++- 9 files changed, 120 insertions(+), 26 deletions(-) create mode 100755 podman-peerjs/00_status.sh diff --git a/functions.sh b/functions.sh index 50b2ba5..c8a6878 100644 --- a/functions.sh +++ b/functions.sh @@ -28,3 +28,19 @@ ensure_pwd_is_scriptdir () { ensure_systemd_as_user_dir_exists () { 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 +} diff --git a/podman-peerjs/00_status.sh b/podman-peerjs/00_status.sh new file mode 100755 index 0000000..97cc3ce --- /dev/null +++ b/podman-peerjs/00_status.sh @@ -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} diff --git a/podman-peerjs/10_install.sh b/podman-peerjs/10_install.sh index fa8d143..cf65be8 100755 --- a/podman-peerjs/10_install.sh +++ b/podman-peerjs/10_install.sh @@ -7,10 +7,9 @@ source ${ABSDIR}/vars.sh ensure_pwd_is_scriptdir 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. exit 1 -fi +} podman run --name ${container_name} --user nobody -p ${listen_if}:${listen_port}:9000 -d ${upstream_image}:${upstream_version} - diff --git a/podman-peerjs/20_enable.sh b/podman-peerjs/20_enable.sh index 5f77cce..99714ce 100755 --- a/podman-peerjs/20_enable.sh +++ b/podman-peerjs/20_enable.sh @@ -7,13 +7,13 @@ source ${ABSDIR}/vars.sh ensure_pwd_is_scriptdir 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 exit 1 -fi +} 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} systemctl --user enable ${service_name} diff --git a/podman-peerjs/30_start.sh b/podman-peerjs/30_start.sh index a84b398..5f6fe21 100755 --- a/podman-peerjs/30_start.sh +++ b/podman-peerjs/30_start.sh @@ -7,11 +7,33 @@ source ${ABSDIR}/vars.sh ensure_pwd_is_scriptdir 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. 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} diff --git a/podman-peerjs/40_stop.sh b/podman-peerjs/40_stop.sh index 0441370..dba94f4 100755 --- a/podman-peerjs/40_stop.sh +++ b/podman-peerjs/40_stop.sh @@ -7,11 +7,28 @@ source ${ABSDIR}/vars.sh ensure_pwd_is_scriptdir ensure_not_root -if ! systemctl --user is-active --quiet service ${service_name}; then - echo Service ${service_name} is already stopped. +check_container_exists ${container_name} || { + echo Container ${container_name} does not exists. 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 - -systemctl --user stop ${service_name} +if check_container_running ${container_name}; then + 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 diff --git a/podman-peerjs/70_disable.sh b/podman-peerjs/70_disable.sh index 0416248..ebc9093 100755 --- a/podman-peerjs/70_disable.sh +++ b/podman-peerjs/70_disable.sh @@ -7,10 +7,14 @@ source ${ABSDIR}/vars.sh ensure_pwd_is_scriptdir ensure_not_root -# check if active in systemd, then -systemctl --user stop ${service_name} -# check if running in podman, then -podman stop ${container_name} +check_container_exists ${container_name} || { + echo Container ${container_name} does not exists + exit 1 +} -# always -systemctl --user disable ${service_name} +check_systemd_unit_exists ${service_name} || { + echo "Systemd unit ${service_name} does not exists" + exit 1 +} + +systemctl --user --now disable ${service_name} diff --git a/podman-peerjs/80_destroy.sh b/podman-peerjs/80_destroy.sh index bb179dd..7722a7b 100755 --- a/podman-peerjs/80_destroy.sh +++ b/podman-peerjs/80_destroy.sh @@ -7,12 +7,24 @@ source ${ABSDIR}/vars.sh ensure_pwd_is_scriptdir ensure_not_root -if ! podman container exists ${container_name}; then - echo container ${container_name} does not exists. +# FAIL if container does not exists +check_container_exists ${container_name} || { + echo Container ${container_name} does not exists. exit 1 -fi +} -# add check if running, ask to stop first -# add check if systemctl exists, ask to disable first +# FAIL if systemd unit is running +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} diff --git a/podman-peerjs/90_prune.sh b/podman-peerjs/90_prune.sh index b005f0e..afa6e1f 100755 --- a/podman-peerjs/90_prune.sh +++ b/podman-peerjs/90_prune.sh @@ -7,5 +7,14 @@ source ${ABSDIR}/vars.sh ensure_pwd_is_scriptdir 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})"