forked from pieceofr/mango-simulation-dos
-
Notifications
You must be signed in to change notification settings - Fork 7
/
create-instance.sh
executable file
·100 lines (95 loc) · 3.03 KB
/
create-instance.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
#!/usr/bin/env bash
set -e
declare -a instance_ip
declare -a instance_name
declare -a instance_zone
create_interval=60
[[ ! "$GC_IMAGE" ]] && GC_IMAGE=mango-simulation-client-230508&& echo GC_IMAGE env not found, use $GC_IMAGE
if [[ ! "$AVAILABLE_ZONE" ]];then
available_zone=( us-west2-b asia-east1-b asia-northeast1-a )
else
available_zone=( $AVAILABLE_ZONE )
fi
function create_gce() {
local vm_name=mango-simulation-tester-`date +%y%m%d-%H-%M-%S`
local project=principal-lane-200702
local img_name=$GC_IMAGE
local machine_type=n1-standard-32
local network_tag=allow-everything,allow-everything-egress
if [[ ! "$1" ]];then
zone=asia-east1-b
fi
ret_create=$(gcloud beta compute instances create $vm_name \
--project=$project \
--source-machine-image=$img_name \
--zone=$1 \
--machine-type=$machine_type \
--network-interface=network-tier=PREMIUM,subnet=default \
--maintenance-policy=MIGRATE \
--service-account=dos-test@principal-lane-200702.iam.gserviceaccount.com \
--scopes=https://www.googleapis.com/auth/cloud-platform \
--tags=$network_tag \
--no-shielded-secure-boot \
--shielded-vtpm \
--shielded-integrity-monitoring \
--format="flattened(name,networkInterfaces[0].accessConfigs[0].natIP)" \
--reservation-affinity=any)
echo $ret_create > ret_create.out
gce_create_exit=$?
# testing
# ret_create="--- name: mango-bencher-tester-221219-07-44-58 nat_ip: 34.83.208.239"
# echo $ret_create > ret_create.out
# gce_create_exit=0
if [[ $gce_create_exit -eq 0 || $gce_create_exit=="0" ]];then
instance_zone+=($zone)
sship=$(sed 's/^.*nat_ip: //g' ret_create.out)
instance_ip+=($sship)
gc_name=$(sed 's/^.*--- name: //g' ret_create.out | sed 's/ nat_ip:.*//g')
instance_name+=($gc_name)
else
exit $gce_create_exit
fi
}
function create_machines() {
instance_ip=()
instance_name=()
instance_zone=()
for i in $(seq 1 $1)
do
if [[ $count -ge ${#available_zone[@]} ]];then
count=0
fi
zone=${available_zone[$count]}
create_gce $zone
let count+=1
echo "gc instance is created in $zone"
sleep $create_interval # avoid too quick build
done
echo "${instance_ip[@]}" > instance_ip.out
echo "${instance_zone[@]}" > instance_name.out
echo "${instance_zone[@]}" > instance_zone.out
}
function append_machines() {
for i in $(seq 1 $1)
do
if [[ $count -ge ${#available_zone[@]} ]];then
count=0
fi
zone=${available_zone[$count]}
create_gce $zone "append"
let count+=1
echo "gc instance is created in $zone"
sleep $create_interval # avoid too quick build
done
echo "${instance_ip[@]}" > instance_ip.out
echo "${instance_zone[@]}" > instance_name.out
echo "${instance_zone[@]}" > instance_zone.out
}
function delete_machines(){
echo ----- stage: remove gc instances ------
for idx in "${!instance_name[@]}"
do
gcloud compute instances delete --quiet ${instance_name[$idx]} --zone=${instance_zone[$idx]}
echo delete $vms
done
}