Skip to content

Commit 80d3ba5

Browse files
committed
Fixed some bugs and added Terminate Instance option
1 parent cf7f4f7 commit 80d3ba5

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

menu.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def launchInstance():
4242

4343
print("Create Instance with instanceID : {}, type : t2.micro, key-name : {}, security-group : {} ?".format(imageID, keyname, securityGroup))
4444
create = input("Enter Y to confirm : ")
45-
if create == 'Y':
45+
if create == 'Y' or create == 'y':
4646
errorInstance = subprocess.call(
4747
"aws ec2 run-instances --image-id {} --count 1 --instance-type t2.micro --key-name {} --security-groups {} --profile {}".format(imageID, keyname, securityGroup, profile))
4848
if errorInstance == 0:
@@ -168,19 +168,27 @@ def awsConfigure():
168168

169169
def awsMenu():
170170
print('\n')
171-
os.system('aws --version')
172-
171+
error = os.system('aws --version')
172+
print(error)
173+
if error != 0:
174+
print("No version of AWS CLI found. Would you like to install AWS CLIv2 ? (Press Y to confirm)")
175+
install = input("> ")
176+
if install == 'y' or install == 'Y':
177+
subprocess.call("msiexec /i https://awscli.amazonaws.com/AWSCLIV2.msi")
178+
return
179+
else:
180+
return
173181
while True:
174182
if not loginAWS:
175183
awsConfigure()
176184
else:
177185
print("\nAWS MENU\
178186
\n--------")
179-
print("\n1. Launch instance \t2. Show Key-Pairs \t3. Delete Key-Pair\
180-
\n4. Show Security-Groups\t5. Delete Security-Group\t6. Add Inbound Rules\
181-
\n7. Show Instances \t8. Start Instance \t9. Stop Instance\
182-
\n10.AWS Configure \t11.List Profiles \t\
183-
\n\nPress Q to exit")
187+
print("\n1. Launch instance \t2. Show Key-Pairs \t3. Delete Key-Pair\
188+
\n4. Show Security-Groups \t5. Delete Security-Group\t6. Add Inbound Rules\
189+
\n7. Show Instances \t8. Start Instance \t9. Stop Instance\
190+
\n10.Terminate Instance \t11.AWS Configure \t12.List Profiles\
191+
\n\nPress Q to exit")
184192
choice = input("> ")
185193
if choice == '1':
186194
launchInstance()
@@ -189,17 +197,19 @@ def awsMenu():
189197
"aws ec2 describe-key-pairs --profile {}".format(profile))
190198
elif choice == '3':
191199
keypair = input("\nEnter name of key-pair to delete : ")
192-
os.system(
200+
error = os.system(
193201
"aws ec2 delete-key-pair --key-name {} --profile {}".format(keypair, profile))
194-
print("Key-Pair : {} deleted.".format(keypair))
202+
if error == 0:
203+
print("Key-Pair : {} deleted.".format(keypair))
195204
elif choice == '4':
196205
os.system(
197206
"aws ec2 describe-security-groups --profile {}".format(profile))
198207
elif choice == '5':
199208
sg = input("\nEnter name of security-group to delete : ")
200-
os.system(
209+
error = os.system(
201210
"aws ec2 delete-security-group --group-name {} --profile {}".format(sg, profile))
202-
print("Security-Group : {} deleted.".format(sg))
211+
if error == 0:
212+
print("Security-Group : {} deleted.".format(sg))
203213
elif choice == '6':
204214
inboundRules()
205215
elif choice == '7':
@@ -208,25 +218,32 @@ def awsMenu():
208218
instance_id = input("\nEnter instance-id : ")
209219
error = subprocess.call("aws ec2 start-instances --instance-ids {} \
210220
--profile {}".format(instance_id, profile))
211-
if error == '0':
221+
if error == 0:
212222
print("Instance started successfully.")
213223
print("rc : {}".format(error))
214224
elif choice == '9':
215225
instance_id = input("\nEnter instance-id : ")
216226
error = subprocess.call("aws ec2 stop-instances --instance-ids {} \
217227
--profile {}".format(instance_id, profile))
218-
if error == '0':
228+
if error == 0:
219229
print("Instance stopped successfully.")
220230
print("rc : {}".format(error))
221231
elif choice == '10':
222-
awsConfigure()
232+
instance_id = input("\nEnter instance-id : ")
233+
error = subprocess.call("aws ec2 terminate-instances --instance-ids {} \
234+
--profile {}".format(instance_id, profile))
235+
if error == 0:
236+
print("Instance termination processed. Instance will be deleted in some time.")
237+
print("rc : {}".format(error))
223238
elif choice == '11':
239+
awsConfigure()
240+
elif choice == '12':
224241
os.system("aws configure list-profiles")
225242
elif choice == 'Q' or choice == 'q':
226243
print("Exiting...\n")
227244
break
228245
else:
229-
print("Invalid choice!!! Choose from 1-11 or Q to exit.\n")
246+
print("Invalid choice!!! Choose from 1-12 or Q to exit.\n")
230247
return
231248

232249

0 commit comments

Comments
 (0)