Skip to content

Commit

Permalink
Fixes #11142: filetemplate technique posthook
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdall committed Jul 19, 2017
1 parent fee26c9 commit fba936d
Show file tree
Hide file tree
Showing 16 changed files with 810 additions and 5 deletions.
24 changes: 19 additions & 5 deletions techniques/fileDistribution/fileTemplate/1.0/fileTemplate.st
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,28 @@ bundle agent fileTemplate {
#Post hook command
&FILE_TEMPLATE_TEMPLATE_POST_HOOK_COMMAND: {command |"posthook[&i&]" string => "&command&";}&

#Post hook persistence
&FILE_TEMPLATE_PERSISTENT_POST_HOOK: {persistence | "persist[&i&]" string => "&persistence&";}&

#Index
"index" slist => getindices("dst");

#Name of the template, from its location
"name[${index}]" string => lastnode("${src[${index}]}", "/");

#canonified names for reporting
#Canonified names for reporting
"src_canon[${index}]" string => canonify("${relative_src}/${src[${index}]}");
"dst_canon[${index}]" string => canonify("${dst[${index}]}");
"temp_canon[${index}]" string => canonify("${temp}/${name[${index}]}");
"temp_dir_canon" string => canonify("${temp}");
"posthook_canon[${index}]" string => canonify("${posthook[${index}]}");

#Cancel persistent classes
pass3::
"cancel" string => "cancel persistence",
classes => cancel_all_classes("posthook_rerun_${index}"),
ifvarclass => "command_execution_${posthook_canon[${index}]}_ok";

classes:
"pass3" expression => "pass2";
"pass2" expression => "pass1";
Expand All @@ -67,7 +76,12 @@ bundle agent fileTemplate {
"posthook_specified_${index}" not => strcmp("${posthook[${index}]}", "");
"file_modified_${index}" expression =>"permissions_${dst_canon[${index}]}_repaired|file_from_template_${dst_canon[${index}]}_repaired";
"posthook_launch_${index}" expression =>"posthook_specified_${index}.file_modified_${index}";
"posthook_not_launch_${index}" expression =>"posthook_specified_${index}.!(file_modified_${index})";
"posthook_persistent_${index}" expression => strcmp("${persist[${index}]}", "true");

#Check if the posthook is persistent and in error, set up a class for 10000 minutes => about 1 week
"posthook_rerun_${index}" expression => "posthook_persistent_${index}.command_execution_${posthook_canon[${index}]}_failed",
scope => "namespace",
persistence => "10000";

methods:
#Ensure the templates are in a safe place
Expand Down Expand Up @@ -135,7 +149,7 @@ bundle agent fileTemplate {
usebundle => command_execution(
"${posthook[${index}]}"
),
ifvarclass => "posthook_launch_${index}";
ifvarclass => "posthook_launch_${index}|posthook_rerun_${index}";

################################################################################
# REPORTING #
Expand Down Expand Up @@ -180,13 +194,13 @@ pass3::
#Case with execution

"any" usebundle => rudder_common_reports_generic("fileTemplate", "command_execution_${posthook_canon[${index}]}", "${trackingkey[${index}]}", "Posthook", "${dst[${index}]}", "The command ${posthook[${index}]} from postHook execution "),
ifvarclass => "posthook_launch_${index}";
ifvarclass => "command_execution_${posthook_canon[${index}]}_reached";


#Case without execution

"report_${index}" usebundle => rudder_common_report("fileTemplate", "result_na", "${trackingkey[${index}]}", "Posthook", "${dst[${index}]}", "No post-modification needed to run"),
ifvarclass => "!file_modified_${index}.posthook_specified_${index}";
ifvarclass => "!file_modified_${index}.posthook_specified_${index}.!command_execution_${posthook_canon[${index}]}_reached";


#Case where no posthook set
Expand Down
18 changes: 18 additions & 0 deletions techniques/fileDistribution/fileTemplate/1.0/metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,24 @@
</CONSTRAINT>
</INPUT>

<SELECT1>
<NAME>FILE_TEMPLATE_PERSISTENT_POST_HOOK</NAME>
<DESCRIPTION>Retry hook on error</DESCRIPTION>
<ITEM>
<VALUE>true</VALUE>
<LABEL>Yes</LABEL>
</ITEM>
<ITEM>
<VALUE>false</VALUE>
<LABEL>False</LABEL>
</ITEM>

<CONSTRAINT>
<DEFAULT>true</DEFAULT>
<MAYBEEMPTY>false</MAYBEEMPTY>
</CONSTRAINT>
</SELECT1>

<SECTION name="Expand template" multivalued="false" component="true" componentKey="FILE_TEMPLATE_AGENT_DESTINATION_PATH">
</SECTION>

Expand Down
72 changes: 72 additions & 0 deletions techniques/fileDistribution/fileTemplate/1.0/tests/loadVars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"displayName": "Variable (string)",
"enabled": true,
"longDescription": "",
"parameters": {
"section": {
"name": "sections",
"sections": [
{
"section": {
"name": "Variable definition",
"vars": [
{
"var": {
"name": "GENERIC_VARIABLE_CONTENT",
"value": "Alice"
}
},
{
"var": {
"name": "GENERIC_VARIABLE_NAME",
"value": "nom1"
}
}
]
}
},
{
"section": {
"name": "Variable definition",
"vars": [
{
"var": {
"name": "GENERIC_VARIABLE_CONTENT",
"value": "1 chemin de la rue"
}
},
{
"var": {
"name": "GENERIC_VARIABLE_NAME",
"value": "adresse1"
}
}
]
}
},
{
"section": {
"name": "Variable definition",
"vars": [
{
"var": {
"name": "GENERIC_VARIABLE_CONTENT",
"value": "Bob"
}
},
{
"var": {
"name": "GENERIC_VARIABLE_NAME",
"value": "nom2"
}
}
]
}
}
]
}
},
"shortDescription": "",
"techniqueName": "genericVariableDefinition",
"techniqueVersion": "2.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{{vars.generic_variable_definition.nom1}}} habite {{{vars.generic_variable_definition.adresse1}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/local/bin/ncf -f
bundle agent init {
vars:
"gen_files" slist => {"test1.conf", "test2.conf", "posthooktest", "persistentPosthooktest"};
"src_files" slist => {"template1", "_tmp_test2_conf.tpl"};
"other_files" slist => {"/tmp/aDirectory"};

methods:
"any" usebundle => command_execution("rm -f /tmp/${gen_files}");
"any" usebundle => command_execution("rm -f /var/rudder/templates/${src_files}");
"any" usebundle => command_execution("rm -Rf ${other_files}");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"displayName": "File content (from remote template) (1,1) (3,1)",
"enabled": true,
"longDescription": "",
"parameters": {
"section": {
"name": "sections",
"sections": [
{
"section": {
"name": "Apply template",
"sections": [
{
"section": {
"name": "Load Template from a file or text input",
"sections": [
{
"section": {
"name": "Expand template"
}
},
{
"section": {
"name": "Posthook"
}
},
{
"section": {
"name": "Put permissions"
}
},
{
"section": {
"name": "Templates directory permissions"
}
},
{
"section": {
"name": "Templates location"
}
}
],
"vars": [
{
"var": {
"name": "FILE_TEMPLATE_AGENT_DESTINATION_PATH",
"value": "/tmp/test3.conf"
}
},
{
"var": {
"name": "FILE_TEMPLATE_GROUP_OWNER",
"value": "root"
}
},
{
"var": {
"name": "FILE_TEMPLATE_OWNER",
"value": "root"
}
},
{
"var": {
"name": "FILE_TEMPLATE_PERMISSIONS",
"value": "700"
}
},
{
"var": {
"name": "FILE_TEMPLATE_PERSISTENT_POST_HOOK",
"value": "false"
}
},
{
"var": {
"name": "FILE_TEMPLATE_RAW_OR_NOT",
"value": "File"
}
},
{
"var": {
"name": "FILE_TEMPLATE_RAW_TEMPLATE",
"value": ""
}
},
{
"var": {
"name": "FILE_TEMPLATE_TEMPLATE",
"value": "lmqoivuqiuvh"
}
},
{
"var": {
"name": "FILE_TEMPLATE_TEMPLATE_POST_HOOK_COMMAND",
"value": ""
}
},
{
"var": {
"name": "FILE_TEMPLATE_TEMPLATE_TYPE",
"value": "mustache"
}
}
]
}
}
]
}
}
]
}
},
"shortDescription": "",
"techniqueName": "fileTemplate",
"techniqueVersion": "1.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[{
"inits": ["test_error_fileTemplate.cf"],
"directives": ["test_error_fileTemplate.json", "loadVars.json"],
"checks": ["test_error_fileTemplate.rb"],
"sharedFiles":["template1"],
"compliance": 66.67
}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "spec_helper"
# Test with an inexistent template file, should failed
describe file("/var/rudder/tmp/templates") do
it { should be_directory }
it { should be_mode 750 }
it { should be_owned_by "root" }
end

describe command("rudder agent run -r") do
its(:stdout) { should match /^R: @@fileTemplate@@result_error@@.*?@@.*?@@.*?@@Load Template from a file or text input@@\/tmp\/test3.conf@@.*?The copy of the file.*?from the policy server to .*? could not be repaired$/m}
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/local/bin/ncf -f
bundle agent init {
}
Loading

0 comments on commit fba936d

Please sign in to comment.