Rework LuCI build system
[project/luci.git] / applications / luci-app-openvpn / luasrc / model / cbi / openvpn.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local fs = require "nixio.fs"
16 local sys = require "luci.sys"
17 local uci = require "luci.model.uci".cursor()
18 local testfullps = luci.sys.exec("ps --help 2>&1 | grep BusyBox") --check which ps do we have
19 local psstring = (string.len(testfullps)>0) and "ps w" or "ps axfw" --set command we use to get pid
20
21 local m = Map("openvpn", translate("OpenVPN"))
22 local s = m:section( TypedSection, "openvpn", translate("OpenVPN instances"), translate("Below is a list of configured OpenVPN instances and their current state") )
23 s.template = "cbi/tblsection"
24 s.template_addremove = "openvpn/cbi-select-input-add"
25 s.addremove = true
26 s.add_select_options = { }
27 s.extedit = luci.dispatcher.build_url(
28 "admin", "services", "openvpn", "basic", "%s"
29 )
30
31 uci:load("openvpn_recipes")
32 uci:foreach( "openvpn_recipes", "openvpn_recipe",
33 function(section)
34 s.add_select_options[section['.name']] =
35 section['_description'] or section['.name']
36 end
37 )
38
39 function s.parse(self, section)
40 local recipe = luci.http.formvalue(
41 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
42 self.sectiontype .. ".select"
43 )
44
45 if recipe and not s.add_select_options[recipe] then
46 self.invalid_cts = true
47 else
48 TypedSection.parse( self, section )
49 end
50 end
51
52 function s.create(self, name)
53 local recipe = luci.http.formvalue(
54 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
55 self.sectiontype .. ".select"
56 )
57 name = luci.http.formvalue(
58 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
59 self.sectiontype .. ".text"
60 )
61 if string.len(name)>3 and not name:match("[^a-zA-Z0-9_]") then
62 uci:section(
63 "openvpn", "openvpn", name,
64 uci:get_all( "openvpn_recipes", recipe )
65 )
66
67 uci:delete("openvpn", name, "_role")
68 uci:delete("openvpn", name, "_description")
69 uci:save("openvpn")
70
71 luci.http.redirect( self.extedit:format(name) )
72 else
73 self.invalid_cts = true
74 end
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 = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
83 if pid and #pid > 0 and tonumber(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 = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
98 self._state = pid and #pid > 0 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 local pid = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
109 sys.process.signal(pid,15)
110 else
111 luci.sys.call("/etc/init.d/openvpn start %s" % section)
112 end
113 luci.http.redirect( self.redirect )
114 end
115
116
117 local port = s:option( DummyValue, "port", translate("Port") )
118 function port.cfgvalue(self, section)
119 local val = AbstractValue.cfgvalue(self, section)
120 return val or "1194"
121 end
122
123 local proto = s:option( DummyValue, "proto", translate("Protocol") )
124 function proto.cfgvalue(self, section)
125 local val = AbstractValue.cfgvalue(self, section)
126 return val or "udp"
127 end
128
129
130 return m