@@ -158,7 +158,6 @@ function up {
158158 echo " Success to add ssh key: $importedKeyName "
159159 else
160160 echo " The key pair $keyname already exists. Please use another key name."
161- importedKeyName=$keyname
162161 fi
163162
164163 if ! checkSecurityGroup; then
@@ -169,44 +168,54 @@ function up {
169168 echo " Security Group already exists."
170169 fi
171170
172- instanceId=$( checkInstance)
173- if [ -z " $instanceId " ]; then
174- instanceState=" "
171+ createAndTagInstance () {
175172 instanceId=$( createEc2 | getValueByKeyword InstanceId)
176173 echo " Creating and running EC2 instance..."
177174 echo " Instance id is $instanceId "
175+
178176 aws ec2 create-tags --resources $instanceId --tags Key=Name,Value=$instanceName
179177 aws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
178+
180179 publicIp=$( waitForOutput " getLatestIpAddress $instanceId " )
181180 echo " Public IP Address is $publicIp "
182181 echo " Will open ssh tunnel soon"
182+
183183 isOpen=$( waitForOutput " ssh-keyscan -H $publicIp | grep ecdsa-sha2-nistp256" )
184184 echo " Opened ssh tunnel"
185+
185186 openSSHTunnel $instanceName $publicIp $portConfigArray
186- storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $importedKeyName $instanceId $publicIp $groupNameaws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
187+ storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $importedKeyName $instanceId $publicIp $groupName
188+ }
189+
190+ instanceId=$( checkInstance)
191+ if [ -z " $instanceId " ]; then
192+ createAndTagInstance
193+ else
194+ instanceState=$( waitForConditionalOutput " checkInstanceState $instanceId " " \" stopping\" " " different" )
195+ if [ $? -ne 0 ]; then
196+ echo " Wait for starting on start command until instance is stopped."
197+ exit 1
198+ fi
199+ echo " Success to add ssh key: $importedKeyName "
187200 else
188- instanceState=$( checkInstanceState $instanceId )
189- if [ " $instanceState " = " running" ]; then
201+ echo " The key pair $keyname already exists. Please use another key name."
202+ importedKeyName=$keyname
203+ fi
204+
205+ case " $instanceState " in
206+ " running" )
190207 echo " EC2 instance is already running."
191- elif [ " $instanceState " = " stopped" ]; then
208+ ;;
209+ " stopped" )
192210 echo " Starting stopped EC2 instance..."
193211 start $instanceName
194- elif [ " $instanceState " = " terminated" ]; then
195- instanceState=" "
196- instanceId=$( createEc2 | getValueByKeyword InstanceId)
197- echo " Creating and running EC2 instance..."
198- echo " Instance id is $instanceId "
199- aws ec2 create-tags --resources $instanceId --tags Key=Name,Value=$instanceName
200- aws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
201- publicIp=$( waitForOutput " getLatestIpAddress $instanceId " )
202- echo " Public IP Address is $publicIp "
203- echo " Will open ssh tunnel soon"
204- isOpen=$( waitForOutput " ssh-keyscan -H $publicIp | grep ecdsa-sha2-nistp256" )
205- echo " Opened ssh tunnel"
206- openSSHTunnel $instanceName $publicIp $portConfigArray
207- storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $importedKeyName $instanceId $publicIp $groupNameaws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
208- else
212+ ;;
213+ " terminated" )
214+ createAndTagInstance
215+ ;;
216+ * )
209217 echo " EC2 instance is in state: $instanceState ."
210- fi
218+ ;;
219+ esac
211220 fi
212221}
0 commit comments