-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-tools.sh
225 lines (184 loc) · 7.88 KB
/
aws-tools.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
#!/bin/bash
source ~/.bashrc
source logger.sh
source utils.sh
if [[ -z $AWS_CLOUD_GAMING_PROFILE ]]; then
AWS_CLOUD_GAMING_PROFILE=personal
fi
checkAwsProfile() {
printInfo "Checking for the $AWS_CLOUD_GAMING_PROFILE AWS Profile in AWS config"
if ! (aws --profile $AWS_CLOUD_GAMING_PROFILE sts get-caller-identity --no-cli-auto-prompt > /dev/null 2>&1); then
printError "AWS config is missing the $AWS_CLOUD_GAMING_PROFILE profile. Add it to your ~/.aws/config file and try running this application again"
exit 1
fi
}
getInstanceState() {
aws --profile $AWS_CLOUD_GAMING_PROFILE ec2 describe-instances --instance-ids "$AWS_TEAM_BUILDING_EC2_INSTANCE_ID" --query 'Reservations[0].Instances[0].State.Name' --no-cli-auto-prompt --output text &
}
showGaugeBoxForAwsCommand() {
declare increment
declare i
increment=$(echo "scale=1; 100 / 30" | bc)
i="0"
printInfo "$2"
{
while [[ $(getInstanceState) != "$1" ]]; do
declare percent
percent=$(printf "%.0f" $i)
echo -e "XXX\n$percent\n$2... \nXXX"
i=$(echo "$i + $increment" | bc)
sleep 5
done
if [[ $(getInstanceState) != "$1" ]]; then
printError "$4"
echo -e "XXX\n0\n$4\nXXX"
return 1
else
echo -e "XXX\n100\n$3\nXXX"
sleep 1
fi
} | whiptail --title "$2..." --gauge "$2..." "$GAUGE_BOX_HEIGHT" "$GAUGE_BOX_WIDTH" 0
printInfo "$3"
}
waitForInstanceToBecomeAvailable() {
printInfo "Waiting for instance to become available"
{
declare increment
increment=$(echo "scale=1; 100/90" | bc)
for ((i=0; i<=100; i=$(printf "%.0f" $(echo "scale=1; $i + $increment" | bc)))); do
if (timeout 10s nc -vz "$AWS_TEAM_BUILDING_EC2_INSTANCE_IP" 8443 2>&1 > /dev/null); then
break
fi
echo "$i"
done
echo 100
sleep 1
} | whiptail --title "Instance Startup" --gauge "Waiting for instance to become available..." "$GAUGE_BOX_HEIGHT" "$GAUGE_BOX_WIDTH" 0
}
getInstanceIp() {
printInfo "Fetching instance IP for id: $AWS_TEAM_BUILDING_EC2_INSTANCE_ID"
aws --profile $AWS_CLOUD_GAMING_PROFILE ec2 describe-instances --instance-ids "$AWS_TEAM_BUILDING_EC2_INSTANCE_ID" --query "Reservations[0].Instances[0].PublicIpAddress" --output text --no-cli-auto-prompt
}
createDcvConnectionProfileFromTemplate() {
printInfo "Creating DCV connection profile from template"
PASSWORD="$(aws --profile $AWS_CLOUD_GAMING_PROFILE ec2 get-password-data --instance-id "$AWS_TEAM_BUILDING_EC2_INSTANCE_ID" --priv-launch-key ~/.ssh/"$AWS_CLOUD_GAMING_SSH_KEY".pem --query 'PasswordData' --output text --no-cli-auto-prompt)"
PASSWORD=$(echo -n $PASSWORD)
sed -i "/^host=/c\host=$AWS_TEAM_BUILDING_EC2_INSTANCE_IP" cloud_gaming_dcv_profile.dcv
sed -i "/^password=/c\password=$PASSWORD" cloud_gaming_dcv_profile.dcv
}
startInstance() {
declare state
declare desiredState=running
state=$(getInstanceState)
printInfo "Starting instance"
if [[ $state == "$desiredState" ]]; then
printError "Instance is already running. Doing nothing"
msgBox "Instance is already running."
else
declare instanceIp
aws --profile $AWS_CLOUD_GAMING_PROFILE ec2 start-instances --instance-ids "$AWS_TEAM_BUILDING_EC2_INSTANCE_ID" --no-cli-auto-prompt > /dev/null 2>&1 &
showGaugeBoxForAwsCommand "$desiredState" "Starting Your Instance" "Successfully Started Your Instance!" "Failed to start your instance!"
printInfo "Checking to see if IP changed"
instanceIp=$(getInstanceIp)
if [[ $instanceIp != "$AWS_TEAM_BUILDING_EC2_INSTANCE_IP" ]]; then
setConfigValue "AWS_TEAM_BUILDING_EC2_INSTANCE_IP" "$instanceIp"
export AWS_TEAM_BUILDING_EC2_INSTANCE_IP="$instanceIp"
createDcvConnectionProfileFromTemplate
fi
waitForInstanceToBecomeAvailable
fi
}
stopInstance() {
declare state
declare desiredState=stopped
state=$(getInstanceState)
printInfo "Stopping instance"
if [[ $state == "$desiredState" ]]; then
printError "Instance is already stopped."
msgBox "Instance is already stopped."
else
aws --profile $AWS_CLOUD_GAMING_PROFILE ec2 stop-instances --instance-ids "$AWS_TEAM_BUILDING_EC2_INSTANCE_ID" --no-cli-auto-prompt > /dev/null 2>&1 &
showGaugeBoxForAwsCommand "$desiredState" "Stopping Your Instance" "Successfully Stopped Your Instance!" "Failed to stop your instance!"
fi
}
rebootInstance() {
declare desiredState=running
printInfo "Rebooting instance"
aws --profile $AWS_CLOUD_GAMING_PROFILE ec2 reboot-instances --instance-ids "$AWS_TEAM_BUILDING_EC2_INSTANCE_ID" --no-cli-auto-prompt > /dev/null 2>&1 &
if ! (showGaugeBoxForAwsCommand "$desiredState" "Restarting Your Instance" "Successfully Restarted Your Instance!"); then
printError "Failed to restart instance. Waiting for user to manually start instance before continuing."
msgBox "$(cat <<-EOF
Failed to restart your instance! Please make sure your instance is started before continuing!
Your instance ID is $AWS_TEAM_BUILDING_EC2_INSTANCE_ID
Hit 'OK' Once your instance is started
EOF
)"
fi
}
getMyIp() {
curl -s -L -X GET http://checkip.amazonaws.com
}
deployCdk() {
printInfo "Deploying CDK"
cd cdk
declare user
declare localIp
declare logFile=/tmp/cdk
user="$(whoami)"
localIp="$(getMyIp)"
{
echo -e "XXX\n0\nRunning npm install... \nXXX"
printInfo "Running npm install"
npm install > /dev/null 2>&1
echo -e "XXX\n50\nBuilding CDK... \nXXX"
printInfo "Running npm run build"
npm run build > /dev/null 2>&1
echo -e "XXX\n100\nDone! \nXXX"
sleep 1
} | whiptail --title "Preparing CDK..." --gauge "Preparing CDK..." "$GAUGE_BOX_HEIGHT" "$GAUGE_BOX_WIDTH" 0
declare pid
declare bootstrapLogFile="${logFile}-bootstrap.log"
printInfo "Bootstrapping CDK and logging to $bootstrapLogFile"
yes | npx cdk --no-color --require-approval never --profile $AWS_CLOUD_GAMING_PROFILE -c "user=$user" -c "localIp=$localIp" bootstrap > $bootstrapLogFile 2>&1 &
pid=$!
showTailBox "Bootstrapping CDK" $pid $bootstrapLogFile
declare deployLogFile="${logFile}-deploy.log"
printInfo "Deploying CDK and logging to $deployLogFile"
yes | npx cdk --no-color --require-approval never --profile $AWS_CLOUD_GAMING_PROFILE -c "user=$user" -c "localIp=$localIp" deploy "TeamBuildingCloudGaming-$user" > $deployLogFile 2>&1 &
pid=$!
showTailBox "Deploy EC2 Instance" $pid $deployLogFile
unset AWS_TEAM_BUILDING_EC2_INSTANCE_ID
unset AWS_TEAM_BUILDING_EC2_INSTANCE_IP
AWS_TEAM_BUILDING_EC2_INSTANCE_ID=$(cat $deployLogFile | grep InstanceId | awk '{print $NF;}')
setConfigValue "AWS_TEAM_BUILDING_EC2_INSTANCE_ID" "$AWS_TEAM_BUILDING_EC2_INSTANCE_ID"
AWS_TEAM_BUILDING_EC2_INSTANCE_IP=$(getInstanceIp)
setConfigValue "AWS_TEAM_BUILDING_EC2_INSTANCE_IP" "$AWS_TEAM_BUILDING_EC2_INSTANCE_IP"
cd ..
waitForInstanceToBecomeAvailable
rebootInstance
waitForInstanceToBecomeAvailable
}
connectToInstanceViaDcvViewer() {
printInfo "Connecting to instance desktop via DCV Viewer"
if ! (hash dcvviewer 2> /dev/null); then
printError "dcvviewer is not installed. Cannot connect to personal instance until first time setup is run."
msgBox "Can't connect to personal instance via DCV Viewer without first time setup. Run the deploy instance task from the instance management menu first!"
fi
if (pgrep -f dcvviewer); then
printError "DCV Viewer is already running."
msgBox "DCV Viewer is already running"
return 0
fi
declare state
state=$(getInstanceState)
if [[ $state != "running" ]]; then
if (whiptail --fb --title "Start your instance?" --yesno "In order to stream, you instance needs to be started. Would you like to start your personal instance?" "$BOX_BOX_HEIGHT" "$BOX_WIDTH"); then
startInstance
else
printError "Unable to start desktop connection: Instance is not running"
msgBox "Can't start desktop connection! Instance is not running. You can start the instance from the personal Instance Management menu."
return 0
fi
fi
dcvviewer cloud_gaming_dcv_profile.dcv --certificate-validation-policy=accept-untrusted > /dev/null 2>&1 &
}