aboutsummaryrefslogtreecommitdiff
path: root/run.sh
blob: d0e7c710b4ad30eb72bdeb8f133a7b1fcd68c387 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/sh
set -e

# Go to root directory
cd "$(dirname $0)"
# Include basic utilities
. utils/echo

REQ_OPS=

while [ $# -gt 0 ]; do
	case "$1" in
		--verbose|-v)
			echo_verbose + 1
			;;
		--quiet|-q)
			echo_verbose - 1
			;;
		--operation|-o)
			shift
			REQ_OPS="$REQ_OPS $1"
			;;
		--key)
			shift
			KEY_FILE="$1"
			;;
		*)
			echo_die "Unknown argument: $1"
			;;
	esac
	shift
done

# Load host configuration
[ -f hosts/"$(hostname)" ] || echo_die "No configuration for host $(hostname) found."
. hosts/"$(hostname)"

# Update git submodules
git submodule update --init --recursive || echo_die "Git submodule update failed!"

# Include rest of the utilities
. utils/ops
. utils/encrypt
. utils/identify
. utils/syscheck # This performs system check
. utils/cleaner
. utils/lock # This also creates lock
. utils/fdata
. utils/diff

# Always add multiconfig operation
OPERATIONS="multiconfig $OPERATIONS"

# Include enabled operations
for OPT in $OPERATIONS; do
	if [ -f ops/"$OPT" ]; then
		. ops/"$OPT"
	else
		echo_die "No operation $OPT."
	fi
done

# Check if operations need some update
if [ -z "$REQ_OPS" ]; then
	for OPT in $OPERATIONS; do
		if ! "$OPT"_check; then
			echo_info "$OPT scheduled"
			REQ_OPS="$REQ_OPS $OPT"
		fi
	done
fi
if [ -z "$REQ_OPS" ]; then
	echo_info "All operations are in check."
	exit
fi

# Run preparations
for OPT in $REQ_OPS; do
	if "$OPT"_prepare; then
		PREP_OPS="$PREP_OPS $OPT"
	else
		echo_error "Preparation failed for $OPT. Skipping."
	fi
done

# Now apply operations
for OPT in $PREP_OPS; do
	if ! "$OPT"_apply; then
		echo_error "Operation $OPT application failed! Please check what happened."
		break
	fi
done

# And at the end run cleanups on all operations we ever executed
for OPT in $REQ_OPS; do
	"$OPT"_clean || echo_warn "Cleanup of operation $OPT failed."
done