Last active
August 29, 2015 14:16
-
-
Save riccardomurri/e00a69ee0ba6209530ec to your computer and use it in GitHub Desktop.
Restart services based on defined class/context names.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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