Add A Template

Agenda

  • Add Template
  • Update Software Profile
  • Update Instance Profile
  • Verification

This how to describes the process of adding a template to be used by a service. To illustrate the process this tutorial adds a index.html file for our html5as service to provide.

Here is the commit introducing this functionality to html5as Software release: commit diff.

Prerequisites

Access Theia

You can access the Theia using the url and logging in with username and password.

Create index.html.in Template

Create index.html.in in slapos/software/html5as-base/templates/ directory by right-clicking templates and selecting New File.

Edit index.html.in Template

Open index.html.in and copy the script into it:

{% if title %}
<h1>{{ title }}</h1>
{% endif %}
<p>Hello World</p>

Add [template_index_html] section

Add [template_index_html] section into software.cfg:

[template_index_html]
recipe = slapos.recipe.build:download
url = ${:_profile_base_location_}/${:_update_hash_filename_}
_update_hash_filename_ = templates/index.html.in
md5sum = d57cb01df5941e139b02a2f7bdabcdc8
mode = 0644

Make sure the md5sum is correct by running:

$ cd ~/srv/project/slapos/software/html5as-base/
$ md5sum templates/index.html.in

Add context key in template-cfg

Add context key in [template-cfg]

[template-cfg]
context =
  ...
  key template_index_html_target template_index_html:target

Edit instance.cfg.in

link to template

Add it in profile [profile-common] section

[profile-common]
...
template_index_html = {{ template_index_html_target }}

This file is responsible to render the instance file. So it needs to be have the path of the index.html.in template.

Edit instance_html5as.cfg.in

Render template

Simple command to put default index.html content in the docroot

[downloader]
recipe = plone.recipe.command
command = rm -r ${html5as:docroot}/*; cp ${default_index_html:rendered} ${html5as:docroot}/

[default_index_html]
recipe = slapos.recipe.template:jinja2
template = {{ parameter_list['template_index_html'] }}
rendered = ${directory:srv}/index.html
title = {{ parameter_dict['title'] }}
context =
  key title :title

A new section [default_index_html] is added to render the template. It gets the path of the template by being rendered by instance.cfg.in which got its path from software.cfg.

The template is being rendered with the context key title taken from the parameter title.

Update instance_html5as.cfg.in md5sum

in software.cfg

Run the command:

$ cd ~/srv/project/slapos/software/html5as-base/
$ md5sum instance_html5as.cfg.in

Copy the result in the md5sum key of [instance_html5as] section of software.cfg

Update instance.cfg.in md5sum

in software.cfg

Run the command:

$ cd ~/srv/project/slapos/software/html5as-base/
$ md5sum instance.cfg.in

Copy the result in the md5sum key of [template-cfg] section of software.cfg

Validation

  1. Inspect the logs
  2. Verify Index Template
To verify if the template is added, the software is required to re-compile and the instance should be re-instantiated. Then we can access Url provided in connection parameter with curl, the content contains your title in <h1> brackets

Re-compiling and re-instantiating

$ slapos node software --all
$ slapos node instance --all

slapos node software and slapos node instance are the commands for compiling and instantiating. Before instantiating please make sure that the compilation is completed.

Inspect the logs

Verify the output of 

$ slapos node software --all
$ slapos node instance --all

to make sure the compiling and instantiating are completed.

Before instantiating please ensure that the compilation is completed.

Verify Index Template

Call the request script to capture the connection parameters:

$ cd ~/srv/project/slapos/software/html5as-base/
$ bash ../../../request-html5as-base.sh 

Then you can verify the index template from server_url by

$ curl your_server_url

As long as you see the title parameter passed from the request script on the output of curl, it means the new template is taken into account. 

Thank You

  • Nexedi SA
  • 147 Rue du Ballon
  • 59110 La Madeleine
  • France
  • +33629024425

For more information, please contact Jean-Paul, CEO of Nexedi (+33 629 02 44 25).