aboutsummaryrefslogtreecommitdiff
path: root/lib/subcommands/sync.bash
blob: 9537922b636c21e098016fea714566b663b72fc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env bash

# SPDX-FileCopyrightText: 2022 Patrick Spek <p.spek@tyil.nl>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

subcommand()
{
	export BASHTARD_PLAYBOOK="$1" ; shift

	# If a specific playbook is given, sync that
	if [[ -n "$BASHTARD_PLAYBOOK" ]]
	then
		sync_playbook "$BASHTARD_PLAYBOOK"
		return
	fi

	notice "bashtard/sync" "Syncing $BASHTARD_ETCDIR"
	pushd -- "$BASHTARD_ETCDIR" > /dev/null
	git pull origin master || return 4
	popd > /dev/null

	# Otherwise, do a full sync
	notice "bashtard/sync" "Syncing remote playbooks"

	# Update all playbook sources
	while read -r playbook _ branch
	do
		pushd -- "$BASHTARD_ETCDIR/playbooks.d/$playbook" > /dev/null \
			|| return 2
		git pull origin "$branch"
		popd > /dev/null
	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_BIN" 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

	# shellcheck disable=SC1090
	. "$playbook_base/playbook.bash"

	playbook_sync
}