#!/sbin/openrc-run # Copyright 2024 Willy Garidol # Distributed under the terms of the GNU General Public License v3 depend() { need localmount net } UC_LOG_PATH=/var/log/user-containers UC_SLOT="${SVCNAME#user-containers.}" UC_USER=${USER:-${UC_SLOT}} UC_COMPOSER_FILE=${COMPOSER_FILE:-docker-compose.yml} UC_CHOWN_DIR=${CHOWN_DIR} if [ "${UC_SLOT}" != "user-containers" ] then UC_HOME=${HOME:-$(su - ${UC_USER} -c "pwd")} fi extra_commands="update" update() { if [ "${UC_SLOT}" != "user-containers" ] then COMMAND="$(which podman)" stop ebegin "Running podman compose pull..." su - ${UC_USER} -c "${COMMAND} compose -f ${UC_COMPOSER_FILE} pull" start else ebegin "Error: do not run this script, run a link to it!" eend 255 fi } description=${DESCRIPTION:-You forgot to describe your container} pidfile="/run/${RC_SVCNAME}.pid" start_pre() { if [ "${UC_SLOT}" != "user-containers" ] then test -e "${UC_LOG_PATH}" || mkdir "${UC_LOG_PATH}" test -e "${UC_LOG_PATH}/${UC_SLOT}" || { mkdir "${UC_LOG_PATH}/${UC_SLOT}" } && chown -R ${UC_USER} "${UC_LOG_PATH}/${UC_SLOT}" if [ -n "${UC_CHOWN_DIR}" -a -e ${UC_CHOWN_DIR} ] then chown -R ${UC_USER} ${UC_CHOWN_DIR} fi else ebegin "Error: do not run this script, run a link to it!" eend 255 fi } start() { ebegin "Starting container '${UC_SLOT}' for user '${UC_USER}' (${UC_HOME})" COMMAND="$(which podman)" ARGUMENTS=(compose -f ${UC_COMPOSER_FILE} up) ebegin " ... ensuring nat table is loaded ..." iptables -L -t nat &> /dev/null ebegin " ... creating '${UC_SLOT}-net' ..." podman network create ${UC_SLOT}-net &> /dev/null su - "${UC_USER}" -c "$(which podman) compose down" &> /dev/null start-stop-daemon -p ${pidfile} \ -1 "${UC_LOG_PATH}/${UC_SLOT}/${UC_SLOT}.out.log" \ -2 "${UC_LOG_PATH}/${UC_SLOT}/${UC_SLOT}.err.log" \ -u ${UC_USER} \ -d ${UC_HOME} \ -b -m \ --start "${COMMAND}" \ -- ${ARGUMENTS[@]} eend $? } stop() { ebegin " ... running 'podman compose down' ..." su - "${UC_USER}" -c "$(which podman) compose -f ${UC_COMPOSER_FILE} down" &> /dev/null start-stop-daemon -p ${pidfile} \ -u ${UC_USER} \ -d ${UC_HOME} \ --stop ${UC_SLOT} eend $? }