Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset committed Feb 24, 2017
1 parent 9b20fc2 commit bef63eb
Showing 1 changed file with 188 additions and 0 deletions.
188 changes: 188 additions & 0 deletions tree/20_cfe_basics/ncf_lib.cf
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,191 @@ body edit_defaults ncf_empty_select(select)
max_file_size => "25M";
edit_backup => "timestamp";
}


# defines ncf_service_action_${service}_${action}
# is-active, is-enabled, is-active-process
# Set variables with the error and the name of the manager
# Test behavior with unkown action
# gérer cas non implementés
bundle agent ncf_services(service, action)
{
vars:

###########################################################################
# systemd
pass1.(!is_check_action|!broken_systemctl).systemctl_utility_present::
"action_command" string => "${paths.path[systemctl]} --no-ask-password --global ${action} ${service}.service";
"method" string => "systemctl";

###########################################################################
# service - only non-boot actions
pass1.!is_boot_action.!systemctl_utility_present.service_utility_present::
"action_command" string => "${paths.path[service]} ${service} ${action}";
"method" string => "service";

###########################################################################
# chkconfig - only boot actions
pass1.is_boot_action.!systemctl_utility_present.chkconfig_utility_present::
"chkconfig_action_cmd[enable]" string => "${paths.path[chkconfig]} ${service} on";
"chkconfig_action_cmd[disable]" string => "${paths.path[chkconfig]} ${service} off";
"chkconfig_action_cmd[is-enabled]" string => "${paths.path[chkconfig]} --list ${service} | grep -q -e 3:on -e B:on";
"action_command" string => "${chkconfig_action_cmd[${action}]}";
"method" string => "chkconfig";

###########################################################################
# update-rc.d - only boot actions
pass1.is_boot_action.!systemctl_utility_present.update_rcd_utility_present::
"update_rcd_action_cmd[enable]" string => "${paths.path[update_rc_d]} ${service} defaults";
"update_rcd_action_cmd[disable]" string => "${paths.path[update_rc_d]} ${service} disable";
"action_command" string => "${update_rcd_action_cmd[${action}]}";
"method" string => "update-rc.d";

###########################################################################
# src - only non-boot actions
pass1.!is_boot_action.!systemctl_utility_present.!service_utility_present.startsrc_utility_present::
"svc_action_cmd[restart]" string => "/usr/bin/stopsrc -s ${service} && until /usr/bin/lssrc -s ${service} | ${paths.grep} -q inoperative; do ${paths.perl} -e 'select(undef,undef,undef,.25)'; done; /usr/bin/startsrc -s ${service_name}";
"svc_action_cmd[refresh]" string => "/usr/bin/refresh -s ${service}";
"svc_action_cmd[reload]" string => "${svc_action_cmd[refresh]}";
"svc_action_cmd[start]" string => "/usr/bin/startsrc -s ${service}";
"svc_action_cmd[stop]" string => "/usr/bin/stopsrc -s ${service}";
"svc_action_cmd[is-active]" string => "/usr/bin/lssrc -s ${service} | grep -q 'active'"
"action_command" string => "${svc_action_cmd[${action}]}";
"method" string => "src";

###########################################################################
# chitab/lsitab - only boot actions
pass1.is_boot_action.!systemctl_utility_present.!chkconfig_utility_present.!update_rcd_utility_present.chitab_utility_present.lsitab_utility_present::
"chitab_action_cmd[enable]" string => "/usr/sbin/chitab \"`lsitab -a | grep '^${service}:' | sed 's/\([^:]*\):\([^:]*\):[^:]*:\(.*\)/\1:\2:respawn:\3/'`\"";
"chitab_action_cmd[disable]" string => "/usr/sbin/chitab \"`lsitab -a | grep '^${service}:' | sed 's/\([^:]*\):\([^:]*\):[^:]*:\(.*\)/\1:\2:off:\3/'`\"";
"lsitab_action_cmd[is-enabled]" string => "/usr/sbin/lsitab -a | egrep '^${service}:[0-9]+:(respawn|boot|bootwait|wait|once|initdefault|sysinit):'";
"action_command" string => "${lsitab_action_cmd[${action}]}";
"method" string => "lsitab/chitab";

###########################################################################
# svcadm/svcs
pass1.!is_boot_action.!systemctl_utility_present.!service_utility_present.!startsrc_utility_present.svcadm_utility_present::
"svc_action_cmd[restart]" string => "${paths.path[svcadm]} restart -s ${service}";
"svc_action_cmd[refresh]" string => "${paths.path[svcadm]} refresh -s ${service}";
"svc_action_cmd[reload]" string => "${svc_action_cmd[refresh]}";
"svc_action_cmd[enable]" string => "${paths.path[svcadm]} enable -s ${service}";
"svc_action_cmd[start]" string => "${paths.path[svcadm]} enable -s -t ${service}";
"svc_action_cmd[disable]" string => "${paths.path[svcadm]} disable -s ${service}";
"svc_action_cmd[stop]" string => "${paths.path[svcadm]} disable -s -t ${service}";
"action_command" string => "${svc_action_cmd[${action}]}";
"method" string => "svcadm";
pass1.is_check_action.!systemctl_utility_present.!service_utility_present.!startsrc_utility_present.svcadm_utility_present
"svc_action_cmd[is-enabled]" string => "${paths.path[svcs]} -l ${service} | egrep '^enabled [ ]+(true|false \(temporary\))'";
"svc_action_cmd[is-active]" string => "${paths.path[svcs]} -H ${service} | grep '^online'"
"action_command" string => "${svc_action_cmd[${action}]}";
"method" string => "svcs";

###########################################################################
# init.d - only non-boot actions
pass1.!is_boot_action.!systemctl_utility_present.!service_utility_present.!startsrc_utility_present.!svcadm_utility_present.init_d_directory_present::
"action_command" string => "/etc/init.d/${service} ${action}";
"method" string => "/etc/init.d/";

pass1.is_boot_action.((!systemctl_utility_present.!chkconfig_utility_present.!lsitab_utility_present.!svcs_utility_present)|broken_systemctl)::
"action_command" string => "${paths.path[test]} -f /etc/rc`runlevel | ${paths.path[cut]} -d' ' -f2`.d/S??${service_name}";
"method" string => "/etc/rcX.d/";

any::
"canonified_service" string => canonify("${service}");
"canonified_action" string => canonify("${action}");
"canonified_action_command" string => canonify("${action_command}");

"old_class_prefix" string => "ncf_service_action_${canonified_service}_${canonified_action}_${canonified_regex}";
"promisers" slist => { @{this.callers_promisers}, cf_null }, policy => "ifdefined";
"class_prefix" string => canonify(join("_", "promisers"));
"args" slist => { "${service}", "${action}", "${regex}" };

classes:

windows::
"is_valid_action" or => {
strcmp("start", "${action}"),
strcmp("stop", "${action}"),
strcmp("restart", "${action}"),
};
"is_restart_action" expression => strcmp("restart", "${action}");

any::
"is_boot_action" or => {
strcmp("enable", "${action}"),
strcmp("disable", "${action}")
};

"is_check_action" or => {
strcmp("is-active", "${action}"),
strcmp("is-enabled", "${action}")
};

"is_process_action" expression => strcmp("is-active-process", "${action}");

"method_found" expression => isvariable("action_command");

# This is to workaround a bug in Debian Jessie, that makes systemctl is-enabled
# fail if run against a service that still uses SysV style scripts
# See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=760616
"init_script_present" expression => fileexists("/etc/init.d/${service}");
"broken_systemctl" expression => "debian_8.init_script_present";

"pass3" expression => "pass2";
"pass2" expression => "pass1";
"pass1" expression => "any";

methods:

pass2.!is_process_action.method_found.!systemctl_utility_present::
"action using command" usebundle => command_execution("${action_command}");

"class copy" usebundle => _classes_copy("command_execution_${canonified_action_command}", "${old_class_prefix}");
"new result classes" usebundle => _classes_copy("${class_prefix}_action_using_command", "${class_prefix}");

(!method_found.!windows)|(windows.!is_valid_action)::
"force_failure_class" usebundle => _classes_failure("${old_class_prefix}");
"force_failure_class" usebundle => _classes_failure("${class_prefix}");

services:

###########################################################################
# windows - only non-boot actions

# Restart causes the agent to fail, so we must replace it by stop and start
windows.is_valid_action.!is_restart_action::
"${service}"
service_policy => "${action}",
classes => classes_generic_two("${old_class_prefix}", "${class_prefix}");

windows.is_restart_action::
"${service}"
service_policy => "stop",
classes => classes_generic_two("${old_class_prefix}_stop_service", "${class_prefix}_stop_service");

"${service}"
service_policy => "start",
classes => classes_generic_two("${old_class_prefix}", "${class_prefix}"),
ifvarclass => "(!has_promiser_stack.${old_class_prefix}_stop_service_ok)|(has_promiser_stack.${class_prefix}_stop_service_ok)";

processes:

is_process_action::
"${service}"
process_count => any_count_two("${old_class_prefix}_checked_ok", "${class_prefix}_checked_ok");

"${service}"
# missing new_class_prefix : we cannot put 2 classes here, so we put the only one that we are sure works
restart_class => "${old_class_prefix}_checked_not_ok";

commands:

# We need to use our own commands: implementation here rather than calling command_execution
# because of a CFEngine bug (https://dev.cfengine.com/issues/5840) that causes systemctl
# to fail if it can't find /dev/tty, which is the case unless using no_output => true
pass2.!is_process_action.systemctl_utility_present::
"${action_command}"
contain => in_shell_and_silent,
classes => classes_generic("${old_class_prefix}");

}

0 comments on commit bef63eb

Please sign in to comment.