Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-16 19:59:57 +03:00
1 parent 818d391022
commit 639362fbf5
40 files changed
+6701 -429

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
# Linux installer layout
`install.sh` and `uninstall.sh` are copied into each Linux release payload.
`service/echoion.service.in` is rendered by the installer for user or system
systemd scope. `tests/installer_test.sh` exercises portable installation,
service opt-in, rollback, tamper protection, preservation, and purge behavior.
System-scoped payloads are normalized to root ownership. Registered-service
prefixes require a trusted path not writable by other identities. Purge uses
recorded directory identity plus an installer marker before removing custom
mutable paths.
Install and uninstall also take a nonblocking advisory lock on the prefix
directory so concurrent lifecycle operations fail before changing the runtime.
Self-contained portable installs add a managed `echoion` launcher and relative
install metadata, allowing a service-free tree to be moved and uninstalled as
one directory.
Create publishable artifacts from the repository root:
```bash
scripts/release.sh --version 1.2.3
```
The default command uses the version-pinned release toolchain container and
does not start the bot. Installation flags and data layouts are documented in
the root README.
+1351
View File
File diff suppressed because it is too large. Load diff
+20
View File
@@ -0,0 +1,20 @@
[Unit]
Description=Echoion Music Bot
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
@SERVICE_IDENTITY@
@WORKING_DIRECTORY@
@ENVIRONMENT_HOME@
@ENVIRONMENT_CORE@
@ENVIRONMENT_STATE@
@EXEC_START@
Restart=on-failure
RestartSec=5s
LimitNOFILE=65536
UMask=0077
[Install]
@INSTALL_TARGET@
+551
View File
@@ -0,0 +1,551 @@
#!/usr/bin/env bash
# shellcheck disable=SC1091,SC2034,SC2329
set -euo pipefail
SCRIPT_DIR=$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
PACKAGING_DIR=$(cd -P -- "$SCRIPT_DIR/.." && pwd -P)
TEST_ROOT=$(mktemp -d -- "$SCRIPT_DIR/.installer-test.XXXXXX")
trap 'rm -rf -- "$TEST_ROOT"' EXIT
fail() {
printf 'FAIL: %s\n' "$*" >&2
exit 1
}
assert_file() {
[[ -f $1 ]] || fail "missing file: $1"
}
assert_not_exists() {
[[ ! -e $1 ]] || fail "unexpected path: $1"
}
assert_contains() {
local path=$1
local text=$2
grep -F -- "$text" "$path" >/dev/null || fail "$path does not contain: $text"
}
assert_not_contains() {
local path=$1
local text=$2
if grep -F -- "$text" "$path" >/dev/null; then
fail "$path unexpectedly contains: $text"
fi
}
make_payload() {
local root=$1
install -d -- "$root/bin" "$root/lib" "$root/share/echoion/web" "$root/service"
install -m 0755 -- "$PACKAGING_DIR/install.sh" "$root/install.sh"
install -m 0755 -- "$PACKAGING_DIR/uninstall.sh" "$root/uninstall.sh"
install -m 0644 -- "$PACKAGING_DIR/service/echoion.service.in" "$root/service/echoion.service.in"
cat > "$root/manifest.json" <<'EOF'
{
"schemaVersion": 2,
"version": "1.2.3",
"files": [
{"path": "bin/echoiond"}
]
}
EOF
cat > "$root/bin/echoiond" <<'EOF'
#!/usr/bin/env bash
if [[ ${1:-} == --validate-install && -n ${2:-} ]]; then
printf '%s\n' "$2" >> "${VALIDATOR_LOG:?}"
if [[ -n ${VALIDATOR_FAIL_PATH:-} && $2 == "$VALIDATOR_FAIL_PATH" ]]; then
exit 1
fi
exit 0
fi
if [[ ${ALLOW_DAEMON_RUN:-0} == 1 ]]; then
printf 'ECHOION_HOME=%s\n' "${ECHOION_HOME:-}" >> "${DAEMON_LOG:?}"
printf 'ECHOION_CORE_PATH=%s\n' "${ECHOION_CORE_PATH:-}" >> "$DAEMON_LOG"
printf 'ECHOION_STATE_DIR=%s\n' "${ECHOION_STATE_DIR:-}" >> "$DAEMON_LOG"
argument_index=0
for argument in "$@"; do
printf 'ARG_%d=%s\n' "$argument_index" "$argument" >> "$DAEMON_LOG"
argument_index=$((argument_index + 1))
done
exit 0
fi
printf 'daemon unexpectedly ran\n' >> "${DAEMON_LOG:?}"
exit 1
EOF
chmod 0755 "$root/bin/echoiond"
printf 'fake dynamic core\n' > "$root/lib/Echoion.ts"
cat > "$root/share/echoion/config.example.json" <<'EOF'
{
"web": {
"webRoot": "./web",
"enabled": false
}
}
EOF
printf '<!doctype html>\n' > "$root/share/echoion/web/index.html"
}
fake_bin=$TEST_ROOT/fake-bin
home=$TEST_ROOT/home
install -d -- "$fake_bin" "$home"
REAL_MV=$(command -v mv)
export REAL_MV
cat > "$fake_bin/systemctl" <<'EOF'
#!/usr/bin/env bash
printf '%s\n' "$*" >> "${SYSTEMCTL_LOG:?}"
if [[ ${SYSTEMCTL_FAIL_RESTART:-0} == 1 && " $* " == *' restart '* ]]; then
exit 1
fi
exit 0
EOF
chmod 0755 "$fake_bin/systemctl"
cat > "$fake_bin/mv" <<'EOF'
#!/usr/bin/env bash
last=
for last in "$@"; do :; done
if [[ ${RECORD_COMMIT_FAIL:-0} == 1 && ${last##*/} == .echoion-install ]]; then
exit 73
fi
exec "${REAL_MV:?}" "$@"
EOF
chmod 0755 "$fake_bin/mv"
export PATH=$fake_bin:$PATH
export HOME=$home
export XDG_CONFIG_HOME=$home/.config
export XDG_STATE_HOME=$home/.state
export SYSTEMCTL_LOG=$TEST_ROOT/systemctl.log
export DAEMON_LOG=$TEST_ROOT/daemon.log
export VALIDATOR_LOG=$TEST_ROOT/validator.log
: > "$SYSTEMCTL_LOG"
: > "$DAEMON_LOG"
: > "$VALIDATOR_LOG"
bash -n "$PACKAGING_DIR/install.sh"
bash -n "$PACKAGING_DIR/uninstall.sh"
trusted_parent=$TEST_ROOT/trusted-parent
trusted_prefix=$trusted_parent/echoion
install -d -m 0700 -- "$trusted_prefix"
(
# shellcheck source=../install.sh
source "$PACKAGING_DIR/install.sh"
validate_trusted_path_ancestors user "$trusted_prefix" "$(id -u)" 1
) || fail 'trusted user service prefix was rejected'
chmod 0770 "$trusted_parent"
if (
# shellcheck source=../install.sh
source "$PACKAGING_DIR/install.sh"
validate_trusted_path_ancestors user "$trusted_prefix" "$(id -u)" 1
) >/dev/null 2>&1; then
fail 'group-writable service prefix ancestor was accepted'
fi
chmod 0700 "$trusted_parent"
if (
# shellcheck source=../install.sh
source "$PACKAGING_DIR/install.sh"
validate_trusted_path_ancestors system "$trusted_prefix" 0 1
) >/dev/null 2>&1; then
fail 'non-root-owned system service prefix was accepted'
fi
if (
# shellcheck source=../install.sh
source "$PACKAGING_DIR/install.sh"
validate_registration_identity user 0
) >/dev/null 2>&1; then
fail 'root user-scope service registration was accepted'
fi
CHOWN_LOG=$TEST_ROOT/chown.log
export CHOWN_LOG
(
# shellcheck source=../install.sh
source "$PACKAGING_DIR/install.sh"
chown() { printf '%s\n' "$*" >> "$CHOWN_LOG"; }
find() { return 0; }
runuser() {
[[ " $* " == *' test -w '* ]] && return 1
return 0
}
normalize_and_validate_system_runtime "$trusted_prefix"
)
assert_contains "$CHOWN_LOG" '-R root:root'
USERDEL_LOG=$TEST_ROOT/userdel.log
export USERDEL_LOG
(
# shellcheck source=../install.sh
source "$PACKAGING_DIR/install.sh"
ACCOUNT_CREATED_THIS_RUN=1
ACCOUNT_IDENTITY_CAPTURED=1
SERVICE_UID=4242
SERVICE_GID=4343
MOCK_UID=4242
MOCK_GID=4343
id() {
case $1 in
-u) printf '%s\n' "$MOCK_UID" ;;
-g) printf '%s\n' "$MOCK_GID" ;;
esac
}
userdel() { printf '%s\n' "$*" >> "$USERDEL_LOG"; }
remove_new_system_user
)
assert_contains "$USERDEL_LOG" 'echoion'
: > "$USERDEL_LOG"
(
# shellcheck source=../install.sh
source "$PACKAGING_DIR/install.sh"
ACCOUNT_CREATED_THIS_RUN=1
ACCOUNT_IDENTITY_CAPTURED=1
SERVICE_UID=4242
SERVICE_GID=4343
MOCK_UID=9999
MOCK_GID=4343
id() {
case $1 in
-u) printf '%s\n' "$MOCK_UID" ;;
-g) printf '%s\n' "$MOCK_GID" ;;
esac
}
userdel() { printf '%s\n' "$*" >> "$USERDEL_LOG"; }
remove_new_system_user
) 2>/dev/null
[[ ! -s $USERDEL_LOG ]] || fail 'recreated service account was deleted'
symlink_prefix=$TEST_ROOT/symlink-versions
install -d -- "$symlink_prefix" "$TEST_ROOT/external-versions"
ln -s -- "$TEST_ROOT/external-versions" "$symlink_prefix/versions"
payload=$TEST_ROOT/payload
make_payload "$payload"
locked_prefix=$TEST_ROOT/locked-prefix
install -d -- "$locked_prefix"
exec {TEST_LOCK_FD}< "$locked_prefix"
flock -n "$TEST_LOCK_FD"
if "$payload/install.sh" --mode portable --prefix "$locked_prefix" \
--register-service none --non-interactive >/dev/null 2>&1; then
fail 'concurrent installer acquired an already-held prefix lock'
fi
flock -u "$TEST_LOCK_FD"
exec {TEST_LOCK_FD}<&-
assert_not_exists "$locked_prefix/versions/1.2.3"
metadata_prefix=$TEST_ROOT/metadata-rollback
metadata_config=$TEST_ROOT/metadata-config
metadata_state=$TEST_ROOT/metadata-state
install -d -m 0750 -- "$metadata_config"
install -d -m 0710 -- "$metadata_state"
printf '{}\n' > "$metadata_config/config.json"
chmod 0640 "$metadata_config/config.json"
config_metadata_before=$(stat -c '%u:%g:%a' -- "$metadata_config")
state_metadata_before=$(stat -c '%u:%g:%a' -- "$metadata_state")
file_metadata_before=$(stat -c '%u:%g:%a' -- "$metadata_config/config.json")
export VALIDATOR_FAIL_PATH=$metadata_prefix/versions/1.2.3
if "$payload/install.sh" --mode portable --prefix "$metadata_prefix" \
--config-dir "$metadata_config" --state-dir "$metadata_state" \
--register-service none --non-interactive >/dev/null 2>&1; then
fail 'installer accepted injected post-metadata validation failure'
fi
unset VALIDATOR_FAIL_PATH
config_metadata_after=$(stat -c '%u:%g:%a' -- "$metadata_config")
state_metadata_after=$(stat -c '%u:%g:%a' -- "$metadata_state")
file_metadata_after=$(stat -c '%u:%g:%a' -- "$metadata_config/config.json")
[[ $config_metadata_after == "$config_metadata_before" ]] ||
fail 'failed install did not restore configuration directory uid/gid/mode'
[[ $state_metadata_after == "$state_metadata_before" ]] ||
fail 'failed install did not restore state directory uid/gid/mode'
[[ $file_metadata_after == "$file_metadata_before" ]] ||
fail 'failed install did not restore configuration file uid/gid/mode'
assert_not_exists "$metadata_prefix/versions/1.2.3"
runtime_root=$TEST_ROOT/runtime-directory
install -d -m 0700 -- "$runtime_root"
if XDG_RUNTIME_DIR=$runtime_root "$payload/install.sh" --mode portable \
--prefix "$runtime_root/echoion" --register-service user \
--non-interactive >/dev/null 2>&1; then
fail 'XDG runtime portable service prefix was accepted'
fi
unsafe_mutable_parent=$TEST_ROOT/group-writable-mutable
install -d -m 0770 -- "$unsafe_mutable_parent"
if "$payload/install.sh" --mode portable --prefix "$TEST_ROOT/mutable-trust-install" \
--config-dir "$unsafe_mutable_parent/config" \
--state-dir "$TEST_ROOT/mutable-trust-state" --register-service user \
--non-interactive >/dev/null 2>&1; then
fail 'group-writable mutable-path ancestor was accepted for a service'
fi
if "$payload/install.sh" --mode portable --prefix "$symlink_prefix" \
--register-service none --non-interactive >/dev/null 2>&1; then
fail 'symlinked versions directory was accepted'
fi
bad_payload=$TEST_ROOT/bad-payload
make_payload "$bad_payload"
sed -i 's/"schemaVersion": 2/"schemaVersion": 1/' "$bad_payload/manifest.json"
if "$bad_payload/install.sh" --mode portable --prefix "$TEST_ROOT/bad-install" \
--register-service none --non-interactive >/dev/null 2>&1; then
fail 'legacy payload manifest was accepted'
fi
self_prefix=$TEST_ROOT/self-contained
"$payload/install.sh" --mode portable --prefix "$self_prefix" \
--data-layout self-contained --register-service none --non-interactive
[[ $(readlink -- "$self_prefix/current") == versions/1.2.3 ]] || fail 'wrong current target'
assert_file "$self_prefix/versions/1.2.3/bin/echoiond"
assert_file "$self_prefix/config/config.json"
[[ -x $self_prefix/echoion ]] || fail 'portable launcher is not executable'
assert_contains "$self_prefix/.echoion-install" 'relocatable=1'
assert_contains "$self_prefix/.echoion-install" 'prefix=.'
assert_contains "$self_prefix/.echoion-install" 'config_dir=config'
assert_contains "$self_prefix/.echoion-install" 'state_dir=data'
assert_contains "$self_prefix/.echoion-install" 'launcher=echoion'
assert_contains "$self_prefix/config/config.json" '"webRoot": "./web"'
assert_not_contains "$self_prefix/config/config.json" "$self_prefix/current"
[[ ! -s $SYSTEMCTL_LOG ]] || fail 'service command ran for register-service none'
[[ ! -s $DAEMON_LOG ]] || fail 'daemon ran during installation'
assert_contains "$VALIDATOR_LOG" "$self_prefix/versions/1.2.3"
self_moved_prefix=$TEST_ROOT/self-contained-moved
mv -- "$self_prefix" "$self_moved_prefix"
assert_not_exists "$self_prefix"
: > "$DAEMON_LOG"
ALLOW_DAEMON_RUN=1 "$self_moved_prefix/echoion" --probe 'two words'
assert_contains "$DAEMON_LOG" "ECHOION_HOME=$self_moved_prefix/current"
assert_contains "$DAEMON_LOG" "ECHOION_CORE_PATH=$self_moved_prefix/current/lib/Echoion.ts"
assert_contains "$DAEMON_LOG" "ECHOION_STATE_DIR=$self_moved_prefix/data"
assert_contains "$DAEMON_LOG" 'ARG_0=--config'
assert_contains "$DAEMON_LOG" "ARG_1=$self_moved_prefix/config/config.json"
assert_contains "$DAEMON_LOG" 'ARG_2=--state-dir'
assert_contains "$DAEMON_LOG" "ARG_3=$self_moved_prefix/data"
assert_contains "$DAEMON_LOG" 'ARG_4=--probe'
assert_contains "$DAEMON_LOG" 'ARG_5=two words'
: > "$DAEMON_LOG"
if ALLOW_DAEMON_RUN=1 "$self_moved_prefix/echoion" --config /tmp/override \
>/dev/null 2>&1; then
fail 'portable launcher accepted a managed config override'
fi
[[ ! -s $DAEMON_LOG ]] || fail 'portable launcher executed daemon after rejected override'
"$self_moved_prefix/uninstall.sh" --purge --non-interactive >/dev/null
assert_not_exists "$self_moved_prefix"
dots_prefix=$TEST_ROOT/consecutive-dot-version
dots_payload=$TEST_ROOT/consecutive-dot-payload
dots_upgrade_payload=$TEST_ROOT/consecutive-dot-upgrade-payload
cp -a -- "$payload" "$dots_payload"
sed -i 's/"version": "1.2.3"/"version": "1.0..rc1"/' "$dots_payload/manifest.json"
cp -a -- "$dots_payload" "$dots_upgrade_payload"
sed -i 's/"version": "1.0..rc1"/"version": "1.0..rc2"/' \
"$dots_upgrade_payload/manifest.json"
"$dots_payload/install.sh" --mode portable --prefix "$dots_prefix" \
--data-layout self-contained --register-service none --non-interactive >/dev/null
"$dots_upgrade_payload/install.sh" --mode portable --prefix "$dots_prefix" \
--data-layout self-contained --register-service none --non-interactive >/dev/null
[[ $(readlink -- "$dots_prefix/current") == versions/1.0..rc2 ]] ||
fail 'legal consecutive-dot version did not upgrade'
"$dots_prefix/uninstall.sh" --purge --non-interactive >/dev/null
assert_not_exists "$dots_prefix"
custom_prefix=$TEST_ROOT/custom-purge-install
custom_config=$TEST_ROOT/custom-configuration
custom_state=$TEST_ROOT/custom-runtime-state
"$payload/install.sh" --mode portable --prefix "$custom_prefix" \
--config-dir "$custom_config" --state-dir "$custom_state" \
--register-service none --non-interactive >/dev/null
assert_file "$custom_config/.echoion-managed"
assert_file "$custom_state/.echoion-managed"
assert_contains "$custom_prefix/.echoion-install" 'config_purge_allowed=1'
assert_contains "$custom_prefix/.echoion-install" 'state_purge_allowed=1'
exec {TEST_LOCK_FD}< "$custom_prefix"
flock -n "$TEST_LOCK_FD"
if "$custom_prefix/uninstall.sh" --purge --non-interactive >/dev/null 2>&1; then
fail 'concurrent uninstaller acquired an already-held prefix lock'
fi
assert_file "$custom_prefix/current/bin/echoiond"
flock -u "$TEST_LOCK_FD"
exec {TEST_LOCK_FD}<&-
"$custom_prefix/uninstall.sh" --purge --non-interactive >/dev/null
assert_not_exists "$custom_prefix"
assert_not_exists "$custom_config"
assert_not_exists "$custom_state"
service_prefix="$TEST_ROOT/portable path % dollar\$"
: > "$SYSTEMCTL_LOG"
"$payload/install.sh" --mode portable --prefix "$service_prefix" \
--data-layout standard --register-service user --non-interactive
unit=$XDG_CONFIG_HOME/systemd/user/echoion.service
assert_file "$unit"
assert_contains "$unit" 'WorkingDirectory="'
assert_contains "$unit" 'portable path %% dollar$/current"'
assert_contains "$unit" 'ExecStart="'
assert_contains "$unit" 'portable path %% dollar$$/current/bin/echoiond"'
assert_contains "$unit" 'Environment="ECHOION_HOME='
assert_contains "$unit" 'Environment="ECHOION_CORE_PATH='
assert_contains "$unit" 'Environment="ECHOION_STATE_DIR='
assert_contains "$unit" '--config "'
assert_contains "$unit" '--state-dir "'
if grep -E '@[A-Z_]+@' "$unit" >/dev/null; then
fail 'generated systemd unit contains an unresolved template marker'
fi
if command -v systemd-analyze >/dev/null 2>&1; then
verify_log=$TEST_ROOT/systemd-verify.log
if ! systemd-analyze verify "$unit" 2> "$verify_log" &&
grep -E 'echoion\.service:|Failed to load unit' "$verify_log" >/dev/null; then
cat "$verify_log" >&2
fail 'generated systemd unit did not verify'
fi
fi
assert_contains "$SYSTEMCTL_LOG" '--user daemon-reload'
assert_contains "$SYSTEMCTL_LOG" '--user disable --now echoion.service'
if grep -E -- '(^| )enable( |$)|(^| )restart( |$)' "$SYSTEMCTL_LOG" >/dev/null; then
fail 'service was activated without --start'
fi
[[ ! -s $DAEMON_LOG ]] || fail 'daemon ran during service registration'
: > "$SYSTEMCTL_LOG"
"$payload/install.sh" --mode portable --prefix "$service_prefix" \
--data-layout standard --register-service user --start --non-interactive
assert_contains "$SYSTEMCTL_LOG" '--user enable echoion.service'
assert_contains "$SYSTEMCTL_LOG" '--user restart echoion.service'
assert_contains "$SYSTEMCTL_LOG" '--user is-active --quiet echoion.service'
: > "$SYSTEMCTL_LOG"
"$service_prefix/uninstall.sh" --non-interactive
assert_not_exists "$unit"
assert_not_exists "$service_prefix/versions"
assert_file "$XDG_CONFIG_HOME/echoion/config.json"
[[ -d $XDG_STATE_HOME/echoion ]] || fail 'state directory was not preserved'
purge_retry_prefix=$TEST_ROOT/purge-retry
purge_retry_config=$TEST_ROOT/purge-retry-config
purge_retry_state=$TEST_ROOT/purge-retry-state
"$payload/install.sh" --mode portable --prefix "$purge_retry_prefix" \
--config-dir "$purge_retry_config" --state-dir "$purge_retry_state" \
--register-service user --non-interactive >/dev/null
marker_backup=$TEST_ROOT/config-marker.backup
cp -a -- "$purge_retry_config/.echoion-managed" "$marker_backup"
printf 'tampered\n' >> "$purge_retry_config/.echoion-managed"
if "$purge_retry_prefix/uninstall.sh" --purge --non-interactive >/dev/null 2>&1; then
fail 'purge accepted a modified ownership marker'
fi
assert_file "$unit"
assert_file "$purge_retry_prefix/current/bin/echoiond"
assert_file "$purge_retry_prefix/.echoion-install"
cp -a -- "$marker_backup" "$purge_retry_config/.echoion-managed"
"$purge_retry_prefix/uninstall.sh" --purge --non-interactive >/dev/null
assert_not_exists "$unit"
assert_not_exists "$purge_retry_prefix"
assert_not_exists "$purge_retry_config"
assert_not_exists "$purge_retry_state"
rollback_prefix=$TEST_ROOT/rollback
"$payload/install.sh" --mode portable --prefix "$rollback_prefix" \
--register-service user --non-interactive >/dev/null
old_unit_hash=$(sha256sum -- "$unit")
old_unit_hash=${old_unit_hash%% *}
upgrade_payload=$TEST_ROOT/upgrade-payload
cp -a -- "$payload" "$upgrade_payload"
sed -i 's/"version": "1.2.3"/"version": "1.2.4"/' "$upgrade_payload/manifest.json"
printf '# changed in 1.2.4\n' >> "$upgrade_payload/service/echoion.service.in"
printf '# changed installer helper in 1.2.4\n' >> "$upgrade_payload/install.sh"
printf '# changed uninstaller helper in 1.2.4\n' >> "$upgrade_payload/uninstall.sh"
export SYSTEMCTL_FAIL_RESTART=1
if "$upgrade_payload/install.sh" --mode portable --prefix "$rollback_prefix" \
--register-service user --start --non-interactive >/dev/null 2>&1; then
fail 'installer accepted a failed service restart'
fi
unset SYSTEMCTL_FAIL_RESTART
[[ $(readlink -- "$rollback_prefix/current") == versions/1.2.3 ]] ||
fail 'failed service upgrade did not restore current'
restored_unit_hash=$(sha256sum -- "$unit")
restored_unit_hash=${restored_unit_hash%% *}
[[ $restored_unit_hash == "$old_unit_hash" ]] || fail 'failed upgrade did not restore unit bytes'
assert_contains "$rollback_prefix/.echoion-install" 'version=1.2.3'
"$rollback_prefix/uninstall.sh" --non-interactive >/dev/null
record_rollback_prefix=$TEST_ROOT/record-rollback
"$payload/install.sh" --mode portable --prefix "$record_rollback_prefix" \
--register-service user --non-interactive >/dev/null
record_old_unit_hash=$(sha256sum -- "$unit")
record_old_unit_hash=${record_old_unit_hash%% *}
record_old_install_hash=$(sha256sum -- "$record_rollback_prefix/install.sh")
record_old_install_hash=${record_old_install_hash%% *}
record_old_uninstall_hash=$(sha256sum -- "$record_rollback_prefix/uninstall.sh")
record_old_uninstall_hash=${record_old_uninstall_hash%% *}
export RECORD_COMMIT_FAIL=1
if "$upgrade_payload/install.sh" --mode portable --prefix "$record_rollback_prefix" \
--register-service user --non-interactive >/dev/null 2>&1; then
fail 'installer accepted a failed atomic record commit'
fi
unset RECORD_COMMIT_FAIL
[[ $(readlink -- "$record_rollback_prefix/current") == versions/1.2.3 ]] ||
fail 'record-commit failure did not restore current'
record_restored_unit_hash=$(sha256sum -- "$unit")
record_restored_unit_hash=${record_restored_unit_hash%% *}
[[ $record_restored_unit_hash == "$record_old_unit_hash" ]] ||
fail 'record-commit failure did not restore unit bytes'
assert_contains "$record_rollback_prefix/.echoion-install" 'version=1.2.3'
assert_not_exists "$record_rollback_prefix/versions/1.2.4"
record_restored_install_hash=$(sha256sum -- "$record_rollback_prefix/install.sh")
record_restored_install_hash=${record_restored_install_hash%% *}
record_restored_uninstall_hash=$(sha256sum -- "$record_rollback_prefix/uninstall.sh")
record_restored_uninstall_hash=${record_restored_uninstall_hash%% *}
[[ $record_restored_install_hash == "$record_old_install_hash" ]] ||
fail 'record-commit failure did not restore installer helper bytes'
[[ $record_restored_uninstall_hash == "$record_old_uninstall_hash" ]] ||
fail 'record-commit failure did not restore uninstaller helper bytes'
"$record_rollback_prefix/uninstall.sh" --non-interactive >/dev/null
scope_rollback_prefix=$TEST_ROOT/scope-rollback
"$payload/install.sh" --mode portable --prefix "$scope_rollback_prefix" \
--register-service user --non-interactive >/dev/null
scope_old_unit_hash=$(sha256sum -- "$unit")
scope_old_unit_hash=${scope_old_unit_hash%% *}
export RECORD_COMMIT_FAIL=1
if "$upgrade_payload/install.sh" --mode portable --prefix "$scope_rollback_prefix" \
--register-service none --non-interactive >/dev/null 2>&1; then
fail 'scope migration accepted a failed record commit'
fi
unset RECORD_COMMIT_FAIL
assert_file "$unit"
scope_restored_unit_hash=$(sha256sum -- "$unit")
scope_restored_unit_hash=${scope_restored_unit_hash%% *}
[[ $scope_restored_unit_hash == "$scope_old_unit_hash" ]] ||
fail 'scope migration failure did not restore old unit bytes'
[[ $(readlink -- "$scope_rollback_prefix/current") == versions/1.2.3 ]] ||
fail 'scope migration failure did not restore current'
assert_contains "$scope_rollback_prefix/.echoion-install" 'service_scope=user'
"$scope_rollback_prefix/uninstall.sh" --non-interactive >/dev/null
tamper_prefix=$TEST_ROOT/tamper
"$payload/install.sh" --mode portable --prefix "$tamper_prefix" \
--register-service user --non-interactive >/dev/null
printf '# local change\n' >> "$unit"
if "$tamper_prefix/uninstall.sh" --non-interactive >/dev/null 2>&1; then
fail 'uninstaller removed a modified unit'
fi
assert_file "$unit"
rm -f -- "$unit"
if "$payload/install.sh" --mode portable --prefix "/tmp/echoion-installer-test-$$" \
--register-service user --non-interactive >/dev/null 2>&1; then
fail 'temporary portable service prefix was accepted'
fi
unsafe_mutable_prefix=$TEST_ROOT/unsafe-mutable/install
if "$payload/install.sh" --mode portable --prefix "$unsafe_mutable_prefix" \
--config-dir "$TEST_ROOT/unsafe-mutable" --register-service none \
--non-interactive >/dev/null 2>&1; then
fail 'mutable directory containing the install prefix was accepted'
fi
in_place=$TEST_ROOT/in-place
make_payload "$in_place"
"$in_place/install.sh" --mode portable --prefix "$in_place" \
--data-layout self-contained --register-service none --non-interactive >/dev/null
assert_file "$in_place/versions/1.2.3/bin/echoiond"
assert_not_exists "$in_place/bin"
[[ $(readlink -- "$in_place/current") == versions/1.2.3 ]] || fail 'in-place current target is wrong'
printf 'installer tests passed\n'
+314
View File
@@ -0,0 +1,314 @@
#!/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: uninstall.sh [--purge] [--non-interactive]
Remove the Echoion runtime and its matching registered service. Configuration
and state are preserved unless --purge is explicitly supplied.
EOF
}
sha256_file() {
local result
result=$(sha256sum -- "$1")
printf '%s\n' "${result%% *}"
}
systemctl_for() {
local scope=$1
shift
if [[ $scope == user ]]; then
systemctl --user "$@"
else
systemctl "$@"
fi
}
acquire_prefix_lock() {
local lock_prefix=$1
command -v flock >/dev/null 2>&1 || die "flock is required for safe uninstallation"
exec {UNINSTALL_LOCK_FD}< "$lock_prefix"
flock -n "$UNINSTALL_LOCK_FD" || die "another Echoion install or uninstall is active for '$lock_prefix'"
}
safe_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
}
safe_purge_path() {
local path=$1
local prefix=$2
[[ $path == /* && $path != / ]] || return 1
local relative=${path#/}
[[ $relative == */* ]] || return 1
case $path in
/etc | /var | /home | /root | /usr | /usr/local | /opt | /tmp | /var/tmp | /run)
return 1
;;
esac
case $path in
"$prefix" | "$prefix/current" | "$prefix/current"/* | "$prefix/versions" | "$prefix/versions"/*)
return 1
;;
esac
return 0
}
validate_managed_purge_path() {
local kind=$1
local path=$2
local safety_prefix=$3
local marker_prefix=$4
local install_id=$5
local allowed=$6
local expected_device=$7
local expected_inode=$8
local expected_uid=$9
local expected_gid=${10}
local expected_hash=${11}
local marker=$path/$MANAGED_MARKER
local device inode uid gid
[[ $allowed == 1 ]] || die "$kind directory is not marked as installer-owned: '$path'"
safe_purge_path "$path" "$safety_prefix" || die "refusing unsafe purge path '$path'"
if [[ ! -e $path && ! -L $path ]]; then
return 0
fi
[[ -d $path && ! -L $path ]] || die "$kind purge path is not a real directory: '$path'"
[[ $expected_device =~ ^[0-9]+$ && $expected_inode =~ ^[0-9]+$ &&
$expected_uid =~ ^[0-9]+$ && $expected_gid =~ ^[0-9]+$ && -n $expected_hash ]] ||
die "$kind purge identity is incomplete"
device=$(stat -c '%d' -- "$path")
inode=$(stat -c '%i' -- "$path")
uid=$(stat -c '%u' -- "$path")
gid=$(stat -c '%g' -- "$path")
[[ $device == "$expected_device" && $inode == "$expected_inode" &&
$uid == "$expected_uid" && $gid == "$expected_gid" ]] ||
die "$kind directory identity changed; refusing purge: '$path'"
[[ -f $marker && ! -L $marker ]] || die "$kind ownership marker is missing: '$marker'"
[[ $(sha256_file "$marker") == "$expected_hash" ]] ||
die "$kind ownership marker changed: '$marker'"
grep -Fx -- "install_id=$install_id" "$marker" >/dev/null ||
die "$kind ownership marker has the wrong install identity"
grep -Fx -- "prefix=$marker_prefix" "$marker" >/dev/null ||
die "$kind ownership marker belongs to another prefix"
}
purge=0
while [[ $# -gt 0 ]]; do
case $1 in
--purge) purge=1 ;;
--non-interactive) ;;
-h | --help)
usage
exit 0
;;
*) die "unknown option: $1" ;;
esac
shift
done
SCRIPT_DIR=$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
prefix=$SCRIPT_DIR
[[ $prefix != / ]] || die "refusing to uninstall from the filesystem root"
acquire_prefix_lock "$prefix"
record=$prefix/$RECORD_NAME
[[ -f $record ]] || die "install record is missing: $record"
record_prefix=
relocatable=0
service_scope=none
service_unit=
service_unit_sha256=
config_dir=
state_dir=
mode=
data_layout=
launcher=
launcher_sha256=
created_system_user=0
service_uid=
service_gid=
install_id=
config_purge_allowed=0
config_dir_device=
config_dir_inode=
config_dir_uid=
config_dir_gid=
config_marker_sha256=
state_purge_allowed=0
state_dir_device=
state_dir_inode=
state_dir_uid=
state_dir_gid=
state_marker_sha256=
key=
value=
while IFS='=' read -r key value; do
case $key in
prefix) record_prefix=$value ;;
relocatable) relocatable=$value ;;
service_scope) service_scope=$value ;;
service_unit) service_unit=$value ;;
service_unit_sha256) service_unit_sha256=$value ;;
config_dir) config_dir=$value ;;
state_dir) state_dir=$value ;;
mode) mode=$value ;;
data_layout) data_layout=$value ;;
launcher) launcher=$value ;;
launcher_sha256) launcher_sha256=$value ;;
created_system_user) created_system_user=$value ;;
service_uid) service_uid=$value ;;
service_gid) service_gid=$value ;;
install_id) install_id=$value ;;
config_purge_allowed) config_purge_allowed=$value ;;
config_dir_device) config_dir_device=$value ;;
config_dir_inode) config_dir_inode=$value ;;
config_dir_uid) config_dir_uid=$value ;;
config_dir_gid) config_dir_gid=$value ;;
config_marker_sha256) config_marker_sha256=$value ;;
state_purge_allowed) state_purge_allowed=$value ;;
state_dir_device) state_dir_device=$value ;;
state_dir_inode) state_dir_inode=$value ;;
state_dir_uid) state_dir_uid=$value ;;
state_dir_gid) state_dir_gid=$value ;;
state_marker_sha256) state_marker_sha256=$value ;;
esac
done < "$record"
case $relocatable in
0)
[[ $record_prefix == "$prefix" ]] ||
die "install record belongs to '$record_prefix', not '$prefix'"
;;
1)
[[ $record_prefix == . && $mode == portable && $data_layout == self-contained ]] ||
die "relocatable install record is incomplete or unsafe"
[[ $config_dir == config && $state_dir == data ]] ||
die "relocatable install record has unsafe mutable paths"
config_dir=$prefix/config
state_dir=$prefix/data
;;
*) die "install record has an invalid relocatable value" ;;
esac
case $service_scope in
none | user | system) ;;
*) die "install record has an invalid service scope" ;;
esac
case $created_system_user in
0 | 1) ;;
*) die "install record has an invalid created_system_user value" ;;
esac
if [[ $service_scope == system || $mode == system || $created_system_user == 1 ]]; then
[[ ${EUID:-$(id -u)} -eq 0 ]] || die "uninstalling the system service requires root"
fi
# Complete every destructive preflight before stopping the service or removing
# any files, so a refused purge leaves the installation fully retryable.
if [[ $service_scope != none ]]; then
[[ -n $service_unit && -n $service_unit_sha256 ]] || die "service record is incomplete"
safe_unit_path "$service_scope" "$service_unit" || die "service record has an unsafe unit path"
[[ -f $service_unit ]] || die "registered service unit is missing: $service_unit"
[[ $(sha256_file "$service_unit") == "$service_unit_sha256" ]] ||
die "service unit was modified; refusing to remove it: $service_unit"
fi
launcher_path=
if [[ -n $launcher ]]; then
[[ $launcher == echoion && $launcher_sha256 =~ ^[0-9a-f]{64}$ ]] ||
die "portable launcher record is incomplete or unsafe"
launcher_path=$prefix/$launcher
[[ -f $launcher_path && ! -L $launcher_path && -x $launcher_path ]] ||
die "recorded portable launcher is missing or unsafe: $launcher_path"
[[ $(sha256_file "$launcher_path") == "$launcher_sha256" ]] ||
die "portable launcher was modified; refusing to remove it: $launcher_path"
elif [[ -n $launcher_sha256 ]]; then
die "portable launcher record is incomplete"
fi
if [[ -e $prefix/current && ! -L $prefix/current ]]; then
die "current is not a symlink; refusing to remove runtime files"
fi
if [[ $purge -eq 1 ]]; then
[[ $install_id =~ ^[A-Za-z0-9-]{16,64}$ ]] || die "install record has no safe install identity"
[[ -n $config_dir && -n $state_dir ]] || die "install record has an empty purge path"
validate_managed_purge_path config "$config_dir" "$prefix" "$record_prefix" "$install_id" \
"$config_purge_allowed" "$config_dir_device" "$config_dir_inode" \
"$config_dir_uid" "$config_dir_gid" "$config_marker_sha256"
validate_managed_purge_path state "$state_dir" "$prefix" "$record_prefix" "$install_id" \
"$state_purge_allowed" "$state_dir_device" "$state_dir_inode" \
"$state_dir_uid" "$state_dir_gid" "$state_marker_sha256"
fi
service_account_present=0
if [[ $purge -eq 1 && $created_system_user == 1 ]]; then
[[ $service_uid =~ ^[0-9]+$ && $service_gid =~ ^[0-9]+$ ]] ||
die "installer-created service account record has no uid/gid identity"
if id -u "$SERVICE_USER" >/dev/null 2>&1; then
[[ $(id -u "$SERVICE_USER") -eq $service_uid &&
$(id -g "$SERVICE_USER") -eq $service_gid ]] ||
die "the recorded $SERVICE_USER account was deleted or replaced"
service_account_present=1
fi
fi
if [[ $service_scope != none ]]; then
if ! systemctl_for "$service_scope" disable --now "$SERVICE_NAME"; then
die "could not stop and disable $SERVICE_NAME; runtime was not removed"
fi
fi
rm -f -- "$prefix/current"
rm -rf -- "$prefix/versions"
if [[ $purge -eq 1 ]]; then
if [[ $config_dir == "$state_dir" ]]; then
rm -rf -- "$config_dir"
else
rm -rf -- "$config_dir" "$state_dir"
fi
fi
if [[ $purge -eq 1 && $created_system_user == 1 && $service_account_present -eq 1 ]]; then
userdel "$SERVICE_USER"
fi
if [[ $service_scope != none ]]; then
rm -f -- "$service_unit"
systemctl_for "$service_scope" daemon-reload || warn "systemd daemon-reload failed"
fi
[[ -z $launcher_path ]] || rm -f -- "$launcher_path"
rm -f -- "$record" "$prefix/install.sh" "$prefix/uninstall.sh"
rmdir -- "$prefix" 2>/dev/null || true
printf 'Echoion runtime removed from %s.\n' "$prefix"
if [[ $purge -eq 0 ]]; then
printf 'Configuration and state were preserved:\n %s\n %s\n' "$config_dir" "$state_dir"
fi