From c974f707b365224fb5d52b874523ddfec4fb6a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Peccatte?= Date: Fri, 8 Aug 2014 18:33:46 +0200 Subject: [PATCH] Fixes #5378: We need a method for the mustache template --- .../file_from_template_mustache.cf | 76 +++++++++++++++++++ tree/30_generic_methods/file_from_template.cf | 14 +--- .../file_from_template_mustache.cf | 33 ++++++++ .../file_from_template_type.cf | 45 +++++++++++ 4 files changed, 156 insertions(+), 12 deletions(-) create mode 100644 tests/acceptance/30_generic_methods/file_from_template_mustache.cf create mode 100644 tree/30_generic_methods/file_from_template_mustache.cf create mode 100644 tree/30_generic_methods/file_from_template_type.cf diff --git a/tests/acceptance/30_generic_methods/file_from_template_mustache.cf b/tests/acceptance/30_generic_methods/file_from_template_mustache.cf new file mode 100644 index 000000000..9ddea3d77 --- /dev/null +++ b/tests/acceptance/30_generic_methods/file_from_template_mustache.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 => { 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_mustache("${init.source_template}", "${init.destination_file}"); +} + +####################################################### + +bundle agent check +{ + vars: + "file_length_check" string => "/bin/cat ${init.destination_file} | wc -l"; + "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 => strcmp(execresult("${file_length_check}", "useshell"), "1"), + ifvarclass => canonify("file_from_template_${init.destination_file}_reached"); + "file_content_ok" + expression => returnszero("${file_content_check}", "useshell"), + ifvarclass => canonify("file_from_template_${init.destination_file}_reached"); + + "ok" expression => "file_exists.file_length_ok.file_content_ok.file_from_template_${init.destination_file_canon}_ok.!file_from_template_${init.destination_file_canon}_error"; + + reports: + ok:: + "$(this.promise_filename) Pass"; + !ok:: + "$(this.promise_filename) FAIL"; + !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}\""; +} diff --git a/tree/30_generic_methods/file_from_template.cf b/tree/30_generic_methods/file_from_template.cf index 48d28fd72..105950028 100644 --- a/tree/30_generic_methods/file_from_template.cf +++ b/tree/30_generic_methods/file_from_template.cf @@ -1,5 +1,5 @@ ##################################################################################### -# Copyright 2013 Normation SAS +# Copyright 2014 Normation SAS ##################################################################################### # # This program is free software: you can redistribute it and/or modify @@ -28,16 +28,6 @@ bundle agent file_from_template(source_template, destination) { - vars: - "class_prefix" string => canonify("file_from_template_${destination}"); - - files: - "${destination}" - create => "true", - edit_template => "${source_template}", - classes => classes_generic("${class_prefix}"); - methods: - "report" usebundle => _logger("Build file ${destination} from template ${source_template}", "${class_prefix}"); - + "placeholder" usebundle => file_from_template_type("${source_template}", "${destination}", "cfengine"); } diff --git a/tree/30_generic_methods/file_from_template_mustache.cf b/tree/30_generic_methods/file_from_template_mustache.cf new file mode 100644 index 000000000..28413b9cf --- /dev/null +++ b/tree/30_generic_methods/file_from_template_mustache.cf @@ -0,0 +1,33 @@ +##################################################################################### +# 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 . +# +##################################################################################### + +# @name File from a mustache template +# @description This is a bundle to build a file from a mustache template +# +# @parameter source_template Source file containing a template to be expanded +# @parameter destination Destination file +# +# @class_prefix file_from_template +# @class_parameter destination +# This bundle will define a class file_from_template_${destination}_{kept,repaired,error,ok,reached} + +bundle agent file_from_template_mustache(source_template, destination) +{ + methods: + "placeholder" usebundle => file_from_template_type("${source_template}", "${destination}", "mustache"); +} diff --git a/tree/30_generic_methods/file_from_template_type.cf b/tree/30_generic_methods/file_from_template_type.cf new file mode 100644 index 000000000..b9e4b5251 --- /dev/null +++ b/tree/30_generic_methods/file_from_template_type.cf @@ -0,0 +1,45 @@ +##################################################################################### +# 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 . +# +##################################################################################### + +# @name File from template with type +# @description This is a bundle to build a file from a template +# +# @parameter source_template Source file containing a template to be expanded +# @parameter destination Destination file +# @parameter template_type Template type (cfengine or mustache) +# +# @class_prefix file_from_template +# @class_parameter destination +# This bundle will define a class file_from_template_${destination}_{kept,repaired,error,ok,reached} + +bundle agent file_from_template_type(source_template, destination, template_type) +{ + vars: + "class_prefix" string => canonify("file_from_template_${destination}"); + + files: + "${destination}" + create => "true", + edit_template => "${source_template}", + template_method => "${template_type}", + classes => classes_generic("${class_prefix}"); + + methods: + "report" usebundle => _logger("Build file ${destination} from ${template_type} template ${source_template}", "${class_prefix}"); + +}