Skip to content

Commit

Permalink
Fix TemplateNotFound error message, extend README (#89)
Browse files Browse the repository at this point in the history
* Fix TemplateNotFound error message, extend README with comment about template paths.
  • Loading branch information
furiousassault authored and dekhtyarev committed Feb 20, 2019
1 parent e9b185b commit 9e9f3b4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ templates
my_file.txt
...
```
Note, `include_file` also support unix glob. You can import all files from directory conf.d/*.conf for example.
> Note, `include_file` also support unix glob. You can import all files from directory conf.d/*.conf for example.
You can put *.j2 templates in 'templates' directory and specify it in config.yaml
```yaml
Expand All @@ -311,6 +311,27 @@ production-zone-1:
templates:
- template: my-deployment.yaml.j2
```
### Template loader path
k8s-handle uses jinja2 template engine and initializes it with base folder specified in the TEMPLATES_DIR env variable.
Jinja environment considers template paths as specified relatively to its base init directory.
Therefore, users **must** specify paths in `{% include %}` (and other) blocks relatively to the base (TEMPLATES_DIR) folder, not relative to the importer template location.
Example
We have the following templates dir content layout:
```
templates /
subdirectory /
template_A.yaml
template_B.yaml
```
In that scheme, if template_A contains jinja2 import of the template_B, that import statement must be
```
{% include "subdirectory/template_B.yaml" %}
```
despite that included template lies as the same level as the template where include is used.
### Tags
If you have a large deployment with many separate parts (for ex. main application and migration job), you can want to deploy them independently. In this case you have two options:
* Use multiple isolated sections (like `production_app`, `production_migration`, etc.)
Expand Down
9 changes: 5 additions & 4 deletions k8s_handle/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ def generate_by_context(self, context):
path = self._generate_file(template, settings.TEMP_DIR, context)
log.info('File "{}" successfully generated'.format(path))
output.append(path)
except TemplateNotFound:
raise TemplateRenderingError('Template "{}" not found'.format(template))
except TemplateNotFound as e:
raise TemplateRenderingError(
"Processing {}: template {} hasn't been found".format(template['template'], e.name))
except (UndefinedError, TemplateSyntaxError) as e:
raise TemplateRenderingError('Unable to render {}, due to: {}'.format(template, e))
return output
Expand All @@ -124,8 +125,8 @@ def _generate_file(self, item, directory, context):
log.info('Trying to generate file from template "{}" in "{}"'.format(item['template'], directory))
template = self._env.get_template(item['template'])
except TemplateNotFound as e:
log.info('Templates path: {}, available templates:{}'.format(self._templates_dir,
self._env.list_templates()))
log.info('Templates path: {}, available templates: {}'.format(self._templates_dir,
self._env.list_templates()))
raise e
except KeyError:
raise RuntimeError('Templates section doesn\'t have any template items')
Expand Down

0 comments on commit 9e9f3b4

Please sign in to comment.