Skip to content

Commit

Permalink
fb_networkd: setup a way to stop and start services around a networkd…
Browse files Browse the repository at this point in the history
… change

Differential Revision: D54094219

fbshipit-source-id: 1f13316e4ba957309552a079a946f0da8bfbe046
  • Loading branch information
joshuamiller01 authored and facebook-github-bot committed Feb 28, 2024
1 parent 6e97aca commit e7b446b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cookbooks/fb_networkd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
1 change: 1 addition & 0 deletions cookbooks/fb_networkd/attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@
'links' => {},
'devices' => {},
'notify_resources' => {},
'stop_before' => [],
}
4 changes: 4 additions & 0 deletions cookbooks/fb_networkd/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions cookbooks/fb_networkd/resources/notify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e7b446b

Please sign in to comment.