dhcp: wrap main routine in class
This commit is contained in:
parent
6ce5a542f2
commit
b540709a35
93
scripts/dhcp
93
scripts/dhcp
@ -36,53 +36,64 @@ GLOBAL_OPTIONS = OptionParser.new do |opts|
|
|||||||
opts.separator <<HELP
|
opts.separator <<HELP
|
||||||
Available subcommands:
|
Available subcommands:
|
||||||
add [options] NAME MACADDRESS: add dhcp lease
|
add [options] NAME MACADDRESS: add dhcp lease
|
||||||
remove [options] NAME: remove dhcp static lease
|
remove [options] NAME: remove dhcp static lease
|
||||||
|
regenerate: regenerate dhcp configuration
|
||||||
|
|
||||||
See 'dhcp COMMAND --help' for more information on a specific command.
|
See 'dhcp COMMAND --help' for more information on a specific command.
|
||||||
HELP
|
HELP
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_command(registry, args)
|
class Application
|
||||||
ipv4, ipv6 = nil, nil
|
def run(args)
|
||||||
parser = OptionParser.new do |opts|
|
GLOBAL_OPTIONS.order!(args)
|
||||||
opts.banner = "Usage: dhcp add [options] NAME MACADDRESS"
|
@registry = DhcpRegistry.new
|
||||||
opts.on("-4", "--ipv4 ADDRESS", "set fixed ipv4 address") do |address|
|
case command = args.shift
|
||||||
ipv4 = address
|
when "add"
|
||||||
|
add_command
|
||||||
|
when "remove"
|
||||||
|
remove_command
|
||||||
|
when nil
|
||||||
|
puts(GLOBAL_OPTIONS.help())
|
||||||
|
exit(0)
|
||||||
|
when "regenerate" # fall through
|
||||||
|
else
|
||||||
|
die "unknown subcommand #{command}"
|
||||||
end
|
end
|
||||||
opts.on("-6", "--ipv6 ADDRESS", "set fixed ipv6 address") do |address|
|
|
||||||
ipv6 = address
|
registry.save
|
||||||
|
registry.update_leases
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def add_command(args)
|
||||||
|
ipv4, ipv6 = nil, nil
|
||||||
|
parser = OptionParser.new do |opts|
|
||||||
|
opts.banner = "Usage: dhcp add [options] NAME MACADDRESS"
|
||||||
|
opts.on("-4", "--ipv4 ADDRESS", "set fixed ipv4 address") do |address|
|
||||||
|
ipv4 = address
|
||||||
|
end
|
||||||
|
opts.on("-6", "--ipv6 ADDRESS", "set fixed ipv6 address") do |address|
|
||||||
|
ipv6 = address
|
||||||
|
end
|
||||||
|
end.order!(args)
|
||||||
|
if ARGV.size < 2
|
||||||
|
$stderr.puts "no enough arguments"
|
||||||
|
die(parser.help)
|
||||||
|
end
|
||||||
|
name, macaddress = args
|
||||||
|
@registry.add_lease(name, macaddress, ipv4, ipv6)
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_command(args)
|
||||||
|
parser = OptionParser.new do |opts|
|
||||||
|
opts.banner = "Usage: dhcp remove NAME"
|
||||||
|
end.order!(args)
|
||||||
|
if args.empty?
|
||||||
|
$stderr.puts "no enough arguments"
|
||||||
|
die(parser.help)
|
||||||
|
end
|
||||||
|
@registry.remove_lease(args.first)
|
||||||
end
|
end
|
||||||
end.order!
|
|
||||||
if ARGV.size < 2
|
|
||||||
$stderr.puts "no enough arguments"
|
|
||||||
die(parser.help)
|
|
||||||
end
|
|
||||||
name, macaddress = args
|
|
||||||
registry.add_lease(name, macaddress, ipv4, ipv6)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_command(registry, args)
|
Application.new.run(ARGV)
|
||||||
parser = OptionParser.new do |opts|
|
|
||||||
opts.banner = "Usage: dhcp remove NAME"
|
|
||||||
end.order!
|
|
||||||
if args.empty?
|
|
||||||
$stderr.puts "no enough arguments"
|
|
||||||
die(parser.help)
|
|
||||||
end
|
|
||||||
registry.remove_lease(args.first)
|
|
||||||
end
|
|
||||||
|
|
||||||
GLOBAL_OPTIONS.order!
|
|
||||||
registry = DhcpRegistry.new
|
|
||||||
case command = ARGV.shift
|
|
||||||
when "add"
|
|
||||||
add_command(registry, ARGV)
|
|
||||||
when "remove"
|
|
||||||
remove_command(registry, ARGV)
|
|
||||||
when nil # just update
|
|
||||||
else
|
|
||||||
die "unknown subcommand #{command}"
|
|
||||||
end
|
|
||||||
|
|
||||||
registry.save
|
|
||||||
registry.update_leases
|
|
||||||
|
Loading…
Reference in New Issue
Block a user