Skip to content

Commit

Permalink
Fixes #7234: Add a generic method for command execution with return c…
Browse files Browse the repository at this point in the history
…ode handling
  • Loading branch information
peckpeck committed Sep 29, 2015
1 parent ec2b811 commit 2e043c5
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tests/acceptance/30_generic_methods/command_execution_result.cf
@@ -0,0 +1,63 @@
#######################################################
#
# Launch the /bin/date command
#
#######################################################

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:
"command_0" string => "exit 0";
"canonified_command_0" string => canonify("${command_name}");
}

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

bundle agent test
{
vars:
"kept_codes" string => "0,1,2";
"repaired_codes" string => "3,4";

methods:
"phA" usebundle => command_execution_result("exit 0", "${kept_codes}", "${repaired_codes}");
"phB" usebundle => command_execution_result("exit 2", "${kept_codes}", "${repaired_codes}");
"phC" usebundle => command_execution_result("exit 3", "${kept_codes}", "${repaired_codes}");
"phD" usebundle => command_execution_result("exit 4", "${kept_codes}", "${repaired_codes}");
"phE" usebundle => command_execution_result("exit 5", "${kept_codes}", "${repaired_codes}");
}

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

bundle agent check
{
classes:
"ok_A" expression => "command_execution_result_exit_0_kept.!command_execution_result_exit_0_repaired.!command_execution_result_exit_0_error";
"ok_B" expression => "command_execution_result_exit_2_kept.!command_execution_result_exit_2_repaired.!command_execution_result_exit_2_error";
"ok_C" expression => "!command_execution_result_exit_3_kept.command_execution_result_exit_3_repaired.!command_execution_result_exit_3_error";
"ok_D" expression => "!command_execution_result_exit_4_kept.command_execution_result_exit_4_repaired.!command_execution_result_exit_4_error";
"ok_E" expression => "!command_execution_result_exit_5_kept.!command_execution_result_exit_5_repaired.command_execution_result_exit_5_error";

"ok" expression => "ok_A.ok_B.ok_C.ok_D.ok_E";

reports:
ok::
"$(this.promise_filename) Pass";
!ok::
"$(this.promise_filename) FAIL";
}
12 changes: 12 additions & 0 deletions tree/20_cfe_basics/ncf_lib.cf
Expand Up @@ -355,6 +355,18 @@ body classes classes_generic_return_codes(x, kept_return_code, repaired_return_c
promise_kept => { "promise_kept_$(x)", "$(x)_kept", "$(x)_ok", "$(x)_not_repaired", "$(x)_reached" };
}

body classes classes_generic_return_code_list(x, kept_return_codes, repaired_return_codes)
{
kept_returncodes => { @{kept_return_codes} };
repaired_returncodes => { @{repaired_return_codes} };

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" };
}

# Enforce or not content of a file
########################################################################
# Same as std_defs, allowing user to specify the file erase policy #
Expand Down
51 changes: 51 additions & 0 deletions tree/30_generic_methods/command_execution_result.cf
@@ -0,0 +1,51 @@
#####################################################################################
# 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:
"class_prefix" string => canonify("command_execution_result_${command}");

"kept_list" slist => string_split("${kept_codes}", ",", "256");
"repaired_list" slist => string_split("${repaired_codes}", ",", "256");

methods:
"report"
usebundle => _logger("Execute the command ${command}", "${class_prefix}"),
ifvarclass => "${class_prefix}_reached";

commands:
"${command}"
contain => in_shell,
classes => classes_generic_return_code_list("${class_prefix}", @{kept_list}, @{repaired_list});
}

0 comments on commit 2e043c5

Please sign in to comment.