Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refs #4873 - Add bundle command_execution_module() and adapt command_execution() and its tests #58

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/acceptance/30_generic_methods/command_execution.cf
Expand Up @@ -39,7 +39,7 @@ bundle agent test
bundle agent check
{
classes:
"ok" expression => "command_execution_${init.canonified_command_name}_ok.!command_execution_${init.canonified_command_name}_error";
"ok" expression => "command_execution_module_${init.canonified_command_name}_ok.!command_execution_module_${init.canonified_command_name}_error";

reports:
ok::
Expand Down
Expand Up @@ -39,7 +39,7 @@ bundle agent test
bundle agent check
{
classes:
"ok" expression => "command_execution_${init.canonified_command_name}_ok.!command_execution_${init.canonified_command_name}_error";
"ok" expression => "command_execution_module_${init.canonified_command_name}_ok.!command_execution_module_${init.canonified_command_name}_error";

reports:
DEBUG::
Expand Down
14 changes: 3 additions & 11 deletions tree/30_generic_methods/command_execution.cf
Expand Up @@ -21,21 +21,13 @@
#
# @parameter command_name Command name
#
# @class_prefix command_execution
# @class_prefix command_execution_module
# @class_parameter command_name
# This bundle will define a class command_execution_${command_name}_{kept,repaired,error,ok,reached}
# This bundle will define a class command_execution_module_${command_name}_{kept,repaired,error,ok,reached}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. We don't change the contract on existing methods. Ever.


bundle agent command_execution(command_name)
{
vars:
"class_prefix" string => canonify("command_execution_${command_name}");

commands:

"${command_name}"
classes => classes_generic("${class_prefix}");

methods:
"report"
usebundle => _logger("Execute the command ${command_name}", "${class_prefix}");
"ph1" usebundle => command_execution_module("${command_name}", "false", "true");
}
51 changes: 51 additions & 0 deletions tree/30_generic_methods/command_execution_module.cf
@@ -0,0 +1,51 @@
#####################################################################################
# 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 <http://www.gnu.org/licenses/>.
#
#####################################################################################

# @name Command execution
# @description Execute a command
#
# @parameter command_name Command name
# @parameter module boolean to know if command should be considered as a CFEngine module (true/false)
# @parameter shell boolean to know if command can be complex or not (true/false)
#
# @class_prefix command_execution_module
# @class_parameter command_name
# This bundle will define a class command_execution_module_${command_name}_{kept,repaired,error,ok,reached}

bundle agent command_execution_module(command_name, module, shell)
{
vars:
"class_prefix"
string => canonify("command_execution_module_${command_name}");

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

commands:
"${command_name}"
module => "${module}",
contain => command_execution_module_contain("${shell}"),
classes => classes_generic("${class_prefix}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with this approach, you'll always have repaired status
You should have a convention in the output of the modules, to define when you have a repaired and when you have a kept
Idealy, if the module changes something, then it is repaired, else it is kept (or you can also fail if it fails)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose that we should use classes based on ${class_prefix} (i.e ${class_prefix}report{success,repaired,failed} but it would be difficult to incite users to echo in their scripts something like "command_execution_module__usr_share_local_bin_my_script_sh_report_success".
Is that what you suggest ?

}

body contain command_execution_module_contain(shell)
{
useshell => "${shell}";
}