1
0
Fork 0
containerctl/containerctl
Enno Tensing 7a863bb7ea
containerctl: list_configs() should not use -type d
Signed-off-by: Enno Tensing <tenno@suij.in>
2025-08-04 20:36:29 +02:00

133 lines
2.4 KiB
Bash
Executable file

#!/bin/sh
# SPDX-FileCopyrightText: (C) 2025 Enno Tensing <tenno+containerctl@suij.in>
#
# SPDX-License-Identifier: GPL-2.0-only
BASEDIR="/var/lib/containerctl"
CONTAINERDIR="${BASEDIR}/containers"
CONFIGDIR="${BASEDIR}/configs"
LOGDIR="/var/log/containerctl"
TODAY="$(date '+%F')"
LOG="${LOGDIR}/${TODAY}"
log_error()
{
printf '[%b] (EE) %b\n' "${TODAY}" "${@}" | tee -a "${LOG}"
}
list_containers()
{
find "${CONTAINERDIR}" -mindepth 1 -type d
}
list_configs()
{
find "${CONFIGDIR}" -mindepth 1 -type f
}
exec_script()
{
container="${1}"
script="${2}"
if [ -z "${container}" ]
then
log_error "No container passed"
exit 1
fi
if [ -z "${script}" ]
then
log_error "No script passed"
exit 1
fi
if ! [ -e "${CONTAINERDIR}/${container}" ]
then
log_error "${container} does not exist, did you forget to generate it?"
exit 1
fi
if ! [ -d "${CONTAINERDIR}/${container}" ]
then
log_error "${container} does not exist"
exit 1
fi
if ! [ -e "${CONTAINERDIR}/${container}/${script}" ]
then
log_error "${script} is invalid, maybe try regenerating the container?"
exit 1
fi
if ! [ -f "${CONTAINERDIR}/${container}/${script}" ]
then
log_error "${script} is invalid, please recreate the container"
exit 1
fi
/bin/sh "${CONTAINERDIR}/${container}/${script}" || log_error "${script} failed!"
}
generate_container()
{
config="${1}"
if [ -z "${config}" ]
then
log_error "No config was passed."
exit 1
fi
if ! [ -e "${CONFIGDIR}" ]
then
log_error "${CONFIGDIR} does not exist"
exit 1
fi
if ! [ -d "${CONFIGDIR}" ]
then
log_error "${CONFIGDIR} is not a directory"
exit 1
fi
if ! [ -e "${CONFIGDIR}/${config}" ]
then
log_error "${config} does not exist!" \
"Please check the name and or location of the desired config."
exit 1
fi
if ! [ -f "${CONFIGDIR}/${config}" ]
then
log_error "${config} is not a file."
exit 1
fi
/usr/bin/env python3 "${BASEDIR}/generate/generate.py" \
"${CONFIGDIR}/${config}" "${LOG}" "${CONTAINERDIR}"
if [ "${?}" = 1 ]
then
log_error "Container generation failed."
exit 1
fi
}
generate_all()
{
list_configs | while read -r config
do
generate_config "$(printf '%b' "${config}" | sed -e "s|${CONFIGDIR}||g")"
done
}
case "${1}" in
"list-containers") list_containers ;;
"list-configs") list_configs ;;
"generate-all") generate_all ;;
"generate") shift; generate_config "${@}" ;;
*) exec_script "${@}" ;;
esac