Don't delete RawTemplate if it referenced by a stack

When creating a nested stack and pre-storing the raw template, we
previously used a try/finally block to ensure that if the stack creation
was never started (e.g. because the template failed validation at the other
end) that we would delete the raw template again rather than leave it lying
around in the DB. However, this catches too broad a range of exceptions. If
an exception such as GreenletExit occurs inside this block, then we will
attempt to delete the raw template, resulting in a DB IntegrityError either
here (because the template id is already referenced by a stack) or when
trying to store the stack (because the template is gone).

Resolve this by only doing the cleanup on HeatExceptions, which indicates
that the other engine has bailed out without storing the stack.

Change-Id: Ie24a82b7ce16281c72d4eb66cca8683248628b46
Closes-Bug: #1626256
(cherry picked from commit 55c21c18e6)
This commit is contained in:
Zane Bitter 2016-09-21 14:57:22 -04:00 committed by Thomas Herve
parent 1cfa30eb08
commit c64e5d5db9
1 changed files with 8 additions and 10 deletions

View File

@ -294,14 +294,14 @@ class StackResource(resource.Resource):
'parent_resource_name': self.name
})
with self.translate_remote_exceptions:
result = None
try:
result = self.rpc_client()._create_stack(self.context,
**kwargs)
finally:
if adopt_data is None and not result:
raw_template.RawTemplate.delete(self.context,
kwargs['template_id'])
except exception.HeatException:
with excutils.save_and_reraise_exception():
if adopt_data is None:
raw_template.RawTemplate.delete(self.context,
kwargs['template_id'])
self.resource_id_set(result['stack_id'])
@ -495,12 +495,10 @@ class StackResource(resource.Resource):
'args': {rpc_api.PARAM_TIMEOUT: timeout_mins}
})
with self.translate_remote_exceptions:
result = None
try:
result = self.rpc_client()._update_stack(self.context,
**kwargs)
finally:
if not result:
self.rpc_client()._update_stack(self.context, **kwargs)
except exception.HeatException:
with excutils.save_and_reraise_exception():
raw_template.RawTemplate.delete(self.context,
kwargs['template_id'])
return cookie