-
Notifications
You must be signed in to change notification settings - Fork 1
/
launch
116 lines (99 loc) · 2.98 KB
/
launch
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
# Starts a spot instance in aws
export AWS_REGION=us-west-2
ZONE=${ZONE:-a}
export AWS_PROFILE=me
NAME=AHTSE-DEV
# List the latest Linux AMI in a region
# aws ssm --profile me --region us-west-2 get-parameters-by-path --path /aws/service/ami-amazon-linux-latest --query Parameters[].Name
# Takes one argument, the short name
latest() {
aws ssm get-parameter --name /aws/service/ami-amazon-linux-latest/${1} --output text\
--query Parameter.Value |tr -d '\r'
}
# Takes one argument, the short name of the architecture
latest_al2() {
latest amzn2-ami-hvm-${1}-gp2
}
# Same as above, but for AL2023
latest_al2023() {
latest al2023-ami-kernel-default-${1}
# latest al2023-ami-minimal-kernel-default-${1}
}
#Get ids for running instances with a given name
get_instid() {
instid=`aws ec2 describe-instances\
--filters Name=tag:Name,Values=${1} Name=instance-state-name,Values=running\
| jq -r .Reservations[].Instances[].InstanceId`
[ -z "${instid}" ] && return 1
echo $instid
}
get_inst_state() {
state=`aws ec2 describe-instances --filters Name=instance-id,Values=${1}\
--query Reservations[].Instances[].State.Name\
--output text\
|tr -d '\r'\
`
[ -z "$state" ] && return 1
echo $state
}
# Two params, instance id and state as string
wait_for_instance_state() {
echo -n Waiting for instance $1 to be $2 >&2
while [ $2 != `get_inst_state $1` ]
do
echo -n . >&2
sleep 1
done
echo \! >&2
}
#Get instance public IP
get_inst_ip() {
state=`aws ec2 describe-instances --filters Name=instance-id,Values=${1}\
--query Reservations[].Instances[].NetworkInterfaces[].Association.PublicIp\
--output text\
|tr -d '\r'\
`
[ -z ${state} ] && return 1
echo $state
}
# defaults to arm64, any argument switches to x86_64
arch=${1:-arm64}
# The best priced zone changes often
if [[ $arch == "arm64" ]]
then
itype=c7gd.4xlarge
else
arch=x86_64
itype=c5ad.2xlarge
fi
AMI=$(latest_al2023 $arch)
# echo $(lspec)
# aws ec2 request-spot-instances --launch-specification "$(lspec)"
hostid=`aws ec2 run-instances\
--image-id $AMI\
--instance-type $itype\
--placement AvailabilityZone=$AWS_REGION$ZONE\
--tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$NAME}]"\
--instance-market-options "MarketType=spot"\
--key-name handler\
--security-group-ids sg-e9ec3297 sg-0509325582d932d26\
--output text\
--query Instances[*].InstanceId\
|tr -d '\r'\
`
[ -z "$hostid" ] && {
echo Failed to start instance >&2
exit 1
}
echo Start instance $hostid
wait_for_instance_state $hostid running
hostip=$(get_inst_ip $hostid)
[ -z "$hostip" ] && {
echo Failed to get public IP for instance $hostid
exit 1
}
echo Host IP is $hostip
# Might need to wait a bit for ssh to be ready
sleep 10
scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bootstrap.bash $hostip:
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $hostip bash -x ./bootstrap.bash