Skip to content

Instantly share code, notes, and snippets.

@soul9
Last active December 15, 2015 18:39
Show Gist options
  • Save soul9/5305147 to your computer and use it in GitHub Desktop.
Save soul9/5305147 to your computer and use it in GitHub Desktop.
cfengine promises for debian service management using the 'insserv' and 'service' commands, and links in /etc/rc*.d/
===== Example
===== Can be used in a bundle like so:
bundle agend foo {
services:
"ejabberd"
service_policy => disable,
service_method => service("disable");
}
===== Service management on debian
body service_method service(state) {
service_bundle => roxor_services("$(this.promiser)", "$(state)");
}
bundle agent roxor_services(service,state) {
vars:
any::
"stakeholders[cfengine3]" slist => { "cfengine_in" };
debian::
"packages" slist => {"insserv", "sysvinit-utils"};
"rcdirs" slist => lsdir("/etc", "rc.\.d", "true");
"start[ejabberd]" string => "/usr/sbin/service ejabberd start";
"stop[ejabberd]" string => "/usr/sbin/service ejabberd stop";
"enable[ejabberd]" string => "/sbin/insserv -d ejabberd";
"disable[ejabberd]" string => "/sbin/insserv -rf ejabberd";
"pattern[ejabberd]" string => ".*beam .*";
"svcname[ejabberd]" string => "ejabberd";
"start[nfs-common]" string => "/usr/sbin/service nfs-common start";
"stop[nfs-common]" string => "/usr/sbin/service nfs-common stop";
"enable[nfs-common]" string => "/sbin/insserv -d nfs-common";
"disable[nfs-common]" string => "/sbin/insserv -rf nfs-common";
"pattern[nfs-common]" string => ".*(rpc.idmapd|rpc.statd|rpc.gssd).*";
"svcname[nfs-common]" string => "nfs-common";
"start[portmap]" string => "/usr/sbin/service portmap start";
"stop[portmap]" string => "/usr/sbin/service portmap stop";
"enable[portmap]" string => "/sbin/insserv -d portmap";
"disable[portmap]" string => "/sbin/insserv -rf portmap";
"pattern[portmap]" string => ".*portmap.*";
"svcname[portmap]" string => "portmap";
"start[ssh]" string => "/usr/sbin/service ssh start";
"stop[ssh]" string => "/usr/sbin/service ssh stop";
"enable[ssh]" string => "/sbin/insserv -d ssh";
"disable[ssh]" string => "/sbin/insserv -rf ssh";
"pattern[ssh]" string => ".*sshd.*";
"svcname[ssh]" string => "ssh";
"start[cfengine]" string => "/usr/sbin/service cfengine3 start";
"stop[cfengine]" string => "/usr/sbin/service cfengine3 stop";
"enable[cfengine]" string => "/sbin/insserv -d cfengine3";
"disable[cfengine]" string => "/sbin/insserv -rf cfengine3";
"pattern[cfengine]" string => "/var/cfengine/bin/cf-(?!agent)*";
"svcname[cfengine]" string => "cfengine3";
ubuntu::
"packages" slist => {"insserv"};
"start[ejabberd]" string => "/usr/sbin/service ejabberd start";
"stop[ejabberd]" string => "/usr/sbin/service ejabberd stop";
"enable[ejabberd]" string => "/sbin/insserv -d ejabberd";
"disable[ejabberd]" string => "/sbin/insserv -rf ejabberd";
packages:
"$(packages)"
package_policy => "add",
package_select => ">=",
package_version => "0",
package_method => r0x,
comment => "Check whether to install the required service management packages";
"$(packages)"
package_policy => "update",
package_select => ">=",
package_version => "0",
package_method => r0x,
comment => "Check whether to update the required service management packages";
classes:
debian::
"enabled" expression => fileexists(lsdir("$(rcdirs)", ".*$(svcname[$(service)])", "true")),
comment => "Check whether service is enabled";
any::
"enable" or => { strcmp("enable","$(state)"),
strcmp("","$(state)")},
comment => "Check whether to enable a service";
"disable" expression => strcmp("disable","$(state)"),
comment => "Check whether to disable a service";
"start" expression => strcmp("start","$(state)"),
comment => "Check whether to enable a service";
"stop" expression => strcmp("stop","$(state)"),
comment => "Check whether to disable a service";
"reload" expression => strcmp("reload","$(state)"),
comment => "Check whether to restart a service";
"disabled" not => "enabled";
processes:
start::
"$(pattern[$(service)])" -> { "@(stakeholders[$(service)])" } ,
comment => "Verify that the service appears in the process table",
restart_class => "restart_$(service)";
stop::
"$(pattern[$(service)])" -> { "@(stakeholders[$(service)])" },
comment => "Verify that the service does not appear in the process",
process_stop => "$(stop[$(service)])",
signals => { "term", "kill"};
reload::
"$(pattern[$(service)])" -> { "@(stakeholders[$(service)])" },
comment => "Stop service to restart it",
process_stop => "$(stop[$(service)])",
signals => { "term", "kill"},
classes => "$(service)_start",
ifvarclass => "!$(service)_start&!promise_repaired";
commands:
"$(start[$(service)])" -> { "@(stakeholders[$(service)])" },
comment => "Execute command to restart the $(service) service",
classes => "promise_repaired",
ifvarclass => "restart_$(service)";
enable&disabled::
"$(enable[$(service)])",
classes => "promise_repaired",
comment => "Execute command to enable the service";
disable&enabled::
"$(disable[$(service)])",
classes => "promise_repaired",
comment => "Execute command to disable the service";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment