-
Notifications
You must be signed in to change notification settings - Fork 9
/
sb_repo.sh
151 lines (131 loc) · 4.54 KB
/
sb_repo.sh
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
#########################################################################
# Title: Saltbox Repo Cloner Script #
# Author(s): desimaniac, salty #
# URL: https://github.com/saltyorg/sb #
# -- #
#########################################################################
# GNU General Public License v3.0 #
#########################################################################
################################
# Variables
################################
VERBOSE=false
BRANCH="master"
SALTBOX_PATH="/srv/git/saltbox"
SALTBOX_REPO="https://github.com/saltyorg/saltbox.git"
################################
# Functions
################################
usage () {
echo "Usage:"
echo " sb_repo -b <branch> Repo branch to use. Default is 'master'."
echo " sb_repo -v Enable Verbose Mode."
echo " sb_repo -h Display this help message."
}
run_cmd() {
local error_output
local cmd_exit_code
if $VERBOSE; then
printf '%s\n' "+ $*" >&2
"$@"
else
error_output=$("$@" 2>&1)
fi
cmd_exit_code=$?
if [ $cmd_exit_code -ne 0 ]; then
echo "Command failed with exit code $cmd_exit_code: $*" >&2
if [ -n "$error_output" ]; then
echo "Error output: $error_output" >&2
fi
exit $cmd_exit_code
fi
}
################################
# Argument Parser
################################
while getopts ':b:vh' f; do
case $f in
b) BRANCH=$OPTARG;;
v) VERBOSE=true;;
h)
usage
exit 0
;;
\?)
echo "Invalid Option: -$OPTARG" 1>&2
echo ""
usage
exit 1
;;
esac
done
################################
# Main
################################
$VERBOSE && echo "git branch selected: $BRANCH"
## Clone Saltbox and pull latest commit
if [ -d "$SALTBOX_PATH" ]; then
if [ -d "$SALTBOX_PATH/.git" ]; then
cd "$SALTBOX_PATH" || exit
run_cmd git fetch --all --prune
# shellcheck disable=SC2086
run_cmd git checkout -f $BRANCH
# shellcheck disable=SC2086
run_cmd git reset --hard origin/$BRANCH
run_cmd git submodule update --init --recursive
$VERBOSE && echo "git branch: $(git rev-parse --abbrev-ref HEAD)"
else
cd "$SALTBOX_PATH" || exit
run_cmd rm -rf library/
run_cmd git init
run_cmd git remote add origin "$SALTBOX_REPO"
run_cmd git fetch --all --prune
# shellcheck disable=SC2086
run_cmd git branch $BRANCH origin/$BRANCH
# shellcheck disable=SC2086
run_cmd git reset --hard origin/$BRANCH
run_cmd git submodule update --init --recursive
$VERBOSE && echo "git branch: $(git rev-parse --abbrev-ref HEAD)"
fi
else
# shellcheck disable=SC2086
run_cmd git clone -b $BRANCH "$SALTBOX_REPO" "$SALTBOX_PATH"
cd "$SALTBOX_PATH" || exit
run_cmd git submodule update --init --recursive
$VERBOSE && echo "git branch: $(git rev-parse --abbrev-ref HEAD)"
fi
release=$(lsb_release -cs 2>/dev/null | grep -v "No LSB modules are available.")
## Copy settings and config files into Saltbox folder
shopt -s nullglob
for i in "$SALTBOX_PATH"/defaults/*.default; do
if [ ! -f "$SALTBOX_PATH/$(basename "${i%.*}")" ]; then
if [[ $release =~ (focal|jammy)$ ]]; then
run_cmd cp -n "${i}" "$SALTBOX_PATH/$(basename "${i%.*}")"
elif [[ $release =~ (noble)$ ]]; then
run_cmd cp --update=none "${i}" "$SALTBOX_PATH/$(basename "${i%.*}")"
fi
fi
done
shopt -u nullglob
## Activate Git Hooks
cd "$SALTBOX_PATH" || exit
run_cmd bash "$SALTBOX_PATH/bin/git/init-hooks"
## Download saltbox.fact file
FACT_URL="https://github.com/saltyorg/ansible-facts/releases/latest/download/saltbox-facts"
FACT_PATH="$SALTBOX_PATH/ansible_facts.d/saltbox.fact"
$VERBOSE && echo "Downloading the saltbox.fact file..."
mkdir -p "$SALTBOX_PATH/ansible_facts.d"
# Execute curl command and store the exit status
curl -fsSL "$FACT_URL" -o "$FACT_PATH"
curl_exit_status=$?
# Check the exit status
if [ $curl_exit_status -eq 0 ]; then
run_cmd chmod +x "$FACT_PATH"
$VERBOSE && echo "The saltbox.fact file downloaded and set as executable."
else
echo "Failed to download the saltbox.fact file. curl exit status: $curl_exit_status" >&2
exit 1
fi
$VERBOSE && echo "sb_repo.sh ran successfully."
exit 0