This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
executable file
·174 lines (154 loc) · 4.4 KB
/
deploy.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
#!/bin/bash
# debug with bash -x deploy.sh
# export FLEETCTL_ENDPOINT: docker host IP and 4001 port
if [ -z "$FLEETCTL_ENDPOINT" ]; then
FLEETCTL_ENDPOINT=http://$(netstat -nr | grep '^0\.0\.0\.0' | awk '{ print $2 }'):4001
export FLEETCTL_ENDPOINT=${FLEETCTL_ENDPOINT}
fi
# get environment from outside: from CLI argument or env variable - for docker
# run docker on vagrant -e fleetenv=vagrant
fleetenv=$1
if [[ -z "${fleetenv}" ]]; then
fleetenv=${environ}
fi
if [[ -z "${fleetenv}" ]]; then
echo Running on AWS by default
else
echo Running on ${fleetenv}
fi
# check if fleetctl fleet client is mounted into the container
if [[ ! -f /usr/bin/fleetctl ]]; then
echo "fleetctl is not mounted, mount it with '-v /usr/bin/fleetctl:/usr/bin/fleetctl'"
exit -1
fi
# helper function
function array_contains_element() {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
# start fleet unit and wait till 'active' and 'running'
function start_fleet_unit() {
fleetctl start $1
# do not wait passive_units to activate
array_contains_element $1 "${passive_units[@]}"
if [ $? -eq 0 ]; then
return 0
fi
# wait for ACTIVE status is 'active' or 3 min timeout
local status=0
while [ $status -lt 180 ] ;do
sleep 1
if [ "$(fleetctl list-units | grep -cw $1)" -ne 0 ]; then
x=($(fleetctl list-units | grep -w $1 | awk '{print $3}'))
array_contains_element "active" "${x[@]}"
if [ $? -eq 0 ]; then
status=180
fi
fi
status=$((status+1))
done
}
# submit new/updates fleet units and destroy previous versions, if exist
# return true, if service updated
function load_fleet_unit() {
local __result=1
if [[ $(fleetctl list-unit-files | grep -w ${1}) ]]; then
fleetctl submit ${1} 1&> .tmp
if cat .tmp | grep -q "differs"; then
fleetctl destroy ${1}
fleetctl submit ${1}
echo "${1} - unit had beed updated ====="
__result=0
else
echo "${1} - update is not required"
fi
rm .tmp
else
fleetctl submit ${1}
echo "${1} - a new unit uploaded >>>>>"
__result=0
fi
return $__result
}
# destroy fleet units without files
function cleanup_fleet_units() {
for file in $(fleetctl list-unit-files -fields unit --no-legend); do
if [ -f $file ]; then
continue
else
# handle template
if [[ $file =~ "@" ]]; then
local template=${file%%@*}@.service
if [ -f $template ]; then
continue
else
fleetctl destroy ${file}
fi
else
fleetctl destroy ${file}
fi
fi
done
}
# deploy = load and start fleet unit, if new unit is loaded or unit is updated
function deploy_fleet_unit() {
load_fleet_unit $1
if [ $? -eq 0 ]; then
start_fleet_unit $1
fi
}
# core units to be started first
declare -a core_units=( skydns.service registrator.service postgres.vagrant.service cadvisor.service logentries.service vault.service result-upload-service.service )
# all units
declare -a all_units=(*.service)
# timer units
declare -a timer_units=( backup-elastic.aws.timer backup-etcd.aws.timer )
# other units
declare -a other_units=( )
# passive units are started by other units or executed only once
declare -a passive_units=( backup-elastic.aws.service backup-etcd.aws.service dex-client-config.service vault-unseal.service )
for u in "${all_units[@]}"; do
array_contains_element $u "${core_units[@]}"
if [ $? -eq 1 ]; then
# for template add two units
if [[ $u =~ @.service ]]; then
other_units+=(${u/@.service/@1.service})
if [[ $(fleetctl list-machines | grep -v MACHINE | wc -l) -gt 1 ]]; then
other_units+=(${u/@.service/@2.service})
if [[ $u =~ [email protected] ]]; then
other_units+=(${u/@.service/@3.service})
fi
fi
else
other_units+=($u)
fi
fi
done
units=( ${core_units[@]} ${other_units[@]} ${timer_units[@]})
for unit in "${units[@]}"; do
echo "depoyment of: $unit"
case "$unit" in
*.vagrant.service )
if [[ "$fleetenv" = "vagrant" ]]; then
deploy_fleet_unit $unit
fi
;;
*.aws.* )
if [[ "$fleetenv" != "vagrant" ]]; then
deploy_fleet_unit $unit
fi
;;
*.service )
deploy_fleet_unit $unit
;;
*.timer )
deploy_fleet_unit $unit
;;
* )
echo not supported file name
;;
esac
done
# cleanup units that do not have corresponding file
cleanup_fleet_units