2021-09-07 20:44:04 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
|
|
|
# Hook for updating local configuration on each pull. This will automatically put files in their
|
|
|
|
# right places, but will not enable or start any services automatically; this is left to the user.
|
|
|
|
#
|
|
|
|
# Move this to '.git/hooks/post-merge' to have to run automatically after every 'git pull' operation.
|
|
|
|
# The script assumes write access to host directories, and a CoreOS host. Don't run on other systems!
|
|
|
|
|
|
|
|
set -euo pipefail
|
2021-09-12 20:53:37 +00:00
|
|
|
shopt -s globstar
|
2021-09-07 20:44:04 +00:00
|
|
|
|
2021-09-08 17:29:54 +00:00
|
|
|
# Global configuration variables.
|
|
|
|
TEMP_CONFIG_PATH="$(git -C "$(dirname "$0")" rev-parse --show-toplevel)"
|
|
|
|
HOST_CONFIG_PATH="/etc/coreos-home-server"
|
|
|
|
SYSTEMD_CONFIG_PATH="/etc/systemd/system"
|
2021-09-07 20:44:04 +00:00
|
|
|
|
2021-09-08 17:29:54 +00:00
|
|
|
# Synchronize CoreOS home-server configuration for specific path.
|
|
|
|
function sync-coreos-config() {
|
2022-09-19 12:02:29 +00:00
|
|
|
local hostdir="$1"
|
|
|
|
local dirname="$(basename "$hostdir")"
|
2021-09-07 20:44:04 +00:00
|
|
|
|
2022-09-19 12:02:29 +00:00
|
|
|
# Check if any remote configuration exists for the name given.
|
|
|
|
if ! compgen -G "$TEMP_CONFIG_PATH"/*/"$dirname" > /dev/null; then
|
|
|
|
printf "configuration not found, skipping.\n"
|
|
|
|
return
|
|
|
|
fi
|
2021-09-08 17:29:54 +00:00
|
|
|
|
2022-09-19 12:02:29 +00:00
|
|
|
# Collect remote configuration files across base directories.
|
|
|
|
for tmpdir in "$TEMP_CONFIG_PATH"/*/"$dirname"; do
|
|
|
|
# Update timestamp for temporary file to match last commit time, in order to ensure correct partial updates.
|
|
|
|
for f in "$tmpdir/"**; do
|
2021-09-19 16:42:22 +00:00
|
|
|
# Skip directories, as updating their commit times will have all files within be synchronized.
|
|
|
|
if test -d "$f"; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2022-09-19 12:02:29 +00:00
|
|
|
touch -t "$(cd "$tmpdir" && git log -n 1 --pretty=format:%cd --date=format-local:%Y%m%d%H%M.%S --date-order -- "$f")" -- "$f"
|
2021-09-12 20:53:37 +00:00
|
|
|
done
|
|
|
|
|
2022-09-19 12:02:29 +00:00
|
|
|
# Link source files into temporary destination directory.
|
|
|
|
mkdir --parents "$TEMP_CONFIG_PATH/.tmp/$dirname"
|
|
|
|
cp --archive --force --target-directory "$TEMP_CONFIG_PATH/.tmp/$dirname" "$tmpdir"/.
|
|
|
|
done
|
|
|
|
|
|
|
|
# Remove files that only exist in local configuration.
|
|
|
|
for f in $(comm -23 <(cd "$hostdir"; find . | sort) <(cd "$TEMP_CONFIG_PATH/.tmp/$dirname"; find . | sort)); do
|
|
|
|
# Don't delete environment files, as these are required for active services.
|
|
|
|
if test "$(basename --suffix=.env "$f")" != "$(basename "$f")"; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
rm --verbose --recursive --force "$(realpath --quiet "$hostdir/$f")"
|
2021-09-08 17:29:54 +00:00
|
|
|
done
|
2022-09-19 12:02:29 +00:00
|
|
|
|
|
|
|
# Copy files from temporary directory to host configuration directory.
|
|
|
|
cp --verbose --recursive --update --target-directory "$hostdir" "$TEMP_CONFIG_PATH/.tmp/$dirname"/*
|
|
|
|
printf "done.\n"
|
2021-09-08 17:29:54 +00:00
|
|
|
}
|
2021-09-07 20:44:04 +00:00
|
|
|
|
2021-09-08 17:29:54 +00:00
|
|
|
# Synchronize systemd service files from CoreOS home-server configuration.
|
|
|
|
function sync-systemd-services() {
|
2021-09-26 19:00:04 +00:00
|
|
|
local buffer="" tmp
|
2021-09-07 20:44:04 +00:00
|
|
|
|
2021-09-08 17:29:54 +00:00
|
|
|
# Copy service files if newer than destination.
|
|
|
|
for src in "$HOST_CONFIG_PATH"/*/systemd/*; do
|
2021-09-26 19:00:04 +00:00
|
|
|
tmp="$(cp --verbose --recursive --update --target-directory "$SYSTEMD_CONFIG_PATH" "$src")"
|
2022-09-19 12:02:29 +00:00
|
|
|
if test -n "$tmp"; then buffer="${buffer}"$'\n'"${tmp}"; fi
|
2021-09-08 17:29:54 +00:00
|
|
|
done
|
|
|
|
|
2022-09-19 12:02:29 +00:00
|
|
|
printf "%s\ndone.\n" "$buffer"
|
2021-09-26 19:00:04 +00:00
|
|
|
if test -n "$buffer"; then return 0; else return 1; fi
|
2021-09-07 20:44:04 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 17:29:54 +00:00
|
|
|
# Synchronize all existing CoreOS home-server configuration.
|
2022-09-19 12:02:29 +00:00
|
|
|
for hostdir in "$HOST_CONFIG_PATH"/*; do
|
|
|
|
if test ! -d "$hostdir"; then
|
2021-09-08 17:29:54 +00:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2022-09-19 12:02:29 +00:00
|
|
|
printf "Synchronizing host configuration for '%s'... " "$hostdir"
|
|
|
|
sync-coreos-config "$hostdir"
|
2021-09-08 17:29:54 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# Synchronize systemd services from local CoreOS home-server configuration.
|
2021-09-26 19:00:04 +00:00
|
|
|
printf "Synchronizing systemd service files... "
|
2021-09-08 17:29:54 +00:00
|
|
|
if sync-systemd-services; then
|
2021-11-13 13:14:37 +00:00
|
|
|
printf "Reloading systemd daemon after service updates...\n"
|
2021-09-08 17:29:54 +00:00
|
|
|
systemctl daemon-reload
|
|
|
|
fi
|