-
Notifications
You must be signed in to change notification settings - Fork 11
/
import_env.sh
executable file
·59 lines (50 loc) · 1.19 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
#!/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() {
cp -v "${1}"/hosts.yml .
cp -v "${1}"/host_vars/*.yml host_vars/
cp -v "${1}"/group_vars/*.yml group_vars/
}
check_sanity "$ENV_PATH" "$BRANCH" || exit $?
switch_branch "$ENV_PATH" "$BRANCH" || exit $?
clean_existing
load_branch "$ENV_PATH"