luci-app-openvpn: run '/etc/init.d/openvpn reload' after commit
[oweals/luci.git] / applications / luci-app-openvpn / luasrc / model / cbi / openvpn.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local fs  = require "nixio.fs"
5 local sys = require "luci.sys"
6 local uci = require "luci.model.uci".cursor()
7 local testfullps = luci.sys.exec("ps --help 2>&1 | grep BusyBox") --check which ps do we have
8 local psstring = (string.len(testfullps)>0) and  "ps w" or  "ps axfw" --set command we use to get pid
9
10 local m = Map("openvpn", translate("OpenVPN"))
11 local s = m:section( TypedSection, "openvpn", translate("OpenVPN instances"), translate("Below is a list of configured OpenVPN instances and their current state") )
12 s.template = "cbi/tblsection"
13 s.template_addremove = "openvpn/cbi-select-input-add"
14 s.addremove = true
15 s.add_select_options = { }
16 s.extedit = luci.dispatcher.build_url(
17         "admin", "services", "openvpn", "basic", "%s"
18 )
19
20 uci:load("openvpn_recipes")
21 uci:foreach( "openvpn_recipes", "openvpn_recipe",
22         function(section)
23                 s.add_select_options[section['.name']] =
24                         section['_description'] or section['.name']
25         end
26 )
27
28 function s.getPID(section) -- Universal function which returns valid pid # or nil
29         local pid = sys.exec("%s | grep -w %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
30         if pid and #pid > 0 and tonumber(pid) ~= nil then
31                 return tonumber(pid)
32         else
33                 return nil
34         end
35 end
36
37 function s.parse(self, section)
38         local recipe = luci.http.formvalue(
39                 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
40                 self.sectiontype .. ".select"
41         )
42
43         if recipe and not s.add_select_options[recipe] then
44                 self.invalid_cts = true
45         else
46                 TypedSection.parse( self, section )
47         end
48 end
49
50 function s.create(self, name)
51         local recipe = luci.http.formvalue(
52                 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
53                 self.sectiontype .. ".select"
54         )
55         name = luci.http.formvalue(
56                 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
57                 self.sectiontype .. ".text"
58         )
59         if #name > 3 and not name:match("[^a-zA-Z0-9_]") then
60                 uci:section(
61                         "openvpn", "openvpn", name,
62                         uci:get_all( "openvpn_recipes", recipe )
63                 )
64
65                 uci:delete("openvpn", name, "_role")
66                 uci:delete("openvpn", name, "_description")
67                 uci:save("openvpn")
68
69                 luci.http.redirect( self.extedit:format(name) )
70         elseif #name > 0 then
71                 self.invalid_cts = true
72         end
73
74         return 0
75 end
76
77
78 s:option( Flag, "enabled", translate("Enabled") )
79
80 local active = s:option( DummyValue, "_active", translate("Started") )
81 function active.cfgvalue(self, section)
82         local pid = s.getPID(section)
83         if pid ~= nil then
84                 return (sys.process.signal(pid, 0))
85                         and translatef("yes (%i)", pid)
86                         or  translate("no")
87         end
88         return translate("no")
89 end
90
91 local updown = s:option( Button, "_updown", translate("Start/Stop") )
92 updown._state = false
93 updown.redirect = luci.dispatcher.build_url(
94         "admin", "services", "openvpn"
95 )
96 function updown.cbid(self, section)
97         local pid = s.getPID(section)
98         self._state = pid ~= nil and sys.process.signal(pid, 0)
99         self.option = self._state and "stop" or "start"
100         return AbstractValue.cbid(self, section)
101 end
102 function updown.cfgvalue(self, section)
103         self.title = self._state and "stop" or "start"
104         self.inputstyle = self._state and "reset" or "reload"
105 end
106 function updown.write(self, section, value)
107         if self.option == "stop" then
108                 luci.sys.call("/etc/init.d/openvpn stop %s" % section)
109         else
110                 luci.sys.call("/etc/init.d/openvpn start %s" % section)
111         end
112         luci.http.redirect( self.redirect )
113 end
114
115
116 local port = s:option( DummyValue, "port", translate("Port") )
117 function port.cfgvalue(self, section)
118         local val = AbstractValue.cfgvalue(self, section)
119         return val or "1194"
120 end
121
122 local proto = s:option( DummyValue, "proto", translate("Protocol") )
123 function proto.cfgvalue(self, section)
124         local val = AbstractValue.cfgvalue(self, section)
125         return val or "udp"
126 end
127
128 function m.on_after_commit(self,map)
129         require("luci.sys").call('/etc/init.d/openvpn reload')
130 end
131
132 return m