dn42-scripts/scripts/openvpn

44 lines
1.3 KiB
Plaintext
Raw Normal View History

2015-01-13 07:19:32 +00:00
#!/usr/bin/ruby
require_relative "utils"
template_path = Pathname.new(File.expand_path("../../templates", __FILE__))
openvpn_path = Pathname.new(File.expand_path("../../openvpn", __FILE__))
openvpn_template = Template.new(template_path.join("openvpn.conf.erb"))
registry = Registry.new
host = registry.data["host"]
host["v4_tunnel"] || die("v4_tunnel not set for host")
registry.data["network"].each do |name, data|
next unless data["type"] == "openvpn"
key = openvpn_path.join("#{name}.key")
unless File.exists?(key)
sh("openvpn", "--genkey", "--secret", key)
end
required_params = [:proto, :lport, :tunnel_v4]
unless data["float"]
required_params += [:remote, :rport]
end
required_params.each do |param|
unless data[param.to_s]
die "#{param.to_s} not set for peer #{name}"
end
end
context = data.merge(own_v4_tunnel: host["v4_tunnel"])
atomic_write(openvpn_path.join("#{name}.conf"), openvpn_template.render(context))
end
GLOBAL_OPTIONS = OptionParser.new do |opts|
opts.banner = "Usage: dhcp [options] [subcommand [options]]"
opts.separator ""
opts.separator <<HELP
Available subcommands:
add [options] NAME MACADDRESS: add dhcp lease
remove [options] NAME: remove dhcp static lease
See 'dhcp COMMAND --help' for more information on a specific command.
HELP
end