Browse Source

Remove parsed template from database

It is no longer used since we now store the parameters and can recreate the
parsed template from the raw template.

Change-Id: Id725bcde7b30ce33d122ccf9c6d5c571f7d754da
Signed-off-by: Zane Bitter <zbitter@redhat.com>
changes/21/9321/1
Zane Bitter 10 years ago
parent
commit
0a6ac9895c
  1. 12
      heat/db/api.py
  2. 35
      heat/db/sqlalchemy/api.py
  3. 49
      heat/db/sqlalchemy/migrate_repo/versions/009_remove_parsed_template.py
  4. 16
      heat/db/sqlalchemy/models.py
  5. 15
      heat/engine/manager.py
  6. 47
      heat/engine/parser.py
  7. 1
      heat/engine/resources.py
  8. 7
      heat/engine/stack.py
  9. 12
      heat/tests/test_stacks.py
  10. 5
      heat/tests/test_waitcondition.py

12
heat/db/api.py

@ -62,18 +62,6 @@ def raw_template_create(context, values):
return IMPL.raw_template_create(context, values)
def parsed_template_get(context, template_id):
return IMPL.parsed_template_get(context, template_id)
def parsed_template_get_all(context):
return IMPL.parsed_template_get_all(context)
def parsed_template_create(context, values):
return IMPL.parsed_template_create(context, values)
def resource_get(context, resource_id):
return IMPL.resource_get(context, resource_id)

35
heat/db/sqlalchemy/api.py

@ -58,28 +58,6 @@ def raw_template_create(context, values):
return raw_template_ref
def parsed_template_get(context, template_id):
result = model_query(context, models.ParsedTemplate).\
filter_by(id=template_id).first()
return result
def parsed_template_get_all(context):
results = model_query(context, models.ParsedTemplate).all()
if not results:
raise NotFound('no parsed templates were found')
return results
def parsed_template_create(context, values):
parsed_template_ref = models.ParsedTemplate()
parsed_template_ref.update(values)
parsed_template_ref.save()
return parsed_template_ref
def resource_get(context, resource_id):
result = model_query(context, models.Resource).\
filter_by(id=resource_id).first()
@ -184,21 +162,14 @@ def stack_delete(context, stack_id):
for e in s.events:
session.delete(e)
rpt = {}
rrt = {}
for r in s.resources:
rpt[r.parsed_template.id] = r.parsed_template
rrt[r.parsed_template.raw_template.id] = \
r.parsed_template.raw_template
session.delete(r)
for pt in rpt.values():
session.delete(pt)
for rt in rrt.values():
session.delete(rt)
rt = s.raw_template
session.delete(s)
session.delete(rt)
session.flush()

49
heat/db/sqlalchemy/migrate_repo/versions/009_remove_parsed_template.py

@ -0,0 +1,49 @@
from sqlalchemy import *
from migrate import *
def upgrade(migrate_engine):
meta = MetaData(bind=migrate_engine)
def fk_name(table, ref_column):
for fk in table.foreign_keys:
if fk.column == ref_column:
return fk.name
resource = Table('resource', meta, autoload=True)
parsed_template = Table('parsed_template', meta, autoload=True)
res_kc = ForeignKeyConstraint([resource.c.parsed_template_id],
[parsed_template.c.id],
name=fk_name(resource,
parsed_template.c.id))
try:
res_kc.drop()
except NotSupportedError:
# SQLite (used in unit tests) cannot drop a Foreign Key constraint
pass
resource.c.parsed_template_id.drop()
parsed_template.drop()
def downgrade(migrate_engine):
meta = MetaData(bind=migrate_engine)
raw_template = Table('raw_template', meta, autoload=True)
parsed_template = Table(
'parsed_template', meta,
Column('id', Integer, primary_key=True),
Column('created_at', DateTime(timezone=False)),
Column('updated_at', DateTime(timezone=False)),
Column('raw_template_id', Integer, ForeignKey("raw_template.id"),
nullable=False),
Column('template', Text())
)
parsed_template.create()
resource = Table('resource', meta, autoload=True)
Column('parsed_template_id', Integer, ForeignKey('parsed_template.id'),
nullable=True).create(resource)

16
heat/db/sqlalchemy/models.py

@ -132,18 +132,6 @@ class RawTemplate(BASE, HeatBase):
__tablename__ = 'raw_template'
id = Column(Integer, primary_key=True)
template = Column(Json)
parsed_template = relationship("ParsedTemplate",
uselist=False, backref="raw_template")
class ParsedTemplate(BASE, HeatBase):
"""Represents a parsed template."""
__tablename__ = 'parsed_template'
id = Column(Integer, primary_key=True)
template = Column(Json)
raw_template_id = Column(Integer, ForeignKey('raw_template.id'),
nullable=False)
class Stack(BASE, HeatBase):
@ -217,10 +205,6 @@ class Resource(BASE, HeatBase):
state_description = Column('state_description', String)
# odd name as "metadata" is reserved
rsrc_metadata = Column('rsrc_metadata', Json)
parsed_template_id = Column(Integer, ForeignKey('parsed_template.id'),
nullable=True)
parsed_template = relationship(ParsedTemplate,
backref=backref('resources'))
stack_id = Column(Integer, ForeignKey('stack.id'),
nullable=False)

15
heat/engine/manager.py

@ -106,8 +106,8 @@ class EngineManager(manager.Manager):
return res
for s in stacks:
ps = parser.Stack(context, s.name,
s.raw_template.parsed_template.template,
s.id, _extract_user_params(params))
s.raw_template.template,
s.id, s.parameters)
mem = {}
mem['StackId'] = "/".join([s.name, str(s.id)])
mem['StackName'] = s.name
@ -145,8 +145,8 @@ class EngineManager(manager.Manager):
s = db_api.stack_get_by_name(context, stack)
if s:
ps = parser.Stack(context, s.name,
s.raw_template.parsed_template.template,
s.id, _extract_user_params(params))
s.raw_template.template,
s.id, s.parameters)
mem = {}
mem['StackId'] = "/".join([s.name, str(s.id)])
mem['StackName'] = s.name
@ -219,13 +219,6 @@ class EngineManager(manager.Manager):
new_s = db_api.stack_create(context, s)
stack.id = new_s.id
pt = {}
pt['template'] = stack.t
pt['raw_template_id'] = new_rt.id
new_pt = db_api.parsed_template_create(None, pt)
stack.parsed_template_id = new_pt.id
greenpool.spawn_n(stack.create, **_extract_args(params))
return {'StackId': "/".join([new_s.name, str(new_s.id)])}

47
heat/engine/parser.py

@ -44,7 +44,6 @@ class Stack(object):
self.res = {}
self.doc = None
self.name = stack_name
self.parsed_template_id = 0
self.metadata_server = metadata_server
# Default Parameters
@ -148,36 +147,6 @@ class Stack(object):
response['ValidateTemplateResult']['Parameters'].append(res)
return response
def parsed_template_get(self):
stack = None
if self.parsed_template_id == 0:
if self.id == 0:
stack = db_api.stack_get(self.context, self.id)
else:
stack = db_api.stack_get_by_name(self.context, self.name)
if stack is None:
return None
self.parsed_template_id = stack.raw_template.parsed_template.id
return db_api.parsed_template_get(self.context,
self.parsed_template_id)
def update_parsed_template(self):
'''
Update the parsed template after each resource has been
created, so commands like describe will work.
'''
pt = self.parsed_template_get()
if pt:
template = self.t.copy()
template['Resources'] = dict((k, r.parsed_template())
for (k, r) in self.resources.items())
pt.update_and_save({'template': template})
else:
logger.warn('Cant find parsed template to update %d' %
self.parsed_template_id)
def state_set(self, new_status, reason='change in resource state'):
if self.id != 0:
stack = db_api.stack_get(self.context, self.id)
@ -214,11 +183,6 @@ class Stack(object):
reason = 'Resource %s failed with: %s' % (str(res),
result)
try:
self.update_parsed_template()
except Exception as ex:
logger.exception('update_parsed_template')
else:
res.state_set(res.CREATE_FAILED,
'Stack creation aborted')
@ -271,12 +235,6 @@ class Stack(object):
stop resource_name and all that depend on it
start resource_name and all that depend on it
'''
if self.parsed_template_id == 0:
stack = db_api.stack_get(self.context, self.id)
if stack:
self.parsed_template_id = stack.raw_template.parsed_template.id
deps = self.dependencies[self[resource_name]]
failed = False
@ -296,11 +254,6 @@ class Stack(object):
except Exception as ex:
logger.exception('create')
failed = True
try:
self.update_parsed_template()
except Exception as ex:
logger.exception('update_parsed_template')
else:
res.state_set(res.CREATE_FAILED)
# TODO(asalkeld) if any of this fails we Should

1
heat/engine/resources.py

@ -213,7 +213,6 @@ class Resource(object):
try:
rs = {'state': self.state,
'stack_id': self.stack.id,
'parsed_template_id': self.stack.parsed_template_id,
'nova_instance': self.instance_id,
'name': self.name,
'rsrc_metadata': metadata,

7
heat/engine/stack.py

@ -54,7 +54,7 @@ class Stack(Resource):
raise exception.NotFound('Nested stack not found in DB')
n = parser.Stack(self.stack.context, st.name,
st.raw_template.parsed_template.template,
st.raw_template.template,
self.instance_id, self._params())
self._nested = n
@ -83,11 +83,6 @@ class Stack(Resource):
new_s = db_api.stack_create(None, s)
self._nested.id = new_s.id
pt = {'template': self._nested.t, 'raw_template_id': new_rt.id}
new_pt = db_api.parsed_template_create(None, pt)
self._nested.parsed_template_id = new_pt.id
self._nested.create()
self.instance_id_set(self._nested.id)

12
heat/tests/test_stacks.py

@ -83,10 +83,6 @@ class stacksTest(unittest.TestCase):
s['username'] = ct['username']
new_s = db_api.stack_create(None, s)
stack.id = new_s.id
pt = {}
pt['template'] = stack.t
pt['raw_template_id'] = new_rt.id
new_pt = db_api.parsed_template_create(None, pt)
stack.create()
self.assertNotEqual(stack.resources['WebServer'], None)
self.assertTrue(stack.resources['WebServer'].instance_id > 0)
@ -113,10 +109,6 @@ class stacksTest(unittest.TestCase):
s['username'] = ct['username']
new_s = db_api.stack_create(None, s)
stack.id = new_s.id
pt = {}
pt['template'] = stack.t
pt['raw_template_id'] = new_rt.id
new_pt = db_api.parsed_template_create(None, pt)
stack.create()
self.assertNotEqual(stack.resources['WebServer'], None)
@ -165,10 +157,6 @@ class stacksTest(unittest.TestCase):
s['username'] = ct['username']
new_s = db_api.stack_create(ctx, s)
stack.id = new_s.id
pt = {}
pt['template'] = stack.t
pt['raw_template_id'] = new_rt.id
new_pt = db_api.parsed_template_create(ctx, pt)
instances.Instance.nova().AndReturn(self.fc)
self.m.ReplayAll()
stack.create()

5
heat/tests/test_waitcondition.py

@ -66,12 +66,7 @@ class stacksTest(unittest.TestCase):
s['username'] = ct['username']
new_s = db_api.stack_create(None, s)
stack.id = new_s.id
pt = {}
pt['template'] = stack.t
pt['raw_template_id'] = new_rt.id
new_pt = db_api.parsed_template_create(None, pt)
stack.parsed_template_id = new_pt.id
return stack
def test_post_success_to_handle(self):

Loading…
Cancel
Save