Files
akyldash/infra/deploy/install-helper.sh
agent e034fdb816
CI / tests-and-images (pull_request) Has been cancelled
Runner smoke test / runner-smoke (pull_request) Has been cancelled
feat(infra): prepare isolated repository CI runner
2026-10-12 00:35:42 +03:00

91 lines
3.6 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
if [[ $EUID -ne 0 ]]; then
echo "Run this installer as root." >&2
exit 1
fi
checkout=/opt/akyldash/current
rollback_source=$checkout/infra/deploy/rollback.py
backup_source=$checkout/infra/backup/archive.py
verify_source=$checkout/infra/bootstrap/verify-helper.sh
compose_source=$checkout/infra/deploy/compose-helper.sh
if [[ ! -d $checkout/.git || ! -f $rollback_source || ! -f $backup_source || ! -f $verify_source || ! -f $compose_source ]]; then
echo "Install the approved repository checkout at $checkout first." >&2
exit 1
fi
if [[ -L $checkout ]]; then
echo "The checkout path must not be a symlink." >&2
exit 1
fi
for directory in /opt /opt/akyldash; do
mode=$(stat -c %a "$directory")
if [[ $(stat -c %U "$directory") != root ]] || (( (8#$mode & 022) != 0 )); then
echo "The checkout parent $directory must be root-owned and not group/world-writable." >&2
exit 1
fi
done
if ! id codex >/dev/null 2>&1 || id -nG codex | tr ' ' '\n' | grep -Eq '^(sudo|docker)$'; then
echo "Create codex without sudo or Docker-group membership before installing the helper." >&2
exit 1
fi
if grep -R -nE '(^|[[:space:],])codex([[:space:],]|$)' /etc/sudoers /etc/sudoers.d --exclude=akyldash-codex; then
echo "Existing codex sudoers rules need owner review before installing the helper." >&2
exit 1
fi
unsafe=$(find "$checkout" -xdev \( ! -user root -o -perm /022 \) -print -quit)
if [[ $(stat -c %U "$checkout") != root || -n $unsafe ]]; then
echo "The checkout must be root-owned and not group/world-writable." >&2
exit 1
fi
if ! command -v visudo >/dev/null 2>&1; then
echo "visudo is required to validate the restricted sudoers rule." >&2
exit 1
fi
for path in /usr/local/sbin/akyldash-verify /usr/local/sbin/akyldash-rollback /usr/local/sbin/akyldash-compose /usr/local/lib/akyldash/archive.py /etc/sudoers.d/akyldash-codex; do
if [[ -L $path ]]; then
echo "Refusing symlink at $path." >&2
exit 1
fi
done
install -d -o root -g root -m 0755 /usr/local/lib/akyldash
install -o root -g root -m 0644 "$backup_source" /usr/local/lib/akyldash/archive.py
install -o root -g root -m 0750 "$verify_source" /usr/local/sbin/akyldash-verify
install -o root -g root -m 0750 "$rollback_source" /usr/local/sbin/akyldash-rollback
install -o root -g root -m 0750 "$compose_source" /usr/local/sbin/akyldash-compose
sudoers_file=/etc/sudoers.d/akyldash-codex
expected=$'codex ALL=(root) NOPASSWD: /usr/local/sbin/akyldash-verify\ncodex ALL=(root) NOPASSWD: /usr/local/sbin/akyldash-rollback *\ncodex ALL=(root) NOPASSWD: /usr/local/sbin/akyldash-compose config\ncodex ALL=(root) NOPASSWD: /usr/local/sbin/akyldash-compose status'
created=false
if [[ -e $sudoers_file ]]; then
if [[ $(stat -c %U:%a "$sudoers_file") != root:440 || $(cat "$sudoers_file") != "$expected" ]]; then
echo "$sudoers_file exists with unexpected contents or permissions; review manually." >&2
exit 1
fi
else
temp=$(mktemp /etc/sudoers.d/.akyldash-codex.XXXXXX)
trap 'rm -f "$temp"' EXIT
printf '%s\n' "$expected" > "$temp"
chmod 0440 "$temp"
visudo -cf "$temp"
visudo -c -f /etc/sudoers
mv -n "$temp" "$sudoers_file"
if [[ $(cat "$sudoers_file") != "$expected" ]]; then
echo "Could not install the expected sudoers rule." >&2
exit 1
fi
created=true
fi
if ! visudo -c -f /etc/sudoers; then
if [[ $created == true ]]; then
rm -f "$sudoers_file"
fi
echo "Sudoers validation failed; the newly added rule was removed." >&2
exit 1
fi
echo "Installed fixed read-only verification/Compose commands and guarded rollback; codex receives no general sudo or Docker access."