From f2f03d6ea4d6d181880a9f9c4f019d24065db4ec Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sat, 16 Apr 2022 14:27:42 +0200 Subject: Initial draft for sync subcommand --- lib/subcommands/bootstrap.bash | 25 +++++++++++--------- lib/subcommands/sync.bash | 53 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 lib/subcommands/sync.bash (limited to 'lib/subcommands') diff --git a/lib/subcommands/bootstrap.bash b/lib/subcommands/bootstrap.bash index b2e540b..8750134 100644 --- a/lib/subcommands/bootstrap.bash +++ b/lib/subcommands/bootstrap.bash @@ -18,29 +18,30 @@ bootstrap_local() { local dirs=( "$BASHTARD_ETCDIR" + "$BASHTARD_ETCDIR/conf.d" "$BASHTARD_ETCDIR/hosts.d" "$BASHTARD_ETCDIR/os.d" "$BASHTARD_ETCDIR/playbooks.d" - "$BASHTARD_ETCDIR/playbook-registry.d/" + "$BASHTARD_ETCDIR/registry.d" + "$BASHTARD_ETCDIR/registry.d/${BASHTARD_PLATFORM[fqdn]}" ) local files=( "$BASHTARD_ETCDIR/defaults" - "$BASHTARD_ETCDIR/playbooks" - "$BASHTARD_ETCDIR/playbook-registry.d/${BASHTARD_PLATFORM[fqdn]}" "$BASHTARD_ETCDIR/hosts.d/${BASHTARD_PLATFORM[fqdn]}" "$BASHTARD_ETCDIR/os.d/${BASHTARD_PLATFORM[key]}" + "$BASHTARD_ETCDIR/playbooks.d/remotes" ) for dir in "${dirs[@]}" do - notice "bootstrap" "Creating $dir" + notice "bashtard/bootstrap" "Creating $dir" mkdir -p -- "$dir" done for file in "${files[@]}" do - notice "bootstrap" "Creating $file" + notice "bashtard/bootstrap" "Creating $file" touch -- "$file" done } @@ -53,20 +54,22 @@ bootstrap_remote() local files=( "$BASHTARD_ETCDIR/hosts.d/${BASHTARD_PLATFORM[fqdn]}" "$BASHTARD_ETCDIR/os.d/${BASHTARD_PLATFORM[key]}" - "$BASHTARD_ETCDIR/playbook-registry.d/${BASHTARD_PLATFORM[fqdn]}" + "$BASHTARD_ETCDIR/registry.d/${BASHTARD_PLATFORM[fqdn]}" ) for file in "${files[@]}" do [[ -f "$file" ]] && continue - notice "bootstrap" "Creating $file" + notice "bashtard/bootstrap" "Creating $file" touch -- "$file" done - while read -r playbook url + while read -r playbook url branch do - notice bootstrap "Cloning $playbook from $url" - git clone "$url" "$BASHTARD_ETCDIR/playbooks.d" - done < "$BASHTARD_ETCDIR/playbooks" + notice "bashtard/bootstrap" "Cloning $playbook from $url" + git clone "$url" "$BASHTARD_ETCDIR/playbooks.d/$playbook" + + # TODO: Clone appropriate branch + done < "$BASHTARD_ETCDIR/playbooks.d/remotes" } diff --git a/lib/subcommands/sync.bash b/lib/subcommands/sync.bash new file mode 100644 index 0000000..af5f282 --- /dev/null +++ b/lib/subcommands/sync.bash @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +subcommand() +{ + export BASHTARD_PLAYBOOK="$1" ; shift + + # If a specific playbook is given, sync that + if [[ ! -z "$BASHTARD_PLAYBOOK" ]] + then + sync_playbook "$BASHTARD_PLAYBOOK" + return + fi + + # Otherwise, do a full sync + notice "bashtard/sync" "Syncing remote playbooks" + + # Update all playbook sources + while read -r playbook url branch + do + pushd -- "$BASHTARD_ETCDIR/playbooks.d/$playbook" + git pull origin "$branch" + done < "$BASHTARD_ETCDIR/playbooks.d/remotes" + + # Run a sync for each registered playbook for this host + while read -r playbook + do + info "bashtard/sync" "Syncing $playbook" + "$BASHTARD_BASEDIR/bin/$BASHTARD_NAME" sync "$playbook" + done < "$BASHTARD_ETCDIR/registry.d/${BASHTARD_PLATFORM[fqdn]}" +} + +sync_playbook() +{ + local playbook_base="$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK" + + notice "bashtard/sync" "Running sync for $BASHTARD_PLAYBOOK" + + if [[ ! -d "$playbook_base" ]] + then + emerg "bashtard/sync" "No such directory: $playbook_base" + return 1 + fi + + if [[ ! -f "$playbook_base/playbook.bash" ]] + then + emerg "bashtard/sync" "No such file: $playbook_base/playbook.bash" + return 1 + fi + + . "$playbook_base/playbook.bash" + + playbook_sync +} -- cgit v1.1