Correct issues with 'post-merge' hook

The use of 'rsync' is not compatible with the SELinux setup used in
CoreOS, and thus simple use of 'cp' and 'rm' is required.
This commit is contained in:
Alex Palaistras 2021-09-08 18:29:54 +01:00
parent 253deb7176
commit 67e8a28b8f
2 changed files with 54 additions and 29 deletions

View File

@ -9,7 +9,7 @@ SyslogIdentifier=%N
PrivateTmp=true
Environment=GIT_REMOTE_URL=https://github.com/deuill/coreos-home-server.git
ExecStartPre=/usr/bin/git clone --verbose --depth=1 -- $GIT_REMOTE_URL %T/coreos-home-server
ExecStart=/bin/sh %T/coreos-home-server/hooks/post-merge
ExecStart=/bin/bash %T/coreos-home-server/hooks/post-merge
[Install]
WantedBy=multi-user.target

View File

@ -8,38 +8,63 @@
set -euo pipefail
# Base configuration variables.
ROOTDIR="$(git -C "$(dirname "$0")" rev-parse --show-toplevel)"
COREOS_CONFIG_DIR="/etc/coreos-home-server"
SYSTEMD_CONFIG_DIR="/etc/systemd/system"
# 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"
function sync-coreos() {
local from="${ROOTDIR}/config" to="${COREOS_CONFIG_DIR}"
rsync --recursive --update --links --times --perms --delete-after --delete-excluded \
--exclude=.git --exclude=*.fcc --exclude=*.ign --filter='protect *.env' "${from}"/*/ "${to}/"
}
# Synchronize CoreOS home-server configuration for specific path.
function sync-coreos-config() {
local path="$1"
function sync-systemd() {
local from="${ROOTDIR}/config" to="${SYSTEMD_CONFIG_DIR}"
rsync --info=name --recursive --update "${from}"/*/systemd/ "${from}"/*/*/systemd/ "${to}/"
}
function main() {
local buffer
# Synchronize local configuration into host directory.
echo "Synchronizing host configuration in '${COREOS_CONFIG_DIR}'..."
sync-coreos
echo "Synchronizing systemd configuration in '${SYSTEMD_CONFIG_DIR}'..."
buffer=$(sync-systemd)
if test -n "${buffer}"; then
echo "Systemd services updated, reloading daemon..."
echo "${buffer}"
sudo systemctl daemon-reload
# Search for configuration in any of the local configuration sub-directories.
for dir in "$TEMP_CONFIG_PATH"/config/*/"$(basename "$path")"; do
if test ! -d "$dir"; then
echo "configuration not found, skipping."
return
fi
# Remove files that only exist in local configuration.
for n in $(comm -23 <(cd "$path"; find . | sort) <(cd "$dir"; find . | sort)); do
rm --verbose --recursive --force "$(realpath --quiet "$path/$n")"
done
cp --verbose --recursive --update --target-directory "$path" "$dir"/*
echo "done."
done
}
# Execute program body.
main "$@"
# Synchronize systemd service files from CoreOS home-server configuration.
function sync-systemd-services() {
local dest
local result=1
# Copy service files if newer than destination.
for src in "$HOST_CONFIG_PATH"/*/systemd/*; do
dest="$SYSTEMD_CONFIG_PATH/$(basename "$src")"
if test "$src" -nt "$dest"; then
cp --verbose --recursive "$src" "$dest"
result=0
fi
done
echo "done."
return $result
}
# Synchronize all existing CoreOS home-server configuration.
for dir in "$HOST_CONFIG_PATH"/*; do
if test ! -d "$dir"; then
continue
fi
echo -n "Synchronizing host configuration for '$dir'... "
sync-coreos-config "$dir"
done
# Synchronize systemd services from local CoreOS home-server configuration.
echo -n "Synchronizing systemd service files... "
if sync-systemd-services; then
echo "Reloading systemd daemon after service updates."
systemctl daemon-reload
fi