#!/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 # 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" # Synchronize CoreOS home-server configuration for specific path. function sync-coreos-config() { local path="$1" # 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 } # 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