diff --git a/cookbooks/fb_networkd/README.md b/cookbooks/fb_networkd/README.md index e2b8120a..ea49c662 100644 --- a/cookbooks/fb_networkd/README.md +++ b/cookbooks/fb_networkd/README.md @@ -157,6 +157,15 @@ will result in: notifies :restart, 'service[some_service]' ``` +If you need to stop a service before a networkd change is made (and then start +it against afterwards) you can use `node['fb_networkd']['stop_before']`. +This is a list of resource names which will be issued a :stop before the +networkd change is made, than a :start at the end of the run. + +```ruby +node.default['fb_networkd']['stop_before'] << 'service[cool_service]' +``` + ### When can Chef make network changes Network changes can be disruptive and have potential for major impact. To mitigate this, `node.interface_change_allowed?(interface)` from `fb_helpers` diff --git a/cookbooks/fb_networkd/attributes/default.rb b/cookbooks/fb_networkd/attributes/default.rb index eed95fce..73384b13 100644 --- a/cookbooks/fb_networkd/attributes/default.rb +++ b/cookbooks/fb_networkd/attributes/default.rb @@ -31,4 +31,5 @@ 'links' => {}, 'devices' => {}, 'notify_resources' => {}, + 'stop_before' => [], } diff --git a/cookbooks/fb_networkd/recipes/default.rb b/cookbooks/fb_networkd/recipes/default.rb index 9ddd3b45..37eab94c 100644 --- a/cookbooks/fb_networkd/recipes/default.rb +++ b/cookbooks/fb_networkd/recipes/default.rb @@ -25,7 +25,11 @@ node.default['fb_systemd']['networkd']['enable'] = true fb_networkd 'manage configuration' do + # Trigger deferred actions (e.g. :restart) notifies :trigger, 'fb_networkd_notify[doit]' + # Trigger service stops (and starts) around networkd changes + notifies :stop, 'fb_networkd_notify[doit]', :before + notifies :start, 'fb_networkd_notify[doit]' end fb_networkd_notify 'doit' do diff --git a/cookbooks/fb_networkd/resources/notify.rb b/cookbooks/fb_networkd/resources/notify.rb index ab68f68c..8dd111c6 100644 --- a/cookbooks/fb_networkd/resources/notify.rb +++ b/cookbooks/fb_networkd/resources/notify.rb @@ -36,3 +36,39 @@ end end end + +action :stop do + if Chef::VERSION.to_i >= 16 + notify_group 'stop resources before networkd change' do # rubocop:disable Chef/Meta/Chef16 + node['fb_networkd']['stop_before'].each do |r| + notifies :stop, r, :immediately + end + action :run + end + else + log 'stop resources before networkd change' do + node['fb_networkd']['stop_before'].each do |r| + notifies :stop, r, :immediately + end + action :write + end + end +end + +action :start do + if Chef::VERSION.to_i >= 16 + notify_group 'start resources after networkd change' do # rubocop:disable Chef/Meta/Chef16 + node['fb_networkd']['stop_before'].each do |r| + notifies :start, r + end + action :run + end + else + log 'start resources after networkd change' do + node['fb_networkd']['stop_before'].each do |r| + notifies :start, r + end + action :write + end + end +end