-
Notifications
You must be signed in to change notification settings - Fork 11
/
import_env.sh
executable file
·73 lines (58 loc) · 1.48 KB
/
import_env.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
#!/usr/bin/env bash
# Auto switch between envs stored in different git branches from a secondary repo
#
# Repo should mirror this repo like so:
#
# /group_vars/*.yml
# /host_vars/*.yml
# /hosts.yml
#
# Usage: ./import_env.sh /path/to/env/repo branch_name
ENV_PATH=$1
BRANCH=$2
function clean_existing() {
rm -v group_vars/*.yml
rm -v host_vars/*.yml
rm -v hosts.yml
}
function check_sanity() {
if [ ! -d "$1" ]; then
echo "ENV_PATH does not exist: $1"
exit 1
fi
if [[ -z ${2} ]]; then
echo "Must set branch name"
exit 2
fi
pushd "$1" || exit 2
local branch_exists
branch_exists=$(git branch --list "${2}")
popd || exit 2
if [[ -z ${branch_exists} ]]; then
echo "Branch name $2 does not exists."
exit 2
fi
return 0
}
function switch_branch() {
pushd "$1" || exit 2
git checkout "${2}" && git pull || exit 2
popd || exit 2
}
function load_branch() {
ln -v "${1}"/hosts.yml hosts.yml
for fullfile in "${1}"/host_vars/*.yml; do
[ -e "$fullfile" ] || continue
filename=$(basename -- "$fullfile")
ln -v "$fullfile" host_vars/"$filename"
done
for fullfile in "${1}"/group_vars/*.yml; do
[ -e "$fullfile" ] || continue
filename=$(basename -- "$fullfile")
ln -v "$fullfile" group_vars/"$filename"
done
}
check_sanity "$ENV_PATH" "$BRANCH" || exit $?
switch_branch "$ENV_PATH" "$BRANCH" || exit $?
clean_existing
load_branch "$ENV_PATH"