Improve the ordering of stopping resources

Create a common function to generate the start order
and stop in the reverse order.

Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
This commit is contained in:
Angus Salkeld 2012-04-10 08:57:55 +10:00
parent c51273efaa
commit a9fd72c21c
1 changed files with 39 additions and 14 deletions

View File

@ -72,9 +72,9 @@ class Stack:
def validate(self): def validate(self):
''' '''
If you are wondering where the actual validation is, me too. If you are wondering where the actual validation is, me too.
it is just not obvious how to respond to validation failures. it is just not obvious how to respond to validation failures.
http://docs.amazonwebservices.com/AWSCloudFormation/latest/APIReference/API_ValidateTemplate.html http://docs.amazonwebservices.com/AWSCloudFormation/latest/APIReference/API_ValidateTemplate.html
''' '''
response = { 'ValidateTemplateResult': { response = { 'ValidateTemplateResult': {
'Description': 'bla', 'Description': 'bla',
@ -98,23 +98,48 @@ class Stack:
response['ValidateTemplateResult']['Parameters'].append(res) response['ValidateTemplateResult']['Parameters'].append(res)
return response return response
def start(self): def resource_append_deps(self, resource, order_list):
# start Volumes first. '''
For the given resource first append it's dependancies then
it's self to order_list.
'''
for r in resource.depends_on:
self.resource_append_deps(self.resources[r], order_list)
if not resource.name in order_list:
order_list.append(resource.name)
def get_start_order(self):
'''
return a list of Resource names in the correct order
for startup.
'''
order = []
for r in self.t['Resources']: for r in self.t['Resources']:
if self.t['Resources'][r]['Type'] == 'AWS::EC2::Volume': if self.t['Resources'][r]['Type'] == 'AWS::EC2::Volume' or \
self.resources[r].start() self.t['Resources'][r]['Type'] == 'AWS::EC2::EIP':
if len(self.resources[r].depends_on) == 0:
order.append(r)
for r in self.t['Resources']: for r in self.t['Resources']:
#print 'calling start [stack->%s]' % (self.resources[r].name) self.resource_append_deps(self.resources[r], order)
return order
def start(self):
'''
start all the resources in the order specified by get_start_order
'''
order = self.get_start_order()
for r in order:
self.resources[r].start() self.resources[r].start()
def stop(self): def stop(self):
# stop VolumeAttachment's first '''
for r in self.t['Resources']: stop all the resources in the reverse order specified by get_start_order
if self.t['Resources'][r]['Type'] == 'AWS::EC2::VolumeAttachment': '''
self.resources[r].stop() order = self.get_start_order()
order.reverse()
for r in self.t['Resources']: for r in order:
self.resources[r].stop() self.resources[r].stop()
def get_outputs(self): def get_outputs(self):