Skip to content

Commit

Permalink
Move function get_resoure_tags outside handler function + Add ability…
Browse files Browse the repository at this point in the history
… to define stop/start schedule as URL to public repository
  • Loading branch information
jussi-ft committed Jul 18, 2016
1 parent 849926f commit 00389de
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions ec2_powercycle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/python
# -*- coding: utf-8 -*-

import boto3
import boto3, requests, re
import collections
from datetime import datetime
from time import gmtime, strftime
Expand Down Expand Up @@ -44,7 +44,13 @@ def getDesiredState(json_string):
return 'running'
except Exception, e:
return False


def get_resoure_tags(data):
tags = {}
for item in data:
tmplist = item.values()
tags[tmplist[1]] = tmplist[0]
return tags

def handler(event = False, context = False):
print '### START - ' + strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) + ' ###'
Expand All @@ -58,7 +64,6 @@ def handler(event = False, context = False):
dryrun = True
print 'DryRun is ' + str(dryrun)
except Exception, e:
print 'Failed to load JSON: ' + str(e)
dryrun = False
if len(exclude_env_tags) > 0:
print 'Excluding instances with environment tag values: ' + str(exclude_env_tags)
Expand All @@ -78,21 +83,21 @@ def handler(event = False, context = False):
sys.exit(0)
print "InstanceIDs with tag " + tag + ':'
for element in instances:
print '\t * ' + str(element['InstanceId'])

def get_resoure_tags(data):
tags = {}
for item in data:
tmplist = item.values()
tags[tmplist[1]] = tmplist[0]
return tags
print '\t * ' + str(element['InstanceId'])

for instance in instances:
#print 'instance details'
print '________________'
#pprint.pprint(instance)
resource_tags = get_resoure_tags(instance['Tags'])
try:
if re.search("http",resource_tags[tag]):
try:
print 'Fetching document from ' + resource_tags[tag]
r = requests.get(resource_tags[tag])
resource_tags[tag] = json.dumps(r.json())
except Exception, e:
print 'Failed to load document ' + resource_tags[tag]
desired_state=getDesiredState(resource_tags[tag])
if desired_state == 'stopped' and str(instance['State']['Name']) == 'running':
print 'Instance ' + instance['InstanceId'] + ' business hours are ' + resource_tags[tag]
Expand Down

0 comments on commit 00389de

Please sign in to comment.