1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#!/var/cfengine/bin/cf-agent -f-
# Copyright 2021 Northern.tech AS
# This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
# To the extent this program is licensed as part of the Enterprise
# versions of Cfengine, the applicable Commercial Open Source License
# (COSL) may apply to this file if you as a licensee so wish it. See
# included file COSL.txt.
#+begin_src cfengine3
body file control
{
# Include the stdlib for local_dcp, policy, delete_lines
inputs => { "$(sys.libdir)/stdlib.cf" };
}
bundle agent example_action_policy
# @brief Example illustrating how action_policy in action bodies control promise actuation and outcomes
{
files:
# We make sure there is some files to operate on, so we simply make a copy
# of ourselves
"$(this.promise_filename).nop"
copy_from => local_dcp( $(this.promise_filename) );
"$(this.promise_filename).warn"
copy_from => local_dcp( $(this.promise_filename) );
"$(this.promise_filename).fix"
copy_from => local_dcp( $(this.promise_filename) );
# We exercise each valid value of action_policy (nop, fix, warn) defining
# classes named for the action_policy
"$(this.promise_filename).nop"
handle => "delete_lines_action_nop",
edit_line => delete_lines_matching ( ".*" ),
action => policy( "nop" ),
classes => results( "namespace", "MY_files_promise_nop" );
"$(this.promise_filename).warn"
handle => "delete_lines_action_warn",
edit_line => delete_lines_matching ( ".*" ),
action => policy( "warn" ),
classes => results( "namespace", "MY_files_promise_warn" );
"$(this.promise_filename).fix"
handle => "delete_lines_action_fix",
edit_line => delete_lines_matching ( ".*" ),
action => policy( "fix" ),
classes => results( "namespace", "MY_files_promise_fix" );
commands:
"/bin/echo Running Command nop"
handle => "command_nop",
action => policy( "nop" ),
classes => results( "namespace", "MY_commands_promise_nop" );
"/bin/echo Running Command warn"
handle => "command_warn",
action => policy( "warn" ),
classes => results( "namespace", "MY_commands_promise_warn" );
"/bin/echo Running Command fix"
handle => "command_fix",
action => policy( "fix" ),
classes => results( "namespace", "MY_commands_promise_fix" );
reports:
"MY classes:$(const.n)$(const.t)$(with)"
with => join( "$(const.n)$(const.t)", sort( classesmatching( "MY_.*" ), "lex" ));
}
bundle agent __main__
{
methods:
"example_action_policy";
}
#+end_src
###############################################################################
#+begin_src mock_example_output
#@ ```
#@ warning: Should edit file '/tmp/action_policy.cf.nop' but only a warning promised
#@ warning: Should edit file '/tmp/action_policy.cf.warn' but only a warning promised
#@ warning: Command '/bin/echo Running Command nop' needs to be executed, but only warning was promised
#@ warning: Command '/bin/echo Running Command warn' needs to be executed, but only warning was promised
#@ R: MY classes:
#@ MY_commands_promise_fix_reached
#@ MY_commands_promise_fix_repaired
#@ MY_commands_promise_nop_error
#@ MY_commands_promise_nop_failed
#@ MY_commands_promise_nop_not_kept
#@ MY_commands_promise_nop_reached
#@ MY_commands_promise_warn_error
#@ MY_commands_promise_warn_failed
#@ MY_commands_promise_warn_not_kept
#@ MY_commands_promise_warn_reached
#@ MY_files_promise_fix_reached
#@ MY_files_promise_fix_repaired
#@ MY_files_promise_nop_error
#@ MY_files_promise_nop_failed
#@ MY_files_promise_nop_not_kept
#@ MY_files_promise_nop_reached
#@ MY_files_promise_warn_error
#@ MY_files_promise_warn_failed
#@ MY_files_promise_warn_not_kept
#@ MY_files_promise_warn_reached
#@ warning: Method 'example_action_policy' invoked repairs, but only warnings promised
#@ ```
#+end_src
|