diff options
author | Patrick Spek <p.spek@tyil.nl> | 2022-04-16 14:27:42 +0200 |
---|---|---|
committer | Patrick Spek <p.spek@tyil.nl> | 2022-04-16 14:27:42 +0200 |
commit | f2f03d6ea4d6d181880a9f9c4f019d24065db4ec (patch) | |
tree | 7fc88e860cee41da90b78df50e5f8723e85eb30b /lib/subcommands | |
parent | d8a2f732b300cdbb892e0878fe87dbb7a0ef6d03 (diff) |
Initial draft for sync subcommand
Diffstat (limited to 'lib/subcommands')
-rw-r--r-- | lib/subcommands/bootstrap.bash | 25 | ||||
-rw-r--r-- | lib/subcommands/sync.bash | 53 |
2 files changed, 67 insertions, 11 deletions
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 +} |