#!/bin/bash function check_port_used() { # shellcheck disable=SC2034 usedport=$(lxc exec "$container" -- bash -c "grep Listen /etc/apache2/sites-enabled/$longname.conf | cut -d' ' -f2") [[ "$usedport" == "$port" ]] } function check_service_running() { lxc exec "$container" -- bash -c "systemctl is-active --quiet apache2.service" } function check_config_defined() { lxc exec "$container" -- bash -c "test -f /var/www/cagettepei/$shortname/config.xml" } function _read() { disable_trace check_db_maria_exists "$longname" check_container "$container" check_port_used check_config_defined check_service_running enable_trace return 0 } function _create() { echo "creating cagettepei instance for <$shortname> ... " mkdir -p "$MIAOU_CONFIGDIR/apps/cagettepei" APP_PORT=$port APP_NAME=$shortname tera -e --env-key env -t "$MIAOU_BASEDIR/templates/apps/cagettepei/cagettepei-host.j2" -o "$MIAOU_CONFIGDIR/apps/cagettepei/$longname.conf" "$MIAOU_CONFIGDIR/miaou.expanded.yaml" echo "template $longname.conf... OK" echo "copying files to container <$container> ... " lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/cagettepei/$longname.conf" "$container/etc/apache2/sites-available/$longname.conf" echo "copying files to container <$container> ... OK" if ! check_db_maria_exists "$longname"; then echo -n "create empty database <$longname> ... " db-maria create "$longname" PREFIX="" echoinfo "DONE" DB_INIT=true else echo "database already exists!" DB_INIT=false fi credential_username=$(load_yaml_from_expanded credential.username) credential_email=$(load_yaml_from_expanded credential.email) echo "initialize cagettepei $shortname $longname ..." lxc exec "$container" -- bash < /var/www/cagettepei/$shortname/config.xml EOT2 else echo "config.xml already defined!" fi a2ensite $longname.conf mkdir -p /var/log/apache2/cagettepei/$shortname systemctl restart apache2 curl localhost:$port sleep 5 if [[ $DB_INIT == true ]]; then echo "Force TABLES initialization" echo "Set administrator password..." echo "insert into User values (1,'fr','c3513c793b13471f3a49bdb22acb66de',1,'$credential_username','Admin', '$credential_email', null, null, null, null, null, null, null, null, null, now(), now(),6,null, null);" | mariadb cagettepei-$shortname -u cagettepei-$shortname -pcagettepei-$shortname -h ct1.lxd echo "TODO: password \`cagette\` should be changed soon!!!" fi EOF echo "initialize cagettepei $shortname $longname ... OK" } function _update() { echo "update" } function _delete() { echo "delete" } function usage() { echo "Usage: $COMMAND_NAME -c|r|u|d --port PORT --container CONTAINER --name NAME" exit 2 } ### MAIN # init_strict source "$(dirname "$0")/../base/common.sh" COMMAND_NAME=$(basename "$0") # read the options TEMP=$(getopt -n "$COMMAND_NAME" -o crud --long port:,container:,name:,fqdn:,domain:,subdomain: -- "$@") # shellcheck disable=SC2181 [[ "$?" -eq 0 ]] || usage eval set -- "$TEMP" action="unset" port="unset" container="unset" shortname="unset" longname="unset" fqdn="unset" # extract options and their arguments into variables. while true; do case "$1" in --port) port=$2 shift 2 ;; --fqdn) fqdn=$2 shift 2 ;; --container) container=$2 shift 2 ;; --name) shortname=$2 longname="cagettepei-$shortname" shift 2 ;; --domain) shift 2 ;; --subdomain) shift 2 ;; -c) [[ "$action" == "unset" ]] || usage action="_create" shift 1 ;; -r) [[ "$action" == "unset" ]] || usage action="_read" shift 1 ;; -u) [[ "$action" == "unset" ]] || usage action="_update" shift 1 ;; -d) [[ "$action" == "unset" ]] || usage action="_delete" shift 1 ;; --) shift break ;; *) echo "Internal error: unrecognized option: <$1>" exit 1 ;; esac done [[ "$action" != unset && "$port" != unset && "$container" != unset && "$fqdn" != unset && "$shortname" != unset ]] || usage . "$MIAOU_BASEDIR/lib/init.sh" $action