mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-10 20:27:02 +01:00
Two levels of pure taxonomy carrying no information, and a 2-directory
cap leaves no room for them. Categories now sit directly under
linux_configuration/: features, fixes, utils, misc, fresh-install,
installers, diagnostics, gaming, boot_recovery, periodic_background, lib,
meta, tests. A script's own lib/ and tests/ are exempt from the cap, so
those stay put.
Depth violations 248 -> 139; roughly 40 of the remainder are inside
digital_wellbeing and hosts, deferred until the guard-lib repoint.
The moves are the easy half. What breaks is every path that counted
levels, and three classes of it had to be found by resolving paths rather
than reading diffs:
- 52 relative `source` lines for lib/common.sh and lib/android.sh. The
correct prefix differs per file: 40 were ../../ and 12 were ../../../,
so a blanket replace would have fixed one set and broken the other.
- REPO_ROOT in 7 fixes/lib/tests harnesses and setup_syncyomi_guard.sh,
each walking up four levels from a directory now two levels shallower.
This caused a real regression -- the fixes suite went red with
"log_warn: command not found" -- caught by running it and comparing
against the pre-move baseline worktree.
- conftest.py's sys.path entries, which took the pytest gate down with
14 collection errors, and the `# shellcheck source=` directives, which
took shellcheck down with SC1091.
16 symlinks in $HOME broke, including the entire i3blocks bar. All
repointed, and cpu_monitor/memory/disk/time/volume were run to confirm
they emit real output rather than merely resolving.
Verified: 1798 tests pass at 100% coverage, 8 of 9 lib/tests suites pass
(the ninth needs the coverage jail, identically at baseline), pre-commit
is clean, and the two untracked secret configs are still ignored after
their gitignore paths changed.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJrqKERAo5QUeEHWS5Asgp
170 lines
4.6 KiB
Bash
Executable File
170 lines
4.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# ============================================================================
|
|
# migrate_hosts_guard_to_guard_lib.sh
|
|
#
|
|
# Completes the guard-lib migration that commit 66c4698 started but never
|
|
# finished on this machine.
|
|
#
|
|
# State this repairs: guard-lib is installed (guardctl, lib/, systemd templates,
|
|
# and BOTH generic pacman hooks) with /etc/guard-lib/targets completely empty,
|
|
# while the pre-guard-lib hosts-guard implementation still does all the real
|
|
# work through its own per-file pacman hooks and systemd units. So every pacman
|
|
# transaction runs two PreTransaction hooks where one would do, and the
|
|
# guard-lib pair - the one that runs FIRST, and the one present for all four
|
|
# recorded hook stalls - does literally nothing.
|
|
#
|
|
# After this runs there is one guard system: guardctl file-guard instances for
|
|
# hosts, nsswitch and resolved, driven by guard-lib's generic unlock-all /
|
|
# relock-all hooks. The legacy hooks and units are retired.
|
|
#
|
|
# NOT migrated here: the "shutdown-schedule" instance that
|
|
# setup_midnight_shutdown.sh registers. Its target /etc/shutdown-schedule.conf
|
|
# does not exist on this machine, so registering it would mean installing a
|
|
# different feature, not finishing this migration. Run that script if you want
|
|
# the midnight-shutdown guard.
|
|
#
|
|
# Every step is idempotent, and --rollback restores the legacy layer.
|
|
# ============================================================================
|
|
|
|
set -euo pipefail
|
|
|
|
# shellcheck source=lib/hosts_guard_migrate.sh
|
|
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib/hosts_guard_migrate.sh"
|
|
# shellcheck source=lib/hosts_guard_rollback.sh
|
|
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib/hosts_guard_rollback.sh"
|
|
|
|
SCRIPT_NAME="$(basename "$0")"
|
|
readonly SCRIPT_NAME
|
|
readonly GUARDCTL="/usr/local/bin/guardctl"
|
|
readonly TARGETS_DIR="/etc/guard-lib/targets"
|
|
readonly STATE_DIR="/var/lib/guard-lib-migration"
|
|
readonly HOOKS_DIR="/etc/pacman.d/hooks"
|
|
# Plugins must live somewhere permanent: guardctl records an ABSOLUTE plugin
|
|
# path in the instance conf, so pointing it at a repo checkout (or worse, a git
|
|
# worktree) silently breaks enforcement the day that directory moves.
|
|
readonly PLUGIN_INSTALL_DIR="/usr/local/share/guard-lib-plugins"
|
|
|
|
readonly LEGACY_HOOKS=(
|
|
"10-unlock-etc-hosts.hook"
|
|
"90-relock-etc-hosts.hook"
|
|
)
|
|
readonly LEGACY_UNITS=(
|
|
"hosts-guard.path"
|
|
"hosts-guard.service"
|
|
"hosts-bind-mount.service"
|
|
"nsswitch-guard.path"
|
|
"nsswitch-guard.service"
|
|
"resolved-guard.path"
|
|
"resolved-guard.service"
|
|
)
|
|
|
|
DRY_RUN=0
|
|
MODE="migrate"
|
|
|
|
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
|
|
readonly SCRIPT_DIR
|
|
PLUGIN_SRC_DIR="$(readlink -f "$SCRIPT_DIR/../../periodic_background/hosts/guard/plugins")"
|
|
readonly PLUGIN_SRC_DIR
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[0;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m'
|
|
|
|
msg() { printf "${GREEN}[✓]${NC} %s\n" "$*"; }
|
|
note() { printf "${BLUE}[i]${NC} %s\n" "$*"; }
|
|
warn() { printf "${YELLOW}[!]${NC} %s\n" "$*"; }
|
|
err() { printf "${RED}[✗]${NC} %s\n" "$*" >&2; }
|
|
|
|
usage() {
|
|
cat <<EOF
|
|
Usage: sudo $SCRIPT_NAME [OPTIONS]
|
|
|
|
Finishes the guard-lib migration for /etc/hosts, /etc/nsswitch.conf and
|
|
/etc/systemd/resolved.conf, then retires the legacy hosts-guard layer.
|
|
|
|
Options:
|
|
--rollback Undo: uninstall the guard-lib instances, restore the legacy
|
|
pacman hooks and re-enable the legacy units.
|
|
--status Show what is currently registered; change nothing.
|
|
--dry-run Print what would happen; change nothing.
|
|
-h, --help Show this help
|
|
EOF
|
|
exit 0
|
|
}
|
|
|
|
run() {
|
|
if ((DRY_RUN == 1)); then
|
|
printf "${YELLOW}DRY-RUN:${NC} %s\n" "$*"
|
|
return 0
|
|
fi
|
|
"$@"
|
|
}
|
|
|
|
require_root() {
|
|
if [[ $EUID -ne 0 ]]; then
|
|
exec sudo -E bash "$0" "$@"
|
|
fi
|
|
}
|
|
|
|
# ----------------------------------------------------------------------------
|
|
# Instance table: name | target | bind_mount | plugin basename | also_watch
|
|
# ----------------------------------------------------------------------------
|
|
instance_spec() { # <name> -> echoes "target|bind|plugin|also_watch"
|
|
case "$1" in
|
|
hosts) echo "/etc/hosts|yes||" ;;
|
|
nsswitch) echo "/etc/nsswitch.conf|no|nsswitch-plugin.sh|" ;;
|
|
resolved) echo "/etc/systemd/resolved.conf|no|resolved-plugin.sh|/etc/systemd/resolved.conf.d" ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
|
|
readonly INSTANCES=(hosts nsswitch resolved)
|
|
|
|
instance_registered() { [[ -f "$TARGETS_DIR/$1.conf" ]]; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Parse arguments
|
|
require_root "$@"
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--rollback)
|
|
MODE="rollback"
|
|
shift
|
|
;;
|
|
--status)
|
|
MODE="status"
|
|
shift
|
|
;;
|
|
--dry-run)
|
|
DRY_RUN=1
|
|
shift
|
|
;;
|
|
-h | --help)
|
|
usage
|
|
;;
|
|
*)
|
|
err "Unknown option: $1"
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
case "$MODE" in
|
|
migrate) do_migrate ;;
|
|
rollback) do_rollback ;;
|
|
status) show_status ;;
|
|
esac
|