-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.sh
executable file
·360 lines (325 loc) · 11.9 KB
/
run.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash
########################### configurations ###########################
# some of these would be overwritten by arguments passed to the command
players=("bitmovin" "dashjs" "shaka" "bola")
experiments=1
shaperDurations=(15) #s
serverIngresses=(5000) #Kbps
serverEgresses=(5000) #Kbps
serverLatencies=(80) #ms
clientIngresses=(5000) #Kbps
clientEgresses=(5000) #Kbps
clientLatencies=(80) #ms
id=$(date '+%s')
awsProfile="default"
placementGroup="pptCluster"
awsKey=""
awsIAMRole="SSMEnabled"
awsSecurityGroup="ppt-security-group"
serverInstanceId=""
clientInstanceIds=""
networkConfig=""
analyticsLicenseKey=""
bitmovinAPIKey=""
analyticsOutputId=""
startTime=""
endTime=""
instancesType="m5ad.xlarge"
title="stc"
clientWarmupTime=1 #s
########################### /configurations ##########################
########################### functions ############################
showError() {
now=$(date -u +"%H:%M:%S")
printf "\e[1;31m>>> [ERROR %s] %s\e[0m\n" "$now" "$1"
cleanExit 1
}
showMessage() {
now=$(date -u +"%H:%M:%S")
printf "\n\e[1;36m>>> [INFO %s] %s\e[0m\n" "$now" "$1"
}
cleanExit() {
showMessage "Killing EC2 instances and clean ups"
aws ec2 terminate-instances --instance-ids $clientInstanceIds $serverInstanceId --profile $awsProfile &>/dev/null
rm -rf "$id"
exit $1
}
########################### /functions ###########################
########################### arguments ############################
argumentIndex=0
for argument in "$@"; do
if [[ $argument == *"--"* ]]; then
case $argument in
"--experiments")
nextArgumentIndex=$((argumentIndex + 2))
experiments="${!nextArgumentIndex}"
if [[ ! $experiments =~ ^[0-9]+$ ]]; then
showError "Experiments parameter should follow by an integer number"
fi
;;
"--shaper")
nextArgumentIndex=$((argumentIndex + 2))
networkConfigFileName="${!nextArgumentIndex}"
networkConfig=$(cat $networkConfigFileName) || showError "Could not load the network config file"
shaperDurations=($(echo "$networkConfig" | jq '.[].duration'))
serverIngresses=($(echo "$networkConfig" | jq '.[].serverIngress'))
serverEgresses=($(echo "$networkConfig" | jq '.[].serverEgress'))
serverLatencies=($(echo "$networkConfig" | jq '.[].serverLatency'))
clientIngresses=($(echo "$networkConfig" | jq '.[].clientIngress'))
clientEgresses=($(echo "$networkConfig" | jq '.[].clientEgress'))
clientLatencies=($(echo "$networkConfig" | jq '.[].clientLatency'))
;;
"--cluster")
nextArgumentIndex=$((argumentIndex + 2))
placementGroup="${!nextArgumentIndex}"
;;
"--title")
nextArgumentIndex=$((argumentIndex + 2))
title="${!nextArgumentIndex}"
;;
"--awsProfile")
nextArgumentIndex=$((argumentIndex + 2))
awsProfile="${!nextArgumentIndex}"
;;
"--awsKey")
nextArgumentIndex=$((argumentIndex + 2))
awsKey="${!nextArgumentIndex}"
;;
"--awsIAMRole")
nextArgumentIndex=$((argumentIndex + 2))
awsIAMRole="${!nextArgumentIndex}"
;;
"--awsSecurityGroup")
nextArgumentIndex=$((argumentIndex + 2))
awsSecurityGroup="${!nextArgumentIndex}"
;;
"--players")
valueIndex=0
newPlayers=()
for value in "$@"; do
if [[ $valueIndex -gt $argumentIndex ]]; then
if [[ $value == *"--"* ]]; then
break
fi
if [[ " ${players[@]} " =~ " ${value} " ]]; then
newPlayers+=($value)
else
showError "Invalid player '$value'"
fi
fi
((valueIndex++))
done
if [[ ${#newPlayers[@]} -lt 1 ]]; then
showError "Define at least one player"
fi
players=(${newPlayers[@]})
;;
*)
showError "Invalid argument '$argument'"
;;
esac
fi
((argumentIndex++))
done
########################### /arguments ############################
printf "\n\e[1;33m>>> Experiment set id: $id %s\e[0m\n"
mkdir "$id"
durationOfExperiment=0
for duration in "${shaperDurations[@]}"; do
durationOfExperiment=$(echo "$durationOfExperiment + $duration" | bc -l)
done
if [[ $durationOfExperiment -gt 596 ]]; then
showError "Maximum duration of each experiment can not be more than test asset length (09:56)"
fi
showMessage "Running $experiments experiment(s) on the following players for ${durationOfExperiment}s each"
printf '%s ' "${players[@]}"
printf "\n"
showMessage "Spinning up server EC2 instance"
aws ec2 run-instances \
--region eu-central-1 \
--image-id ami-0ab838eeee7f316eb \
--instance-type $instancesType \
--key-name $awsKey \
--placement "GroupName = $placementGroup" \
--iam-instance-profile Name=$awsIAMRole \
--security-groups $awsSecurityGroup \
--tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=ppt-server-$id}]" \
--profile $awsProfile >"$id/instance.json" || showError "Failed to run the aws command. Check your aws credentials."
serverInstanceId=$(jq -r '.Instances[].InstanceId' <"$id/instance.json")
printf '%s ' "${serverInstanceId[@]}"
printf "\n"
showMessage "Spinning up client EC2 instance(s)"
aws ec2 run-instances \
--region eu-central-1 \
--image-id ami-0ab838eeee7f316eb \
--count ${#players[@]} \
--instance-type $instancesType \
--key-name $awsKey \
--placement "GroupName = $placementGroup" \
--iam-instance-profile Name=$awsIAMRole \
--security-groups $awsSecurityGroup \
--tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=ppt-client-$id}]" \
--profile $awsProfile >"$id/instances.json" || showError "Failed to run the aws command. Check your aws credentials."
clientInstanceIds=$(jq -r '.Instances[].InstanceId' <"$id/instances.json")
printf '%s ' "${clientInstanceIds[@]}"
printf "\n"
showMessage "Waiting for instances to be in running state"
stateCodes=0
while [ $stateCodes == 0 ] || [ $(($stateCodesSum / ${#stateCodes[@]})) != 16 ]; do
stateCodesSum=0
sleep 3
stateCodes=($(aws ec2 describe-instances --instance-ids $clientInstanceIds $serverInstanceId --profile $awsProfile | jq '.Reservations[].Instances[].State.Code'))
for stateCode in "${stateCodes[@]}"; do
((stateCodesSum += stateCode))
done
done
echo "all up [$stateCodesSum]"
clientPublicIps=($(aws ec2 describe-instances --instance-ids $clientInstanceIds --profile $awsProfile | jq -r '.Reservations[].Instances[].PublicIpAddress'))
serverPublicIp=($(aws ec2 describe-instances --instance-ids $serverInstanceId --profile $awsProfile | jq -r '.Reservations[].Instances[].PublicIpAddress'))
serverPrivateIp=$(jq -r '.Instances[].PrivateIpAddress' <"$id/instance.json")
configSkeleton=$(cat configSkeleton.json)
((durationOfExperiment += clientWarmupTime)) # warm up client
config="${configSkeleton/--id--/$id}"
config="${config/--title--/$title}"
config="${config/--alk--/$analyticsLicenseKey}"
config="${config/--serverIp--/$serverPrivateIp}"
config="${config/--experimentDuration--/$durationOfExperiment}"
shaperIndex=0
networkConfig="{
\"duration\": ${clientWarmupTime},
\"serverIngress\": 0,
\"serverEgress\": 0,
\"serverLatency\": 0,
\"clientIngress\": 0,
\"clientEgress\": 0,
\"clientLatency\": 0
}"
while [ $shaperIndex -lt "${#shaperDurations[@]}" ]; do
if [[ $networkConfig != "" ]]; then
networkConfig+=","
fi
networkConfig+="{
\"duration\": ${shaperDurations[shaperIndex]},
\"serverIngress\": ${serverIngresses[shaperIndex]},
\"serverEgress\": ${serverEgresses[shaperIndex]},
\"serverLatency\": ${serverLatencies[shaperIndex]},
\"clientIngress\": ${clientIngresses[shaperIndex]},
\"clientEgress\": ${clientEgresses[shaperIndex]},
\"clientLatency\": ${clientLatencies[shaperIndex]}
}"
((shaperIndex++))
done
networkConfig="[${networkConfig}]"
config="${config/\"--shapes--\"/$networkConfig}"
playerIndex=0
for publicIp in "${clientPublicIps[@]}"; do
if [[ $playerIndex == 0 ]]; then
config="${config/--player--/${players[playerIndex]}}"
else
config="${config/${players[playerIndex - 1]}/${players[playerIndex]}}"
fi
echo "$config" >"$id/config.json"
showMessage "Waiting for client network interface to be reachable [${players[playerIndex]}]"
while ! nc -w5 -z "$publicIp" 22; do
sleep 1
done
showMessage "Injecting scripts and configurations into client instance"
scp -oStrictHostKeyChecking=no -i "./$awsKey.pem" client/init.sh client/start.sh "$id/config.json" ec2-user@"$publicIp":/home/ec2-user
((playerIndex++))
done
showMessage "Waiting for server network interface to be reachable"
while ! nc -w5 -z "$serverPublicIp" 22; do
sleep 1
done
showMessage "Injecting scripts and configurations into server instance"
scp -oStrictHostKeyChecking=no -i "./$awsKey.pem" server/init.sh server/start.sh "$id/config.json" ec2-user@"$serverPublicIp":/home/ec2-user
showMessage "Executing initializer script(s)"
SSMCommandId=$(aws ssm send-command \
--instance-ids $clientInstanceIds $serverInstanceId \
--document-name "AWS-RunShellScript" \
--comment "Initialize" \
--parameters commands="/home/ec2-user/init.sh" \
--output-s3-bucket-name "ppt-output" \
--output-s3-key-prefix "init-out/$id" \
--query "Command.CommandId" \
--profile $awsProfile | sed -e 's/^"//' -e 's/"$//')
echo "$SSMCommandId"
SSMCommandResult="InProgress"
timer=0
while [[ $SSMCommandResult == *"InProgress"* ]]; do
minutes=$((timer / 60))
seconds=$((timer % 60))
printf '\r%s' "~ $minutes:$seconds "
if [ $((timer % 5)) == 0 ]; then
SSMCommandResult=$(aws ssm list-command-invocations --command-id $SSMCommandId --profile $awsProfile | jq -r '.CommandInvocations[].Status')
sleep 0.4
else
sleep 1
fi
((timer += 1))
done
printf "\n"
if [[ $SSMCommandResult == *"Failed"* ]]; then
showError "Failed to initiate the instance(s). Check the S3 bucket for details"
fi
currentExperiment=0
startTime=$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")
while [ $currentExperiment -lt $experiments ]; do
((currentExperiment++))
showMessage "Running experiment $currentExperiment of $experiments [+$clientWarmupTime(s) Client warmup time]"
SSMCommandId=$(aws ssm send-command \
--instance-ids $clientInstanceIds $serverInstanceId \
--document-name "AWS-RunShellScript" \
--comment "Start" \
--parameters commands="/home/ec2-user/start.sh" \
--output-s3-bucket-name "ppt-output" \
--output-s3-key-prefix "start-out/$id" \
--query "Command.CommandId" \
--profile $awsProfile | sed -e 's/^"//' -e 's/"$//')
echo "$SSMCommandId"
SSMCommandResult="InProgress"
time=$durationOfExperiment
timer=$time
while [[ $SSMCommandResult == *"InProgress"* ]]; do
minutes=$((timer / 60))
seconds=$((timer % 60))
printf '\r%s' "~ $minutes:$seconds "
if [ $((timer % 30)) == 0 ] || [[ $((time - timer)) -gt $time ]]; then
SSMCommandResult=$(aws ssm list-command-invocations --command-id $SSMCommandId --profile $awsProfile | jq -r '.CommandInvocations[].Status')
sleep 0.4
else
sleep 1
fi
((timer -= 1))
done
printf "\n"
if [[ $SSMCommandResult == *"Failed"* ]]; then
showError "Failed to run experiment(s). Check the S3 bucket for details"
fi
done
#ppt-analytics-ext-id
endTime=$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")
showMessage "Requesting the analytics data"
requestResult=$(curl -s -X POST https://api.bitmovin.com/v1/analytics/exports/ \
-H 'Content-Type: application/json' \
-H 'X-Api-Key: '$bitmovinAPIKey \
-d '{
"startTime": "'$startTime'",
"endTime": "'$endTime'",
"name": "ppt-analytics-request-'$id'",
"licenseKey": "'$analyticsLicenseKey'",
"output": {
"outputPath": "analytics/'$id'/",
"outputId": "'$analyticsOutputId'"
}
}')
requestStatus=$(echo "$requestResult" | jq -r '.status')
taskId=$(echo "$requestResult" | jq -r '.data.result.id')
taskStatus=$(echo "$requestResult" | jq -r '.data.result.status')
if [ $taskStatus == 'ERROR' ] || [ $requestStatus == 'ERROR' ]; then
showError 'Failed to request the analytics data'
echo $requestResult
else
echo $taskId
fi
cleanExit 0