Repository navigation
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·214 lines (178 loc) · 6.18 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·214 lines (178 loc) · 6.18 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
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
#/bin/bash
# Runs the container on the ECS cluster for remote development
set -e
DEV_USER=${1:-"elliot"}
export AWS_PROFILE=${2:-"ansilo"}
export AWS_DEFAULT_REGION=${3:-"ap-southeast-2"}
export AWS_PAGER=""
if [[ -z "$DEV_USER" ]] || [[ -z "$AWS_PROFILE" ]];
then
echo "Usage: $0 [user] [aws profile=ansilo] [region]"
exit 1
fi
DIR=$(realpath $(dirname $0))
SECURITY_GROUP_NAME="dev-container-sg-$DEV_USER"
VPC_ID="vpc-0eca650e59ad5b70e"
AVAILABILITY_ZONE="${AWS_DEFAULT_REGION}a"
EBS_NAME=devcontainer-storage-$DEV_USER
EBS_SIZE=150
echo "Retrieving EBS Volume $EBS_NAME..."
EBS_VOLUME_JSON=$(aws ec2 describe-volumes --filters="Name=tag:Name,Values=$EBS_NAME" --query='Volumes[0]')
if [[ -z $EBS_VOLUME_JSON ]] || [[ "$EBS_VOLUME_JSON" == "null" ]];
then
echo "Could not find EBS volume $EBS_NAME, creating..."
EBS_VOLUME_JSON=$(aws ec2 create-volume \
--availability-zone $AVAILABILITY_ZONE \
--volume-type gp3 \
--size $EBS_SIZE \
--tag-specifications "ResourceType=volume,Tags=[{Key=Name,Value=$EBS_NAME}]")
fi
EBS_ID=$(echo $EBS_VOLUME_JSON | jq -r '.VolumeId')
echo "Found EBS volume: $EBS_ID ($EBS_NAME)"
echo "Retrieving security group $SECURITY_GROUP_NAME..."
SECURITY_GROUP_JSON=$(aws ec2 describe-security-groups --filters="Name=group-name,Values=$SECURITY_GROUP_NAME" --query='SecurityGroups[0]')
if [[ -z $SECURITY_GROUP_JSON ]] || [[ "$SECURITY_GROUP_JSON" == "null" ]];
then
echo "Could not find security group $SECURITY_GROUP_NAME, creating..."
SECURITY_GROUP_JSON=$(aws ec2 create-security-group --group-name $SECURITY_GROUP_NAME --description "Devcontainer security group - $DEV_USER" --vpc-id $VPC_ID)
fi
SECURITY_GROUP_ID=$(echo $SECURITY_GROUP_JSON | jq -r '.GroupId')
echo "Found security group $SECURITY_GROUP_NAME ($SECURITY_GROUP_ID)"
CURRENT_IP_ADDRESS=$(curl -sSf https://ipinfo.io/json | jq -r '.ip')
echo "Current outbound IP: $CURRENT_IP_ADDRESS"
echo "Revoking existing ingress..."
aws ec2 revoke-security-group-ingress --group-id $SECURITY_GROUP_ID --ip-permissions \
"$(aws ec2 describe-security-groups --output json --group-ids $SECURITY_GROUP_ID --query "SecurityGroups[0].IpPermissions")"
echo "Will authorise access from $CURRENT_IP_ADDRESS/32 over 22/tcp and 2222/tcp to $SECURITY_GROUP_NAME"
aws ec2 authorize-security-group-ingress \
--group-id=$SECURITY_GROUP_ID \
--ip-permissions="IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges=[{CidrIp=$CURRENT_IP_ADDRESS/32,Description='Authorise ssh from $DEV_USER@$HOSTNAME'}]"
aws ec2 authorize-security-group-ingress \
--group-id=$SECURITY_GROUP_ID \
--ip-permissions="IpProtocol=tcp,FromPort=2222,ToPort=2222,IpRanges=[{CidrIp=$CURRENT_IP_ADDRESS/32,Description='Authorise ssh from $DEV_USER@$HOSTNAME'}]"
echo "Authorised!"
cleanup() {
if [[ ! -z $SPOT_FLEET_ID ]];
then
echo "Stopping spot fleet $SPOT_FLEET_ID..."
aws ec2 cancel-spot-fleet-requests \
--spot-fleet-request-ids $SPOT_FLEET_ID \
--terminate-instances \
>/dev/null 2>&1
echo "Fleet stopped!"
fi
echo "Done!"
}
trap cleanup EXIT
echo "Requesting spot fleet..."
SPOT_FLEET_ID=$(aws ec2 request-spot-fleet \
--spot-fleet-request-config file://${DIR}/spot-fleet-config.json \
--query 'SpotFleetRequestId' \
--output text)
echo "Requested spot fleet: $SPOT_FLEET_ID"
echo "Waiting for spot fleet instance on in $SPOT_FLEET_ID"
TRIES=0
while ((TRIES < 30));
do
SPOT_INSTANCE_JSON=$(aws ec2 describe-spot-fleet-instances \
--spot-fleet-request-id $SPOT_FLEET_ID \
--query 'ActiveInstances[0]')
if [[ -z $SPOT_INSTANCE_JSON ]] || [[ "$SPOT_INSTANCE_JSON" == "null" ]];
then
echo "No instance found, sleeping..."
sleep 10
let "TRIES+=1"
continue
fi
echo "Found instance: $SPOT_INSTANCE_JSON"
SPOT_INSTANCE_ID=$(echo $SPOT_INSTANCE_JSON | jq -r '.InstanceId')
echo "Spot instance ID: $SPOT_INSTANCE_ID"
break
done
echo "Getting public IP from $SPOT_INSTANCE_ID..."
PUB_IP=$(aws ec2 describe-instances \
--filters "Name=instance-id,Values=$SPOT_INSTANCE_ID" \
--query 'Reservations[*].Instances[*].PublicIpAddress' \
--output text)
echo "Found instance ip: $PUB_IP"
echo "Waiting for sshd on spot instance at $PUB_IP"
TRIES=0
while ((TRIES < 30));
do
echo "Connecting..."
set +e
CON_CODE=$(timeout 2 nc -vz $PUB_IP 22; echo $?)
set -e
if [[ $CON_CODE == 0 ]];
then
break
fi
echo "Failed to connect, sleeping..."
sleep 5
let "TRIES+=1"
done
echo "SSH is listening on instance..."
echo "Attaching volume $EBS_ID to instance $SPOT_INSTANCE_ID..."
aws ec2 attach-volume \
--instance-id $SPOT_INSTANCE_ID \
--volume-id $EBS_ID \
--device /dev/xvdf
echo "Volume attached"
echo "Updating ~/.ssh/config with ansilodevinstance"
cp -a ~/.ssh/config ~/.ssh/config.bk
set +e
if [[ $(grep ansilodevinstanceip ~/.ssh/config > /dev/null; echo $?) == "0" ]];
then
set -e
sed -i.bk "s/.*#ansilodevinstanceip/HostName $PUB_IP #ansilodevinstanceip/" ~/.ssh/config
else
set -e
echo "
Host ansilodevinstance
HostName $PUB_IP #ansilodevinstanceip
User ubuntu
StrictHostKeyChecking no
" >> ~/.ssh/config
fi
echo "Provisioning instance..."
ssh ansilodevinstance \
SPOT_FLEET_ID=$SPOT_FLEET_ID \
EBS_ID=$EBS_ID \
DEV_USER=$DEV_USER \
"bash -s" < $DIR/init-instance.sh
echo "Instance provisioned!"
echo "Waiting for sshd on devcontainer at $PUB_IP"
TRIES=0
while ((TRIES < 30));
do
echo "Connecting..."
set +e
CON_CODE=$(timeout 2 nc -vz $PUB_IP 2222; echo $?)
set -e
if [[ $CON_CODE == 0 ]];
then
break
fi
echo "Failed to connect, sleeping..."
sleep 5
let "TRIES+=1"
done
echo "Updating ~/.ssh/config with ansilodev"
cp -a ~/.ssh/config ~/.ssh/config.bk
set +e
if [[ $(grep ansilodevcontainerip ~/.ssh/config > /dev/null; echo $?) == "0" ]];
then
set -e
sed -i.bk "s/.*#ansilodevcontainerip/HostName $PUB_IP #ansilodevcontainerip/" ~/.ssh/config
else
set -e
echo "
Host ansilodev
HostName $PUB_IP #ansilodevcontainerip
User vscode
StrictHostKeyChecking no
Post 2222
" >> ~/.ssh/config
fi
echo "Ready to ssh ansilodev! sleeping..."
while :; do sleep 100; done