Created
September 4, 2015 03:57
-
-
Save honux77/4cf98aa2b0626ac3c64b to your computer and use it in GitHub Desktop.
AWS Python Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/local/bin/python3 | |
| from os.path import expanduser | |
| import sys | |
| import boto3 | |
| #set env | |
| home = expanduser("~") | |
| print (home) | |
| #getName from Tag | |
| def getNameTag(tags): | |
| for tag in tags: | |
| if tag['Key'] == 'Name': | |
| return tag['Value'] | |
| def getState(state): | |
| return state['Name']; | |
| #run/stop single instance | |
| def changeRunStop(instance): | |
| if getState(instance.state) == "stopped": | |
| sys.stdout.write("Start instance (y/N) ") | |
| yn = input() | |
| if yn == "y": | |
| instance.start() | |
| elif getState(instance.state) == "running": | |
| sys.stdout.write("Stop instance (y/N) "); | |
| yn = input() | |
| if yn == "y": | |
| instance.stop() | |
| else: | |
| print("Can't choose right action to do for", instance.instance_id) | |
| #define functions for switch | |
| def printConf(): | |
| print ("Your Configurations") | |
| print (open(home + "/.aws/config").read()) | |
| def printCred(): | |
| print ("Your Credentials") | |
| print (open(home + "/.aws/credentials").read()) | |
| def printInstances(): | |
| print ("Your Instances") | |
| ec2 = boto3.resource("ec2") | |
| all = ec2.instances.all() | |
| count = 1; | |
| instances={}; | |
| for i in all: | |
| print(count, ":", i.instance_id, getState(i.state), getNameTag(i.tags)) | |
| instances[count] = i; | |
| count = count + 1; | |
| sys.stdout.write("Which one do you want to change to run / stop? "); | |
| choose = input(); | |
| if choose == "": | |
| print("Nothing chosen"); | |
| else: | |
| changeRunStop(instances[int(choose)]); | |
| def quit(): | |
| print ("Thank you") | |
| exit() | |
| #map for the switch alternative | |
| options = { | |
| 0:quit, | |
| 1:printConf, | |
| 2:printCred, | |
| 3:printInstances, | |
| } | |
| # print menu, menu is like: | |
| ''' | |
| +------------------------------------+ | |
| | 1.show config | | |
| | 2.show credential | | |
| | 3.show instances | | |
| | 0.quit | | |
| +------------------------------------+ | |
| ''' | |
| while True: | |
| menu = open("menu.txt").read() | |
| print (menu) | |
| # print ("choose (1-3)",) | |
| sys.stdout.write("choose(1-4, 0 to quit) ") | |
| num = int(input()) | |
| options[num]() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment