-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·284 lines (251 loc) · 8.53 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
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
#!/usr/bin/env bash
BROOKLIN_ALL_OPTION="--brooklin"
BROOKLIN_CONTROL_OPTION="--ctrl"
BROOKLIN_EXP_OPTION="--exp"
TEST_DRIVER_OPTION="--driver"
TEST_DRIVER_HOST_OPTION="--td"
CLEAN_OPTION="--clean"
CONFIG_ONLY_OPTION="--config-only"
CONFIG_PATH_OPTION="--cp"
VERBOSE_OPTION="-v"
BROOKLIN_CONTROL_TAG="brooklin.cert.control"
BROOKLIN_EXP_TAG="brooklin.cert.candidate"
INCLUDE_TEST_DRIVER=0
INCLUDE_BROOKLIN_ALL=0
INCLUDE_BROOKLIN_CONTROL=0
INCLUDE_BROOKLIN_EXP=0
INCLUDE_CONFIG_ONLY=0
PROMPT_FOR_2FA=1
CLEAN=0
VERBOSE=0
CA_BUNDLE_FILE="/etc/riddler/ca-bundle.crt"
APPLICATION_CFG_FILE="application.cfg"
DEPENDENCIES_DIR=dependencies
DEPENDENCIES_TARBALL="$DEPENDENCIES_DIR.tar.gz"
DIST_DIR=dist
GEN_CERT_SCRIPT="gen-cert.sh"
INSTALL_AGENT_SCRIPT="install-agent.sh"
INSTALL_CONFIG_SCRIPT="install-config.sh"
INSTALL_DRIVER_SCRIPT="install-driver.sh"
SCRIPT_NAME=$(basename "$0")
SOURCES_DIR=src
TEST_DRIVER_HOST="lor1-app36225.prod.linkedin.com"
CONFIG_PATH=".."
function usage {
echo "usage: $SCRIPT_NAME OPTIONS"
echo "Deploy test scripts to remote hosts"
echo
echo "OPTIONS"
echo " $TEST_DRIVER_OPTION Include test scripts on the default test driver host: $TEST_DRIVER_HOST"
echo " $TEST_DRIVER_HOST_OPTION hostname Include test scripts on the specified hostname"
echo " $BROOKLIN_CONTROL_OPTION Include test agent on $BROOKLIN_CONTROL_TAG cluster"
echo " $BROOKLIN_EXP_OPTION Include test agent on $BROOKLIN_EXP_TAG cluster"
echo " $BROOKLIN_ALL_OPTION Same as specifying both $BROOKLIN_CONTROL_OPTION and $BROOKLIN_EXP_OPTION"
echo " $CLEAN_OPTION Remove files on all included host and clusters"
echo " $CONFIG_ONLY_OPTION Include $APPLICATION_CFG_FILE file and install script on $BROOKLIN_CONTROL_TAG or $BROOKLIN_EXP_TAG"
echo " $CONFIG_PATH_OPTION path Include $APPLICATION_CFG_FILE from a specified path. Defaults to '..' if not specified"
echo " $VERBOSE_OPTION Turn on verbose logging"
echo " -h Display help"
exit "$1"
}
function validate_arguments() {
# Validate Brooklin options
if [[ $INCLUDE_BROOKLIN_ALL == 1 ]]; then
sum=$((INCLUDE_BROOKLIN_CONTROL+INCLUDE_BROOKLIN_EXP))
if [[ $sum == 1 ]]; then
>&2 echo "Cannot specify $BROOKLIN_ALL_OPTION and one of"\
"$BROOKLIN_CONTROL_OPTION or $BROOKLIN_EXP_OPTION"
usage 1
fi
if [[ $INCLUDE_CONFIG_ONLY == 1 ]]; then
>&2 echo "Cannot specify $CONFIG_ONLY_OPTION/$CONFIG_PATH_OPTION and $BROOKLIN_ALL_OPTION."\
"Must specify either $BROOKLIN_CONTROL_OPTION or $BROOKLIN_EXP_OPTION with $CONFIG_ONLY_OPTION/$CONFIG_PATH_OPTION"
usage 1
fi
fi
# Validate Brooklin config options
if [[ $INCLUDE_CONFIG_ONLY == 1 ]]; then
sum=$((INCLUDE_BROOKLIN_CONTROL+INCLUDE_BROOKLIN_EXP))
if [[ $sum != 1 ]]; then
>&2 echo "Cannot specify $CONFIG_ONLY_OPTION/$CONFIG_PATH_OPTION and none or both of"\
"$BROOKLIN_CONTROL_OPTION or $BROOKLIN_EXP_OPTION"
usage 1
fi
fi
}
function exit_on_failure() {
if [ $? -ne 0 ]; then
>&2 echo "$1"
>&2 echo "Try running ./$SCRIPT_NAME -v to see detailed logs"
exit 1
fi
}
function redirect_output() {
if [ "$VERBOSE" -eq "0" ]; then
"$@" > /dev/null 2>&1
else
"$@"
fi
}
function prompt_for_2FA() {
if [[ $PROMPT_FOR_2FA == 1 ]]; then
read -p "Press Enter when you are you ready to type in your 2FA password" -r
PROMPT_FOR_2FA=0 # Do not show this prompt again
fi
}
function copy_scripts_to_cluster() {
echo "Copying scripts to $1 ..."
hosts=$(eh -e "$1")
while read -r h; do
redirect_output scp "../$INSTALL_AGENT_SCRIPT" $DIST_DIR/brooklin-certification-*.tar.gz "$USER"@"$h":~/
done <<< "$hosts"
}
function cleanup_cluster() {
echo "Cleaning up $1 ..."
redirect_output mssh -r "$1" "./$INSTALL_AGENT_SCRIPT --clean $2"
}
function copy_configs_to_cluster() {
echo "Copying configs to $1 from $CONFIG_PATH ..."
hosts=$(eh -e "$1")
while read -r h; do
redirect_output scp "../$INSTALL_CONFIG_SCRIPT" "$CONFIG_PATH/$APPLICATION_CFG_FILE" "$USER"@"$h":~/
done <<< "$hosts"
}
function cleanup_cluster_config() {
echo "Cleaning up config $1 ..."
redirect_output mssh -r "$1" "./$INSTALL_CONFIG_SCRIPT --clean"
}
if [[ $# -eq 0 ]]; then
usage 2
fi
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
$TEST_DRIVER_HOST_OPTION)
TEST_DRIVER_HOST="$2"
INCLUDE_TEST_DRIVER=1
shift # past argument
shift # past value
;;
$TEST_DRIVER_OPTION)
INCLUDE_TEST_DRIVER=1
shift # past argument
;;
$BROOKLIN_CONTROL_OPTION)
INCLUDE_BROOKLIN_CONTROL=1
shift # past argument
;;
$BROOKLIN_EXP_OPTION)
INCLUDE_BROOKLIN_EXP=1
shift # past argument
;;
$BROOKLIN_ALL_OPTION)
INCLUDE_BROOKLIN_ALL=1
shift # past argument
;;
$CLEAN_OPTION)
CLEAN=1
shift # past argument
;;
$CONFIG_ONLY_OPTION)
INCLUDE_CONFIG_ONLY=1
shift # past argument
;;
$CONFIG_PATH_OPTION)
CONFIG_PATH="$2"
INCLUDE_CONFIG_ONLY=1
shift # past argument
shift # past value
;;
-v)
VERBOSE=1
shift # past argument
;;
-h|--help)
usage 0
;;
*) # unknown option
echo "Unrecognized option: $1"
usage 1
;;
esac
done
validate_arguments
if [[ $INCLUDE_BROOKLIN_ALL == 1 ]]; then
INCLUDE_BROOKLIN_EXP=1
INCLUDE_BROOKLIN_CONTROL=1
fi
if [[ $CLEAN == 0 ]]; then
redirect_output pushd $SOURCES_DIR
# Generate tarball containing all script files
echo "Generating scripts tarball ..."
redirect_output rm -rf $DIST_DIR
redirect_output pipenv run python setup.py sdist
exit_on_failure "Generating scripts tarball failed"
# Test driver
if [[ $INCLUDE_TEST_DRIVER == 1 ]]; then
# Generate tarball for all dependencies
echo "Generating dependencies tarball ..."
redirect_output rm -rf $DEPENDENCIES_DIR
redirect_output mkdir $DEPENDENCIES_DIR
exit_on_failure "Creating $DEPENDENCIES_DIR failed"
pipenv lock -r | tail -n +2 > $DEPENDENCIES_DIR/requirements.txt
exit_on_failure "Generating requirements file failed"
redirect_output pipenv run python -m pip download -r $DEPENDENCIES_DIR/requirements.txt -d $DEPENDENCIES_DIR
exit_on_failure "Downloading dependencies failed"
redirect_output tar -czvf $DEPENDENCIES_TARBALL $DEPENDENCIES_DIR
exit_on_failure "Creating scripts tarball failed"
prompt_for_2FA
# Copy scripts and tarballs
echo "Copying scripts and tarballs to $TEST_DRIVER_HOST"
redirect_output scp "../$INSTALL_DRIVER_SCRIPT" "../$GEN_CERT_SCRIPT" $CA_BUNDLE_FILE $DEPENDENCIES_TARBALL $DIST_DIR/brooklin-certification-*.tar.gz "$USER"@"$TEST_DRIVER_HOST":~/
exit_on_failure "Copying scripts tarball failed"
echo "Installing test driver on $TEST_DRIVER_HOST"
redirect_output mssh -n "$TEST_DRIVER_HOST" "./$INSTALL_DRIVER_SCRIPT"
exit_on_failure "Installing test driver failed"
fi
# Brooklin control
if [[ $INCLUDE_BROOKLIN_CONTROL == 1 ]]; then
if [[ $INCLUDE_CONFIG_ONLY == 1 ]]; then
copy_configs_to_cluster "%prod-lor1.tag_hosts:$BROOKLIN_CONTROL_TAG"
else
copy_scripts_to_cluster "%prod-lor1.tag_hosts:$BROOKLIN_CONTROL_TAG" $BROOKLIN_ALL_OPTION
fi
fi
# Brooklin experiment
if [[ $INCLUDE_BROOKLIN_EXP == 1 ]]; then
if [[ $INCLUDE_CONFIG_ONLY == 1 ]]; then
copy_configs_to_cluster "%prod-lor1.tag_hosts:$BROOKLIN_EXP_TAG"
else
copy_scripts_to_cluster "%prod-lor1.tag_hosts:$BROOKLIN_EXP_TAG" $BROOKLIN_ALL_OPTION
fi
fi
# Clean up generated files
redirect_output rm -rf $DEPENDENCIES_DIR $DEPENDENCIES_TARBALL $DIST_DIR *.egg-info
exit_on_failure "Cleaning up generated files and directories failed"
redirect_output popd
else # $CLEAN == 1
# Test driver
if [[ $INCLUDE_TEST_DRIVER == 1 ]]; then
echo "Cleaning up test driver host $TEST_DRIVER_HOST"
redirect_output mssh -n "$TEST_DRIVER_HOST" "./$INSTALL_DRIVER_SCRIPT --clean"
exit_on_failure "Cleaning up test driver host $TEST_DRIVER_HOST failed"
fi
# Brooklin control
if [[ $INCLUDE_BROOKLIN_CONTROL == 1 ]]; then
if [[ $INCLUDE_CONFIG_ONLY == 1 ]]; then
cleanup_cluster_config "%prod-lor1.tag_hosts:$BROOKLIN_CONTROL_TAG"
else
cleanup_cluster "%prod-lor1.tag_hosts:$BROOKLIN_CONTROL_TAG" $BROOKLIN_ALL_OPTION
fi
fi
# Brooklin experiment
if [[ $INCLUDE_BROOKLIN_EXP == 1 ]]; then
if [[ $INCLUDE_CONFIG_ONLY == 1 ]]; then
cleanup_cluster_config "%prod-lor1.tag_hosts:$BROOKLIN_EXP_TAG"
else
cleanup_cluster "%prod-lor1.tag_hosts:$BROOKLIN_EXP_TAG" $BROOKLIN_ALL_OPTION
fi
fi
fi