Skip to content

Commit ce36c9e

Browse files
hiroTochigidogi
andauthored
smoother up (fixes #57) (#59)
Co-authored-by: dogi <[email protected]>
1 parent 386b1e3 commit ce36c9e

File tree

1 file changed

+154
-140
lines changed

1 file changed

+154
-140
lines changed

src/aws/up.sh

Lines changed: 154 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -10,175 +10,189 @@ groupName=luftballons-sg
1010
instanceName=luftballon
1111
checkSSH=~/.ssh/$publickey
1212

13+
checkSshKey() {
14+
aws ec2 describe-key-pairs --key-names $keyname &> /dev/null
15+
return $?
16+
}
17+
18+
checkSecurityGroup() {
19+
aws ec2 describe-security-groups --group-names $groupName &> /dev/null
20+
return $?
21+
}
1322

23+
checkInstance() {
24+
aws ec2 describe-instances --filters "Name=tag:Name,Values=$instanceName" --query "Reservations[*].Instances[*].InstanceId" --output text
25+
}
26+
27+
checkInstanceState() {
28+
ID=$1
29+
aws ec2 describe-instances --instance-ids $ID --query "Reservations[*].Instances[*].State.Name" --output text
30+
}
1431

1532
function importSshKey()
1633
{
17-
if [[ -f ~/.ssh/$publickey ]]
18-
then
19-
aws ec2 import-key-pair --key-name "$keyname" --public-key-material fileb://~/.ssh/$publickey
20-
else
21-
echo 'ssh key pair (~/.ssh/$publickey) do not exist ~/.ssh/$publickey'
22-
echo 'Please generate the ssh key by the commad "ssh-keygen -t rsa"'
23-
exit 1
24-
fi
34+
if [[ -f ~/.ssh/$publickey ]]; then
35+
aws ec2 import-key-pair --key-name "$keyname" --public-key-material fileb://~/.ssh/$publickey
36+
else
37+
echo 'ssh key pair (~/.ssh/$publickey) do not exist ~/.ssh/$publickey'
38+
echo 'Please generate the ssh key by the commad "ssh-keygen -t rsa"'
39+
exit 1
40+
fi
2541
}
2642

2743
function addPort(){
28-
aws ec2 authorize-security-group-ingress \
29-
--group-name $groupName \
30-
--protocol tcp \
31-
--port $1 \
32-
--cidr 0.0.0.0/0
44+
aws ec2 authorize-security-group-ingress \
45+
--group-name $groupName \
46+
--protocol tcp \
47+
--port $1 \
48+
--cidr 0.0.0.0/0
3349
}
3450

3551
function addUDPPort() {
36-
aws ec2 authorize-security-group-ingress \
37-
--group-name $groupName \
38-
--protocol udp \
39-
--port $1 \
40-
--cidr 0.0.0.0/0
52+
aws ec2 authorize-security-group-ingress \
53+
--group-name $groupName \
54+
--protocol udp \
55+
--port $1 \
56+
--cidr 0.0.0.0/0
4157
}
4258

43-
4459
function createSecurityGroups(){
45-
aws ec2 create-security-group \
46-
--group-name $groupName \
47-
--description "luftballons security group"
48-
49-
if [ -z "$portConfigArray" ]
50-
then
51-
portConfigArray="8080:80,8443:443,2022:22"
52-
fi
53-
54-
portArray=($(makePortArray "$portConfigArray"))
55-
56-
for i in "${portArray[@]}"
57-
do
58-
addPort $i
59-
echo $i
60-
done
61-
62-
if [ -z "$udpPortConfigArray" ]
63-
then
64-
udpPortConfigArray="1194"
65-
fi
66-
67-
portArray=($udpPortConfigArray)
68-
69-
for i in "${portArray[@]}"
70-
do
71-
addUDPPort $i
72-
echo $i
73-
done
60+
aws ec2 create-security-group \
61+
--group-name $groupName \
62+
--description "luftballons security group"
63+
if [ -z "$portConfigArray" ]; then
64+
portConfigArray="8080:80,8443:443,2022:22"
65+
fi
66+
portArray=($(makePortArray "$portConfigArray"))
67+
for i in "${portArray[@]}"; do
68+
addPort $i
69+
echo $i
70+
done
71+
if [ -z "$udpPortConfigArray" ]; then
72+
udpPortConfigArray="1194"
73+
fi
74+
portArray=($udpPortConfigArray)
75+
for i in "${portArray[@]}"; do
76+
addUDPPort $i
77+
echo $i
78+
done
7479
}
7580

7681
function createEc2(){
77-
image="ami-0750fb43a63427eff"
78-
#image="ami-01e5ff16fd6e8c542"
79-
aws ec2 run-instances \
80-
--count 1 \
81-
--image-id $image \
82-
--instance-type t2.micro \
83-
--key-name $keyname \
84-
--security-groups $groupName
82+
image="ami-0750fb43a63427eff"
83+
#image="ami-01e5ff16fd6e8c542"
84+
aws ec2 run-instances \
85+
--count 1 \
86+
--image-id $image \
87+
--instance-type t2.micro \
88+
--key-name $keyname \
89+
--security-groups $groupName
8590
}
8691

8792
function findData(){
88-
keyWord=$1
89-
grep $keyWord | awk -F':' '{ print $2 }' | sed 's/ //g; s/"//g; s/,//g'
93+
keyWord=$1
94+
grep $keyWord | awk -F':' '{ print $2 }' | sed 's/ //g; s/"//g; s/,//g'
9095
}
9196

9297
function deleteKeyword(){
93-
keyWord=$1
94-
sed "s/$keyWord//g; s/ //g"
98+
keyWord=$1
99+
sed "s/$keyWord//g; s/ //g"
95100
}
96101

97102
function getValueByKeyword(){
98-
keyWord=$1
99-
findData $keyWord | deleteKeyword $keyWord
103+
keyWord=$1
104+
findData $keyWord | deleteKeyword $keyWord
100105
}
101106

102107
function usage {
103-
echo "script usage: $(basename \$0 aws up) [-n ssh key name] [-p] [-a change key name, instance name, and group name]" >&2
104-
echo 'Start Luftballon.'
105-
echo ' -n Change SSH key name on AWS'
106-
echo ' -a Change SSH key name, instance name, and group name'
107-
echo ' -p Use stored port Numbers instead of the default port number.'
108-
exit 1
108+
echo "script usage: $(basename \$0 aws up) [-n ssh key name] [-p] [-a change key name, instance name, and group name]" >&2
109+
echo 'Start Luftballon.'
110+
echo ' -n Change SSH key name on AWS'
111+
echo ' -a Change SSH key name, instance name, and group name'
112+
echo ' -p Use stored port Numbers instead of the default port number.'
113+
exit 1
109114
}
110115

111116
function up {
112-
while getopts 'n:pN:a:' OPTION; do
113-
case "$OPTION" in
114-
n)
115-
keyname=$OPTARG
116-
;;
117-
p)
118-
portConfigArray=$(getArrayValueAsStringByKey $instanceName tcpPortArray)
119-
udpPortConfigArray=$(getArrayValueAsStringByKey $instanceName udpPortArray)
120-
if [ -z "$portConfigArray" ]
121-
then
122-
echo "There is no stored port numbers. The default port numbers are used"
123-
fi
124-
if [ -z "$udpPortConfigArray" ]
125-
then
126-
echo "There is no stored udp port numbers. The default port numbers are used"
127-
fi
128-
;;
129-
a)
130-
groupName=$OPTARG-sg
131-
instanceName=$OPTARG
132-
keyname=$OPTARG
133-
;;
134-
?)
135-
usage
136-
;;
137-
esac
138-
done
139-
shift "$(($OPTIND -1))"
140-
141-
aws --version || ( echo "Run './installAwsCli.sh' first. AWS CLI is not installed." && exit 1 )
142-
143-
if test ! -f "$checkSSH"; then
144-
echo "Run 'ssh-keygen' first, with an empty passphrase for no passphrase. Missing ssh key." && exit 1
145-
fi
146-
147-
if [ -z $keyname ]
148-
then
149-
keyname=luftballon
150-
fi
151-
152-
153-
keyName=$(importSshKey | getValueByKeyword KeyName )
154-
155-
if [ -z $keyName ]
156-
then
157-
exit 1
158-
fi
159-
160-
echo "Success to add ssh key: $keyName"
161-
162-
createSecurityGroups
163-
echo "Add security group"
164-
165-
instanceId=$(createEc2 | getValueByKeyword InstanceId )
166-
echo "Create EC2 Instance"
167-
echo "Instance id is $instanceId"
168-
169-
170-
aws ec2 create-tags --resources $instanceId --tags Key=Name,Value=$instanceName
171-
aws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
172-
173-
174-
publicIp=$(waitForOutput "getLatestIpAddress $instanceId")
175-
echo "Public IP Address is $publicIp"
176-
177-
echo "Will open ssh tunnel soon"
178-
isOpen=$(waitForOutput "ssh-keyscan -H $publicIp | grep ecdsa-sha2-nistp256")
179-
echo "Opened ssh tunnel"
180-
181-
openSSHTunnel $instanceName $publicIp $portConfigArray
182-
183-
storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $keyName $instanceId $publicIp $groupName
117+
while getopts 'n:pN:a:' OPTION; do
118+
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
149+
150+
if [ -z $keyname ]; then
151+
keyname=luftballon
152+
fi
153+
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
163+
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."
170+
fi
171+
172+
instanceId=$(checkInstance)
173+
if [ -z "$instanceId" ]; then
174+
instanceState=""
175+
instanceId=$(createEc2 | getValueByKeyword InstanceId)
176+
echo "Creating and running EC2 instance..."
177+
echo "Instance id is $instanceId"
178+
aws ec2 create-tags --resources $instanceId --tags Key=Name,Value=$instanceName
179+
aws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
180+
publicIp=$(waitForOutput "getLatestIpAddress $instanceId")
181+
echo "Public IP Address is $publicIp"
182+
echo "Will open ssh tunnel soon"
183+
isOpen=$(waitForOutput "ssh-keyscan -H $publicIp | grep ecdsa-sha2-nistp256")
184+
echo "Opened ssh tunnel"
185+
openSSHTunnel $instanceName $publicIp $portConfigArray
186+
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
194+
else
195+
echo "EC2 instance is in state: $instanceState."
196+
fi
197+
fi
184198
}

0 commit comments

Comments
 (0)