forked from Capgemini/Apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.sh
executable file
·198 lines (168 loc) · 4.9 KB
/
common.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/bash
# Util functions cloud reusable.
APOLLO_ROOT=$(dirname "${BASH_SOURCE}")/..
DEFAULT_CONFIG="${APOLLO_ROOT}/bootstrap/${APOLLO_PROVIDER}/${APOLLO_CONFIG_FILE-"config-default.sh"}"
APOLLO_INVENTORY="${APOLLO_INVENTORY-inventory}"
APOLLO_PLAYBOOK="${APOLLO_PLAYBOOK-site.yml}"
if [ -f "${DEFAULT_CONFIG}" ]; then
source "${DEFAULT_CONFIG}"
fi
verify_prereqs() {
if [[ "$(which terraform)" == "" ]]; then
echo -e "${color_red}Can't find terraform in PATH, please fix and retry.${color_norm}"
exit 1
fi
check_terraform_version
if [[ "$(which ansible-playbook)" == "" ]]; then
echo -e "${color_red}Can't find ansible-playbook in PATH, please fix and retry.${color_norm}"
exit 1
fi
if [[ "$(which python)" == "" ]]; then
echo -e "${color_red}Can't find python in PATH, please fix and retry.${color_norm}"
exit 1
fi
}
check_terraform_version() {
local IFS='.'
local current_version_string="${2:-$( terraform --version | awk 'NR==1 {print $2}' )}"
local requirement_version_string=${1:-0.6.12}
local -a current_version=( ${current_version_string#'v'} )
local -a requirement_version=( ${requirement_version_string} )
local n diff
local result=0
for (( n=0; n<${#requirement_version[@]}; n+=1 )); do
diff=$((current_version[n]-requirement_version[n]))
if [ $diff -ne 0 ] ; then
[ $diff -le 0 ] && result=1 || result=0
break
fi
done
echo "You are running Terraform ${current_version_string}..."
if [ $result -eq 1 ]; then
echo -e "${color_red}Terraform >= ${requirement_version_string} is required, please fix and retry.${color_norm}"
exit 1
fi
}
get_apollo_variables() {
local plugin_namespace=${1:-"APOLLO_"}
local -a var_list=()
local IFS=$'\n'
for env_var in $( env | grep "${plugin_namespace}" ); do
# This deletes shortest match of $substring from front of $string. ${string#substring}
var_value=${env_var#${plugin_namespace}}
var=$( echo "${var_value}" | awk -F = '{ print $1 }' )
value=${var_value#*=}
var_list+=( "${var}='${value}'" )
done
echo "${var_list[@]}"
}
apollo_launch() {
if [[ "$@" == "-i" ]]; then
get_terraform_modules
terraform_apply
run_if_exist "ansible_ssh_config"
ansible_playbook_run
run_if_exist "set_vpn"
open_urls
elif [ "$@" ]; then
eval $@
else
get_terraform_modules
terraform_apply
run_if_exist "ansible_ssh_config"
ansible_playbook_run
run_if_exist "set_vpn"
fi
}
run_if_exist() {
if [ "$(type -t "${1}")" = function ]; then
$1
fi
}
get_master_url() {
local master_url=''
if [[ $APOLLO_PROVIDER == "vagrant" ]]; then
master_url="http://master1"
else
pushd "${APOLLO_ROOT}/terraform/${APOLLO_PROVIDER}" > /dev/null
master_url="http://$(terraform output master.1.ip)"
popd > /dev/null
fi
echo "${master_url}"
}
open_urls() {
local master_url=$(get_master_url)
local open_cmd=""
if [ -a /usr/bin/open ]; then
open_cmd=/usr/bin/open
elif [ -a /usr/bin/xdg-open ]; then
open_cmd=/usr/bin/xdg-open
elif start; then
open_cmd=start
fi
if [ -a ${open_cmd} ]; then
"${open_cmd}" "${master_url}:5050"
"${open_cmd}" "${master_url}:8080"
"${open_cmd}" "${master_url}:8500"
fi
}
ansible_playbook_run() {
pushd "${APOLLO_ROOT}"
get_ansible_inventory
install_contributed_roles
ansible-playbook --inventory-file="${APOLLO_ROOT}/${APOLLO_INVENTORY}" \
--tags "${ANSIBLE_TAGS:-all}" \
${ANSIBLE_LOG} --extra-vars "$( get_apollo_variables APOLLO_)" \
${ANSIBLE_EXARGS:-} \
${APOLLO_PLAYBOOK}
popd
}
ansible_dcos_install() {
export APOLLO_PLAYBOOK='dcos.yml'
ansible_playbook_run
}
ansible_upgrade_mesoscluster() {
export APOLLO_PLAYBOOK='rolling-upgrade-mesoscluster.yml'
ansible_playbook_run
}
ansible_upgrade_maintenance() {
export APOLLO_PLAYBOOK='rolling-upgrade-maintenance.yml'
ansible_playbook_run
}
install_contributed_roles() {
pushd "${APOLLO_ROOT}"
ansible-galaxy install --force -r contrib-plugins/plugins.yml
ansible-galaxy install --force -r requirements.yml
popd
}
get_ansible_inventory() {
pushd $APOLLO_ROOT
if [ ! -f inventory/terraform.py ]; then
curl -sS https://raw.githubusercontent.com/Capgemini/terraform.py/1.2/terraform.py -o inventory/terraform.py
chmod 755 inventory/terraform.py
fi
popd
}
get_terraform_modules() {
pushd "${APOLLO_ROOT}/terraform/${APOLLO_PROVIDER}"
# Downloads terraform modules.
terraform get
# Make any dependencies
if ls -1 .terraform/modules/*/Makefile >/dev/null 2>&1; then
for dir in .terraform/modules/*/Makefile;
do
make -C $(/usr/bin/dirname $dir)
done
fi
popd
}
terraform_apply() {
pushd "${APOLLO_ROOT}/terraform/${APOLLO_PROVIDER}"
terraform apply
popd
}
# Helper function to get the fist octet of an IPv4 address.
get_network_identifier() {
ip="$1"
echo $(echo $ip | tr "." " " | awk '{ print $1 }')
}