#!/usr/bin/env ruby require_relative "utils" require "optparse" class DhcpRegistry < Registry def add_lease(name, macaddress, ipv4, ipv6) data["network"][name] ||= {} host = data["network"][name] host["macaddress"] = macaddress host["ipv4"] = ipv4 if ipv4 host["ipv6"] = ipv6 if ipv6 end def remove_lease(name) if data["network"].delete(name).nil? die "no such lease name #{name} in registry.json" end end def update_leases template_path = Pathname.new(File.expand_path("../../templates", __FILE__)) dhcp_template = Template.new(template_path.join("dhcp.conf.erb")) static_leases = data["network"].select do |name, data| data["mac"] && (data["ipv4"] || data["ipv6"]) end.map do |name, data| TemplateContext.new(data.merge(name: name)) end dhcp_path = Pathname.new(File.expand_path("../../dhcp.peers.conf", __FILE__)) File.open(dhcp_path, "w+").write(dhcp_template.render(leases: static_leases)) reload_dhcp end def reload_dhcp command = try(data, "host", "reload_command") if command.nil? puts "skip to reload dhcpd because reload_command is not defined" else sh(command) end end end GLOBAL_OPTIONS = OptionParser.new do |opts| opts.banner = "Usage: dhcp [options] [subcommand [options]]" opts.separator "" opts.separator <