Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset committed May 12, 2021
1 parent 36dac5b commit e26b1b9
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 65 deletions.
@@ -1,13 +1,3 @@
#######################################################
#
# Read a file into a string
# Currently in staging since the method does not behave
# as intended. The fix need to deprecate this one and
# develop a new one. Until then, the test will be set
# to staging.
#
#######################################################

bundle common acc_path
{
vars:
Expand All @@ -28,23 +18,23 @@ bundle agent init
vars:
"tmp" string => getenv("TEMP", 1024);

"file[0]" string => "${tmp}/etc/hosts0";
"value[0]" string => "127.0.0.1";
"lens[0]" string => "Hosts";
"path[0]" string => "/etc/hosts/1/ipaddr";
"file[0]" string => "${tmp}/etc/sshd_config0";
"value[0]" string => "yes";
"lens[0]" string => "Sshd";
"path[0]" string => "${tmp}/etc/sshd_config0/PermitRootLogin";
"status[0]" string => "success";
"mode[0]" string => "enforce";

"file[1]" string => "${tmp}/etc/hosts1";
"value[1]" string => "127.0.0.1";
"lens[1]" string => "";
"path[1]" string => "/etc/hosts/1/ipaddr";
"status[1]" string => "error";
"file[1]" string => "${tmp}/etc/sshd_config1";
"value[1]" string => "no";
"lens[1]" string => "Sshd";
"path[1]" string => "${tmp}/etc/sshd_config1/PermitRootLogin";
"status[1]" string => "repaired";
"mode[1]" string => "enforce";

#file is null but the lens should be enough for augeas
"file[2]" string => "";
"value[2]" string => "127.0.0.1";
"value[2]" string => "127.0.0.1";
"lens[2]" string => "Hosts";
"path[2]" string => "/etc/hosts/1/ipaddr";
"status[2]" string => "success";
Expand All @@ -60,8 +50,8 @@ bundle agent init
"indices" slist => getindices("file");

files:
"${tmp}/etc/hosts${indices}"
copy_from => local_cp("/etc/hosts");
"${tmp}/etc/sshd_config${indices}"
copy_from => local_cp("${this.promise_dirname}/file_augeas_set.sshd_config_yes");
}

#######################################################
Expand All @@ -74,8 +64,8 @@ bundle agent test
methods:
"ph0" usebundle => apply_gm("file_augeas_set", @{args0}, "${init.status[0]}", "ph0", "${init.mode[0]}");
"ph1" usebundle => apply_gm("file_augeas_set", @{args1}, "${init.status[1]}", "ph1", "${init.mode[1]}");
"ph2" usebundle => apply_gm("file_augeas_set", @{args2}, "${init.status[2]}", "ph2", "${init.mode[2]}");
"ph3" usebundle => apply_gm("file_augeas_set", @{args3}, "${init.status[3]}", "ph3", "${init.mode[3]}");
# "ph2" usebundle => apply_gm("file_augeas_set", @{args2}, "${init.status[2]}", "ph2", "${init.mode[2]}");
# "ph3" usebundle => apply_gm("file_augeas_set", @{args3}, "${init.status[3]}", "ph3", "${init.mode[3]}");
}

#######################################################
Expand All @@ -84,7 +74,7 @@ bundle agent check
{
classes:

"classes_ok" expression => "ph0_ok.ph1_ok.ph2_ok.ph3_ok";
"classes_ok" expression => "ph0_ok.ph1_ok";

"ok" expression => "classes_ok";

Expand Down
@@ -0,0 +1,7 @@
# Authentication:

#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
@@ -0,0 +1,7 @@
# Authentication:

#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
113 changes: 73 additions & 40 deletions tree/30_generic_methods/file_augeas_set.cf
Expand Up @@ -17,7 +17,7 @@
#####################################################################################
#
# @name File Augeas set
# @description Use Augeas binaries to call Augtool commands and options to set a node label's value.
# @description Use augeas commands and options to set a node label's value.
#
# @documentation Augeas is a tool that provides an abstraction layer for all the complexities that turn around editing files with regular expressions.
# It's a tree based hierarchy tool, that handles system configuration files where you can securely modify your files and to do so you have to provide
Expand All @@ -27,16 +27,18 @@
#
# In this method, we introduce using augtool commands and options in order to set the value of a given node's label in the parameters
# (which means that you simply want to modify your configuration file), this can be done by specifying the path to it.
# The method has in total 4 parameters: path, value, lens and hosts.
#
# Actually there is two ways you can use this method, either you simply provide a path to the node's label as a parameter or you specify a file
# Actually there are two ways you can use this method, either you simply provide a path to the node's label as a parameter or you specify a file
# associated with a lens then you put the regular path. When you only specify the path to the node's label, your request will includes by default Augeas
# charging all the lenses and files, on the other hand, if you have a specific file for example such as a Json file and you want to associate it
# to the existing Json lens, then in that case, you need to fill in addition the file and the lens parameter, this way Augeas won't load all its files and
# lenses except the ones you have specified.
#
# The generic method will set a node label's value on the agent, otherwise, if Augeas isn't installed on the agent, it will produces an error.
# The method provides a way to make a backup of the file you modified before applying any changes on the node, you can find them in the '/var/rudder/modified-files/' directory.
# *Warning*: When you don't specify the file and lens to use, no backup of the file is made before
# editing it.
#
# *Note*: Specifying the file and lens makes the edition way faster as it avoids having to load
# all the default lenses and files.
#
# #### Two uses cases examples:
#
Expand All @@ -56,58 +58,89 @@
#
# @parameter path The path to the file and node label
# @parameter value The value to set
# @parameter lens The lens specified by the user in case he wants to load a specified lens associated with its file
# @parameter file The file specified by the user in case he wants to load a specified file associated with its lens
# @parameter lens Load a specific lens (optional)
# @parameter file Load a specific file (optional)
# @parameter_constraint file "allow_empty_string" : true
# @parameter_constraint lens "allow_empty_string" : true
# @class_prefix file_augeas_set
# @class_parameter path


bundle agent file_augeas_set(path, value, lens, file)
{
vars:
"old_class_prefix" string => canonify("file_augeas_set_${path}");
"args" slist => {"${path}", "${value}", "${lens}","${file}"};
"report_param" string => join("_", args);
"class_prefix" string => canonify("file_augeas_set_${report_param}");

pass1.file_defined::
"result" string => "printf \"set /augeas/load/${lens}/lens \"${lens}.lns\" \n set /augeas/load/${lens}/incl \"${file}\" \n load \n set /files${path} ${value} \n save\" | ${ncf_paths.path[augtool]} --noautoload -b";
"timestamp" string => execresult("LANG=C date +%s","useshell");
"date" string => canonify(execresult("LANG=C date +\"%a_%b_%e_%H_%M_%S_%Y\"","useshell"));
"cfile" string => canonify("${file}");

pass1.!file_defined::
"result" string => "printf \"set /files${path} ${value} \n save\" | ${ncf_paths.path[augtool]}";

classes:
"pass2" expression => "pass1";
"pass1";

pass2::
"variable_defined" expression => returnszero("${result}","useshell");
"copy" expression => returnszero("mv ${file}.augsave /var/rudder/modified-files/${cfile}_$(timestamp)_$(date)_cf_before_edit","useshell"),
if => fileexists("${file}.augsave");
"augeas_binaries" expression => fileexists("${ncf_paths.path[augtool]}");

pass1::
"file_defined" expression => not(strcmp("${file}",""));

methods:
pass2.augeas_binaries.variable_defined::
vars:
"old_class_prefix" string => canonify("file_augeas_set_${path}");
"args" slist => {"${path}", "${value}", "${lens}","${file}"};
"report_param" string => join("_", args);
"class_prefix" string => canonify("file_augeas_set_${report_param}");

"cpath" string => canonify("${path}");

pass1.file_defined::
"command" string => "printf \"set /augeas/load/${lens}/lens \"${lens}.lns\" \n set /augeas/load/${lens}/incl \"${file}\" \n load \n set /files${path} ${value} \n save\" | ${ncf_paths.path[augtool]} --noautoload --new";
"cfile" string => canonify("${file}");

pass1.!file_defined::
"command" string => "printf \"set /files${path} ${value} \n save\" | ${ncf_paths.path[augtool]}";

classes:
"pass3" expression => "pass2";
"pass2" expression => "pass1";
"pass1";

"should_report" expression => "${report_data.should_report}";

pass1::
"augeas_binaries" expression => fileexists("${ncf_paths.path[augtool]}");
"file_defined" expression => not(strcmp("${file}",""));

pass3::
"repaired" expression => regcmp("Saved.*", "${rudder_augeas.${cpath}}");
"aug_ok" expression => "variable_string_from_command_${cpath}_ok";
"cp_ok" expression => "file_from_local_source_${cfile}_ok";
"ok" expression => "aug_ok",
if => "!file_defined";
"ok" expression => "aug_ok.(!repaired|cp_ok)",
if => "file_defined";

methods:
pass2::
"disable_reporting_${class_prefix}" usebundle => disable_reporting;

"run" usebundle => variable_string_from_command("rudder_augeas", "${cpath}", "${command}");
# allows automatic backup in case file path was provided
"copy file" usebundle => file_from_local_source("${file}.augnew.*", "${file}"),
if => "file_defined.repaired";

# FIXME remove augnew

"reenable_reporting_${class_prefix}" usebundle => enable_reporting,
if => "should_report";

pass3.augeas_binaries.ok.!repaired::
"success" usebundle => _classes_success("${old_class_prefix}");
"success" usebundle => _classes_success("${class_prefix}");
"report" usebundle => _log("Setting the node value for ${path}", "${old_class_prefix}", "${class_prefix}", @{args});

pass2.augeas_binaries.!variable_defined::
pass3.augeas_binaries.ok.repaired::
"repaired" usebundle => _classes_repaired("${old_class_prefix}");
"repaired" usebundle => _classes_repaired("${class_prefix}");
"report" usebundle => _log("Setting the node value for ${path}", "${old_class_prefix}", "${class_prefix}", @{args});

pass3.augeas_binaries.!ok::
"failure" usebundle => _classes_failure("${old_class_prefix}");
"failure" usebundle => _classes_failure("${class_prefix}");
"report" usebundle => _log("Augeas binaries does exist, but your request has failed. Setting the node value for ${path}","${old_class_prefix}", "${class_prefix}", @{args});

pass2.!augeas_binaries::
pass3.!augeas_binaries::
"failure" usebundle => _classes_failure("${old_class_prefix}");
"failure" usebundle => _classes_failure("${class_prefix}");
"report" usebundle => _log("Augeas binaries does not exist.", "${old_class_prefix}", "${class_prefix}", @{args});

# fixme report augtool output

reports:
"TUTU: ${command}";
"TOTO: ${rudder_augeas.${cpath}}";
}


0 comments on commit e26b1b9

Please sign in to comment.