Skip to content

Commit

Permalink
Merge pull request #253 from peckpeck/ust_7278/add_new_class_paramete…
Browse files Browse the repository at this point in the history
…r_to_command_execution

Fixes #7278: Add new class parameter to command_execution_*
  • Loading branch information
ncharles committed Oct 22, 2015
2 parents 48481dc + 5cf5e6a commit 074e2be
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 6 deletions.
59 changes: 56 additions & 3 deletions tree/20_cfe_basics/ncf_lib.cf
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,14 @@ body select_region ncf_section_selector(section_start, section_end)
}


# Modular local copy_from body
# Modular local copy_from body
body copy_from ncf_local_cp_method(from, method)
{
source => "${from}";
compare => "${method}";
}

# Modular remote copy_from body
# Modular remote copy_from body
# This body gets the CFEngine port from ncf's configuration.
body copy_from ncf_remote_cp_method(from, server, method)
{
Expand All @@ -342,7 +342,7 @@ body copy_from ncf_remote_cp_method(from, server, method)
}

# Define x prefixes/suffixed with promises outcome, based on return codes
body classes classes_generic_return_codes_two(x, y, kept_return_code, repaired_return_code, failed_return_code)
body classes classes_generic_return_single_code_two(x, y, kept_return_code, repaired_return_code, failed_return_code)
{
kept_returncodes => { "${kept_return_code}" };
repaired_returncodes => { "${repaired_return_code}" };
Expand All @@ -368,6 +368,59 @@ body classes classes_generic_return_codes_two(x, y, kept_return_code, repaired_r
"promise_kept_$(y)", "$(y)_kept", "$(y)_ok", "$(y)_not_repaired", "$(y)_reached" };
}

# Define x prefixes/suffixed with promises outcome, based on return codes
body classes classes_generic_return_code_list_two(x, y, kept_return_codes, repaired_return_codes)
{
kept_returncodes => { @{kept_return_codes} };
repaired_returncodes => { @{repaired_return_codes} };

!has_promiser_stack::
promise_repaired => { "promise_repaired_$(x)", "$(x)_repaired", "$(x)_ok", "$(x)_reached" };
repair_failed => { "repair_failed_$(x)", "$(x)_failed", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached", "${x}_error" };
repair_denied => { "repair_denied_$(x)", "$(x)_denied", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached", "${x}_error" };
repair_timeout => { "repair_timeout_$(x)", "$(x)_timeout", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached", "${x}_error" };
promise_kept => { "promise_kept_$(x)", "$(x)_kept", "$(x)_ok", "$(x)_not_repaired", "$(x)_reached" };

has_promiser_stack::
promise_repaired => { "promise_repaired_$(x)", "$(x)_repaired", "$(x)_ok", "$(x)_reached",
"promise_repaired_$(y)", "$(y)_repaired", "$(y)_ok", "$(y)_reached" };
repair_failed => { "repair_failed_$(x)", "$(x)_failed", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached", "${x}_error",
"repair_failed_$(y)", "$(y)_failed", "$(y)_not_ok", "$(y)_not_kept", "$(y)_not_repaired", "$(y)_reached", "${y}_error" };
repair_denied => { "repair_denied_$(x)", "$(x)_denied", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached", "${x}_error",
"repair_denied_$(y)", "$(y)_denied", "$(y)_not_ok", "$(y)_not_kept", "$(y)_not_repaired", "$(y)_reached", "${y}_error"};
repair_timeout => { "repair_timeout_$(x)", "$(x)_timeout", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached", "${x}_error",
"repair_timeout_$(y)", "$(y)_timeout", "$(y)_not_ok", "$(y)_not_kept", "$(y)_not_repaired", "$(y)_reached", "${y}_error" };
promise_kept => { "promise_kept_$(x)", "$(x)_kept", "$(x)_ok", "$(x)_not_repaired", "$(x)_reached",
"promise_kept_$(y)", "$(y)_kept", "$(y)_ok", "$(y)_not_repaired", "$(y)_reached" };
}

# Define x prefixes/suffixed with true/false outcome, based on return codes
# Note that an unknown return code is an error and doesn't return false nor true, just an error.
body classes classes_generic_return_boolean_list_two(x, y, boolean_prefix, true_return_codes, false_return_codes)
{
kept_returncodes => { @{true_return_codes} };
repaired_returncodes => { @{false_return_codes} };

!has_promiser_stack::
promise_repaired => { "promise_kept_$(x)", "$(x)_kept", "$(x)_ok", "$(x)_not_repaired", "$(x)_reached", "${boolean_prefix}_false" };
repair_failed => { "repair_failed_$(x)", "$(x)_failed", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached", "${x}_error" };
repair_denied => { "repair_denied_$(x)", "$(x)_denied", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached", "${x}_error" };
repair_timeout => { "repair_timeout_$(x)", "$(x)_timeout", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached", "${x}_error" };
promise_kept => { "promise_kept_$(x)", "$(x)_kept", "$(x)_ok", "$(x)_not_repaired", "$(x)_reached", "${boolean_prefix}_true" };

has_promiser_stack::
promise_repaired => { "promise_kept_$(x)", "$(x)_kept", "$(x)_ok", "$(x)_not_repaired", "$(x)_reached",
"promise_kept_$(y)", "$(y)_kept", "$(y)_ok", "$(y)_not_repaired", "$(y)_reached", "${boolean_prefix}_false" };
repair_failed => { "repair_failed_$(x)", "$(x)_failed", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached", "${x}_error",
"repair_failed_$(y)", "$(y)_failed", "$(y)_not_ok", "$(y)_not_kept", "$(y)_not_repaired", "$(y)_reached", "${y}_error" };
repair_denied => { "repair_denied_$(x)", "$(x)_denied", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached", "${x}_error",
"repair_denied_$(y)", "$(y)_denied", "$(y)_not_ok", "$(y)_not_kept", "$(y)_not_repaired", "$(y)_reached", "${y}_error" };
repair_timeout => { "repair_timeout_$(x)", "$(x)_timeout", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached", "${x}_error",
"repair_timeout_$(y)", "$(y)_timeout", "$(y)_not_ok", "$(y)_not_kept", "$(y)_not_repaired", "$(y)_reached", "${y}_error" };
promise_kept => { "promise_kept_$(x)", "$(x)_kept", "$(x)_ok", "$(x)_not_repaired", "$(x)_reached",
"promise_kept_$(y)", "$(y)_kept", "$(y)_ok", "$(y)_not_repaired", "$(y)_reached", "${boolean_prefix}_true" };
}

# WARNING !!!
# For new class prefix migration only, this will be removed when migration is over
# Define x and y prefixed/suffixed with promise outcome
Expand Down
2 changes: 1 addition & 1 deletion tree/30_generic_methods/command_execution.cf
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ bundle agent command_execution(command_name)

methods:
"report"
usebundle => _logger("Execute the command ${command_name}", "${old_class_prefix}", "${class_prefix}", @{args} ),
usebundle => _logger("Execute the command ${command_name}", "${old_class_prefix}", "${class_prefix}", @{args}),
ifvarclass => "(!has_promiser_stack.${old_class_prefix}_reached)|(has_promiser_stack.${class_prefix}_reached)";

commands:
Expand Down
53 changes: 53 additions & 0 deletions tree/30_generic_methods/command_execution_result.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#####################################################################################
# Copyright 2013 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 Command execution result
# @description Execute a command and create outcome classes depending on its exit code
#
# @parameter command The command to run
# @parameter kept_codes List of codes that produce a kept status separated with commas (ex: 1,2,5)
# @parameter repaired_codes List of codes that produce a repaired status separated with commas (ex: 3,4,6)
#
# @class_prefix command_execution_result
# @class_parameter command_name
#
# This bundle will define a class command_execution_result_${command_name}_{kept,repaired,not_ok,ok,reached}
# depending on the exit codes given in parameters.
# If an exit code is not in the list it will lead to an error status.
# If you want 0 to be a success you have to list it in the kept_codes list

bundle agent command_execution_result(command, kept_codes, repaired_codes)
{
vars:
"old_class_prefix" string => canonify("command_execution_result_${command}");
"class_prefix" string => canonify(join("_", "this.callers_promisers"));
"args" slist => { "${command}", "${kept_codes}", "${repaired_codes}" };

"kept_list" slist => splitstring("${kept_codes}", "\s*,\s*", "256");
"repaired_list" slist => splitstring("${repaired_codes}", "\s*,\s*", "256");

methods:
"report"
usebundle => _logger("Execute the command ${command}", "${old_class_prefix}", "${class_prefix}", @{args}),
ifvarclass => "(!has_promiser_stack.${old_class_prefix}_reached)|(has_promiser_stack.${class_prefix}_reached)";

commands:
"${command}"
contain => in_shell,
classes => classes_generic_return_code_list_two("${old_class_prefix}", "${class_prefix}", @{kept_list}, @{repaired_list});
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ bundle agent http_request_check_status_headers(method, url, expected_status, hea
args => "-I -s -o /dev/null -w \"%{http_code}\" ${url} -X ${method} -H '${headers}' | /bin/grep -q ${expected_status}",
contain => in_shell,
# We don't define repaired class, as we only query and expect, so using 999 to avoid using this feature
classes => classes_generic_return_codes_two("${old_class_prefix}", "${class_prefix}", "0", "999", "1");
classes => classes_generic_return_single_code_two("${old_class_prefix}", "${class_prefix}", "0", "999", "1");

}
2 changes: 1 addition & 1 deletion tree/30_generic_methods/logger_rudder.cf
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ bundle agent _logger_rudder_v2(expected_reports_source, message, class_prefix, a
"args_line1" string => string_head(nth("args", "0"), "80");

# normalize message to fit syslog output (first line)
"message_lines" slist => string_split("${message}", "${const.n}", "2");
"message_lines" slist => splitstring("${message}", "${const.n}", "2");
"message_line1" string => nth("message_lines", "0");

classes:
Expand Down

0 comments on commit 074e2be

Please sign in to comment.