Skip to content

Commit

Permalink
Merge c2215d1 into 95ec90d
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeniy L committed Oct 26, 2015
2 parents 95ec90d + c2215d1 commit 4dd7b39
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
22 changes: 13 additions & 9 deletions solar/solar/core/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import tempfile
import errno

from jinja2 import Template

from solar import utils
from solar.core.log import log
from solar.core.transports.ssh import SSHSyncTransport, SSHRunTransport

Expand Down Expand Up @@ -90,9 +89,11 @@ def _render_action(self, resource, action):
log.debug('action file: %s', action_file)
args = self._make_args(resource)

with open(action_file) as f:
tpl = Template(f.read())
return tpl.render(str=str, zip=zip, **args)
return utils.render_template(
action_file,
str=str,
zip=zip,
**args)

def _render_dir(self, resource, _path):
args = self._make_args(resource)
Expand All @@ -101,10 +102,13 @@ def _render_dir(self, resource, _path):
target_f = f[:-6]
full_target = os.path.join(_path, target_f)
full_src = os.path.join(_path, f)
with open(full_src, 'rb') as orig_f:
with open(full_target, 'wb') as tmpl_f:
tpl = Template(orig_f.read())
tmpl_f.write(tpl.render(str=str, zip=zip, **args))
with open(full_target, 'wb') as tmpl_f:
tpl = utils.render_template(
full_src,
str=str,
zip=zip,
**args)
tmpl_f.write(tpl)
log.debug("Rendered: %s", full_target)
os.remove(full_src)

Expand Down
17 changes: 14 additions & 3 deletions solar/solar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from uuid import uuid4

from jinja2 import Template
from jinja2 import Environment

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -69,9 +69,20 @@ def generate_uuid():
return str(uuid4())


def render_template(template_path, params):
def to_json(data):
return json.dumps(data)


def to_pretty_json(data):
return json.dumps(data, indent=4)


def render_template(template_path, **params):
env = Environment()
env.filters['to_json'] = to_json
env.filters['to_pretty_json'] = to_pretty_json
with io.open(template_path) as f:
temp = Template(f.read())
temp = env.from_string(f.read())

return temp.render(**params)

Expand Down

0 comments on commit 4dd7b39

Please sign in to comment.