diff --git a/lib/service_config.cf b/lib/service_config.cf new file mode 100644 index 0000000000..dbd2841f97 --- /dev/null +++ b/lib/service_config.cf @@ -0,0 +1,53 @@ +# todo, maybe chmod, user, group for config_final_path +# check pid file for service stop/start? +bundle agent service_config(service_name, + config_template_path, + config_final_path, + validate_config_command, + template_data) +{ +reports: + "template_data is ${with}" with => storejson(template_data); + vars: + "staged_config" string => "$(config_final_path).staged"; + methods: + "staged config rendered" usebundle => file_make_mustache( $(staged_config), $(config_template_path), @(template_data) ); + "final config and restart" usebundle => validate_config_and_restart_service( + $(staged_config), + $(validate_config_command), + $(config_final_path), + $(service_name) + ); + + +} +bundle agent validate_config_and_restart_service( staged_config, validate_config_command, config_final_path, service_name ) +{ + files: + "$(config_final_path)" + copy_from => local_dcp( $(staged_config) ), + if => and( + isnewerthan( $(config_final_path), $(staged_config) ), + returnszero("$(validate_config_command) $(staged_config)", "useshell") + ), + classes => results("bundle", "$(service_name)_config"); + + services: + "$(service_name)" + service_policy => "stop", + if => and( + isnewerthan( $(config_final_path), $(staged_config) ), + returnszero("$(validate_config_command) $(staged_config)", "useshell") + ), + classes => results("bundle", "$(service_name)_stopped_after_validated_config"); + + "$(service_name)" + service_policy => "start", + if => and( "$(service_name)_config_repaired", + "$(service_name)_stopped_after_validated_config_repaired"); + + "$(service_name)" + service_policy => "restart", + if => and( "$(service_name)_config_repaired", + not( "$(service_name)_stoppoed_after_validated_config_repaired") ); +} diff --git a/lib/stdlib.cf b/lib/stdlib.cf index 017a5eebfa..dace8a57ad 100644 --- a/lib/stdlib.cf +++ b/lib/stdlib.cf @@ -39,6 +39,7 @@ bundle common stdlib_common "input[files]" string => "$(this.promise_dirname)/files.cf"; "input[edit_xml]" string => "$(this.promise_dirname)/edit_xml.cf"; "input[services]" string => "$(this.promise_dirname)/services.cf"; + "input[service_config]" string => "$(this.promise_dirname)/service_config.cf"; "input[processes]" string => "$(this.promise_dirname)/processes.cf"; "input[storage]" string => "$(this.promise_dirname)/storage.cf"; "input[databases]" string => "$(this.promise_dirname)/databases.cf";