#!/usr/bin/env bash set -euo pipefail if [[ $EUID -ne 0 ]]; then echo 'Run this configuration step as root.' >&2 exit 1 fi checkout=/opt/akyldash/current example=$checkout/deploy/production/.env.example config_dir=/etc/akyldash env_file=$config_dir/production.env secret_file=$config_dir/review_secret if [[ ! -d $checkout/.git || -L $checkout || $(stat -c %U "$checkout") != root || ! -f $example ]]; then echo 'Refusing configuration: approved root-owned checkout is unavailable.' >&2 exit 1 fi unsafe=$(find "$checkout" -xdev \( ! -user root -o -perm /022 \) -print -quit) if [[ -n $unsafe ]] || ! git -C "$checkout" diff --quiet HEAD -- deploy/production/.env.example; then echo 'Refusing configuration: checkout is writable or .env.example differs from Git.' >&2 exit 1 fi if [[ ! -d $config_dir || $(stat -c %U:%G:%a "$config_dir") != root:root:700 ]]; then echo 'Expected /etc/akyldash to be root-owned mode 0700.' >&2 exit 1 fi if [[ -e $env_file || -L $env_file || -e $secret_file || -L $secret_file ]]; then echo 'Production env or review secret already exists; refusing to overwrite either file.' >&2 exit 1 fi env_tmp=$(mktemp "$config_dir/.production.env.XXXXXX") secret_tmp=$(mktemp "$config_dir/.review_secret.XXXXXX") trap 'rm -f "$env_tmp" "$secret_tmp"' EXIT install -o root -g root -m 0600 "$example" "$env_tmp" openssl rand -hex 32 > "$secret_tmp" chown root:root "$secret_tmp" chmod 0600 "$secret_tmp" mv -n "$env_tmp" "$env_file" if [[ -e $env_tmp ]]; then echo 'Production env already appeared; refusing to continue.' >&2 exit 1 fi mv -n "$secret_tmp" "$secret_file" if [[ -e $secret_tmp ]]; then rm -f "$env_file" echo 'Review secret already appeared; removed the new env file and stopped.' >&2 exit 1 fi echo 'Created root-owned production.env and review_secret with mode 0600. No services or volumes were changed.'