-
Notifications
You must be signed in to change notification settings - Fork 126
/
create-spot-instance.sh
executable file
·68 lines (56 loc) · 2.82 KB
/
create-spot-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
#!/bin/bash
set -xa
baseResourcesStackName=$1
shift
stackName=$1
shift
timeToLiveInMinutes=$1
shift
instanceTypeConfig=''
if [[ -n "$DEEPRACER_INSTANCE_TYPE" ]]; then
instanceTypeConfig="InstanceType=$DEEPRACER_INSTANCE_TYPE"
fi
BUCKET=$(aws cloudformation describe-stacks --stack-name $baseResourcesStackName | jq '.Stacks | .[] | .Outputs | .[] | select(.OutputKey=="Bucket") | .OutputValue' | tr -d '"')
amiId=$(aws ec2 describe-images --owners 747447086422 --filters "Name=state,Values=available" "Name=is-public,Values=true" --query 'sort_by(Images, &CreationDate)[-1].ImageId' | tr -d '"')
set +xa
#validate stack name meets DeepRacer import constraints
stackNameLength=$(expr length "$stackName")
if grep '^[-0-9a-zA-Z]*$' <<<$stackName;
then
if [ $stackNameLength -le 64 ];
then
echo "Stack name ok for automatic deepracer import"
else
echo ""
echo "Stack name is not acceptable for deepracer import. Must be 64 characters or less."
exit 1
fi
else
echo ""
echo "Stack name is not acceptable for deepracer import. Valid characters: A-Z, a-z, 0-9, and hypens (-). No spaces or underscores (_)."
exit 1
fi
chmod +x ./validation.sh
./validation.sh
if [[ $? -ne 0 ]]; then
while true; do
echo -e "\e[1;33m ########## Error found in your custom files, want to continue anyway? \e[0m"
read -p "[y / n]: " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
fi
set -x
source custom-files/run.env
aws s3 cp custom-files s3://${BUCKET}/${DR_LOCAL_S3_CUSTOM_FILES_PREFIX} --recursive
aws cloudformation deploy --stack-name $stackName --parameter-overrides ${instanceTypeConfig} ResourcesStackName=$baseResourcesStackName DeepRacerImportName=$stackName Name= TimeToLiveInMinutes=$timeToLiveInMinutes AmiId=$amiId BUCKET=$BUCKET CUSTOMFILELOCATION=$DR_LOCAL_S3_CUSTOM_FILES_PREFIX --template-file spot-instance.yaml --capabilities CAPABILITY_IAM --s3-bucket $BUCKET --s3-prefix cf_templates
ASG=$(aws cloudformation describe-stacks --stack-name ${stackName} --query "Stacks[].Outputs[].OutputValue" --output text)
EC2_ID=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names $ASG --query 'AutoScalingGroups[].Instances[].InstanceId' --output text)
EC2_IP=$(aws ec2 describe-instances --instance-ids ${EC2_ID} --query 'Reservations[].Instances[].PublicIpAddress[]' --output text)
echo "Logs will upload every 2 minutes to https://s3.console.aws.amazon.com/s3/buckets/${BUCKET}/${stackName}/logs/"
echo "Training should start shortly on ${EC2_IP}:8080"
echo "Once started, you should also be able to monitor training progress through ${EC2_IP}:8100/menu.html"
echo "Once training is finished, you should see your imported model $stackName in the DeepRacer console, unless you disable that functionality"