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
|
||||
Available subcommands:
|
||||
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.
|
||||
HELP
|
||||
end
|
||||
|
||||
def add_command(registry, 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
|
||||
class Application
|
||||
def run(args)
|
||||
GLOBAL_OPTIONS.order!(args)
|
||||
@registry = DhcpRegistry.new
|
||||
case command = args.shift
|
||||
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
|
||||
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.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
|
||||
|
||||
def remove_command(registry, args)
|
||||
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
|
||||
Application.new.run(ARGV)
|
||||
|
Loading…
Reference in New Issue
Block a user