-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcp_runner.sh
executable file
·264 lines (238 loc) · 9.57 KB
/
gcp_runner.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
#!/bin/bash
account_name=$3
max_instance_count=$4
defaults=("instance_type cpus ram provider kvm_on gpu_sku launch_command avd_props avd_config results_dir id")
delimiter=${2:-,}
#Get first line of a file https://stackoverflow.com/a/11099042
header=($(cat $1 | head -n 1))
header=( "${header[@],,}" )
#Check if a variable is an array https://stackoverflow.com/a/27254437
if [[ ${#header[@]} -gt 1 ]]; then
headerArr=${header[@]}
else
IFS=$delimiter read -r -a headerArr <<< "$header"
fi
IFS=$'\n'
cols=$(echo ${defaults[@]} ${headerArr[@]} | sed 's/ /\n/g' | sort | uniq -d)
cols=($cols)
rows=($(tail -n +2 $1))
# rows=( "${rows[@],,}" )
get_col_index() {
# echo $(head -1 $2 | sed 's/,/\n/g' | nl | grep -e "$1" | awk '{print $1}')
local element=$1
shift
local arr=("$@")
for i in "${!arr[@]}";
do
if [[ "${arr[$i]}" = "${element}" ]];
then
echo $(($i+1))
break
fi
done
}
start_time=$(date +"%Y-%m-%d %H:%M:%S")
get_image_count() {
IFS=$'\n'
n=0
repo_dates=$(docker-hub repos --orgname blaiseyuri | awk -F '|' '{ print $6 }' | tail -n +8| date +"%Y-%m-%d %H:%M:%S" -f -)
repo_dates=( $repo_dates )
for i in "${repo_dates[@]}";
do
if [ $(($(date -d "$i" +%s))) -gt $(($(date -d "$start_time" +%s))) ]; then
n=$(($n+1))
fi
done
echo $n
}
#Set default values
gpu_sku=0
kvm_on=0
avd_config=""
avd_props=""
avd_name=""
image_regexp=""
emulator_version=""
results_dir=""
provider=settings['provider']
adb=""
command=""
instance_regex=''
id=""
var_file="emu_docker_vars.tf"
tfvar_file="gcp_vars.auto.tfvars"
# Each column is assigned to atleast one array of variable names
# The array is used for setting up the benchmarks and terraform
IFS=' '
results_dir_list='provider avd_name gpu_sku kvm_on id'
results_dir_array=( $results_dir_list )
input_cols="results_dir instance_type"
required_vars=( $input_cols )
optional_vars=("avd_props avd_config gpu_sku adb kvm_on launch_command")
# terraform_vars="results_dir instance_type avd_props avd_config gpu_sku adb kvm_on launch_command destroy"
terraform_vars=$(grep variable $var_file | cut -d'"' -f 2 | tr '\n' ' ')
terraform_vars=( $terraform_vars )
vm_vars=("image_regexp kvm_on launch_command")
#Create an associative array of the variable values for later logging
declare -A settings
for i in "${header[@]}"
do
:
settings["$i"]=$(get_col_index $i ${headerArr[@]})
done
if ! command -v gcloud &> /dev/null
then
credentials_path=$(cat ./templates/gcp/*.tfvars | grep "gcp_credentials" | cut -d "=" -f2 | tr -d '"')
echo "Installing gcloud cli"
docker pull gcr.io/google.com/cloudsdktool/google-cloud-cli:latest
docker run -ti --name gcloud-config gcr.io/google.com/cloudsdktool/google-cloud-cli gcloud auth login --cred-file="$credentials_path"
alias gcloud="docker run -ti --name gcloud-config gcr.io/google.com/cloudsdktool/google-cloud-cli gcloud"
fi
if ! command -v docker-hub &> /dev/null
then
echo "Installing docker-hub cli"
pip install docker-hub
fi
current_instance_count=$(gcloud compute instances list --filter=status:RUNNING | wc -l)
local_instance_count=$((1))
local_image_count=$((0))
starting_image_count=$(docker-hub repos --orgname $account_name | wc -l)
for r in "${rows[@]}"
do
:
# read file line and begin inputting row values
IFS=$delimiter read -r -a r <<< "$r"
results_dir="" #reset results_dir value
provider_idx=${settings["provider"]}
r_provider=${r[$provider_idx-1]}
# Print out instance and benchmark config values
echo "Columns:" "${header[@]}"
echo "ROW: $r"
echo "PROVIDER: $r_provider"
# If the results directory is included in row value assign the value
if [[ ("${cols[@]}" == *"results_dir"*) ]]; then
results_dir=settings["results_dir"]
fi
# If result directory has no value create one using the row values
if [[ results_dir == *""* ]]; then
for x in "${results_dir_array[@]}"
do
:
if [ -v settings["$x"] ]; then
idx="${settings["$x"]}"
if [[ "$x" == *"kvm_on"* ]]; then
results_dir+="_kvm_${r[$idx-1]}"
elif [[ "$x" == *"gpu_sku"* ]]; then
results_dir+="_gpu_${r[$idx-1]}"
elif [[ "$x" == *"id"* ]]; then
results_dir+="_${r[$idx-1]}"
else
results_dir+="_${r[$idx-1]}"
fi
fi
done
fi
# check for RAM & CPU otherwise exit
if [[ ("${cols[@]}" == *"cpus"* && "${cols[@]}" == *"ram"*) ]];
then
ram_idx=$(get_col_index 'ram' ${headerArr[@]})
cpus_idx=$(get_col_index 'cpus' ${headerArr[@]})
results_dir+="_${r[$ram_idx-1]}GB"
results_dir+="_${r[$cpus_idx-1]}cores"
instance_type=$(./find_instance_name.sh ${r[$cpus_idx-1]} ${r[$ram_idx-1]} $r_provider)
echo "INSTANCE_TYPE: $instance_type"
if [ -z "$instance_type" ]; then
final_results_dir="${results_dir:1}"
err_msg="No Instance found for ${row[@]} Skipping for now..."
mkdir -p ./$final_results_dir
touch ./$final_results_dir/log.txt
chmod 777 ./$final_results_dir/log.txt
echo "${r[@]}" >> ./$final_results_dir/log.txt
echo "$err_msg" >> ./$final_results_dir/log.txt
continue
else
cols[${#cols[@]}]="instance_type"
r[${#r[@]}]=$instance_type
addInstanceType=true #For adding the instance_type to tfvars file
fi
fi
if [[ ("${cols[@]}" == *"launch_command"*) ]];
then
echo "custom script being loaded"
fi
results_dir="${results_dir:1}"
if [[ -e $results_dir || -L $results_dir ]] ; then
i=1
while [[ -e ${results_dir}_$i || -L ${results_dir}_$i ]] ; do
((i++))
done
results_dir=${results_dir}_$i
fi
echo "Results_Directory: $results_dir"
mkdir -p ./"$results_dir"
touch ./"$results_dir"/settings.txt
chmod 777 ./"$results_dir"/settings.txt
echo "${header[@]}" >> ./"$results_dir"/settings.txt
echo "${r[@]}" >> ./"$results_dir"/settings.txt
echo "" > ./"$results_dir"/"$tfvar_file"
for i in "${terraform_vars[@]}"
do
:
if [[ -v settings["$i"] ]]; then
idx=${settings["$i"]}
echo "VARIABLES: $i,${r[$idx-1]}"
echo "$i" = "${r[$idx-1]}" >> ./"$results_dir"/"$tfvar_file"
fi
done
# Add the instance type if not included in the file as a column
if [[ -v addInstanceType ]]; then
echo "instance_type = \"${instance_type}\"" >> ./"$results_dir"/"$tfvar_file"
fi
# image_count=$(($(docker-hub repos --orgname $account_name | wc -l)-$starting_image_count))
image_count=$(get_image_count $start_time)
echo "Image Count: $image_count"
max_jobs=$((image_count+max_instance_count))
echo "Starting Manager..."
echo "Start Time: $start_time"
echo "GCP_Instance_count: $current_instance_count"
echo "Local_instance_count: $local_instance_count"
echo "local_image_count: $local_image_count"
echo "Docker image_count: $max_jobs"
while [ "$local_image_count" -ge "$max_jobs" ] && [ "$local_image_count" -ge $max_instance_count ]
do
:
echo "Max jobs count of $max_instance_count reached, waiting on next image..."
image_count=$(($(docker-hub repos --orgname $account_name | wc -l)-$starting_image_count))
local_image_count=image_count
max_jobs=$(($image_count+$max_instance_count))
sleep 60s
done
waiting=0;
while [ $waiting -eq 0 ]
do
:
current_instance_count=$(gcloud compute instances list --filter=status:RUNNING | wc -l)
if [ "$current_instance_count" -lt "$max_instance_count" ]
then
waiting=1;
local_instance_count=$(($local_instance_count+1))
local_image_count=$(($local_image_count+1))
cp -a ./"$var_file" ./"$results_dir"/
cp -a ./templates.hcl ./"$results_dir"/terragrunt.hcl
cp -a ./templates/$r_provider/. ./"$results_dir"/
cd ./"$results_dir" || exit
terragrunt init
# terragrunt plan -var-file=""$tfvar_file"" -auto-approve 2>&1 | tee terraform_logs.txt &
echo yes | terragrunt apply -var-file="$tfvar_file" -auto-approve 2>&1 | tee terraform_logs.txt &
cd ..
sleep 5s
else
echo "Maximum Instance count reached waiting on available instances...."
echo "GCP_instance_count: $current_instance_count"
echo "Total_instance_count: $local_instance_count"
echo "local_image_count: $local_image_count"
echo "Docker image_count: $image_count"
sleep 60s
fi
done
done