Skip to content

Commit b5ea781

Browse files
committed
feat: when the state is terminated, create the new instance
1 parent ce36c9e commit b5ea781

File tree

1 file changed

+84
-71
lines changed

1 file changed

+84
-71
lines changed

src/aws/up.sh

Lines changed: 84 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
portConfigArray=
44
udpPortConfigArray=
55

6-
publickey=`treehouses sshtunnel key name | cut -d ' ' -f 5`.pub
6+
publickey=$(treehouses sshtunnel key name | cut -d ' ' -f 5).pub
77

88
keyname=
99
groupName=luftballons-sg
1010
instanceName=luftballon
1111
checkSSH=~/.ssh/$publickey
1212

1313
checkSshKey() {
14-
aws ec2 describe-key-pairs --key-names $keyname &> /dev/null
14+
aws ec2 describe-key-pairs --key-names $keyname &>/dev/null
1515
return $?
1616
}
1717

1818
checkSecurityGroup() {
19-
aws ec2 describe-security-groups --group-names $groupName &> /dev/null
19+
aws ec2 describe-security-groups --group-names $groupName &>/dev/null
2020
return $?
2121
}
2222

@@ -29,18 +29,17 @@ checkInstanceState() {
2929
aws ec2 describe-instances --instance-ids $ID --query "Reservations[*].Instances[*].State.Name" --output text
3030
}
3131

32-
function importSshKey()
33-
{
32+
function importSshKey() {
3433
if [[ -f ~/.ssh/$publickey ]]; then
35-
aws ec2 import-key-pair --key-name "$keyname" --public-key-material fileb://~/.ssh/$publickey
34+
aws ec2 import-key-pair --key-name "$keyname" --public-key-material fileb://~/.ssh/$publickey
3635
else
3736
echo 'ssh key pair (~/.ssh/$publickey) do not exist ~/.ssh/$publickey'
3837
echo 'Please generate the ssh key by the commad "ssh-keygen -t rsa"'
3938
exit 1
4039
fi
4140
}
4241

43-
function addPort(){
42+
function addPort() {
4443
aws ec2 authorize-security-group-ingress \
4544
--group-name $groupName \
4645
--protocol tcp \
@@ -56,7 +55,7 @@ function addUDPPort() {
5655
--cidr 0.0.0.0/0
5756
}
5857

59-
function createSecurityGroups(){
58+
function createSecurityGroups() {
6059
aws ec2 create-security-group \
6160
--group-name $groupName \
6261
--description "luftballons security group"
@@ -78,28 +77,28 @@ function createSecurityGroups(){
7877
done
7978
}
8079

81-
function createEc2(){
80+
function createEc2() {
8281
image="ami-0750fb43a63427eff"
8382
#image="ami-01e5ff16fd6e8c542"
8483
aws ec2 run-instances \
8584
--count 1 \
8685
--image-id $image \
8786
--instance-type t2.micro \
8887
--key-name $keyname \
89-
--security-groups $groupName
88+
--security-groups $groupName
9089
}
9190

92-
function findData(){
91+
function findData() {
9392
keyWord=$1
94-
grep $keyWord | awk -F':' '{ print $2 }' | sed 's/ //g; s/"//g; s/,//g'
93+
grep $keyWord | awk -F':' '{ print $2 }' | sed 's/ //g; s/"//g; s/,//g'
9594
}
9695

97-
function deleteKeyword(){
96+
function deleteKeyword() {
9897
keyWord=$1
9998
sed "s/$keyWord//g; s/ //g"
10099
}
101100

102-
function getValueByKeyword(){
101+
function getValueByKeyword() {
103102
keyWord=$1
104103
findData $keyWord | deleteKeyword $keyWord
105104
}
@@ -116,61 +115,82 @@ function usage {
116115
function up {
117116
while getopts 'n:pN:a:' OPTION; do
118117
case "$OPTION" in
119-
n)
120-
keyname=$OPTARG
121-
;;
122-
p)
123-
portConfigArray=$(getArrayValueAsStringByKey $instanceName tcpPortArray)
124-
udpPortConfigArray=$(getArrayValueAsStringByKey $instanceName udpPortArray)
125-
if [ -z "$portConfigArray" ]; then
126-
echo "There is no stored port numbers. The default port numbers are used"
127-
fi
128-
if [ -z "$udpPortConfigArray" ]; then
129-
echo "There is no stored udp port numbers. The default port numbers are used"
130-
fi
131-
;;
132-
a)
133-
groupName=$OPTARG-sg
134-
instanceName=$OPTARG
135-
keyname=$OPTARG
136-
;;
137-
?)
138-
usage
139-
;;
140-
esac
141-
done
142-
shift "$(($OPTIND -1))"
143-
144-
aws --version || ( echo "Run './installAwsCli.sh' first. AWS CLI is not installed." && exit 1 )
145-
146-
if test ! -f "$checkSSH"; then
147-
echo "Run 'ssh-keygen' first, with an empty passphrase for no passphrase. Missing ssh key." && exit 1
148-
fi
118+
n)
119+
keyname=$OPTARG
120+
;;
121+
p)
122+
portConfigArray=$(getArrayValueAsStringByKey $instanceName tcpPortArray)
123+
udpPortConfigArray=$(getArrayValueAsStringByKey $instanceName udpPortArray)
124+
if [ -z "$portConfigArray" ]; then
125+
echo "There is no stored port numbers. The default port numbers are used"
126+
fi
127+
if [ -z "$udpPortConfigArray" ]; then
128+
echo "There is no stored udp port numbers. The default port numbers are used"
129+
fi
130+
;;
131+
a)
132+
groupName=$OPTARG-sg
133+
instanceName=$OPTARG
134+
keyname=$OPTARG
135+
;;
136+
?)
137+
usage
138+
;;
139+
esac
140+
done
141+
shift "$(($OPTIND - 1))"
149142

150-
if [ -z $keyname ]; then
151-
keyname=luftballon
152-
fi
143+
aws --version || (echo "Run './installAwsCli.sh' first. AWS CLI is not installed." && exit 1)
153144

154-
if ! checkSshKey ; then
155-
importedKeyName=$(importSshKey | getValueByKeyword KeyName )
156-
if [ -z $importedKeyName ]; then
157-
exit 1
158-
fi
159-
echo "Success to add ssh key: $importedKeyName"
160-
else
161-
echo "The key pair $keyname already exists. Please use another key name."
162-
fi
145+
if test ! -f "$checkSSH"; then
146+
echo "Run 'ssh-keygen' first, with an empty passphrase for no passphrase. Missing ssh key." && exit 1
147+
fi
163148

164-
if ! checkSecurityGroup; then
165-
createSecurityGroups
166-
echo "Add security group"
167-
# Add rules to Security Group as needed
168-
else
169-
echo "Security Group already exists."
149+
if [ -z $keyname ]; then
150+
keyname=luftballon
151+
fi
152+
153+
if ! checkSshKey; then
154+
importedKeyName=$(importSshKey | getValueByKeyword KeyName)
155+
if [ -z $importedKeyName ]; then
156+
exit 1
170157
fi
158+
echo "Success to add ssh key: $importedKeyName"
159+
else
160+
echo "The key pair $keyname already exists. Please use another key name."
161+
fi
171162

172-
instanceId=$(checkInstance)
173-
if [ -z "$instanceId" ]; then
163+
if ! checkSecurityGroup; then
164+
createSecurityGroups
165+
echo "Add security group"
166+
# Add rules to Security Group as needed
167+
else
168+
echo "Security Group already exists."
169+
fi
170+
171+
instanceId=$(checkInstance)
172+
if [ -z "$instanceId" ]; then
173+
instanceState=""
174+
instanceId=$(createEc2 | getValueByKeyword InstanceId)
175+
echo "Creating and running EC2 instance..."
176+
echo "Instance id is $instanceId"
177+
aws ec2 create-tags --resources $instanceId --tags Key=Name,Value=$instanceName
178+
aws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
179+
publicIp=$(waitForOutput "getLatestIpAddress $instanceId")
180+
echo "Public IP Address is $publicIp"
181+
echo "Will open ssh tunnel soon"
182+
isOpen=$(waitForOutput "ssh-keyscan -H $publicIp | grep ecdsa-sha2-nistp256")
183+
echo "Opened ssh tunnel"
184+
openSSHTunnel $instanceName $publicIp $portConfigArray
185+
storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $importedKeyName $instanceId $publicIp $groupNameaws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
186+
else
187+
instanceState=$(checkInstanceState $instanceId)
188+
if [ "$instanceState" = "running" ]; then
189+
echo "EC2 instance is already running."
190+
elif [ "$instanceState" = "stopped" ]; then
191+
echo "Starting stopped EC2 instance..."
192+
start $instanceName
193+
elif [ "$instanceState" = "terminated" ]; then
174194
instanceState=""
175195
instanceId=$(createEc2 | getValueByKeyword InstanceId)
176196
echo "Creating and running EC2 instance..."
@@ -184,13 +204,6 @@ function up {
184204
echo "Opened ssh tunnel"
185205
openSSHTunnel $instanceName $publicIp $portConfigArray
186206
storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $importedKeyName $instanceId $publicIp $groupNameaws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
187-
else
188-
instanceState=$(checkInstanceState $instanceId)
189-
if [ "$instanceState" = "running" ]; then
190-
echo "EC2 instance is already running."
191-
elif [ "$instanceState" = "stopped" ]; then
192-
echo "Starting stopped EC2 instance..."
193-
start $instanceName
194207
else
195208
echo "EC2 instance is in state: $instanceState."
196209
fi

0 commit comments

Comments
 (0)