start/stop openvpn profiles

This commit is contained in:
Jörg Thalheim 2015-01-19 08:37:35 +01:00
parent bb224c163a
commit a9f49bc6f5
3 changed files with 45 additions and 1 deletions

View File

@ -16,7 +16,20 @@
"start_port": 5001, "start_port": 5001,
"end_port": 5020, "end_port": 5020,
"v4_public": "dn42.higgsboson.tk", "v4_public": "dn42.higgsboson.tk",
"v6_public": "dn42.higgsboson.tk" "v6_public": "dn42.higgsboson.tk",
"bird": {
"template_name": "dnpeer",
"reload_command": "birdc reload all"
},
"openvpn": {
"start_command": "systemctl start 'openvpn@<%=peer_name%>'",
"stop_command": "systemctl stop 'openvpn@<%=peer_name%>'",
"enable_command": "systemctl enable 'openvpn@<%=peer_name%>'",
"disable_command": "systemctl disable 'openvpn@<%=peer_name%>'"
},
"dns": {
"reload_command": "rndc reload"
}
}, },
"network": { "network": {
"hax404": { "hax404": {

View File

@ -24,6 +24,9 @@ class OpenvpnRegistry < Registry
puts "==== openvpn.conf for #{name} =====" puts "==== openvpn.conf for #{name} ====="
puts peer_template puts peer_template
puts "===================================\n" puts "===================================\n"
service_command("start", name)
service_command("enable", name)
end end
def remove_peer(peer) def remove_peer(peer)
@ -34,6 +37,9 @@ class OpenvpnRegistry < Registry
unless File.exists?(key) unless File.exists?(key)
FileUtils.rm_f(key) FileUtils.rm_f(key)
end end
service_command("stop", name)
service_command("disable", name)
end end
def update_configurations def update_configurations
@ -105,6 +111,17 @@ class OpenvpnRegistry < Registry
end end
die "no free local ports in range #{@start_port}:#{@end_port}" die "no free local ports in range #{@start_port}:#{@end_port}"
end end
def service_command(command_type, peer_name)
openvpn = @host["openvpn"] || {}
cmd_name = "#{command_type}_command"
command = openvpn[cmd_name]
if command.nil?
puts "skip to #{command_type} openvpn because #{cmd_name} is not defined"
else
CommandTemplate.new(command).execute(peer_name: peer_name)
end
end
end end
GLOBAL_OPTIONS = OptionParser.new do |opts| GLOBAL_OPTIONS = OptionParser.new do |opts|

View File

@ -55,3 +55,17 @@ class Template
@erb.result(TemplateContext.new(params).get_binding) @erb.result(TemplateContext.new(params).get_binding)
end end
end end
class CommandTemplate
def initialize(command)
@erb = ERB.new(command, nil, '-')
end
def execute(params={})
args = params.dup
args.each do |k,v|
args[k] = Shellwords.escape(v)
end
cmd = @erb.result(TemplateContext.new(args).get_binding)
sh(cmd)
end
end