diff --git a/alpine/template b/alpine/template index fc6b456..6c0a841 100755 --- a/alpine/template +++ b/alpine/template @@ -15,7 +15,8 @@ localfile="$1" remotefile="$2" remotetmp_base="/var/tmp/4server" -while read -r host; do +# Read hosts from file descriptor 3 to prevent ssh from consuming stdin +while read -r host <&3; do host_env_file="$host_vars_dir/$host/$host.env" if [ ! -f "$host_env_file" ]; then @@ -28,24 +29,19 @@ while read -r host; do source "$host_env_file" set +a - NEBULA_KEY=$(<"$host_vars_dir/$host/$host.key") NEBULA_CRT=$(<"$host_vars_dir/$host/$host.crt") SSH_PRIVATE=$(<"$host_vars_dir/$host/$host") SSH_PUBLIC=$(<"$host_vars_dir/$host/$host.pub") -content=$(<"$localfile") - -for key in "${keys[@]}"; do - value="${!key}" # indirect reference - # Replace placeholder {{KEY}} with value using Bash's parameter expansion - content="${content//\{\{$key\}\}/$value}" -done - - - + content=$(<"$localfile") + for key in "${keys[@]}"; do + value="${!key}" # indirect reference + # Replace placeholder {{KEY}} with value using Bash's parameter expansion + content="${content//\{\{$key\}\}/$value}" + done # Copy content to remote temporary file remotetmp="${remotetmp_base}_${host}" @@ -55,7 +51,5 @@ done # Move temporary file to final location with doas ssh "$host" "doas mv '$remotetmp' '$remotefile'" -done < /app/host_vars/hosts - - +done 3< /app/host_vars/hosts diff --git a/app/migrate b/app/migrate index 0867aa0..0d980a5 100755 --- a/app/migrate +++ b/app/migrate @@ -1,8 +1,5 @@ #!/bin/bash - -#!/bin/bash - -rex "doas sqlite3 /4server/data/contracts.db <<'EOF' -ALTER TABLE containers -ALTER COLUMN contract DROP NOT NULL; +rex "doas sqlite3 /4server/data/contracts.db <> "$OUTPUT" + echo -e "$DATA" | tr '\n' ' ' >> "$OUTPUT" + echo >> "$OUTPUT" # newline at the end + echo "Measurement for $DATE written to $OUTPUT" + + # Wait until 00:15 to start next day + while [ "$(date +%H:%M)" != "00:15" ]; do + sleep 30 + done +done + diff --git a/app/sbin/nuke/ODOO_19 b/app/sbin/nuke/ODOO_19 index 6a91876..593686a 100755 --- a/app/sbin/nuke/ODOO_19 +++ b/app/sbin/nuke/ODOO_19 @@ -2,10 +2,10 @@ # Load functions source /4server/sbin/ODOO_19/ODOO_19.lib - -# Config variables -UUID="${UUID:-default}" -STAGING="${STAGING:-false}" +if [[ -z "$UUID" ]]; then + echo "Error: UUID not set. Aborting." + exit 1 +fi POSTGRES_HOST="${POSTGRES_HOST:-beedb}" POSTGRES_PORT="${POSTGRES_PORT:-5432}" @@ -15,13 +15,16 @@ ODOO_DB_USER="${UUID}" export ODOO_DB_PASSWORD=$(echo "$SECRET" | jq -r '.psql') BASEURL="${BASEURL:-/4server/data/$UUID}" +BACKUPURL="/4backup/$UUID" doas docker stop "$UUID" doas docker rm "$UUID" if [ -n "${UUID:-}" ]; then echo "Removing directory: $BASEURL" - doas rm -rf "$BASEURL" + #doas rm -rf "$BASEURL" + echo "Removing backup directory $BACKUPURL" + #doas rm -rf $BACKUPURL fi diff --git a/app/sbin/nukeContainer b/app/sbin/nukeContainer index 34d2119..2f69c44 100755 --- a/app/sbin/nukeContainer +++ b/app/sbin/nukeContainer @@ -15,6 +15,30 @@ if [[ -z "$UUID" ]]; then exit 1 fi + +get_container_status() { + local uuid="$1" + # Get the container ID or name matching the UUID + CONTAINER_ID=$(docker ps -a --filter "name=$uuid" --format "{{.ID}}") + if [[ -z "$CONTAINER_ID" ]]; then + echo "not_found" + return + fi + + STATUS=$(docker inspect -f '{{.State.Status}}' "$CONTAINER_ID") + echo "$STATUS" +} + +# Check if container exists +STATUS=$(get_container_status "$UUID") +if [[ "$STATUS" == "running" ]]; then + echo "Container $UUID is still running. Aborting deletion." + exit 2 +fi + + + + get_contract_info # Extract the second part of UUID (split by "-") @@ -34,3 +58,11 @@ case "$SECOND_PART" in ;; esac +sqlite3 "/4server/data/contracts.db" < /4server/data/log/stopContainer.log 2>&1 +echo "$(date '+%Y-%m-%d %H:%M') Stop container $1" + +docker stop $1 + diff --git a/app/templates/init.d/cpu b/app/templates/init.d/cpu new file mode 100755 index 0000000..368fc08 --- /dev/null +++ b/app/templates/init.d/cpu @@ -0,0 +1,17 @@ +#!/sbin/openrc-run +# OpenRC service for /4server/sbin/cpu + +name="cpu" +description="Logs cpu usage" + +command="/4server/sbin/cpu" +command_background="yes" +pidfile="/run/cpu.pid" +output_log="/4server/data/log/cpu.log" +error_log="/4server/data/log/cpu.log" + +depend() { + need localmount + after bootmisc +} + diff --git a/app/update b/app/update index 18265c7..581441c 100755 --- a/app/update +++ b/app/update @@ -68,6 +68,13 @@ rex doas chown root:root /etc/init.d/cleanTmp rex doas rc-update add cleanTmp default rex doas rc-service cleanTmp restart +#INSTALL cpu service +template templates/init.d/cpu /etc/init.d/cpu +rex doas chmod 0755 /etc/init.d/cpu +rex doas chown root:root /etc/init.d/cpu +rex doas rc-update add cpu default +rex doas rc-service cpu restart + ### Infrastructure ##### Docker rex doas rc-service docker start diff --git a/app/vault/host_vars.img b/app/vault/host_vars.img index 7a5ad02..7642f0a 100644 Binary files a/app/vault/host_vars.img and b/app/vault/host_vars.img differ diff --git a/app/vault/open b/app/vault/open index 1fd06cd..43841d3 100755 --- a/app/vault/open +++ b/app/vault/open @@ -4,6 +4,7 @@ set -euo pipefail VAULT_FILE="/app/vault/host_vars.img" MAPPER_NAME="host_vars_crypt" MOUNT_POINT="/app/host_vars" +LOOP_DEVICE="/dev/loop50" mkdir -p "$MOUNT_POINT" @@ -13,19 +14,36 @@ if cryptsetup status "$MAPPER_NAME" >/dev/null 2>&1; then cryptsetup close "$MAPPER_NAME" fi -# Open +# Detach loop device if already in use +if losetup "$LOOP_DEVICE" >/dev/null 2>&1; then + echo "Detaching stale loop device $LOOP_DEVICE..." + losetup -d "$LOOP_DEVICE" +fi + +# Create loop device if missing +if [ ! -e "$LOOP_DEVICE" ]; then + echo "Creating loop device $LOOP_DEVICE..." + mknod "$LOOP_DEVICE" b 7 50 + chmod 660 "$LOOP_DEVICE" +fi + +# Attach vault file to loop device +echo "Attaching $VAULT_FILE to $LOOP_DEVICE..." +losetup "$LOOP_DEVICE" "$VAULT_FILE" + +# Open encrypted volume echo "Opening encrypted volume..." -cryptsetup open "$VAULT_FILE" "$MAPPER_NAME" +cryptsetup open "$LOOP_DEVICE" "$MAPPER_NAME" # Format if needed -if ! blkid /dev/mapper/"$MAPPER_NAME" >/dev/null 2>&1; then +if ! blkid "/dev/mapper/$MAPPER_NAME" >/dev/null 2>&1; then echo "No filesystem found, creating ext4..." - mkfs.ext4 /dev/mapper/"$MAPPER_NAME" + mkfs.ext4 "/dev/mapper/$MAPPER_NAME" fi # Mount echo "Mounting at $MOUNT_POINT..." -mount /dev/mapper/"$MAPPER_NAME" "$MOUNT_POINT" +mount "/dev/mapper/$MAPPER_NAME" "$MOUNT_POINT" echo "Vault is mounted at $MOUNT_POINT"