Files
Echoion/packaging/install.sh
T
faumaray 639362fbf5 Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-16 19:59:57 +03:00

1352 lines
44 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
PROGRAM=${0##*/}
SERVICE_NAME=echoion.service
SERVICE_USER=echoion
RECORD_NAME=.echoion-install
MANAGED_MARKER=.echoion-managed
die() {
printf '%s: %s\n' "$PROGRAM" "$*" >&2
exit 1
}
warn() {
printf '%s: warning: %s\n' "$PROGRAM" "$*" >&2
}
usage() {
cat <<'EOF'
Usage: install.sh [OPTIONS]
Install an Echoion release without starting it by default.
Options:
--mode MODE system, user, or portable (default: user)
--prefix PATH installation root
--data-layout LAYOUT standard or self-contained (default: standard)
--config-dir PATH override the configuration directory
--state-dir PATH override the writable state directory
--register-service SCOPE none, user, or system
--start enable and start the registered service
--non-interactive accept defaults instead of prompting
-h, --help show this help
System and user installs register a matching service by default. Portable
installs do not register a service unless explicitly requested. Registration
alone leaves the service disabled and stopped; only --start activates it.
EOF
}
require_value() {
[[ $# -ge 2 && -n $2 ]] || die "$1 requires a value"
}
validate_choice() {
local value=$1
local label=$2
shift 2
local candidate
for candidate in "$@"; do
[[ $value == "$candidate" ]] && return 0
done
die "invalid $label '$value' (expected: $*)"
}
reject_control_characters() {
local label=$1
local value=$2
[[ ! $value =~ [[:cntrl:]] ]] ||
die "$label contains an unsupported control character"
}
absolute_path() {
local value=$1
realpath -m -- "$value"
}
prompt_value() {
local variable=$1
local label=$2
local default_value=$3
local answer
printf '%s [%s]: ' "$label" "$default_value" >&2
IFS= read -r answer || true
printf -v "$variable" '%s' "${answer:-$default_value}"
}
prompt_yes_no() {
local variable=$1
local label=$2
local answer
printf '%s [y/N]: ' "$label" >&2
IFS= read -r answer || true
case $answer in
y | Y | yes | YES | Yes) printf -v "$variable" '%s' 1 ;;
*) printf -v "$variable" '%s' 0 ;;
esac
}
manifest_version() {
local manifest=$1
local line
while IFS= read -r line; do
if [[ $line =~ \"version\"[[:space:]]*:[[:space:]]*\"([^\"]+)\" ]]; then
printf '%s\n' "${BASH_REMATCH[1]}"
return 0
fi
done < "$manifest"
return 1
}
manifest_is_v2_with_files() {
local manifest=$1
local line
local schema_ok=0
local files_ok=0
while IFS= read -r line; do
if [[ $line =~ \"schemaVersion\"[[:space:]]*:[[:space:]]*2([,[:space:]]|$) ]]; then
schema_ok=1
fi
if [[ $line =~ \"path\"[[:space:]]*:[[:space:]]*\"[^\"]+\" ]]; then
files_ok=1
fi
done < "$manifest"
[[ $schema_ok -eq 1 && $files_ok -eq 1 ]]
}
sha256_file() {
local result
result=$(sha256sum -- "$1")
printf '%s\n' "${result%% *}"
}
json_escape() {
local value=$1
value=${value//\\/\\\\}
value=${value//\"/\\\"}
printf '%s' "$value"
}
systemd_setting_escape() {
local value=$1
value=${value//\\/\\\\}
value=${value//\"/\\\"}
value=${value//%/%%}
printf '%s' "$value"
}
systemd_exec_escape() {
local value
value=$(systemd_setting_escape "$1")
value=${value//\$/\$\$}
printf '%s' "$value"
}
render_config() {
local source=$1
local destination=$2
local web_root=$3
local escaped
local temporary
local line
local found=0
escaped=$(json_escape "$web_root")
temporary=$(mktemp --tmpdir="$(dirname -- "$destination")" '.config.json.XXXXXX')
while IFS= read -r line || [[ -n $line ]]; do
if [[ $line =~ ^([[:space:]]*)\"webRoot\"[[:space:]]*: ]]; then
printf '%s"webRoot": "%s",\n' "${BASH_REMATCH[1]}" "$escaped" >> "$temporary"
found=$((found + 1))
else
printf '%s\n' "$line" >> "$temporary"
fi
done < "$source"
if [[ $found -ne 1 ]]; then
rm -f -- "$temporary"
die "configuration template has no unique webRoot field"
fi
chmod 0600 "$temporary"
mv -f -- "$temporary" "$destination"
}
effective_uid() {
printf '%s\n' "${EUID:-$(id -u)}"
}
validate_registration_identity() {
local scope=$1
local uid=$2
if [[ $scope == user && $uid -eq 0 ]]; then
die "refusing to register a root user service; use system scope instead"
fi
}
validate_trusted_path_ancestors() {
local scope=$1
local path=$2
local service_uid=$3
local require_leaf_owner=${4:-0}
local current owner mode root_owner
root_owner=$(stat -c '%u' -- /)
current=$path
while [[ ! -e $current ]]; do
[[ $current != / ]] || break
current=$(dirname -- "$current")
done
while :; do
[[ -d $current && ! -L $current ]] ||
die "service path component is not a real directory: '$current'"
owner=$(stat -c '%u' -- "$current")
mode=$(stat -c '%a' -- "$current")
if (( (8#$mode & 0022) != 0 )); then
die "service path component is group/world-writable: '$current'"
fi
if [[ $scope == system ]]; then
[[ $owner -eq $root_owner ]] ||
die "system service path is not owned by root: '$current'"
elif [[ $owner -ne $root_owner && $owner -ne $service_uid ]]; then
die "user service path has an untrusted owner: '$current'"
fi
[[ $current != / ]] || break
current=$(dirname -- "$current")
done
if [[ $require_leaf_owner -eq 1 ]]; then
owner=$(stat -c '%u' -- "$path")
if [[ $scope == system ]]; then
[[ $owner -eq $root_owner ]] || die "system service prefix is not owned by root: '$path'"
else
[[ $owner -eq $service_uid ]] ||
die "user service prefix is not owned by uid $service_uid: '$path'"
fi
fi
}
acquire_prefix_lock() {
local lock_prefix=$1
command -v flock >/dev/null 2>&1 || die "flock is required for safe installation"
exec {INSTALL_LOCK_FD}< "$lock_prefix"
flock -n "$INSTALL_LOCK_FD" || die "another Echoion install or uninstall is active for '$lock_prefix'"
}
reject_temporary_prefix() {
local prefix=$1
local candidate
for candidate in /tmp /var/tmp /dev/shm /run "${TMPDIR:-}" "${XDG_RUNTIME_DIR:-}"; do
[[ -n $candidate ]] || continue
candidate=$(absolute_path "$candidate")
case $prefix in
"$candidate" | "$candidate"/*)
die "refusing to register a service from temporary path '$prefix'"
;;
esac
done
}
validate_mutable_path_ancestors() {
local scope=$1
local path=$2
local service_uid=$3
local existing=$path
if [[ -e $existing || -L $existing ]]; then
existing=$(dirname -- "$existing")
else
while [[ ! -e $existing ]]; do
[[ $existing != / ]] || break
existing=$(dirname -- "$existing")
done
fi
validate_trusted_path_ancestors "$scope" "$existing" "$service_uid" 0
}
validate_mutable_directory() {
local scope=$1
local path=$2
local expected_uid=$3
local service_uid=$4
local owner mode
[[ -d $path && ! -L $path ]] || die "mutable service path is not a real directory: '$path'"
owner=$(stat -c '%u' -- "$path")
mode=$(stat -c '%a' -- "$path")
[[ $owner -eq $expected_uid ]] || die "mutable service path has the wrong owner: '$path'"
(( (8#$mode & 0022) == 0 )) ||
die "mutable service path is group/world-writable: '$path'"
validate_trusted_path_ancestors "$scope" "$(dirname -- "$path")" "$service_uid" 0
}
systemctl_for() {
local scope=$1
shift
if [[ $scope == user ]]; then
systemctl --user "$@"
else
systemctl "$@"
fi
}
safe_recorded_unit_path() {
local scope=$1
local unit=$2
[[ $unit == /* && $unit != *'/../'* && $unit != *'/./'* ]] || return 1
case $scope:$unit in
system:/etc/systemd/system/echoion.service) return 0 ;;
user:/*/systemd/user/echoion.service) return 0 ;;
*) return 1 ;;
esac
}
render_unit() {
local template=$1
local destination=$2
local scope=$3
local home=$4
local core=$5
local state=$6
local executable=$7
local config=$8
local escaped_home escaped_core escaped_state escaped_executable escaped_config
local escaped_exec_state
local line
escaped_home=$(systemd_setting_escape "$home")
escaped_core=$(systemd_setting_escape "$core")
escaped_state=$(systemd_setting_escape "$state")
escaped_executable=$(systemd_exec_escape "$executable")
escaped_config=$(systemd_exec_escape "$config")
escaped_exec_state=$(systemd_exec_escape "$state")
: > "$destination"
while IFS= read -r line || [[ -n $line ]]; do
case $line in
@SERVICE_IDENTITY@)
if [[ $scope == system ]]; then
printf 'User=%s\nGroup=%s\n' "$SERVICE_USER" "$SERVICE_USER" >> "$destination"
fi
;;
@WORKING_DIRECTORY@)
printf 'WorkingDirectory="%s"\n' "$escaped_home" >> "$destination"
;;
@ENVIRONMENT_HOME@)
printf 'Environment="ECHOION_HOME=%s"\n' "$escaped_home" >> "$destination"
;;
@ENVIRONMENT_CORE@)
printf 'Environment="ECHOION_CORE_PATH=%s"\n' "$escaped_core" >> "$destination"
;;
@ENVIRONMENT_STATE@)
printf 'Environment="ECHOION_STATE_DIR=%s"\n' "$escaped_state" >> "$destination"
;;
@EXEC_START@)
printf 'ExecStart="%s" --config "%s" --state-dir "%s"\n' \
"$escaped_executable" "$escaped_config" "$escaped_exec_state" >> "$destination"
;;
@INSTALL_TARGET@)
if [[ $scope == system ]]; then
printf 'WantedBy=multi-user.target\n' >> "$destination"
else
printf 'WantedBy=default.target\n' >> "$destination"
fi
;;
*) printf '%s\n' "$line" >> "$destination" ;;
esac
done < "$template"
}
render_portable_launcher() {
local destination=$1
cat > "$destination" <<'EOF'
#!/usr/bin/env bash
# Echoion portable launcher
set -euo pipefail
die() {
printf 'echoion: %s\n' "$*" >&2
exit 1
}
launcher=$(realpath -e -- "${BASH_SOURCE[0]}") || die 'cannot resolve launcher path'
prefix=$(cd -P -- "$(dirname -- "$launcher")" && pwd -P) ||
die 'cannot resolve portable prefix'
[[ $prefix != / ]] || die 'refusing to run from the filesystem root'
current=$prefix/current
versions=$prefix/versions
[[ -d $versions && ! -L $versions ]] ||
die "versions directory is missing or unsafe: $versions"
[[ -L $current ]] || die "current release link is missing: $current"
target=$(readlink -- "$current")
[[ $target =~ ^versions/[A-Za-z0-9][A-Za-z0-9._-]{0,63}$ ]] ||
die "unsafe current release target: $target"
daemon=$current/bin/echoiond
config=$prefix/config/config.json
state=$prefix/data
core=$current/lib/Echoion.ts
[[ -x $daemon ]] || die "daemon is missing or not executable: $daemon"
[[ -f $config && ! -L $config ]] || die "configuration is missing or unsafe: $config"
[[ -d $state && ! -L $state ]] || die "state directory is missing or unsafe: $state"
[[ -f $core ]] || die "Core library is missing: $core"
for argument in "$@"; do
case $argument in
--config | --config=* | --state-dir | --state-dir=*)
die "$argument is managed by the portable launcher"
;;
esac
done
export ECHOION_HOME=$current
export ECHOION_CORE_PATH=$core
export ECHOION_STATE_DIR=$state
exec "$daemon" --config "$config" --state-dir "$state" "$@"
EOF
}
load_old_record() {
local record=$1
local key value
OLD_PREFIX=
OLD_SERVICE_SCOPE=none
OLD_SERVICE_UNIT=
OLD_SERVICE_SHA256=
OLD_CREATED_SYSTEM_USER=0
OLD_SERVICE_UID=
OLD_SERVICE_GID=
OLD_INSTALL_ID=
OLD_CONFIG_PURGE_ALLOWED=0
OLD_CONFIG_DIR_DEVICE=
OLD_CONFIG_DIR_INODE=
OLD_CONFIG_DIR_UID=
OLD_CONFIG_DIR_GID=
OLD_CONFIG_MARKER_SHA256=
OLD_STATE_PURGE_ALLOWED=0
OLD_STATE_DIR_DEVICE=
OLD_STATE_DIR_INODE=
OLD_STATE_DIR_UID=
OLD_STATE_DIR_GID=
OLD_STATE_MARKER_SHA256=
OLD_RELOCATABLE=0
OLD_LAUNCHER=
OLD_LAUNCHER_SHA256=
[[ -f $record ]] || return 0
while IFS='=' read -r key value; do
case $key in
prefix) OLD_PREFIX=$value ;;
service_scope) OLD_SERVICE_SCOPE=$value ;;
service_unit) OLD_SERVICE_UNIT=$value ;;
service_unit_sha256) OLD_SERVICE_SHA256=$value ;;
created_system_user) OLD_CREATED_SYSTEM_USER=$value ;;
service_uid) OLD_SERVICE_UID=$value ;;
service_gid) OLD_SERVICE_GID=$value ;;
install_id) OLD_INSTALL_ID=$value ;;
config_purge_allowed) OLD_CONFIG_PURGE_ALLOWED=$value ;;
config_dir_device) OLD_CONFIG_DIR_DEVICE=$value ;;
config_dir_inode) OLD_CONFIG_DIR_INODE=$value ;;
config_dir_uid) OLD_CONFIG_DIR_UID=$value ;;
config_dir_gid) OLD_CONFIG_DIR_GID=$value ;;
config_marker_sha256) OLD_CONFIG_MARKER_SHA256=$value ;;
state_purge_allowed) OLD_STATE_PURGE_ALLOWED=$value ;;
state_dir_device) OLD_STATE_DIR_DEVICE=$value ;;
state_dir_inode) OLD_STATE_DIR_INODE=$value ;;
state_dir_uid) OLD_STATE_DIR_UID=$value ;;
state_dir_gid) OLD_STATE_DIR_GID=$value ;;
state_marker_sha256) OLD_STATE_MARKER_SHA256=$value ;;
relocatable) OLD_RELOCATABLE=$value ;;
launcher) OLD_LAUNCHER=$value ;;
launcher_sha256) OLD_LAUNCHER_SHA256=$value ;;
esac
done < "$record"
case $OLD_RELOCATABLE in
0)
[[ $OLD_PREFIX == "$prefix" ]] ||
die "install record belongs to '$OLD_PREFIX', not '$prefix'"
;;
1)
[[ $OLD_PREFIX == . ]] || die "relocatable install record has an unsafe prefix"
;;
*) die "install record has an invalid relocatable value" ;;
esac
}
verify_recorded_unit() {
local scope=$1
local unit=$2
local expected_hash=$3
[[ -n $unit && -n $expected_hash ]] || die "existing service record is incomplete"
safe_recorded_unit_path "$scope" "$unit" || die "existing service record has an unsafe unit path"
[[ -f $unit ]] || die "recorded service unit is missing: $unit"
[[ $(sha256_file "$unit") == "$expected_hash" ]] ||
die "recorded service unit was modified; refusing to overwrite it: $unit"
}
verify_recorded_launcher() {
local launcher=$1
local expected_hash=$2
local path
[[ $launcher == echoion && $expected_hash =~ ^[0-9a-f]{64}$ ]] ||
die "existing portable launcher record is incomplete or unsafe"
path=$prefix/$launcher
[[ -f $path && ! -L $path && -x $path ]] ||
die "recorded portable launcher is missing or unsafe: $path"
[[ $(sha256_file "$path") == "$expected_hash" ]] ||
die "recorded portable launcher was modified; refusing to overwrite it: $path"
}
remove_recorded_service() {
local scope=$1
local unit=$2
local expected_hash=$3
verify_recorded_unit "$scope" "$unit" "$expected_hash"
systemctl_for "$scope" disable --now "$SERVICE_NAME" || return 1
rm -f -- "$unit" || return 1
systemctl_for "$scope" daemon-reload || return 1
}
ensure_system_user() {
local state=$1
local passwd_entry shell group_gid
if id -u "$SERVICE_USER" >/dev/null 2>&1; then
SERVICE_UID=$(id -u "$SERVICE_USER")
SERVICE_GID=$(id -g "$SERVICE_USER")
passwd_entry=$(getent passwd "$SERVICE_USER") ||
die "could not inspect existing $SERVICE_USER account"
IFS=: read -r _ _ _ _ _ _ shell <<< "$passwd_entry"
case ${shell##*/} in
nologin | false) ;;
*) die "existing $SERVICE_USER account does not use a non-login shell" ;;
esac
group_gid=$(getent group "$SERVICE_USER" | cut -d: -f3) || true
[[ -n $group_gid && $group_gid -eq $SERVICE_GID ]] ||
die "existing $SERVICE_USER account has no matching primary group"
if [[ ${OLD_CREATED_SYSTEM_USER:-0} == 1 ]]; then
[[ ${OLD_SERVICE_UID:-} =~ ^[0-9]+$ && ${OLD_SERVICE_GID:-} =~ ^[0-9]+$ ]] ||
die "installer-created service account record has no uid/gid identity"
[[ $SERVICE_UID -eq $OLD_SERVICE_UID && $SERVICE_GID -eq $OLD_SERVICE_GID ]] ||
die "the recorded $SERVICE_USER account was deleted or replaced"
fi
return 0
fi
local nologin=/usr/sbin/nologin
[[ -x $nologin ]] || nologin=/sbin/nologin
useradd --system --user-group --home-dir "$state" --shell "$nologin" \
--no-create-home "$SERVICE_USER"
CREATED_SYSTEM_USER=1
ACCOUNT_CREATED_THIS_RUN=1
SERVICE_UID=$(id -u "$SERVICE_USER")
SERVICE_GID=$(id -g "$SERVICE_USER")
ACCOUNT_IDENTITY_CAPTURED=1
}
remove_new_system_user() {
[[ ${ACCOUNT_CREATED_THIS_RUN:-0} -eq 1 ]] || return 0
if [[ ${ACCOUNT_IDENTITY_CAPTURED:-0} -eq 0 ]]; then
userdel "$SERVICE_USER" || warn "could not remove newly created $SERVICE_USER account"
elif id -u "$SERVICE_USER" >/dev/null 2>&1 &&
[[ $(id -u "$SERVICE_USER") -eq ${SERVICE_UID:-999999} ]] &&
[[ $(id -g "$SERVICE_USER") -eq ${SERVICE_GID:-999999} ]]; then
userdel "$SERVICE_USER" || warn "could not remove newly created $SERVICE_USER account"
else
warn "new $SERVICE_USER account identity changed; refusing automatic deletion"
fi
}
validate_service_access() {
local scope=$1
local executable=$2
local config=$3
local state=$4
if [[ $scope == user ]]; then
[[ -x $executable ]] || die "service user cannot execute '$executable'"
[[ -r $config ]] || die "service user cannot read '$config'"
[[ -w $state ]] || die "service user cannot write '$state'"
return 0
fi
command -v runuser >/dev/null 2>&1 ||
die "runuser is required to validate the system service account"
runuser -u "$SERVICE_USER" -- test -x "$executable" ||
die "$SERVICE_USER cannot traverse the prefix or execute echoiond"
runuser -u "$SERVICE_USER" -- test -r "$config" ||
die "$SERVICE_USER cannot read '$config'"
runuser -u "$SERVICE_USER" -- test -w "$state" ||
die "$SERVICE_USER cannot write '$state'"
}
normalize_and_validate_system_runtime() {
local root=$1
local foreign writable path
chown -R root:root -- "$root"
foreign=$(find "$root" \( ! -uid 0 -o ! -gid 0 \) -print -quit)
[[ -z $foreign ]] || die "immutable runtime entry is not root-owned: '$foreign'"
foreign=$(find "$root" ! -type l -perm /022 -print -quit)
[[ -z $foreign ]] || die "immutable runtime entry is group/world-writable: '$foreign'"
writable=$(runuser -u "$SERVICE_USER" -- find "$root" -writable -print -quit) ||
die "could not verify immutable runtime permissions as $SERVICE_USER"
[[ -z $writable ]] || die "$SERVICE_USER can modify immutable runtime entry: '$writable'"
for path in "$root" "$(dirname -- "$root")" "$(dirname -- "$(dirname -- "$root")")"; do
if runuser -u "$SERVICE_USER" -- test -w "$path"; then
die "$SERVICE_USER can replace immutable runtime path: '$path'"
fi
done
}
new_install_id() {
local value
if [[ -r /proc/sys/kernel/random/uuid ]]; then
IFS= read -r value < /proc/sys/kernel/random/uuid
else
value=$(printf '%s:%s:%s:%s' "$prefix" "$$" "$RANDOM" "$(date +%s%N)" |
sha256sum)
value=${value%% *}
fi
printf '%s\n' "$value"
}
write_managed_marker() {
local directory=$1
local marker=$directory/$MANAGED_MARKER
local temporary
temporary=$(mktemp --tmpdir="$directory" '.echoion-managed.XXXXXX')
{
printf 'format=1\n'
printf 'install_id=%s\n' "$INSTALL_ID"
printf 'relocatable=%s\n' "$RELOCATABLE"
printf 'prefix=%s\n' "$INSTALL_RECORD_PREFIX"
} > "$temporary"
chmod 0600 "$temporary"
mv -f -- "$temporary" "$marker"
}
marker_matches() {
local directory=$1
local expected_hash=$2
local marker=$directory/$MANAGED_MARKER
[[ -f $marker && ! -L $marker && -n $expected_hash ]] || return 1
[[ $(sha256_file "$marker") == "$expected_hash" ]] || return 1
grep -Fx -- "install_id=$INSTALL_ID" "$marker" >/dev/null || return 1
grep -Fx -- "prefix=$INSTALL_RECORD_PREFIX" "$marker" >/dev/null
}
capture_managed_directory() {
local kind=$1
local directory=$2
local old_allowed=$3
local old_device=$4
local old_inode=$5
local old_uid=$6
local old_gid=$7
local old_marker_hash=$8
local created=$9
local allowed=0 marker_hash='' device inode uid gid
device=$(stat -c '%d' -- "$directory")
inode=$(stat -c '%i' -- "$directory")
uid=$(stat -c '%u' -- "$directory")
gid=$(stat -c '%g' -- "$directory")
if [[ $old_allowed == 1 && $device == "$old_device" && $inode == "$old_inode" &&
$uid == "$old_uid" && $gid == "$old_gid" ]] && marker_matches "$directory" "$old_marker_hash"; then
allowed=1
marker_hash=$old_marker_hash
elif [[ $created -eq 1 ]]; then
write_managed_marker "$directory"
allowed=1
marker_hash=$(sha256_file "$directory/$MANAGED_MARKER")
elif [[ $old_allowed == 1 ]]; then
warn "$kind directory identity changed; disabling automatic purge for '$directory'"
fi
printf -v "${kind^^}_PURGE_ALLOWED" '%s' "$allowed"
printf -v "${kind^^}_DIR_DEVICE" '%s' "$device"
printf -v "${kind^^}_DIR_INODE" '%s' "$inode"
printf -v "${kind^^}_DIR_UID" '%s' "$uid"
printf -v "${kind^^}_DIR_GID" '%s' "$gid"
printf -v "${kind^^}_MARKER_SHA256" '%s' "$marker_hash"
}
snapshot_path_metadata() {
local path=$1
[[ -e $path && ! -L $path ]] || return 0
stat -c '%d:%i:%u:%g:%a' -- "$path"
}
restore_path_metadata() {
local path=$1
local snapshot=$2
local expected_device expected_inode uid gid mode current_device current_inode
[[ -n $snapshot ]] || return 0
IFS=: read -r expected_device expected_inode uid gid mode <<< "$snapshot"
[[ -e $path && ! -L $path ]] || return 1
current_device=$(stat -c '%d' -- "$path") || return 1
current_inode=$(stat -c '%i' -- "$path") || return 1
[[ $current_device == "$expected_device" && $current_inode == "$expected_inode" ]] || return 1
chown "$uid:$gid" -- "$path" || return 1
chmod "$mode" -- "$path"
}
write_record() {
local destination=$1
local temporary
local unit=$2
local unit_hash=$3
temporary=$(mktemp --tmpdir="$(dirname -- "$destination")" '.echoion-install.XXXXXX')
RECORD_TEMPORARY=$temporary
umask 077
{
printf 'format=1\n'
printf 'mode=%s\n' "$mode"
printf 'relocatable=%s\n' "$RELOCATABLE"
printf 'prefix=%s\n' "$INSTALL_RECORD_PREFIX"
printf 'version=%s\n' "$version"
printf 'data_layout=%s\n' "$data_layout"
printf 'config_dir=%s\n' "$INSTALL_RECORD_CONFIG_DIR"
printf 'state_dir=%s\n' "$INSTALL_RECORD_STATE_DIR"
printf 'config_created=%s\n' "$CONFIG_CREATED"
printf 'service_scope=%s\n' "$register_service"
printf 'service_name=%s\n' "$SERVICE_NAME"
printf 'service_unit=%s\n' "$unit"
printf 'service_unit_sha256=%s\n' "$unit_hash"
printf 'created_system_user=%s\n' "$CREATED_SYSTEM_USER"
printf 'service_uid=%s\n' "$SERVICE_UID"
printf 'service_gid=%s\n' "$SERVICE_GID"
printf 'install_id=%s\n' "$INSTALL_ID"
printf 'config_purge_allowed=%s\n' "$CONFIG_PURGE_ALLOWED"
printf 'config_dir_device=%s\n' "$CONFIG_DIR_DEVICE"
printf 'config_dir_inode=%s\n' "$CONFIG_DIR_INODE"
printf 'config_dir_uid=%s\n' "$CONFIG_DIR_UID"
printf 'config_dir_gid=%s\n' "$CONFIG_DIR_GID"
printf 'config_marker_sha256=%s\n' "$CONFIG_MARKER_SHA256"
printf 'state_purge_allowed=%s\n' "$STATE_PURGE_ALLOWED"
printf 'state_dir_device=%s\n' "$STATE_DIR_DEVICE"
printf 'state_dir_inode=%s\n' "$STATE_DIR_INODE"
printf 'state_dir_uid=%s\n' "$STATE_DIR_UID"
printf 'state_dir_gid=%s\n' "$STATE_DIR_GID"
printf 'state_marker_sha256=%s\n' "$STATE_MARKER_SHA256"
printf 'launcher=%s\n' "$LAUNCHER_RECORD_PATH"
printf 'launcher_sha256=%s\n' "$LAUNCHER_SHA256"
} > "$temporary"
chmod 0600 "$temporary"
mv -f -- "$temporary" "$destination"
RECORD_TEMPORARY=
}
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
return 0
fi
mode=user
mode_set=0
prefix=
prefix_set=0
data_layout=standard
data_layout_set=0
config_dir=
state_dir=
register_service=
register_service_set=0
start=0
start_set=0
non_interactive=0
while [[ $# -gt 0 ]]; do
case $1 in
--mode)
require_value "$@"
mode=$2
mode_set=1
shift 2
;;
--prefix)
require_value "$@"
prefix=$2
prefix_set=1
shift 2
;;
--data-layout)
require_value "$@"
data_layout=$2
data_layout_set=1
shift 2
;;
--config-dir)
require_value "$@"
config_dir=$2
shift 2
;;
--state-dir)
require_value "$@"
state_dir=$2
shift 2
;;
--register-service)
require_value "$@"
register_service=$2
register_service_set=1
shift 2
;;
--start)
start=1
start_set=1
shift
;;
--non-interactive)
non_interactive=1
shift
;;
-h | --help)
usage
exit 0
;;
--)
shift
[[ $# -eq 0 ]] || die "unexpected positional arguments: $*"
;;
*) die "unknown option: $1" ;;
esac
done
if [[ $non_interactive -eq 0 && -t 0 && -t 1 ]]; then
[[ $mode_set -eq 1 ]] || prompt_value mode 'Install mode (system/user/portable)' user
validate_choice "$mode" mode system user portable
case $mode in
system) default_prefix=/opt/echoion ;;
user) default_prefix=${HOME:?HOME is required}/.local/opt/echoion ;;
portable) default_prefix=$PWD/echoion ;;
esac
[[ $prefix_set -eq 1 ]] || prompt_value prefix 'Install prefix' "$default_prefix"
[[ $data_layout_set -eq 1 ]] ||
prompt_value data_layout 'Data layout (standard/self-contained)' standard
if [[ $register_service_set -eq 0 ]]; then
case $mode in
system) default_service=system ;;
user) default_service=user ;;
portable) default_service=none ;;
esac
prompt_value register_service 'Register service (none/user/system)' "$default_service"
fi
[[ $start_set -eq 1 ]] || prompt_yes_no start 'Enable and start the service now'
fi
validate_choice "$mode" mode system user portable
validate_choice "$data_layout" data-layout standard self-contained
if [[ -z $register_service ]]; then
case $mode in
system) register_service=system ;;
user) register_service=user ;;
portable) register_service=none ;;
esac
fi
validate_choice "$register_service" service-scope none user system
[[ $start -eq 0 || $register_service != none ]] || die "--start requires a registered service"
INSTALL_UID=$(effective_uid)
validate_registration_identity "$register_service" "$INSTALL_UID"
case $mode in
system) default_prefix=/opt/echoion ;;
user) default_prefix=${HOME:?HOME is required}/.local/opt/echoion ;;
portable) default_prefix=$PWD/echoion ;;
esac
prefix=${prefix:-$default_prefix}
reject_control_characters prefix "$prefix"
prefix=$(absolute_path "$prefix")
[[ $prefix != / ]] || die "the filesystem root cannot be an install prefix"
if [[ $mode == system || $register_service == system ]]; then
[[ $INSTALL_UID -eq 0 ]] || die "system installation/service registration requires root"
fi
RUNTIME_TRUST_SCOPE=$register_service
if [[ $RUNTIME_TRUST_SCOPE == none && $mode == system ]]; then
RUNTIME_TRUST_SCOPE=system
fi
if [[ $RUNTIME_TRUST_SCOPE != none ]]; then
reject_temporary_prefix "$prefix"
validate_trusted_path_ancestors "$RUNTIME_TRUST_SCOPE" "$prefix" "$INSTALL_UID" 0
fi
if [[ $data_layout == self-contained ]]; then
default_config_dir=$prefix/config
default_state_dir=$prefix/data
elif [[ $mode == system || $register_service == system ]]; then
default_config_dir=/etc/echoion
default_state_dir=/var/lib/echoion
else
default_config_dir=${XDG_CONFIG_HOME:-${HOME:?HOME is required}/.config}/echoion
default_state_dir=${XDG_STATE_HOME:-${HOME:?HOME is required}/.local/state}/echoion
fi
config_dir=${config_dir:-$default_config_dir}
state_dir=${state_dir:-$default_state_dir}
reject_control_characters config-dir "$config_dir"
reject_control_characters state-dir "$state_dir"
config_dir=$(absolute_path "$config_dir")
state_dir=$(absolute_path "$state_dir")
[[ $config_dir != / && $state_dir != / ]] || die "config/state directory cannot be '/'"
for mutable_path in "$config_dir" "$state_dir"; do
case $mutable_path in
/etc | /var | /usr | /usr/local | /opt | /home | /root | /tmp | /var/tmp | /run)
die "config/state directory must be a dedicated Echoion directory: '$mutable_path'"
;;
esac
case $prefix in
"$mutable_path" | "$mutable_path"/*)
die "config/state directories cannot contain the installation prefix"
;;
esac
case $mutable_path in
"$prefix" | "$prefix/current" | "$prefix/current"/* | "$prefix/versions" | "$prefix/versions"/*)
die "config/state directories cannot be placed in the immutable runtime tree"
;;
esac
done
if [[ $RUNTIME_TRUST_SCOPE != none ]]; then
validate_mutable_path_ancestors "$RUNTIME_TRUST_SCOPE" "$config_dir" "$INSTALL_UID"
validate_mutable_path_ancestors "$RUNTIME_TRUST_SCOPE" "$state_dir" "$INSTALL_UID"
fi
config_file=$config_dir/config.json
RELOCATABLE=0
INSTALL_RECORD_PREFIX=$prefix
INSTALL_RECORD_CONFIG_DIR=$config_dir
INSTALL_RECORD_STATE_DIR=$state_dir
if [[ $mode == portable && $data_layout == self-contained &&
$config_dir == "$prefix/config" && $state_dir == "$prefix/data" ]]; then
RELOCATABLE=1
INSTALL_RECORD_PREFIX=.
INSTALL_RECORD_CONFIG_DIR=config
INSTALL_RECORD_STATE_DIR=data
fi
SCRIPT_DIR=$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
PAYLOAD_ROOT=$SCRIPT_DIR
manifest=$PAYLOAD_ROOT/manifest.json
[[ -f $manifest ]] || die "payload manifest is missing: $manifest"
version=$(manifest_version "$manifest") || die "payload manifest has no version"
[[ $version =~ ^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$ ]] || die "unsafe release version '$version'"
manifest_is_v2_with_files "$manifest" ||
die "payload manifest must use schemaVersion 2 and contain files"
[[ -x $PAYLOAD_ROOT/bin/echoiond ]] || die "payload executable is missing or not executable"
[[ -f $PAYLOAD_ROOT/lib/Echoion.ts ]] || die "payload Core library is missing"
[[ -f $PAYLOAD_ROOT/share/echoion/config.example.json ]] || die "configuration template is missing"
[[ -d $PAYLOAD_ROOT/share/echoion/web ]] || die "web assets are missing"
[[ -f $PAYLOAD_ROOT/service/echoion.service.in ]] || die "service template is missing"
[[ -x $PAYLOAD_ROOT/uninstall.sh ]] || die "payload uninstaller is missing or not executable"
if ! "$PAYLOAD_ROOT/bin/echoiond" --validate-install "$PAYLOAD_ROOT"; then
die "offline source payload validation failed for Echoion $version"
fi
CREATED_SYSTEM_USER=0
ACCOUNT_CREATED_THIS_RUN=0
ACCOUNT_IDENTITY_CAPTURED=0
SERVICE_UID=
SERVICE_GID=
CONFIG_CREATED=0
CONFIG_DIR_CREATED=0
STATE_DIR_CREATED=0
CONFIG_DIR_METADATA_SNAPSHOT=
STATE_DIR_METADATA_SNAPSHOT=
CONFIG_FILE_METADATA_SNAPSHOT=
DESTINATION_CREATED=0
TRANSACTION_ACTIVE=0
INSTALL_COMMITTED=0
NEW_UNIT_MUTATION_ARMED=0
OLD_SERVICE_MUTATION_ARMED=0
HELPER_MUTATION_ARMED=0
LAUNCHER_MUTATION_ARMED=0
RECORD_TEMPORARY=
UNIT_TEMPORARY=
INSTALL_HELPER_TEMPORARY=
UNINSTALL_HELPER_TEMPORARY=
LAUNCHER_TEMPORARY=
LAUNCHER_PATH=$prefix/echoion
LAUNCHER_RECORD_PATH=
LAUNCHER_SHA256=
WANT_LAUNCHER=$RELOCATABLE
STAGING=
TRANSACTION_BACKUP_DIR=
restore_current_link() {
local target=$1
local temporary_directory
if [[ -z $target ]]; then
rm -f -- "$prefix/current"
return
fi
temporary_directory=$(mktemp -d --tmpdir="$prefix" '.current-rollback.XXXXXX') || return
ln -s -- "$target" "$temporary_directory/current" || return
mv -Tf -- "$temporary_directory/current" "$prefix/current" || return
rmdir -- "$temporary_directory" || true
}
rollback_install() {
set +e
if [[ $NEW_UNIT_MUTATION_ARMED -eq 1 ]]; then
systemctl_for "$register_service" disable --now "$SERVICE_NAME" || true
if [[ -f $TRANSACTION_BACKUP_DIR/new-scope-unit ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/new-scope-unit" "$unit_path"
else
rm -f -- "$unit_path"
fi
systemctl_for "$register_service" daemon-reload || true
fi
if [[ $OLD_SERVICE_MUTATION_ARMED -eq 1 && -f $TRANSACTION_BACKUP_DIR/old-scope-unit ]]; then
install -d -m 0755 -- "$(dirname -- "$OLD_SERVICE_UNIT")"
cp -a -- "$TRANSACTION_BACKUP_DIR/old-scope-unit" "$OLD_SERVICE_UNIT"
systemctl_for "$OLD_SERVICE_SCOPE" daemon-reload || true
fi
if [[ $HELPER_MUTATION_ARMED -eq 1 ]]; then
if [[ -f $TRANSACTION_BACKUP_DIR/install-helper ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/install-helper" "$prefix/install.sh"
else
rm -f -- "$prefix/install.sh"
fi
if [[ -f $TRANSACTION_BACKUP_DIR/uninstall-helper ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/uninstall-helper" "$prefix/uninstall.sh"
else
rm -f -- "$prefix/uninstall.sh"
fi
fi
if [[ $LAUNCHER_MUTATION_ARMED -eq 1 ]]; then
if [[ -f $TRANSACTION_BACKUP_DIR/launcher ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/launcher" "$LAUNCHER_PATH"
else
rm -f -- "$LAUNCHER_PATH"
fi
fi
restore_current_link "$old_current"
if [[ -f $TRANSACTION_BACKUP_DIR/install-record ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/install-record" "$record"
else
rm -f -- "$record"
fi
if [[ -n ${OLD_SERVICE_UNIT:-} && -f ${OLD_SERVICE_UNIT:-} ]]; then
if [[ $OLD_SERVICE_WAS_ENABLED -eq 1 ]]; then
systemctl_for "$OLD_SERVICE_SCOPE" enable "$SERVICE_NAME" || true
else
systemctl_for "$OLD_SERVICE_SCOPE" disable "$SERVICE_NAME" || true
fi
if [[ $OLD_SERVICE_WAS_ACTIVE -eq 1 ]]; then
systemctl_for "$OLD_SERVICE_SCOPE" restart "$SERVICE_NAME" || true
else
systemctl_for "$OLD_SERVICE_SCOPE" stop "$SERVICE_NAME" || true
fi
fi
}
cleanup_install() {
local status=$?
trap - EXIT
if [[ $status -ne 0 && $TRANSACTION_ACTIVE -eq 1 && $INSTALL_COMMITTED -eq 0 ]]; then
rollback_install
fi
[[ -z $RECORD_TEMPORARY ]] || rm -f -- "$RECORD_TEMPORARY"
[[ -z $UNIT_TEMPORARY ]] || rm -f -- "$UNIT_TEMPORARY"
[[ -z $INSTALL_HELPER_TEMPORARY ]] || rm -f -- "$INSTALL_HELPER_TEMPORARY"
[[ -z $UNINSTALL_HELPER_TEMPORARY ]] || rm -f -- "$UNINSTALL_HELPER_TEMPORARY"
[[ -z $LAUNCHER_TEMPORARY ]] || rm -f -- "$LAUNCHER_TEMPORARY"
[[ -z $STAGING ]] || rm -rf -- "$STAGING"
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 && $DESTINATION_CREATED -eq 1 ]]; then
rm -rf -- "$destination"
fi
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 && $CONFIG_CREATED -eq 1 ]]; then
rm -f -- "$config_file"
fi
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 && $CONFIG_DIR_CREATED -eq 1 ]]; then
rm -rf -- "$config_dir"
fi
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 && $STATE_DIR_CREATED -eq 1 &&
$state_dir != "$config_dir" ]]; then
rm -rf -- "$state_dir"
fi
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 ]]; then
restore_path_metadata "$config_file" "$CONFIG_FILE_METADATA_SNAPSHOT" ||
warn "could not restore configuration file metadata for '$config_file'"
restore_path_metadata "$state_dir" "$STATE_DIR_METADATA_SNAPSHOT" ||
warn "could not restore state directory metadata for '$state_dir'"
restore_path_metadata "$config_dir" "$CONFIG_DIR_METADATA_SNAPSHOT" ||
warn "could not restore configuration directory metadata for '$config_dir'"
fi
[[ -z $TRANSACTION_BACKUP_DIR ]] || rm -rf -- "$TRANSACTION_BACKUP_DIR"
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 ]]; then
remove_new_system_user
fi
exit "$status"
}
trap cleanup_install EXIT
if [[ -e $prefix/versions || -L $prefix/versions ]]; then
[[ -d $prefix/versions && ! -L $prefix/versions ]] ||
die "versions path is not a real directory: $prefix/versions"
fi
install -d -m 0755 -- "$prefix" "$prefix/versions"
if [[ $RUNTIME_TRUST_SCOPE != none ]]; then
validate_trusted_path_ancestors "$RUNTIME_TRUST_SCOPE" "$prefix" "$INSTALL_UID" 1
validate_trusted_path_ancestors "$RUNTIME_TRUST_SCOPE" "$prefix/versions" "$INSTALL_UID" 1
fi
acquire_prefix_lock "$prefix"
record=$prefix/$RECORD_NAME
load_old_record "$record"
if [[ -n ${OLD_LAUNCHER:-} ]]; then
verify_recorded_launcher "$OLD_LAUNCHER" "${OLD_LAUNCHER_SHA256:-}"
elif [[ -n ${OLD_LAUNCHER_SHA256:-} ]]; then
die "existing portable launcher record is incomplete"
fi
if [[ $WANT_LAUNCHER -eq 1 && ( -e $LAUNCHER_PATH || -L $LAUNCHER_PATH ) &&
-z ${OLD_LAUNCHER:-} ]]; then
die "refusing to overwrite an unrecognized portable launcher: $LAUNCHER_PATH"
fi
if [[ ${OLD_CREATED_SYSTEM_USER:-0} == 1 ]]; then
[[ ${OLD_SERVICE_UID:-} =~ ^[0-9]+$ && ${OLD_SERVICE_GID:-} =~ ^[0-9]+$ ]] ||
die "installer-created service account record has no uid/gid identity"
[[ $(id -u "$SERVICE_USER") -eq $OLD_SERVICE_UID &&
$(id -g "$SERVICE_USER") -eq $OLD_SERVICE_GID ]] ||
die "the recorded $SERVICE_USER account was deleted or replaced"
CREATED_SYSTEM_USER=1
SERVICE_UID=$OLD_SERVICE_UID
SERVICE_GID=$OLD_SERVICE_GID
fi
if [[ $mode == system || $register_service == system ]]; then
ensure_system_user "$state_dir"
fi
destination=$prefix/versions/$version
if [[ -e $destination || -L $destination ]]; then
[[ -d $destination && ! -L $destination && -f $destination/manifest.json ]] ||
die "release destination already exists but is incomplete: $destination"
cmp -s -- "$manifest" "$destination/manifest.json" ||
die "release '$version' is already installed with a different manifest"
else
STAGING=$(mktemp -d --tmpdir="$prefix/versions" ".staging-$version.XXXXXX")
cp -a -- "$PAYLOAD_ROOT/bin" "$PAYLOAD_ROOT/lib" "$PAYLOAD_ROOT/share" \
"$PAYLOAD_ROOT/service" "$PAYLOAD_ROOT/manifest.json" "$STAGING/"
[[ -x $STAGING/bin/echoiond && -f $STAGING/lib/Echoion.ts ]] ||
die "staged release failed layout validation"
mv -- "$STAGING" "$destination"
STAGING=
DESTINATION_CREATED=1
fi
if [[ $mode == system || $register_service == system ]]; then
normalize_and_validate_system_runtime "$destination"
fi
[[ -e $config_dir || -L $config_dir ]] || CONFIG_DIR_CREATED=1
[[ -e $state_dir || -L $state_dir ]] || STATE_DIR_CREATED=1
CONFIG_DIR_METADATA_SNAPSHOT=$(snapshot_path_metadata "$config_dir")
STATE_DIR_METADATA_SNAPSHOT=$(snapshot_path_metadata "$state_dir")
CONFIG_FILE_METADATA_SNAPSHOT=$(snapshot_path_metadata "$config_file")
if [[ $CONFIG_DIR_CREATED -eq 0 && $RUNTIME_TRUST_SCOPE == system ]]; then
validate_mutable_directory system "$config_dir" "$(stat -c '%u' -- /)" "$INSTALL_UID"
elif [[ $CONFIG_DIR_CREATED -eq 0 && $RUNTIME_TRUST_SCOPE == user ]]; then
validate_mutable_directory user "$config_dir" "$INSTALL_UID" "$INSTALL_UID"
fi
if [[ $STATE_DIR_CREATED -eq 0 && $RUNTIME_TRUST_SCOPE == system ]]; then
validate_mutable_directory system "$state_dir" "$SERVICE_UID" "$INSTALL_UID"
elif [[ $STATE_DIR_CREATED -eq 0 && $RUNTIME_TRUST_SCOPE == user ]]; then
validate_mutable_directory user "$state_dir" "$INSTALL_UID" "$INSTALL_UID"
fi
install -d -m 0700 -- "$config_dir" "$state_dir"
if [[ ! -e $config_file && ! -L $config_file ]]; then
render_config "$destination/share/echoion/config.example.json" "$config_file" \
"./web"
CONFIG_CREATED=1
elif [[ -L $config_file || ! -f $config_file ]]; then
die "configuration path exists but is not a regular file: $config_file"
fi
if [[ $mode == system || $register_service == system ]]; then
[[ $config_dir != "$state_dir" ]] ||
die "system installations require separate configuration and state directories"
chown "$SERVICE_USER:$SERVICE_USER" "$state_dir"
chown "root:$SERVICE_USER" "$config_dir" "$config_file"
chmod 0750 "$config_dir" "$state_dir"
chmod 0640 "$config_file"
else
chmod 0700 "$config_dir" "$state_dir"
chmod 0600 "$config_file"
fi
if [[ $RUNTIME_TRUST_SCOPE == system ]]; then
validate_mutable_directory system "$config_dir" "$(stat -c '%u' -- /)" "$INSTALL_UID"
validate_mutable_directory system "$state_dir" "$SERVICE_UID" "$INSTALL_UID"
elif [[ $RUNTIME_TRUST_SCOPE == user ]]; then
validate_mutable_directory user "$config_dir" "$INSTALL_UID" "$INSTALL_UID"
validate_mutable_directory user "$state_dir" "$INSTALL_UID" "$INSTALL_UID"
fi
if [[ ${OLD_INSTALL_ID:-} =~ ^[A-Za-z0-9-]{16,64}$ ]]; then
INSTALL_ID=$OLD_INSTALL_ID
else
INSTALL_ID=$(new_install_id)
fi
capture_managed_directory config "$config_dir" "${OLD_CONFIG_PURGE_ALLOWED:-0}" \
"${OLD_CONFIG_DIR_DEVICE:-}" "${OLD_CONFIG_DIR_INODE:-}" "${OLD_CONFIG_DIR_UID:-}" \
"${OLD_CONFIG_DIR_GID:-}" "${OLD_CONFIG_MARKER_SHA256:-}" "$CONFIG_DIR_CREATED"
capture_managed_directory state "$state_dir" "${OLD_STATE_PURGE_ALLOWED:-0}" \
"${OLD_STATE_DIR_DEVICE:-}" "${OLD_STATE_DIR_INODE:-}" "${OLD_STATE_DIR_UID:-}" \
"${OLD_STATE_DIR_GID:-}" "${OLD_STATE_MARKER_SHA256:-}" "$STATE_DIR_CREATED"
candidate_executable=$destination/bin/echoiond
if ! "$candidate_executable" --validate-install "$destination"; then
die "offline payload validation failed for Echoion $version"
fi
if [[ $register_service != none ]]; then
validate_service_access "$register_service" "$candidate_executable" "$config_file" "$state_dir"
fi
old_current=
if [[ -L $prefix/current ]]; then
old_current=$(readlink -- "$prefix/current")
[[ $old_current =~ ^versions/[A-Za-z0-9][A-Za-z0-9._-]{0,63}$ ]] ||
die "current symlink has an unsafe target: $old_current"
elif [[ -e $prefix/current ]]; then
die "current exists but is not a symlink: $prefix/current"
fi
unit_path=
unit_hash=
unit_root=
if [[ $register_service == user ]]; then
unit_root=${XDG_CONFIG_HOME:-${HOME:?HOME is required}/.config}
reject_control_characters service-unit-root "$unit_root"
unit_root=$(absolute_path "$unit_root")
validate_trusted_path_ancestors user "$unit_root" "$INSTALL_UID" 0
install -d -m 0755 -- "$unit_root/systemd/user"
validate_trusted_path_ancestors user "$unit_root" "$INSTALL_UID" 1
unit_path=$unit_root/systemd/user/$SERVICE_NAME
elif [[ $register_service == system ]]; then
unit_path=/etc/systemd/system/$SERVICE_NAME
fi
service_scope_changes=0
OLD_SERVICE_WAS_ENABLED=0
OLD_SERVICE_WAS_ACTIVE=0
if [[ ${OLD_SERVICE_SCOPE:-none} != none ]]; then
if systemctl_for "$OLD_SERVICE_SCOPE" is-enabled --quiet "$SERVICE_NAME"; then
OLD_SERVICE_WAS_ENABLED=1
fi
if systemctl_for "$OLD_SERVICE_SCOPE" is-active --quiet "$SERVICE_NAME"; then
OLD_SERVICE_WAS_ACTIVE=1
fi
fi
if [[ ${OLD_SERVICE_SCOPE:-none} != none &&
( ${OLD_SERVICE_SCOPE:-none} != "$register_service" || ${OLD_SERVICE_UNIT:-} != "$unit_path" ) ]]; then
verify_recorded_unit "$OLD_SERVICE_SCOPE" "$OLD_SERVICE_UNIT" "$OLD_SERVICE_SHA256"
service_scope_changes=1
fi
if [[ $register_service != none && -e $unit_path ]]; then
if [[ ${OLD_SERVICE_UNIT:-} == "$unit_path" && ${OLD_SERVICE_SCOPE:-} == "$register_service" ]]; then
verify_recorded_unit "$OLD_SERVICE_SCOPE" "$OLD_SERVICE_UNIT" "$OLD_SERVICE_SHA256"
else
die "refusing to overwrite unrecognized service unit: $unit_path"
fi
fi
if [[ $register_service != none ]]; then
install -d -m 0755 -- "$(dirname -- "$unit_path")"
UNIT_TEMPORARY=$(mktemp --tmpdir="$(dirname -- "$unit_path")" '.echoion.service.XXXXXX')
render_unit "$destination/service/echoion.service.in" "$UNIT_TEMPORARY" "$register_service" \
"$prefix/current" "$prefix/current/lib/Echoion.ts" "$state_dir" \
"$prefix/current/bin/echoiond" "$config_file"
chmod 0644 "$UNIT_TEMPORARY"
fi
INSTALL_HELPER_TEMPORARY=$(mktemp --tmpdir="$prefix" '.install-helper.XXXXXX')
UNINSTALL_HELPER_TEMPORARY=$(mktemp --tmpdir="$prefix" '.uninstall-helper.XXXXXX')
install -m 0755 -- "$PAYLOAD_ROOT/install.sh" "$INSTALL_HELPER_TEMPORARY"
install -m 0755 -- "$PAYLOAD_ROOT/uninstall.sh" "$UNINSTALL_HELPER_TEMPORARY"
if [[ $WANT_LAUNCHER -eq 1 ]]; then
LAUNCHER_TEMPORARY=$(mktemp --tmpdir="$prefix" '.echoion-launcher.XXXXXX')
render_portable_launcher "$LAUNCHER_TEMPORARY"
chmod 0755 "$LAUNCHER_TEMPORARY"
LAUNCHER_RECORD_PATH=echoion
LAUNCHER_SHA256=$(sha256_file "$LAUNCHER_TEMPORARY")
fi
TRANSACTION_BACKUP_DIR=$(mktemp -d --tmpdir="$prefix" '.install-rollback.XXXXXX')
if [[ -f $record ]]; then
cp -a -- "$record" "$TRANSACTION_BACKUP_DIR/install-record"
fi
if [[ $service_scope_changes -eq 1 ]]; then
cp -a -- "$OLD_SERVICE_UNIT" "$TRANSACTION_BACKUP_DIR/old-scope-unit"
fi
if [[ $register_service != none && -f $unit_path ]]; then
cp -a -- "$unit_path" "$TRANSACTION_BACKUP_DIR/new-scope-unit"
fi
if [[ -f $prefix/install.sh && $prefix/install.sh != "$INSTALL_HELPER_TEMPORARY" ]]; then
cp -a -- "$prefix/install.sh" "$TRANSACTION_BACKUP_DIR/install-helper"
fi
if [[ -f $prefix/uninstall.sh && $prefix/uninstall.sh != "$UNINSTALL_HELPER_TEMPORARY" ]]; then
cp -a -- "$prefix/uninstall.sh" "$TRANSACTION_BACKUP_DIR/uninstall-helper"
fi
if [[ -n ${OLD_LAUNCHER:-} ]]; then
cp -a -- "$LAUNCHER_PATH" "$TRANSACTION_BACKUP_DIR/launcher"
fi
TRANSACTION_ACTIVE=1
HELPER_MUTATION_ARMED=1
mv -f -- "$INSTALL_HELPER_TEMPORARY" "$prefix/install.sh"
INSTALL_HELPER_TEMPORARY=
mv -f -- "$UNINSTALL_HELPER_TEMPORARY" "$prefix/uninstall.sh"
UNINSTALL_HELPER_TEMPORARY=
if [[ $WANT_LAUNCHER -eq 1 || -n ${OLD_LAUNCHER:-} ]]; then
LAUNCHER_MUTATION_ARMED=1
if [[ $WANT_LAUNCHER -eq 1 ]]; then
mv -f -- "$LAUNCHER_TEMPORARY" "$LAUNCHER_PATH"
LAUNCHER_TEMPORARY=
else
rm -f -- "$LAUNCHER_PATH"
fi
fi
if [[ $service_scope_changes -eq 1 ]]; then
OLD_SERVICE_MUTATION_ARMED=1
remove_recorded_service "$OLD_SERVICE_SCOPE" "$OLD_SERVICE_UNIT" "$OLD_SERVICE_SHA256" ||
die "could not remove the previously registered service"
fi
if [[ $register_service != none ]]; then
NEW_UNIT_MUTATION_ARMED=1
mv -f -- "$UNIT_TEMPORARY" "$unit_path"
UNIT_TEMPORARY=
unit_hash=$(sha256_file "$unit_path")
fi
new_link_directory=$(mktemp -d --tmpdir="$prefix" '.current-link.XXXXXX')
ln -s -- "versions/$version" "$new_link_directory/current"
mv -Tf -- "$new_link_directory/current" "$prefix/current"
rmdir -- "$new_link_directory"
if [[ $register_service != none ]]; then
systemctl_for "$register_service" daemon-reload || die "systemd rejected the installed service unit"
if [[ $start -eq 1 ]]; then
if ! systemctl_for "$register_service" enable "$SERVICE_NAME" ||
! systemctl_for "$register_service" restart "$SERVICE_NAME" ||
! systemctl_for "$register_service" is-active --quiet "$SERVICE_NAME"; then
warn "new service failed health validation; restoring previous release and unit"
die "service failed to start"
fi
else
systemctl_for "$register_service" disable --now "$SERVICE_NAME" ||
die "could not leave the registered service disabled and stopped"
fi
fi
write_record "$record" "$unit_path" "$unit_hash"
INSTALL_COMMITTED=1
TRANSACTION_ACTIVE=0
if [[ $PAYLOAD_ROOT == "$prefix" ]]; then
[[ -n $prefix && $prefix != / ]] || die "unsafe in-place install prefix"
rm -rf -- "${prefix:?}/bin" "${prefix:?}/lib" "${prefix:?}/share" \
"${prefix:?}/service"
rm -f -- "$prefix/manifest.json"
fi
printf 'Echoion %s installed in %s\n' "$version" "$prefix"
printf 'Configuration: %s\n' "$config_file"
printf 'State: %s\n' "$state_dir"
if [[ $register_service == none ]]; then
if [[ $WANT_LAUNCHER -eq 1 ]]; then
printf 'Run: %q\n' "$LAUNCHER_PATH"
else
printf 'Run: %q --config %q --state-dir %q\n' \
"$prefix/current/bin/echoiond" "$config_file" "$state_dir"
fi
elif [[ $start -eq 1 ]]; then
printf '%s is enabled and running.\n' "$SERVICE_NAME"
else
printf '%s was registered but is disabled and stopped.\n' "$SERVICE_NAME"
fi