Skip to content

Commit c8d225e

Browse files
jaymzhfacebook-github-bot
authored andcommitted
New cookbook: fb_dhcprelay (facebook#291)
Summary: Very simple cookbook to manage ISC DHCP Relay Signed-off-by: Phil Dibowitz <phil@ipom.com> Pull Request resolved: facebook#291 Test Plan: It's an API example cookbook, no plans to use in prod. Differential Revision: D70408624 fbshipit-source-id: 1b3ae3553eac040bf997e77ab04a1724339a1488
1 parent f14f81e commit c8d225e

File tree

7 files changed

+203
-0
lines changed

7 files changed

+203
-0
lines changed

cookbooks/fb_dhcprelay/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
fb_dhcprelay Cookbook
2+
=====================
3+
Manage ISC DHCP Relay
4+
5+
Requirements
6+
------------
7+
8+
Attributes
9+
----------
10+
* node['fb_dhcprelay']['manage_packages']
11+
* node['fb_dhcprelay']['sysconfig']
12+
13+
Usage
14+
-----
15+
16+
ISC DHCP Relay is a very simple package which forwards DHCP requests and
17+
responses across a router. It does not have a configuration file and is
18+
configured purely through command-line options.
19+
20+
### Configuration (sysconfig)
21+
22+
The sysconfig hash is how you can configure dhcprelay. There are two values
23+
here: `servers`, and `options`. On Debian-derived OSes, there is also
24+
`interfaces`. All 3 are arrays.
25+
26+
**NOTE**: You must use all-lowercase keys in the `sysconfig` hash, this
27+
cookbook will upcase them for you. Using non-all-lowercase keys will cause the
28+
run to fail.
29+
30+
You must point `servers` to the list of DHCP servers to forward requests to.
31+
Then, on Debian systems you'll want to specify which interface or interfaces to
32+
listen to in 'interfaces`. Finally, you'll want to specify, at a minimum the
33+
`-iu` and `-id` options to specify upstream and downstream interface(s). For
34+
example, let's say you have a 3-legged router with `eth0` being WAN, `eth1`
35+
being the internal network that has a DHCP server and `eth2` being the internal
36+
network without a DHCP server. On Debian-derived OSes you would do:
37+
38+
```ruby
39+
{
40+
'servers' => ['10.0.0.200'], # whatever your DHCP server is
41+
'interfaces' => ['eth2'],
42+
'options' => ['-iu eth1', '-id eth2'],
43+
}.each do |key, val|
44+
node.default['fb_dhcprelay']['sysconfig'][key] = val
45+
end
46+
```
47+
48+
Or on Fedora-derived OSes:
49+
50+
```ruby
51+
{
52+
'servers' => ['10.0.0.200'], # whatever your DHCP server is
53+
'options' => ['-iu eth1', '-id eth2', '-i eth2'],
54+
}.each do |key, val|
55+
node.default['fb_dhcprelay']['sysconfig'][key] = val
56+
end
57+
```
58+
59+
*NOTE*: Fedora's package does not include a sysconfig file or a way to specify
60+
options, so this cookbook adds a drop-in unit file to add such functionality
61+
and a sysconfig file. You can see
62+
[bz#2348883](https://bugzilla.redhat.com/show_bug.cgi?id=2348883) for details.
63+
64+
### Packages
65+
66+
By default this cookbook will install the appropriate package(s). To disable
67+
this set `node['fb_dhcprelay']['manage_packages']` to `false`.
68+
69+
### A note on EOL
70+
71+
Technically, ISC has deprecated DHCP Relay. However, it is still currently the
72+
primary DHCP Relay used in the world and the only one widely packaged. OpenBSD
73+
has forked it, but that fork is not yet available for other OSes. You can see
74+
[this page](https://www.isc.org/blogs/dhcp-client-relay-eom/) for details.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#
2+
# vim: syntax=ruby:expandtab:shiftwidth=2:softtabstop=2:tabstop=2
3+
#
4+
# Copyright (c) 2025-present, Meta Platforms, Inc.
5+
# Copyright (c) 2025-present, Phil Dibowitz
6+
# All rights reserved.
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#
20+
21+
sysconfig = {
22+
'servers' => [],
23+
'options' => [],
24+
}
25+
if ::ChefUtils.debian?
26+
sysconfig['interfaces'] = []
27+
end
28+
29+
default['fb_dhcprelay'] = {
30+
'manage_packages' => true,
31+
'sysconfig' => sysconfig,
32+
}

cookbooks/fb_dhcprelay/metadata.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name 'fb_dhcprelay'
2+
maintainer 'Meta Platforms, Inc.'
3+
maintainer_email 'noreply@meta.com'
4+
license 'Apache-2.0'
5+
description 'Installs/Configures ISC DHCP Relay'
6+
version '0.1.0'
7+
depends 'fb_systemd'
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#
2+
# Cookbook:: fb_dhcprelay
3+
# Recipe:: default
4+
#
5+
# Copyright (c) 2025-present, Meta Platforms, Inc.
6+
# Copyright (c) 2025-present, Phil Dibowitz
7+
# All rights reserved.
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
#
21+
22+
# When RHEL10 dropped ISC, they also dropped replay, which is a bummer.
23+
# Requested EPEL10 branch in
24+
# https://bugzilla.redhat.com/show_bug.cgi?id=2348940
25+
if node.el_min_version?(10)
26+
fail 'fb_dhcprelay: RHEL/CentOS 10+ no longer includes DHCP Relay'
27+
end
28+
29+
if fedora_derived?
30+
pkgs = %w{dhcp-relay}
31+
svc = 'dhcrelay'
32+
sysconfig = "/etc/sysconfig/#{svc}"
33+
elsif debian?
34+
pkgs = %w{isc-dhcp-relay}
35+
svc = 'isc-dhcp-relay'
36+
sysconfig = "/etc/default/#{svc}"
37+
end
38+
39+
package 'dhcp-relay packages' do
40+
only_if { node['fb_dhcprelay']['manage_packages'] }
41+
package_name pkgs
42+
action :upgrade
43+
notifies :restart, 'service[dhcprelay]'
44+
end
45+
46+
whyrun_safe_ruby_block 'validate sysconfig' do
47+
block do
48+
node['fb_dhcprelay']['sysconfig'].each_key do |key|
49+
if key != key.downcase
50+
fail "fb_dhcprelay: Non-lowercase key #{key} found in " +
51+
'node["fb_dhcprelay"]["sysconfig"] - please use lowercase key names'
52+
end
53+
end
54+
end
55+
end
56+
57+
template sysconfig do
58+
source 'sysconfig.erb'
59+
owner node.root_user
60+
group node.root_user
61+
mode '0644'
62+
notifies :restart, 'service[dhcprelay]'
63+
end
64+
65+
if fedora_derived?
66+
fb_systemd_override 'add-configurability' do
67+
unit_name "#{svc}.service"
68+
content <<~UNIT
69+
[Service]
70+
EnvironmentFile=-#{sysconfig}
71+
ExecStart=
72+
ExecStart=/usr/sbin/dhcrelay -d --no-pid $OPTIONS $SERVERS
73+
UNIT
74+
notifies :restart, 'service[dhcprelay]'
75+
end
76+
end
77+
78+
service 'dhcprelay' do
79+
service_name svc
80+
action [:enable, :start]
81+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This file is controlled by Chef, do not modify!
2+
<% node['fb_dhcprelay']['sysconfig'].each do |key, val| %>
3+
<%= key.upcase %>="<%= val.join(' ') %>"
4+
<% end %>

cookbooks/test_services/metadata.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
depends 'fb_apache'
1111
depends 'fb_apt_cacher'
1212
depends 'fb_bind'
13+
depends 'fb_dhcprelay'
1314
depends 'fb_ejabberd'
1415
depends 'fb_influxdb'
1516
depends 'fb_reprepro'

cookbooks/test_services/recipes/default.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
include_recipe 'fb_sasl'
2222
include_recipe 'fb_bind'
23+
unless node.el_min_version?(10)
24+
include_recipe 'fb_dhcprelay'
25+
end
26+
node.default['fb_dhcprelay']['sysconfig']['servers'] = ['10.1.1.1']
2327

2428
# Currently fb_vsftpd is broken on debian
2529
# https://github.com/facebook/chef-cookbooks/issues/149

0 commit comments

Comments
 (0)