Merge ~jocave/plainbox:expose-env-vars-to-jinja2-templates into plainbox:master

Proposed by Jonathan Cave
Status: Merged
Approved by: Sylvain Pineau
Approved revision: 4705690d128640f4f156599d7f638ddcbd43c60d
Merged at revision: 3809873698670e021e7c9e960357fe4eb0bd353d
Proposed branch: ~jocave/plainbox:expose-env-vars-to-jinja2-templates
Merge into: plainbox:master
Diff against target: 79 lines (+44/-2)
2 files modified
docs/manpages/plainbox-template-units.rst (+35/-1)
plainbox/impl/unit/unit.py (+9/-1)
Reviewer Review Type Date Requested Status
Sylvain Pineau (community) Approve
Review via email: mp+319460@code.launchpad.net

Description of the change

Allow use of system environment variables in all job fields when using the jinja2 template-engine for templating.

To post a comment you must log in.
Revision history for this message
Jonathan Cave (jocave) wrote :

Modified the "additional features" section to address in which engines those variables are available.

Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

one last doc fix, see inline. Otherwise looks good. thanks for digging into plainbox internals!

review: Needs Fixing
Revision history for this message
Maciej Kisielewski (kissiel) wrote :

Two comments below.

Revision history for this message
Jonathan Cave (jocave) wrote :

Applied Maciej's and Sylvain's suggestions.

Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

+1, thx

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/docs/manpages/plainbox-template-units.rst b/docs/manpages/plainbox-template-units.rst
2index 259862f..a737950 100644
3--- a/docs/manpages/plainbox-template-units.rst
4+++ b/docs/manpages/plainbox-template-units.rst
5@@ -171,7 +171,7 @@ that generates a job for each hard drive available on the system::
6 _summary: Check stats changes for each disk
7 id: disk/stats
8 requires: device.category == 'DISK'
9- _description:
10+ _description:
11 This test generates some disk activity and checks the stats to ensure drive
12 activity is being recorded properly.
13 command:
14@@ -206,3 +206,37 @@ After migration to a template unit job, it looks like this::
15 The ``template-resource`` used here (``device``) refers to a resource job using the ``udev_resource`` script to get information about the system. The ``udev_resource`` script returns a list of items with attributes such as ``path`` and ``name``, so we can use these directly in our template.
16
17 We end up with a shorter (from 19 lines to 11!) and more readable template.
18+
19+Simple Jinja templates example
20+------------------------------
21+
22+Jinja2 can be used as the templating engine instead of python string formatting. This allows the author to access some powerful templating features including expressions.
23+
24+First here is the previous disk stats example converted to jinja2::
25+
26+ unit: template
27+ template-resource: device
28+ template-filter: device.category == 'DISK'
29+ template-engine: jinja2
30+ plugin: shell
31+ category_id: 2013.com.canonical.plainbox::disk
32+ id: disk/stats_{{ name }}
33+ requires:
34+ device.path == "{{ path }}"
35+ block_device.{{ name }}_state != 'removable'
36+ user: root
37+ command: disk_stats_test {{ name }}
38+ _description: This test checks {{ name }} disk stats, generates some activity and rechecks stats to verify they've changed. It also verifies that disks appear in the various files they're supposed to.
39+
40+Template engine additional features
41+-----------------------------------
42+
43+Plainbox populates the template parameter dictionary with some extra keys to aid the author.
44+
45+``__index__``:
46+ If a template unit can result in N content jobs then this variable is equal to how many jobs have been created so far.
47+
48+``__system_env__``:
49+ When the plainbox encounters a template to render it will populate this variable with the executing shell's enviroment variables as ``os.environ``
50+
51+ Available for ``template-engine``: ``jinja2``
52diff --git a/plainbox/impl/unit/unit.py b/plainbox/impl/unit/unit.py
53index 3e740e0..a46e5f5 100644
54--- a/plainbox/impl/unit/unit.py
55+++ b/plainbox/impl/unit/unit.py
56@@ -27,6 +27,7 @@ import collections
57 import hashlib
58 import json
59 import logging
60+import os
61 import string
62
63 from jinja2 import Template
64@@ -579,7 +580,14 @@ class Unit(metaclass=UnitType):
65 value = self._data.get(name, default)
66 if value is not None and self.is_parametric:
67 if self.template_engine == 'jinja2':
68- value = Template(value).render(self.parameters)
69+ # Add the current system environment variables to the
70+ # parameters so that they can be used in all fields (i.e. not
71+ # just in the command shell). By adding here rather than in the
72+ # template instantiation we avoid problems with creation of
73+ # checkpoints
74+ tmp_params = self.parameters.copy()
75+ tmp_params.update({'__system_env__': os.environ})
76+ value = Template(value).render(tmp_params)
77 else:
78 value = string.Formatter().vformat(value, (), self.parameters)
79 return value

Subscribers

People subscribed via source and target branches