Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #9022: Add jinja2 templating support to ncf #447

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 76 additions & 0 deletions tests/acceptance/30_generic_methods/file_from_template_jinja2.cf
@@ -0,0 +1,76 @@
#######################################################
#
# Test checking if a file can be generated from a local template
#
#######################################################

bundle common acc_path
{
vars:
"root" string => getenv("NCF_TESTS_ACCEPTANCE", 1024);
}

body common control
{
inputs => { "${acc_path.root}/default.cf.sub", "${acc_path.root}/default_ncf.cf.sub", "@{ncf_inputs.default_files}" };
bundlesequence => { configuration, default("${this.promise_filename}") };
version => "1.0";
}

#######################################################

bundle agent init
{
vars:
"tmp" string => getenv("TEMP", 1024);
"source_template" string => "${tmp}/source_template";
"destination_file" string => "${tmp}/destination_test";
"destination_file_canon" string => canonify("${destination_file}");

files:
"${source_template}"
create => "true",
edit_line => insert_lines("sys.host is: {{ vars.sys.host }}");

}

#######################################################

bundle agent test
{
methods:
"ph1" usebundle => file_from_template_jinja2("${init.source_template}", "${init.destination_file}");
}

#######################################################

bundle agent check
{
vars:
"file_length_check" string => "/usr/bin/test `/bin/cat ${init.destination_file} | wc -l` = '1'";
"file_content_check" string => "/bin/cat ${init.destination_file} | head -n1 | grep '^sys.host is: ${sys.host}$'";

classes:
# By default, should create the file if it doesn't exist
"file_exists" expression => fileexists("${init.destination_file}");
"file_length_ok"
expression => returnszero("${file_length_check}", "useshell"),
ifvarclass => canonify("file_ensure_lines_present_${init.destination_file}_reached");
"file_content_ok"
expression => returnszero("${file_content_check}", "useshell"),
ifvarclass => canonify("file_ensure_lines_present_${init.destination_file}_reached");

"ok" expression => "file_exists.file_length_ok.file_content_ok.file_ensure_lines_present_${init.destination_file_canon}_ok.!file_ensure_lines_present_${init.destination_file_canon}_error";

reports:
ok::
"$(this.promise_filename) Pass";
!ok::
"$(this.promise_filename) FAIL :/usr/bin/test `/bin/cat ${init.destination_file} | wc -l` = '1':/bin/cat ${init.destination_file} | head -n1 | grep '^sys.host is: ${sys.host}$'";
!file_exists::
"File ${init.destination_file} didn't appear to exist";
!file_length_ok::
"There was not exactly one line in ${init.destination_file}";
!file_content_ok::
"The file content in ${init.destination_file} was not exactly \"sys.host is: ${sys.host}\"";
}
44 changes: 44 additions & 0 deletions tree/30_generic_methods/file_from_template_jinja2.cf
@@ -0,0 +1,44 @@
#####################################################################################
# Copyright 2014 Normation SAS
#####################################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, Version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#####################################################################################

# @name File from a mustache template
# @description This is a bundle to build a file from a mustache template
# @agent_version >=3.6
#
# @parameter source_template Source file containing a template to be expanded
# @parameter destination Destination file
#
# @class_prefix file_from_template
# @class_parameter destination

bundle agent file_from_template_jinja2(source_template, destination)
{

vars:
"jinja2_script" string => "/home/amousset/JINJA/cli.py";
"datatstate_file" string => "/tmp/last-datatstate.json";
state_dumped::
"content" string => execresult("/usr/bin/python ${jinja2_script} --strict ${source_template} ${datatstate_file}", "noshell");

classes:
"state_dumped" expression => dumpdatastate("${datatstate_file}");

methods:
"check to content of the file" usebundle => file_enforce_content("${destination}", "${content}", "true");

}