2025-07-22 15:51:26 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
2025-07-26 19:04:27 +02:00
|
|
|
# SPDX-FileCopyrightText: (C) 2025 Enno Tensing <tenno+containerctl@suij.in>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
2025-07-22 15:51:26 +02:00
|
|
|
BASEDIR="/var/lib/containerctl"
|
|
|
|
CONTAINERDIR="${BASEDIR}/containers"
|
|
|
|
CONFIGDIR="${BASEDIR}/configs"
|
|
|
|
LOGDIR="/var/log/containerctl"
|
|
|
|
TODAY="$(date '+%F')"
|
|
|
|
LOG="${LOGDIR}/${TODAY}"
|
|
|
|
|
|
|
|
log_error()
|
|
|
|
{
|
2025-07-26 16:51:40 +02:00
|
|
|
printf '[%b] (EE) %b\n' "${TODAY}" "${@}" | tee -a "${LOG}"
|
2025-07-22 15:51:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
list_dir()
|
|
|
|
{
|
|
|
|
find "${1}" -mindepth 1 -type d
|
|
|
|
}
|
|
|
|
|
|
|
|
list_containers()
|
|
|
|
{
|
|
|
|
list_dir "${CONTAINERDIR}"
|
|
|
|
}
|
|
|
|
|
|
|
|
list_configs()
|
|
|
|
{
|
|
|
|
list_dir "${CONFIGDIR}"
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|