diff --git a/containerctl b/containerctl new file mode 100755 index 0000000..2f2275f --- /dev/null +++ b/containerctl @@ -0,0 +1,134 @@ +#!/bin/sh + +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' "${@}" | tee -a "${LOG}" +} + +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