# Services bodies bundle common services_common # @brief Enumerate policy files used by this policy file for inclusion to inputs { vars: "inputs" slist => { "$(this.promise_dirname)/common.cf", "$(this.promise_dirname)/paths.cf" }; } body file control # @brief Include policy files used by this policy file as part of inputs { inputs => { @(services_common.inputs) }; } ##------------------------------------------------------- ## service promises ##------------------------------------------------------- body service_method bootstart # @brief Start the service and all its dependencies at boot time # # **See also:** `service_autostart_policy`, `service_dependence_chain` { service_autostart_policy => "boot_time"; service_dependence_chain => "start_parent_services"; windows:: service_type => "windows"; } ## body service_method force_deps # @brief Start all dependendencies when this service starts, and stop all # dependent services when this service stops. # # The service does not get automatically started. # # **See also:** `service_autostart_policy`, `service_dependence_chain` { service_dependence_chain => "all_related"; windows:: service_type => "windows"; } body service_method standard_services # @brief Default services_method for when you wan't to call it explicitly # # By default this service_method is *not* used. The call for standard_services # is within the core and not here. In case you use a promise like: # # ```cf3 # services: # "ssh" # service_policy => "start"; # ``` # # Then this method is skipped and CFEngine calls standard_services bundle # directly. This is here as a helper in case you wan't to be explicit # with your service promise and point it to standard_services (for readability, # documentation, etc). # # Do note that any options defined in this method does not apply to service # promises without explicit template_method call for standard_services. # # **Example:** # # ```cf3 # services: # "ssh" # service_policy => "start", # service_method => standard_services; # ``` { service_bundle => default:standard_services( $(this.promiser), $(this.service_policy) ); } body service_method systemd_services # @brief systemd service method # # **Example:** # # ```cf3 # services: # "ssh" # service_policy => "enabled", # service_method => systemd_services; # ``` { service_bundle => default:systemd_services( $(this.promiser), $(this.service_policy) ); } bundle agent standard_services(service,state) # @brief Standard services bundle, used by CFEngine by default # @author CFEngine AS # @param service Name of service to control # @param state The desired state for that service: "start", "restart", "reload", "stop", or "disable". "enable", "enabled", and "disabled" are also able to be used when systemd is detected. "active" and "inactive" states are supported for systemd managed hosts and can be used for controlling the services currently running state, but make no promises about the service state on boot. # # This bundle is used by CFEngine if you don't specify a services # handler explicitly, and will work with systemd or chkconfig or other # non-sysvinit service managers. It will try to automate service # discovery, unlike `classic_services` which requires known service # names. If it can't do the automatic management, it will pass control # to `classic_services`. # # This bundle receives the service name and the desired service state, # then does the needful to reach the desired state. # # If you're running systemd, systemctl will be used via `systemd_services`. # # Else, if chkconfig is present, it will be used. # # Else, if the service command is available, if will be used. # # Else, if the svcadm command is available, if will be used. Note you # have to supply the full SMF service identifier. # # Else, control is passed to `classic_services`. # # Note you do **not** have to call this bundle from `services` # promises. You can simply make a `methods` call to it. That would # enable you to use systemd states like `try-restart` for instance. # # **Example:** # # ```cf3 # services: # "sshd" service_policy => "start"; # uses `standard_services` # # methods: # "" usebundle => standard_services("sshd", "start"); # direct # ``` # # Alternatively, since services promises are an abstraction around bundles, the service state can be promised via a methods type promise. # # ```cf3 # # methods: # "SSHD should be running" usebundle => standard_services("sshd", "start"); # ``` { vars: "c_service" string => canonify("$(service)"); freebsd:: "init" string => ifelse(fileexists("/usr/local/etc/rc.d/$(service)"), "/usr/local/etc/rc.d/$(service)", "/etc/rc.d/$(service)"); !freebsd:: "init" string => "/etc/init.d/$(service)"; start|restart|reload:: "chkconfig_mode" string => "on"; "svcadm_mode" string => "enable"; stop|disable:: "chkconfig_mode" string => "off"; "svcadm_mode" string => "disable"; classes: # define a class named after the desired state "$(state)" expression => "any"; "non_disabling" or => { "start", "stop", "restart", "reload" }; "chkconfig" expression => "!systemd._stdlib_path_exists_chkconfig"; "sysvservice" expression => "!systemd.!chkconfig._stdlib_path_exists_service"; "smf" expression => "!systemd.!chkconfig.!sysvservice._stdlib_path_exists_svcadm"; "fallback" expression => "!systemd.!chkconfig.!sysvservice.!smf"; "have_init" expression => fileexists($(init)); chkconfig.have_init.freebsd:: "running" -> { "CFE-3513" } expression => returnszero("$(init) onestatus > /dev/null", "useshell"); chkconfig.have_init.!freebsd:: "running" expression => returnszero("$(init) status > /dev/null", "useshell"); sysvservice.have_init:: "running" expression => returnszero("$(paths.service) $(service) status > /dev/null", "useshell"); chkconfig.SuSE:: "onboot" expression => returnszero("$(paths.chkconfig) $(service) | $(paths.grep) 'on$' >/dev/null", "useshell"), comment => "SuSE chkconfig outputs current state to stdout rather than as an exit code"; chkconfig.!SuSE:: "onboot" expression => returnszero("$(paths.chkconfig) $(service)", "noshell"), comment => "We need to know if the service is configured to start at boot or not"; # We redirect stderr and stdout to dev null so that we do not create noise in the logs "chkconfig_$(c_service)_unregistered" not => returnszero("$(paths.chkconfig) --list $(service) &> /dev/null", "useshell"), comment => "We need to know if the service is registered with chkconfig so that we can perform other chkconfig operations, if the service is not registered it must be added. Note we do not automatically try to add the service at this time."; commands: chkconfig.stop.onboot:: # Only chkconfig disable if it's currently set to start on boot "$(paths.chkconfig) $(service) $(chkconfig_mode)" classes => kept_successful_command, contain => silent; chkconfig.start.!onboot:: # Only chkconfig enable service if it's not already set to start on boot, and if its a registered chkconfig service "$(paths.chkconfig) $(service) $(chkconfig_mode)" if => "!chkconfig_$(c_service)_unregistered", classes => kept_successful_command, contain => silent; chkconfig.have_init.(((start|restart).!running)|((stop|restart|reload).running)).non_disabling:: "$(init) $(state)" contain => silent; sysvservice.start.!running:: "$(paths.service) $(service) start" handle => "standard_services_sysvservice_not_running_start", classes => kept_successful_command, comment => "If the service should be running and it is not currently running then we should issue the standard service command to start the service."; sysvservice.restart:: "$(paths.service) $(service) restart" handle => "standard_services_sysvservice_restart", classes => kept_successful_command, comment => "If the service should be restarted we issue the standard service command to restart or reload the service. There is no restriction based on the services current state as restart can start a service that was not already running."; sysvservice.reload.running:: "$(paths.service) $(service) reload" handle => "standard_services_sysvservice_reload", classes => kept_successful_command, comment => "If the service should be reloaded we issue the standard service command to reload the service. It is restricted to when the service is running as a reload should not start services that are not already running. This may not be triggered as service state parameters are limited and translated to the closest meaning."; sysvservice.((stop|disable).running):: "$(paths.service) $(service) stop" handle => "standard_services_sysvservice_stop", classes => kept_successful_command, comment => "If the service should be stopped or disabled and it is currently running then we should issue the standard service command to stop the service."; smf:: "$(paths.svcadm) $(svcadm_mode) $(service)" classes => kept_successful_command; methods: fallback:: "classic" usebundle => classic_services($(service), $(state)); systemd:: "systemd" usebundle => systemd_services( $(service), $(state) ); reports: verbose_mode.systemd:: "$(this.bundle): using systemd layer to $(state) $(service)"; verbose_mode.systemd.!service_loaded:: "$(this.bundle): Service $(service) unit file is not loaded; doing nothing"; verbose_mode.chkconfig:: "$(this.bundle): using chkconfig layer to $(state) $(service) (chkconfig mode $(chkconfig_mode))" if => "!chkconfig_$(c_service)_unregistered.((start.!onboot)|(stop.onboot))"; verbose_mode.chkconfig:: "$(this.bundle): skipping chkconfig layer to $(state) $(service) because $(service) is not registered with chkconfig (chkconfig --list $(service))" if => "chkconfig_$(c_service)_unregistered"; verbose_mode.sysvservice:: "$(this.bundle): using System V service / Upstart layer to $(state) $(service)"; verbose_mode.smf:: "$(this.bundle): using Solaris SMF to $(state) $(service) (svcadm mode $(svcadm_mode))"; verbose_mode.fallback:: "$(this.bundle): falling back to classic_services to $(state) $(service)"; } body action fresh_systemd_state # @brief An 'action' body ensuring the state information for a systemd service is always fresh # # This 'action' body disables caching for functions, in particular the # execresult*() family of functions. # # Although it's now the same as the 'immediate' action body, this may change in # the future. { cfengine:: # ^Needed for versions 3.15.x and older that did not # support empty bodies. When 3.15.x is no longer # supported, this can be removed. @if minimum_version(3.18.1) # Beginning with 3.18.1 ifelapsed being set to 0 results in bypassing of # function caching. In versions prior to 3.18, if an action body with # ifelapsed set to 0 was used in a vars type promise a warning was # emitted. This is guarded to suppress that warning in older versions # where this setting would not change the behavior. ifelapsed => "0"; @endif } bundle agent systemd_services(service,state) # @brief Manage systemd service state # @author Bryan Burke # @param service specific service to control # @param state The desired state for that service: "active", "inactive", "restart", "reload", "enabled", "disabled", "start", and "stop" are specifically understood states. Any other custom state will be passed through to systemctl. # # **State descriptions:** # # * active - Service should be running, no promise about state on boot made. # * inactive - Service should not be running, no promise about state on boot made. # * restart - Service should be restarted, no promise about state on boot made. # * reload - Service should be reloaded, no promise about state on boot made. # * enabled - Service should be enabled, no promise about state on boot made. # * disabled - Service should be reloaded, no promise about state on boot made. # * start - Service should be running, service should be started on boot (active + enabled). # * stop - Service should not be running, service should not be started on boot (inactive + disabled). # # **Example:** # # ```cf3 # services: # # Uses `standard_services`, dynamic decision about init subsystem # "sshd" # service_policy => "enabled"; # # # Explicitly use `systemd_services` # "sshd" # service_policy => "running", # service_policy => "systemd_services"; # ``` # # Alternatively, since services promises are an abstraction around bundles, the service state can be promised via a methods type promise. # # ```cf3 # # methods: # "SSHD should be running" usebundle => systemd_services("sshd", "enabled"); # ``` { vars: systemd:: "call_systemctl" string => "$(paths.systemctl) --no-ask-password --global --system"; "systemd_properties" string => "-pLoadState,CanStop,UnitFileState,ActiveState,LoadState,CanStart,CanReload"; "systemd_service_info" slist => string_split(execresult("$(call_systemctl) $(systemd_properties) show $(service)", "noshell"), "\n", "10"), action => fresh_systemd_state; # Ensure this info is always fresh and not cached [CFE-3753] classes: systemd:: "service_enabled" expression => reglist(@(systemd_service_info), "UnitFileState=enabled"); "service_enabled" -> { "CFE-2923" } expression => returnszero( "$(call_systemctl) is-enabled $(service) > /dev/null 2>&1", useshell); "service_active" -> { "CFE-3238" } expression => reglist(@(systemd_service_info), "ActiveState=(active|activating)"); "service_loaded" expression => reglist(@(systemd_service_info), "LoadState=loaded"); "service_notfound" expression => reglist(@(systemd_service_info), "LoadState=not-found"); "can_stop_service" expression => reglist(@(systemd_service_info), "CanStop=yes"); "can_start_service" expression => reglist(@(systemd_service_info), "CanStart=yes"); "can_reload_service" expression => reglist(@(systemd_service_info), "CanReload=yes"); "request_start" expression => strcmp("start", "$(state)"); "request_stop" expression => strcmp("stop", "$(state)"); "request_reload" expression => strcmp("reload", "$(state)"); "request_restart" expression => strcmp("restart", "$(state)"); "request_disable" expression => strcmp("disable", "$(state)"); "request_disabled" expression => strcmp("disabled", "$(state)"); "request_enable" expression => strcmp("enable", "$(state)"); "request_enabled" expression => strcmp("enabled", "$(state)"); "request_active" expression => strcmp("active", "$(state)"); "request_inactive" expression => strcmp("inactive", "$(state)"); "action_custom" expression => "!(request_start|request_stop|request_reload|request_restart|request_disable|request_disabled|request_enable|request_enabled|request_active|request_inactive)"; "action_start" expression => "(request_start|request_active).!service_active.can_start_service"; "action_stop" expression => "(request_stop|request_inactive).service_active.can_stop_service"; "action_reload" expression => "request_reload.service_active.can_reload_service"; "action_restart" or => { "request_restart", # Possibly undesirable... if a reload is # requested, and the service "can't" be # reloaded, then we restart it instead. "request_reload.!can_reload_service.service_active", }; # Starting a service implicitly enables it "action_enable" expression => "(request_start|request_enable|request_enabled).!service_enabled"; # Respectively, stopping it implicitly disables it "action_disable" expression => "(request_disable|request_disabled|request_stop).service_enabled"; commands: systemd.service_loaded:: # note this class is defined in `inventory/linux.cf` # conveniently, systemd states map to `services` states, except # for `enable` "$(call_systemctl) -q start $(service)" if => "action_start"; "$(call_systemctl) -q stop $(service)" if => "action_stop"; "$(call_systemctl) -q reload $(service)" if => "action_reload"; "$(call_systemctl) -q restart $(service)" if => "action_restart"; "$(call_systemctl) -q enable $(service)" if => "action_enable"; "$(call_systemctl) -q disable $(service)" if => "action_disable"; # Custom action for any of the non-standard systemd actions such a # status, try-restart, isolate, et al. "$(call_systemctl) $(state) $(service)" if => "action_custom"; reports: systemd.service_notfound.(start|restart|reload).(inform_mode|verbose_mode):: "$(this.bundle): Could not find service: $(service)"; } bundle agent classic_services(service,state) # @brief Classic services bundle # @author CFEngine AS # @author Tero Kantonen # @param service specific service to control # @param state desired state for that service # # This bundle is used by `standard_services` if it doesn't have an # automatic driver for the current service manager. # # It receives the service name and the desired service state, then # does the needful to reach the desired state. # # **Example:** # # ```cf3 # services: # "ntp" service_policy => "start"; # "ssh" service_policy => "stop"; # ``` # # There's multiple ways you can add new services to this list. # Here's few examples: # # a) The zeroconf mode; If the new service matches these rules, # you don't need to add anything to the standard_services: # # 1. Your init script basename = `$(service)` # 2. Your init script argument = `$(state)` # 3. Your init script lives in `/etc/init.d/` (for non-*bsd), # or `/etc/rc.d/` (for *bsd) # 4. Your process regex pattern = `\b$(service)\b` # 5. You call the init as `/etc/init.d/