116 lines
5.3 KiB
Bash
116 lines
5.3 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
runner_die() { printf 'ERROR: %s\n' "$*" >&2; return 1; }
|
|
|
|
runner_ensure_account() {
|
|
local user=$1 home=$2 entry uid passwd_home shell groups uid_min
|
|
if ! entry=$(getent passwd "$user"); then
|
|
[[ ! -e $home && ! -L $home ]] || { runner_die "runner home already exists without its account: $home"; return 1; }
|
|
useradd --system --create-home --home-dir "$home" --shell /usr/sbin/nologin "$user" || return 1
|
|
return
|
|
fi
|
|
IFS=: read -r _ _ uid _ _ passwd_home shell <<<"$entry"
|
|
uid_min=$(awk '$1 == "UID_MIN" && $2 ~ /^[0-9]+$/ {print $2; exit}' /etc/login.defs)
|
|
[[ $uid =~ ^[0-9]+$ && $uid -gt 0 && $uid_min =~ ^[0-9]+$ && $uid -lt $uid_min && $passwd_home == "$home" && $shell == /usr/sbin/nologin ]] || { runner_die "existing $user account has an unexpected UID, home, or shell"; return 1; }
|
|
[[ $(id -gn "$user") == "$user" ]] || { runner_die "existing $user account has an unexpected primary group"; return 1; }
|
|
groups=$(id -nG "$user")
|
|
[[ $groups == "$user" ]] || { runner_die "existing $user account has unexpected supplementary groups: $groups"; return 1; }
|
|
[[ -d $home && ! -L $home && $(stat -c %U "$home") == "$user" ]] || { runner_die "existing $user home is missing or has unexpected ownership"; return 1; }
|
|
}
|
|
|
|
runner_ensure_image() {
|
|
local image=$1 size=$2 bytes=$3 actual filesystem
|
|
if [[ -e $image || -L $image ]]; then
|
|
[[ -f $image && ! -L $image ]] || { runner_die "existing runner image is not a regular file: $image"; return 1; }
|
|
actual=$(stat -c %s "$image") || return
|
|
[[ $actual == "$bytes" ]] || { runner_die "existing runner image has size $actual bytes; expected $bytes; refusing to resize or format"; return 1; }
|
|
filesystem=$(blkid -p -s TYPE -o value "$image" 2>/dev/null || true)
|
|
[[ $filesystem == ext4 ]] || { runner_die "existing runner image does not contain ext4; refusing to format"; return 1; }
|
|
return
|
|
fi
|
|
fallocate -l "$size" "$image" || return 1
|
|
mkfs.ext4 -F -q "$image" || return 1
|
|
actual=$(stat -c %s "$image") || return
|
|
filesystem=$(blkid -p -s TYPE -o value "$image" 2>/dev/null || true)
|
|
[[ $actual == "$bytes" && $filesystem == ext4 ]] || { runner_die 'new runner image failed size or ext4 verification'; return 1; }
|
|
}
|
|
|
|
runner_install_file() {
|
|
local source=$1 destination=$2 owner=$3 group=$4 mode=${5#0}
|
|
if [[ -e $destination || -L $destination ]]; then
|
|
[[ -f $destination && ! -L $destination ]] && cmp -s "$source" "$destination" || { runner_die "existing runner file differs from reviewed configuration: $destination"; return 1; }
|
|
[[ $(stat -c '%U:%G:%a' "$destination") == "$owner:$group:$mode" ]] || { runner_die "existing runner file has unexpected ownership or permissions: $destination"; return 1; }
|
|
return
|
|
fi
|
|
install -o "$owner" -g "$group" -m "0$mode" "$source" "$destination"
|
|
}
|
|
|
|
runner_install_content() {
|
|
local destination=$1 owner=$2 group=$3 mode=$4 temporary
|
|
temporary=$(mktemp)
|
|
cat >"$temporary"
|
|
if ! runner_install_file "$temporary" "$destination" "$owner" "$group" "$mode"; then
|
|
rm -f "$temporary"
|
|
return 1
|
|
fi
|
|
rm -f "$temporary"
|
|
}
|
|
|
|
runner_validate_mount() {
|
|
local image=$1 store=$2 target filesystem source backing mount_info
|
|
mount_info=$(findmnt --noheadings --output TARGET,FSTYPE,SOURCE --mountpoint "$store") || { runner_die 'runner store is not mounted'; return 1; }
|
|
read -r target filesystem source <<<"$mount_info" || { runner_die 'runner store mount information is empty'; return 1; }
|
|
[[ $target == "$store" && $filesystem == ext4 && $source == /dev/loop* ]] || { runner_die 'runner store is mounted from an unexpected target, filesystem, or device'; return 1; }
|
|
backing=$(losetup --noheadings --output BACK-FILE "$source") || { runner_die 'cannot identify the runner loop backing file'; return 1; }
|
|
[[ $(realpath -e "$backing") == "$(realpath -e "$image")" ]] || { runner_die 'runner store is backed by a different image'; return 1; }
|
|
}
|
|
|
|
runner_ensure_mount() {
|
|
local image=$1 store=$2 unit_name=$3 unit_file=$4 unit_tmp mounted=0
|
|
[[ -d $store && ! -L $store ]] || { runner_die "runner mount point is missing or is not a directory: $store"; return 1; }
|
|
if mountpoint -q "$store"; then
|
|
runner_validate_mount "$image" "$store" || return 1
|
|
mounted=1
|
|
fi
|
|
unit_tmp=$(mktemp)
|
|
if ! cat >"$unit_tmp" <<EOF
|
|
[Unit]
|
|
Description=Bounded storage for the AkylDash CI runner
|
|
Before=local-fs.target
|
|
|
|
[Mount]
|
|
What=$image
|
|
Where=$store
|
|
Type=ext4
|
|
Options=loop,nosuid,nodev
|
|
|
|
[Install]
|
|
WantedBy=local-fs.target
|
|
EOF
|
|
then
|
|
rm -f "$unit_tmp"
|
|
runner_die 'failed to prepare runner mount unit'
|
|
return 1
|
|
fi
|
|
if [[ -e $unit_file || -L $unit_file ]]; then
|
|
if [[ ! -f $unit_file || -L $unit_file ]] || ! cmp -s "$unit_tmp" "$unit_file"; then
|
|
rm -f "$unit_tmp"
|
|
runner_die "existing runner mount unit differs from the reviewed configuration: $unit_file"
|
|
return 1
|
|
fi
|
|
[[ $(stat -c '%u:%g:%a' "$unit_file") == "$(id -u):$(id -g):644" ]] || { rm -f "$unit_tmp"; runner_die 'existing runner mount unit has unexpected ownership or permissions'; return 1; }
|
|
else
|
|
install -m 0644 "$unit_tmp" "$unit_file" || { rm -f "$unit_tmp"; return 1; }
|
|
fi
|
|
rm -f "$unit_tmp"
|
|
|
|
systemctl daemon-reload || return 1
|
|
if ((mounted)); then
|
|
systemctl enable "$unit_name" || return 1
|
|
else
|
|
systemctl enable "$unit_name" || return 1
|
|
systemctl start "$unit_name" || return 1
|
|
runner_validate_mount "$image" "$store" || return 1
|
|
fi
|
|
}
|