示例#1
0
myj = json.loads(myjson)  

# Check the return, if empty will be in format {'Reservations': []}
if len(myj["Reservations"]) == 0:
   ec2log.logit("ERROR: The return value is empty, no instances meet the criteria entered", True)
   sys.exit(2)

# Add the returned value to a data structure for later use.
InstanceList = []
ec2lib.UpdateInstanceList(InstanceList, myj)
 
# Process AWS CLI calls ---------------------------------------   
# Start, stop depending on status or get instance id and status
if args.Action == 'status':
   for ec2info in InstanceList:
      ec2lib.printec2Info(ec2info, ec2log)  
elif args.Action == 'stop':
   cmd = 'aws ec2 stop-instances --instance-ids'
   for ec2info in InstanceList:
      if ec2info[1] != 'running':
         ec2log.logit(ec2info[0] + ' is not in a state to stop [current status: ' + ec2info[1] + ']', True)
         continue
      else:
         cmd = cmd + ' ' + ec2info[0]	 
         ec2log.logit("Stopping: " + cmd, True)
         p = ec2lib.RunCliCommand(cmd, True)
elif args.Action == 'start':
   for ec2info in InstanceList:
      if ec2info[1] != 'stopped':
         print(ec2info[0] + ' is not in a state to start [current status: ' + ec2info[1] + ']')
         ec2log.logit(ec2info[0] + ' is not in a state to start [current status: ' + ec2info[1] + ']', True)
示例#2
0
      PublicIp = myj['Reservations'][count]['Instances'][0]['PublicIpAddress']
   except:
      PublicIp = 'NONE'
   Status = myj['Reservations'][count]['Instances'][0]['State']['Name']
   for tag in myj['Reservations'][count]['Instances'][0]['Tags']:
      if (tag['Key'] == 'Name'):
         NameTag = tag['Value']
   ec2lib.addInstance(InstanceId, Status, NameTag, PublicIp, InstanceList)
   count = count + 1

   
# Process AWS CLI calls ---------------------------------------   
# Start, stop depending on status or get instance id and status
if args.Action == 'status':
   for ec2info in InstanceList:
      ec2lib.printec2Info(ec2info)  
elif args.Action == 'stop':
   cmd = 'aws ec2 stop-instances --instance-ids'
   for ec2info in InstanceList:
      if ec2info[1] != 'running':
         print(ec2info[0] + ' is not in a state to stop [current status: ' + ec2info[1] + ']')
         sys.exit(2)
      else:
         cmd = cmd + ' ' + ec2info[0]	 
   print("Stopping: " + cmd)
   p = Popen(cmd, shell=True,
                 stdout=PIPE,
                 stderr=STDOUT)
   ec2lib.formatReturn(p.communicate()) # Display return values
elif args.Action == 'start':
   cmd = 'aws ec2 start-instances --instance-ids'