##################################################################
#
# cfe_internal_processes
# - restart CFE processes by specific time (not in service)
#
##################################################################

bundle agent cfe_internal_processes
{
  vars:

      "cf_components"   slist => {
                                  "cf-key",
                                  "cf-monitord",
                                  "cf-promises",
                                  "cf-runagent",
                                  "cf-serverd",
                                  "cf-hub"
      },
      comment => "Define all cfengine robot agents",
      handle => "cfe_internal_processes_vars_cf_components";

    windows::

      "stop_signal"  string => "kill",
      comment => "Define a stop signal for Windows",
      handle => "cfe_internal_processes_vars_stop_signal_windows";

    !windows::

      "stop_signal"  string => "term",
      comment => "Define a stop signal for *NIX",
      handle => "cfe_internal_processes_vars_stop_signal_unix";

      #

  classes:
      #  NOTE: We do not restart by default, can be added if desired
      #  "restart_cf" expression => "Monday.Hr05.Min00_05"
      #                  comment => "Define a class to restart cfengine processes",
      #                   handle => "cfe_internal_processes_classes_restart_cf";

      #

  processes:

    restart_cf::

      "$(cf_components)"

      signals => { "$(stop_signal)" },
      comment => "Kill cfengine processes",
      handle => "cfe_internal_processes_stop_cf_components";

    restart_cf.!windows::

      "cf-execd"
      signals => { "$(stop_signal)" },
      comment => "Kill cf-execd process on *NIX",
      handle => "cfe_internal_processes_stop_cf_execd";

      #

  commands:

    restart_cf.!windows::

      "$(sys.cf_execd)"
      comment => "Restart cf-execd process",
      classes => kept_successful_command,
      handle => "cfe_internal_processes_commands_cf_execd";

      #

  services:

    restart_cf.windows::

      "CfengineNovaExec"
      service_policy => "stop",
      comment => "Stop the executor service, part of scheduled restart",
      handle => "cfe_internal_processes_services_stop_cf_execd_windows";

      "CfengineNovaExec"
      service_policy => "start",
      comment => "Start the executor service, part of scheduled restart",
      handle => "cfe_internal_processes_services_start_cf_execd_windows";

      #

  reports:

    restart_cf::

      "Reloaded configuration of all Cfengine components"
      comment => "Alert restarting message",
      handle => "cfe_internal_processes_reports";

}

##################################################################
#
# cfe_internal_enable
# - start CFE agents when booting (not in service)
#
##################################################################

bundle agent cfe_internal_enable
{
  vars:

    freebsd::

      "rc_conf[cfengine_enable]"   string => "\"YES\"",
      comment => "Define a variable to start cfengine at boot time",
      handle => "cfe_internal_enable_vars_rc_conf_freebsd";

    netbsd::

      "rc_conf[cfengine_enable]"  string => "\"YES\"",
      comment => "Define a variable to start cfengine at boot time",
      handle => "cfe_internal_enable_vars_rc_conf_netbsd";

    linux::

      "rc_files"   slist => {
                              "/etc/rc1.d/K11cfengine3",
                              "/etc/rc2.d/S90cfengine3",
                              "/etc/rc3.d/S90cfengine3",
                              "/etc/rc4.d/S90cfengine3",
                              "/etc/rc5.d/S90cfengine3",
                              "/etc/rc6.d/K11cfengine3"
      },
      comment => "Define a list of rc files on Linux",
      handle => "cfe_internal_enable_vars_rc_files";

      #

  files:

    freebsd::

      "/usr/local/etc/rc.d/cfengine3.sh"
      comment => "Ensure that the cfengine rc.d script is executable",
      handle => "cfe_internal_enable_files_cfengine3_sh_freebsd",
      perms => mog("755", "root", "wheel");

      "/etc/rc.conf"
      comment => "cfengine libraries should be enabled in rc.conf if appropriate",
      handle => "cfe_internal_enable_files_rc_conf_freebsd_1",
      edit_line => append_if_no_line("[ -e $(sys.workdir)/lib ] && /sbin/ldconfig -m $(sys.workdir)/lib");

      "/etc/rc.conf"
      comment => "cfengine should be enabled in rc.conf",
      handle => "cfe_internal_enable_files_rc_conf_freebsd_2",
      edit_line => set_variable_values("cfengine_enable.rc_conf");

    netbsd::

      "/usr/local/etc/rc.d/cfengine3.sh"
      comment => "Ensure that the cfengine rc.d script is executable",
      handle => "cfe_internal_enable_files_cfengine3_sh_netbsd",
      perms => mog("755", "root", "wheel");

      "/etc/rc.d/cfengine"
      comment => "Move cfengine rc.d script to the correct location",
      handle => "cfe_internal_enable_files_rc_d_netbsd",
      copy_from => local_cp("/usr/local/etc/rc.d/cfengine3.sh");

      "/etc/rc.conf"
      comment => "cfengine should be enabled in rc.conf",
      handle => "cfe_internal_enable_files_rc_conf_netbsd",
      edit_line => set_variable_values("cfengine_enable.rc_conf");

    linux::

      "$(rc_files)"
      comment => "Ensure that startup scripts are properly linked",
      handle => "cfe_internal_enable_files_rc_files_linux",
      link_from => ln_s("/etc/init.d/cfengine3");
}

##################################################################
#
# cfe_internal_build_software_report
# - force machines to have package promises to be able to generate
#   software reports (not in service)
#
##################################################################

bundle agent cfe_internal_build_software_report
{
  packages:

    redhat::

      "wget"
      comment => "Install packages from the list",
      handle => "cfe_internal_build_software_report_redhat",
      package_policy => "add",
      package_method => yum;

    debian::

      "wget"
      comment => "Install packages from the list",
      handle => "cfe_internal_build_software_report_debian",
      package_policy => "add",
      package_method => apt;

    (SuSE|suse)::

      "wget"
      comment => "Install packages from the list",
      handle => "cfe_internal_build_software_report_suse",
      package_policy => "add",
      package_method => zypper;

}
