Skip to content

Instantly share code, notes, and snippets.

@riccardomurri
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riccardomurri/e00a69ee0ba6209530ec to your computer and use it in GitHub Desktop.
Save riccardomurri/e00a69ee0ba6209530ec to your computer and use it in GitHub Desktop.
Restart services based on defined class/context names.
body common control
{
inputs => {
"lib/cfengine_stdlib.cf",
};
bundlesequence => { "test" };
}
bundle agent test
{
vars:
"services" slist => { "foo", "bar", "baz" };
"restart" slist => { "foo", "baz", "quux" };
commands:
# Simulate defining global classes as a result of some action.
# (Contexts defined in the `classes:` section are local to the
# bundle.)
#
"/bin/true $(restart)"
classes => if_repaired("restart_$(restart)");
"/bin/true $(services)"
classes => if_repaired("have_service_$(services)");
"/bin/true"
classes => if_repaired("commands_done");
methods:
commands_done::
"test"
usebundle => restart_services;
}
bundle agent restart_services
{
vars:
"_services"
slist => classesmatching("restart_.+");
methods:
"maybe restart $(_services)"
usebundle => _maybe_restart_service("$(_services)");
reports:
linux::
"_services = $(_services)";
}
bundle agent _maybe_restart_service(serv_ctx)
{
classes:
"parsed" expression => regextract("(?ix) restart_([a-z0-9_]+)",
"$(serv_ctx)",
"parts");
vars:
# **Note:** Due to normal ordering, this is actually executed
# ***before* the `classes:` section above, so we have to check
# **that `parsed` is defined at the 2nd pass ...
parsed::
"servname" string => "$(parts[1])";
reports:
parsed::
"servname = $(servname)";
"Would restart service $(servname)"
ifvarclass => "have_service_$(servname).restart_$(servname)";
"Would *not* restart service $(servname)"
ifvarclass => "have_service_$(servname).!restart_$(servname)";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment