-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch-experiment.sh
More file actions
executable file
·153 lines (134 loc) · 6.66 KB
/
launch-experiment.sh
File metadata and controls
executable file
·153 lines (134 loc) · 6.66 KB
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
#!/bin/bash
set -ex
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-a|--ami) MASTER_AMI="$2"; shift ;;
-t|--type) MASTER_INSTANCE_TYPE="$2"; shift ;;
-r|--region) REGION="$2"; shift ;;
-k|--key) KEY_NAME="$2"; shift ;;
-kp|--key-path) KEY_PATH="$2"; shift ;;
-s|--security-group) SECURITY_GROUP="$2"; shift ;;
-iam|--iam-profile) IAM_ROLE="$2"; shift ;;
-nw|--num-workers) NUM_WORKERS="$2"; shift ;;
-n|--tries) N_TRIES="$2"; shift ;;
-p|--path) RESULTS_PATH="$2"; shift ;;
-d|--duration) DURATION="$2"; shift ;;
-h|--help)
echo "Usage: $0 [options]"
echo "Options:"
echo " -a, --ami Master AMI ID"
echo " -t, --type Master instance type"
echo " -r, --region AWS region"
echo " -k, --key Key pair name"
echo " -kp, --key-path Path to the private key file"
echo " -s, --security-group Security group ID"
echo " -iam, --iam-profile IAM instance profile ARN. The master node needs to have an IAM role to create EC2 workers"
echo " -n, --tries Number of tries"
echo " -nw, --num-workers Number of worker nodes to chain when starting the master node"
echo " -d, --duration Duration of each experiment in seconds"
echo " -p, --path Path to save the experiment results (default: experiment_results/[n]_workers)"
echo " -h, --help Show this help message"
exit 0
;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
# Set default results path if not provided
if [ -z "$RESULTS_PATH" ]; then
RESULTS_PATH="experiment_results/${NUM_WORKERS}_workers"
fi
# Check if all required parameters are set
if [ -z "$MASTER_AMI" ] || [ -z "$MASTER_INSTANCE_TYPE" ] || [ -z "$REGION" ] || [ -z "$KEY_NAME" ] || [ -z "$KEY_PATH" ] || [ -z "$SECURITY_GROUP" ] || [ -z "$IAM_ROLE" ] || [ -z "$N_TRIES" ] || [ -z "$NUM_WORKERS" ] || [ -z "$DURATION" ]; then
echo "Error: Missing required parameters."
echo "Usage: $0 [options]"
echo "Options:"
echo " -a, --ami Master AMI ID"
echo " -t, --type Master instance type"
echo " -r, --region AWS region"
echo " -k, --key Key pair name"
echo " -kp, --key-path Path to the private key file"
echo " -s, --security-group Security group ID"
echo " -iam, --iam-profile IAM instance profile ARN. The master node needs to have an IAM role to create EC2 workers"
echo " -n, --tries Number of tries"
echo " -nw, --num-workers Number of worker nodes to chain when starting the master node"
echo " -d, --duration Duration of each experiment in seconds"
echo " -p, --path Path to save the experiment results (default: experiment_results/[n]_workers)"
echo " -h, --help Show this help message"
exit 1
fi
echo "Using MASTER_AMI: $MASTER_AMI"
echo "Using MASTER_INSTANCE_TYPE: $MASTER_INSTANCE_TYPE"
echo "Using REGION: $REGION"
echo "Using KEY_NAME: $KEY_NAME"
echo "Number of tries: $N_TRIES"
echo "Number of workers to chain per try: $NUM_WORKERS"
echo "Duration of each experiment: $DURATION seconds"
USER_DATA_SCRIPT=$(sed -e "s/{{NUM_WORKERS}}/$NUM_WORKERS/" master-config-user-data.sh | base64 -w 0)
mkdir -p $RESULTS_PATH
cd client
jq --argjson duration $DURATION '.runTest = true | .testDuration = $duration' config.json > tmp.json && mv tmp.json config.json
for ((i=1; i<=N_TRIES; i++)); do
echo "Attempt $i of $N_TRIES"
# Create a master node EC2 instance with user data script
INSTANCE_ID=$(aws ec2 run-instances \
--image-id $MASTER_AMI \
--count 1 \
--instance-type $MASTER_INSTANCE_TYPE \
--key-name $KEY_NAME \
--query 'Instances[0].InstanceId' \
--security-group-ids $SECURITY_GROUP \
--iam-instance-profile "Arn=$IAM_ROLE" \
--output text \
--region $REGION \
--placement "AvailabilityZone=${REGION}a" \
--user-data $USER_DATA_SCRIPT)
# Add a name tag to the instance
aws ec2 create-tags \
--resources $INSTANCE_ID \
--tags Key=Name,Value="Mediasoup LLLS Experiment Master" \
--region $REGION
# Wait for the instance to be in running state
aws ec2 wait instance-running --instance-ids $INSTANCE_ID --region $REGION
echo "Instance is running"
# Get the public DNS of the instance
INSTANCE_DNS=$(aws ec2 describe-instances \
--instance-ids $INSTANCE_ID \
--query 'Reservations[0].Instances[0].PublicDnsName' \
--output text \
--region $REGION)
echo "Server DNS: $INSTANCE_DNS"
# ping with a GET request until it responds with 200 pong
while ! curl -s -o /dev/null -w "%{http_code}" http://$INSTANCE_DNS:4000/ready | grep -q 200; do
echo "Waiting for master and workers to be available..."
sleep 5
done
# edit the config.json file with the server DNS
jq --arg dns "http://$INSTANCE_DNS:4000" '.serverUrl = $dns' config.json > tmp.json && mv tmp.json config.json
pnpm run start
# Move the experiment results to the experiment_results directory
mkdir -p ../$RESULTS_PATH/try_$i/recordings
mkdir -p ../$RESULTS_PATH/try_$i/stats
mv recordings/* ../$RESULTS_PATH/try_$i/recordings
mv stats/* ../$RESULTS_PATH/try_$i/stats
# Get all instances ids with name Mediasoup LLLS Experiment Worker
WORKER_INSTANCE_IDS=$(aws ec2 describe-instances \
--filters "Name=tag:Name,Values=Mediasoup LLLS Experiment Worker" "Name=instance-state-name,Values=running" \
--query 'Reservations[*].Instances[*].InstanceId' \
--output text \
--region $REGION)
# scp all csv files to the local machine
for worker_instance_id in $WORKER_INSTANCE_IDS; do
WORKER_INSTANCE_DNS=$(aws ec2 describe-instances \
--instance-ids $worker_instance_id \
--query 'Reservations[0].Instances[0].PublicDnsName' \
--output text \
--region $REGION)
scp -i $KEY_PATH -o StrictHostKeyChecking=no ubuntu@$WORKER_INSTANCE_DNS:~/*.csv ../$RESULTS_PATH/try_$i/stats/$worker_instance_id.csv
done
# Terminate the instance after the experiment
aws ec2 terminate-instances --instance-ids $INSTANCE_ID $WORKER_INSTANCE_IDS --region $REGION > /dev/null 2>&1
aws ec2 wait instance-terminated --instance-ids $INSTANCE_ID $WORKER_INSTANCE_IDS --region $REGION
echo "Instances Master: $INSTANCE_ID and Workers: $WORKER_INSTANCE_IDS terminated"
done