Hooks
2 minute read
Hook Methods
onDHCPRequestBefore(request)
Called before the DHCP response is generated.
onDHCPRequestAfter(request, response)
Called after the DHCP response is generated.
request
: See https://pkg.go.dev/beryju.io/gravity/pkg/roles/dhcp#Request4response
: See https://pkg.go.dev/github.com/insomniacslk/dhcp/dhcpv4#DHCPv4
Environment
gravity
Object
log(msg: any)
Logs a message to the stdout of the Gravity node this hook is run on.
node: string
The identifier of the node this hook is run on.
version: string
The version of Gravity on the node this hook is run on.
role
A reference to the Role instance this hook was triggered by.
net
Object
parseIP(ip: string, family: string)
Parse an IP address from the string ip
and return it as an array of bytes. family
determines if the IP should be parsed as IPv4 or IPv6.
strconv
Object
toBytes(input: string)
Convert a string to an array of bytes.
dhcp
Object
Opt(code: number, data: byte[])
Create a DHCP option with the code and data given.
GetString(code: number, opts dhcpv4.Options)
Return the string representation of a DHCP option, for example dhcp.GetString(77, req.Options)
Examples
Set Option 43 for UniFi Adoption
const UniFiPrefix = [0x01, 0x04];
const UniFiIP = net.parseIP("192.168.1.100", "v4");
function onDHCPRequestAfter(req, res) {
res.UpdateOption(dhcp.Opt(43, [...UniFiPrefix, ...UniFiIP]))
}
Set Boot filename for iPXE
const iPXEScript = "https://boot.ipxe.org/demo/";
function onDHCPRequestAfter(req, res) {
if (dhcp.GetString(77, req.Options) == "iPXE") {
res.BootFileName = iPXEScript;
res.UpdateOption(dhcp.Opt(67, strconv.toBytes(iPXEScript)));
}
}