This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
forked from jskeates/tutum-startup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.sh
executable file
·292 lines (209 loc) · 5.66 KB
/
script.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/bin/bash
# --
# Usage:
#
# ./script.sh $DOCKERCLOUD_AUTH $DEPLOYMENT_TIMEOUT
# --
# --
# Stop script if any command fails and run _cleanup() function
# --
set -e
trap _cleanup ERR
# --
# Functions
# --
function _cleanup {
printf "[docker-cloud-startup] ERROR - STOPPING EARLY.\n"
}
function _error {
printf "[docker-cloud-startup] -> Error: $1.\n"
}
function _finished {
printf "[docker-cloud-startup] SCRIPT COMPLETE.\n"
}
function _ok {
printf "[docker-cloud-startup] -> ok.\n"
}
function _output {
printf "[docker-cloud-startup] $1\n"
}
function _result {
printf "[docker-cloud-startup] -> $1\n"
}
# --
# Validate number of arguments
# --
_output "Checking arguments..."
if [ "$#" -lt 3 ]; then
_error "illegal number of parameters"
exit 1
else
_ok
fi
# --
# START
# --
# --
# Version-specific variables
# --
_output "Setting final variables..."
METADATA_SERVICE_URI="http://169.254.169.254/latest/meta-data"
_result "METADATA_SERVICE_URI: \"$METADATA_SERVICE_URI\""
# CLI versions prior to 1.0.5 do not have namespace support
DOCKER_CLOUD_CLI_VERSION="1.0.7"
_result "DOCKER_CLOUD_CLI_VERSION: \"$DOCKER_CLOUD_CLI_VERSION\""
AWS_CLI_VERSION="1.9.20"
_result "AWS_CLI_VERSION: \"$AWS_CLI_VERSION\""
_ok
# --
# Find OS kind
# --
_output "Finding OS..."
OS_KIND="unknown"
if which apt-get > /dev/null; then
OS_KIND="debian"
elif which yum > /dev/null; then
OS_KIND="fedora"
fi
_result "OS: \"$OS_KIND\""
_ok
# --
# Install dependencies
# --
_output "Installing dependencies..."
case "$OS_KIND" in
debian)
locale-gen en_GB.UTF-8
apt-get update
apt-get install -y python-pip curl
# Manually install jq-1.5 for startswith and ltrimstr support
curl -Lo /usr/bin/jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64
chmod ugo+x /usr/bin/jq
;;
fedora)
echo "en_GB.UTF-8" > /etc/locale.conf
# Fedora doesn't have Python pre-installed
yum install -y python curl
# pip & jq aren't in the yum repos
curl -O https://bootstrap.pypa.io/get-pip.py
python get-pip.py
curl -Lo /usr/bin/jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64
chmod ugo+x /usr/bin/jq
# We also need to configure sudo for the agent install to work
echo "Defaults:root !requiretty" >> /etc/sudoers.d/91-cloud-sudoers-requiretty
chmod 440 /etc/sudoers.d/91-cloud-sudoers-requiretty
;;
unknown)
echo "This OS is not supported"
exit 2
;;
esac
_ok
# --
# Install CLI apps
# --
_output "Installing CLI apps..."
pip install -q docker-cloud==$DOCKER_CLOUD_CLI_VERSION awscli==$AWS_CLI_VERSION
_ok
# --
# Set Docker Cloud env vars
# --
_output "Setting Docker Cloud environment variables..."
export DOCKERCLOUD_AUTH=$1
_result "DOCKERCLOUD_AUTH: \"Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\""
export DOCKERCLOUD_NAMESPACE=$2
_result "DOCKERCLOUD_NAMESPACE: \"$DOCKERCLOUD_NAMESPACE\""
_ok
# --
# Set AWS env vars
# --
_output "Setting AWS environment variables..."
export AWS_DEFAULT_REGION=$(curl -f ${METADATA_SERVICE_URI}/placement/availability-zone | sed 's/.$//')
_result "AWS_DEFAULT_REGION: \"$AWS_DEFAULT_REGION\""
_ok
# --
# INPUT variables
# --
_output "Setting input variables..."
DEPLOYMENT_TIMEOUT=$3
_result "DEPLOYMENT_TIMEOUT: \"$DEPLOYMENT_TIMEOUT\""
REDEPLOY_STACKS=$4
_result "REDEPLOY_STACKS: \"$REDEPLOY_STACKS\""
_ok
# --
# Register node
# --
_output "Registering node..."
BYO_COMMAND=$(docker-cloud node byo | sed -n 4p | sed -e 's/^[ \t]*//')
_result "\"$BYO_COMMAND\""
eval $BYO_COMMAND
_ok
# --
# Set node UUID env var now that agent has been installed
# --
_output "Setting Docker Cloud node UUID..."
export NODE_UUID=$(cat /etc/dockercloud/agent/dockercloud-agent.conf | jq -r .UUID)
_result "NODE_UUID: \"$NODE_UUID\""
_ok
# --
# Set node UUID as AWS tag
# --
INSTANCE_ID=$(curl -f ${METADATA_SERVICE_URI}/instance-id)
_result "INSTANCE_ID: \"$INSTANCE_ID\""
_output "Add AWS tags..."
aws ec2 create-tags --resources $INSTANCE_ID --tags Key=UUID,Value=$NODE_UUID
_ok
# --
# Wait for node to be deployed
# --
_output "Wait for node to be deployed..."
timeout $DEPLOYMENT_TIMEOUT bash -c "while [ \"\$(docker-cloud node inspect $NODE_UUID | jq -r .state)\" != \"Deployed\" ]; do sleep 5; done;"
if [ $? != 0 ]; then _error "node never came up"; exit 2; fi
_ok
# --
# Set node tags in docker cloud based on EC2 tags (tag must include "Docker-Cloud")
# --
_output "Add docker cloud node tags..."
CLUSTER_NAME=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" | jq -r '.Tags | map(select(.Key == "Node Cluster Name")) | .[].Value')
LABELS="-t Cluster=$CLUSTER_NAME"
_result "TAG: \"Cluster=$CLUSTER_NAME\""
# E.g.: 'Docker-Cloud-NodeType=Worker' produces 'NodeType=Worker'
EC2_TAGS=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" | jq -r '.Tags | map(select(.Key | startswith("Docker-Cloud-"))) | .[].Key+"="+.[].Value | ltrimstr("Docker-Cloud-")')
for TAG in $EC2_TAGS
do
LABELS="$LABELS -t $TAG"
_result "TAG: \"$TAG\""
done
docker-cloud tag add $LABELS $NODE_UUID
_ok
# --
# Redeploy stacks
# --
if [ "$REDEPLOY_STACKS" != "" ]; then
_output "Redeploy stack..."
# Splits stack list on comma char
docker-cloud stack redeploy --sync $(echo -n $REDEPLOY_STACKS | sed 's/,/ /g')
_ok
fi
# --
# Cleanup instance
# --
_output "Cleanup instance..."
unset AWS_DEFAULT_REGION DOCKERCLOUD_NAMESPACE NODE_UUID
pip uninstall docker-cloud awscli -y
case "$OS_KIND" in
debian)
apt-get purge -y python-pip jq
;;
fedora)
pip uninstall pip -y
rm /usr/bin/jq
;;
esac
# --
# Cleanup history
# --
_output "Cleanup history..."
cat /dev/null > ~/.bash_history && history -c
_ok
_finished