don't try building x86_64 image
This commit is contained in:
@@ -8,23 +8,34 @@ struct Lease
|
||||
include JSON::Serializable
|
||||
|
||||
@[JSON::Field(key: "Address")]
|
||||
property address : String
|
||||
property address_bytes : Array(Int32)
|
||||
|
||||
@[JSON::Field(key: "MACAddress")]
|
||||
property mac_address : String?
|
||||
@[JSON::Field(key: "HardwareAddress")]
|
||||
property hardware_address : Array(Int32)?
|
||||
|
||||
@[JSON::Field(key: "Hostname")]
|
||||
property hostname : String?
|
||||
|
||||
@[JSON::Field(key: "Lifetime")]
|
||||
property lifetime : Int64?
|
||||
@[JSON::Field(key: "ExpirationUSec")]
|
||||
property expiration_usec : Int64?
|
||||
|
||||
def address : String
|
||||
address_bytes.join('.')
|
||||
end
|
||||
end
|
||||
|
||||
struct DHCPServer
|
||||
include JSON::Serializable
|
||||
|
||||
@[JSON::Field(key: "Leases")]
|
||||
property leases : Array(Lease)?
|
||||
end
|
||||
|
||||
struct NetworkStatus
|
||||
include JSON::Serializable
|
||||
|
||||
@[JSON::Field(key: "OfferedDHCPLeases")]
|
||||
property offered_dhcp_leases : Array(Lease)?
|
||||
@[JSON::Field(key: "DHCPServer")]
|
||||
property dhcp_server : DHCPServer?
|
||||
end
|
||||
|
||||
def sanitize_hostname(hostname : String) : String?
|
||||
@@ -86,7 +97,7 @@ def get_leases(interface : String, networkctl_path : String? = nil) : Array(Leas
|
||||
raise "networkctl failed (exit code #{$?.exit_status}): #{output}" unless $?.success?
|
||||
|
||||
status = NetworkStatus.from_json(output)
|
||||
status.offered_dhcp_leases || [] of Lease
|
||||
status.dhcp_server.try(&.leases) || [] of Lease
|
||||
end
|
||||
|
||||
def write_if_changed(content : String, path : String) : Bool
|
||||
@@ -107,8 +118,8 @@ end
|
||||
interface = "koti"
|
||||
domain = "home.arpa"
|
||||
output_path = "/var/lib/unbound/dhcp-hosts.conf"
|
||||
leases_json_path = "/var/lib/router/leases.json"
|
||||
networkctl_path : String? = nil
|
||||
unbound_control_path : String? = nil
|
||||
|
||||
OptionParser.parse do |parser|
|
||||
parser.banner = "Usage: dhcp-leases-to-unbound [options]"
|
||||
@@ -125,20 +136,30 @@ OptionParser.parse do |parser|
|
||||
output_path = o
|
||||
end
|
||||
|
||||
parser.on("--leases-json PATH", "Output path for leases JSON (default: /var/lib/router/leases.json)") do |path|
|
||||
leases_json_path = path
|
||||
end
|
||||
|
||||
parser.on("--networkctl PATH", "Path to networkctl binary (default: networkctl from PATH)") do |path|
|
||||
networkctl_path = path
|
||||
end
|
||||
|
||||
parser.on("--unbound-control PATH", "Path to unbound-control binary (default: unbound-control from PATH)") do |path|
|
||||
unbound_control_path = path
|
||||
end
|
||||
|
||||
parser.on("-h", "--help", "Show this help") do
|
||||
puts parser
|
||||
exit
|
||||
end
|
||||
end
|
||||
|
||||
def reload_unbound(unbound_control_path : String?)
|
||||
cmd = unbound_control_path ? "#{unbound_control_path} reload" : "unbound-control reload"
|
||||
puts "Reloading Unbound..."
|
||||
result = system(cmd)
|
||||
unless result
|
||||
# Fallback to systemctl
|
||||
system("systemctl reload unbound")
|
||||
end
|
||||
end
|
||||
|
||||
begin
|
||||
# Get leases from networkd
|
||||
leases = get_leases(interface, networkctl_path)
|
||||
@@ -149,18 +170,9 @@ begin
|
||||
# Write Unbound config if changed
|
||||
changed = write_if_changed(config, output_path)
|
||||
|
||||
# Also write leases JSON for dashboard
|
||||
FileUtils.mkdir_p(File.dirname(leases_json_path))
|
||||
File.write(leases_json_path, leases.to_json)
|
||||
|
||||
# Reload Unbound if config changed
|
||||
if changed
|
||||
puts "DHCP leases updated, reloading Unbound..."
|
||||
result = system("unbound-control reload")
|
||||
unless result
|
||||
# Fallback to systemctl if unbound-control fails
|
||||
system("systemctl reload unbound")
|
||||
end
|
||||
reload_unbound(unbound_control_path)
|
||||
else
|
||||
puts "No DHCP lease changes detected."
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user