summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2017-03-25 20:51:52 +0200
committerLars Wirzenius <liw@liw.fi>2017-03-25 20:51:52 +0200
commitc1ae4298b1b3438243b75f4e2099ed39d3657cf4 (patch)
tree277708b5351695b8002e46dd68eab89fe0265731
parentfa88885ae6c832d0f4eb9bc7d2399de304cdc259 (diff)
downloadvmdb2-c1ae4298b1b3438243b75f4e2099ed39d3657cf4.tar.gz
Make State attributes available to Jinja2
-rw-r--r--vmdb/app.py15
-rw-r--r--vmdb/state.py7
2 files changed, 14 insertions, 8 deletions
diff --git a/vmdb/app.py b/vmdb/app.py
index f29b7b3..a2bd814 100644
--- a/vmdb/app.py
+++ b/vmdb/app.py
@@ -64,7 +64,7 @@ class Vmdb2(cliapp.Application):
for step in steps:
logging.info('Running step: %r', step)
steps_taken.append(step)
- expanded_step = self.expand_step_spec(step)
+ expanded_step = self.expand_step_spec(step, state)
runner = self.step_runners.find(step)
runner.run(expanded_step, self.settings, state)
except Exception as e:
@@ -77,23 +77,24 @@ class Vmdb2(cliapp.Application):
def run_teardowns(self, steps_taken, state):
for step in reversed(steps_taken):
logging.info('Running teardown: %r', step)
- expanded_step = self.expand_step_spec(step)
+ expanded_step = self.expand_step_spec(step, state)
runner = self.step_runners.find(step)
runner.teardown(expanded_step, self.settings, state)
- def expand_step_spec(self, step):
+ def expand_step_spec(self, step, state):
expanded = {}
for key in step:
- expanded[key] = self.expand_jinja2(step[key])
+ expanded[key] = self.expand_jinja2(step[key], state)
return expanded
- def expand_jinja2(self, value):
+ def expand_jinja2(self, value, state):
template = jinja2.Template(value)
- vars = self.create_template_vars()
+ vars = self.create_template_vars(state)
return template.render(**vars)
- def create_template_vars(self):
+ def create_template_vars(self, state):
vars = dict()
for key in self.settings:
vars[key] = self.settings[key]
+ vars.update(state.as_dict())
return vars
diff --git a/vmdb/state.py b/vmdb/state.py
index d522b3c..d9f3840 100644
--- a/vmdb/state.py
+++ b/vmdb/state.py
@@ -18,4 +18,9 @@
class State(object):
- pass
+ def as_dict(self):
+ return {
+ key: getattr(self, key)
+ for key in dir(self)
+ if not key.startswith('_') and not key == 'as_dict'
+ }