Rework LuCI build system
[project/luci.git] / applications / luci-app-ddns / luasrc / model / cbi / ddns / overview.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
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 NXFS = require "nixio.fs"
16 local CTRL = require "luci.controller.ddns"     -- this application's controller
17 local DISP = require "luci.dispatcher"
18 local HTTP = require "luci.http"
19 local SYS  = require "luci.sys"
20 local DDNS = require "luci.tools.ddns"          -- ddns multiused functions
21
22 -- show hints ?
23 show_hints = not (DDNS.check_ipv6()             -- IPv6 support
24                 and DDNS.check_ssl()            -- HTTPS support
25                 and DDNS.check_proxy()          -- Proxy support
26                 and DDNS.check_bind_host()      -- DNS TCP support
27                 )
28 need_update = CTRL.update_needed()              -- correct ddns-scripts version
29
30 -- html constants
31 font_red = [[<font color="red">]]
32 font_off = [[</font>]]
33 bold_on  = [[<strong>]]
34 bold_off = [[</strong>]]
35
36 -- cbi-map definition -- #######################################################
37 m = Map("ddns")
38
39 -- first need to close <a> from cbi map template our <a> closed by template
40 --m.title = [[</a><a href="javascript:alert(']] .. CTRL.show_versions() ..[[')">]] ..
41 --              translate("Dynamic DNS")
42 m.title = [[</a><a href="#" onclick="onclick_maptitle();">]] ..
43                 translate("Dynamic DNS")
44
45 m.description = translate("Dynamic DNS allows that your router can be reached with " ..
46                         "a fixed hostname while having a dynamically changing " ..
47                         "IP address.")
48
49 m.on_after_commit = function(self)
50         if self.changed then    -- changes ?
51                 if SYS.init.enabled("ddns") then        -- ddns service enabled, restart all
52                         os.execute("/etc/init.d/ddns restart")
53                 else    -- ddns service disabled, send SIGHUP to running
54                         os.execute("killall -1 dynamic_dns_updater.sh")
55                 end
56         end
57 end
58
59 -- SimpleSection definiton -- ##################################################
60 -- with all the JavaScripts we need for "a good Show"
61 a = m:section( SimpleSection )
62 a.template = "ddns/overview_status"
63
64 -- SimpleSection definition -- #################################################
65 -- show Hints to optimize installation and script usage
66 -- only show if         service not enabled
67 --              or      no IPv6 support
68 --              or      not GNU Wget and not cURL       (for https support)
69 --              or      not GNU Wget but cURL without proxy support
70 --              or      not BIND's host
71 --              or      ddns-scripts package need update
72 if show_hints or need_update or not SYS.init.enabled("ddns") then
73         s = m:section( SimpleSection, translate("Hints") )
74
75         -- ddns_scripts needs to be updated for full functionality
76         if need_update then
77                 local dv = s:option(DummyValue, "_update_needed")
78                 dv.titleref = DISP.build_url("admin", "system", "packages")
79                 dv.rawhtml  = true
80                 dv.title = font_red .. bold_on ..
81                         translate("Software update required") .. bold_off .. font_off
82                 dv.value = translate("The currently installed 'ddns-scripts' package did not support all available settings.") ..
83                                 "<br />" ..
84                                 translate("Please update to the current version!")
85         end
86
87         -- DDNS Service disabled
88         if not SYS.init.enabled("ddns") then
89                 local dv = s:option(DummyValue, "_not_enabled")
90                 dv.titleref = DISP.build_url("admin", "system", "startup")
91                 dv.rawhtml  = true
92                 dv.title = bold_on ..
93                         translate("DDNS Autostart disabled") .. bold_off
94                 dv.value = translate("Currently DDNS updates are not started at boot or on interface events." .. "<br />" ..
95                                 "You can start/stop each configuration here. It will run until next reboot.")
96         end
97
98         -- Show more hints on a separate page
99         if show_hints then
100                 local dv = s:option(DummyValue, "_separate")
101                 dv.titleref = DISP.build_url("admin", "services", "ddns", "hints")
102                 dv.rawhtml  = true
103                 dv.title = bold_on ..
104                         translate("Show more") .. bold_off
105                 dv.value = translate("Follow this link" .. "<br />" ..
106                                 "You will find more hints to optimize your system to run DDNS scripts with all options")
107         end
108 end
109
110 -- TableSection definition -- ##################################################
111 ts = m:section( TypedSection, "service",
112         translate("Overview"),
113         translate("Below is a list of configured DDNS configurations and their current state." .. "<br />" ..
114                 "If you want to send updates for IPv4 and IPv6 you need to define two separate Configurations " ..
115                 "i.e. 'myddns_ipv4' and 'myddns_ipv6'") )
116 ts.sectionhead = translate("Configuration")
117 ts.template = "cbi/tblsection"
118 ts.addremove = true
119 ts.extedit = DISP.build_url("admin", "services", "ddns", "detail", "%s")
120 function ts.create(self, name)
121         AbstractSection.create(self, name)
122         HTTP.redirect( self.extedit:format(name) )
123 end
124
125 -- Domain and registered IP -- #################################################
126 dom = ts:option(DummyValue, "_domainIP",
127         translate("Hostname/Domain") .. "<br />" .. translate("Registered IP") )
128 dom.template = "ddns/overview_doubleline"
129 function dom.set_one(self, section)
130         local domain = self.map:get(section, "domain") or ""
131         if domain ~= "" then
132                 return domain
133         else
134                 return [[<em>]] .. translate("config error") .. [[</em>]]
135         end
136 end
137 function dom.set_two(self, section)
138         local domain = self.map:get(section, "domain") or ""
139         if domain == "" then return "" end
140         local dnsserver = self.map:get(section, "dnsserver") or ""
141         local use_ipv6 = tonumber(self.map:get(section, "use_ipv6") or 0)
142         local force_ipversion = tonumber(self.map:get(section, "force_ipversion") or 0)
143         local force_dnstcp = tonumber(self.map:get(section, "force_dnstcp") or 0)
144         local command = [[/usr/lib/ddns/dynamic_dns_lucihelper.sh]]
145         if not NXFS.access(command, "rwx", "rx", "rx") then
146                 NXFS.chmod(command, 755)
147         end
148         command = command .. [[ get_registered_ip ]] .. domain .. [[ ]] .. use_ipv6 ..
149                 [[ ]] .. force_ipversion .. [[ ]] .. force_dnstcp .. [[ ]] .. dnsserver
150         local ip = SYS.exec(command)
151         if ip == "" then ip = translate("no data") end
152         return ip
153 end
154
155 -- enabled
156 ena = ts:option( Flag, "enabled",
157         translate("Enabled"))
158 ena.template = "ddns/overview_enabled"
159 ena.rmempty = false
160 function ena.parse(self, section)
161         DDNS.flag_parse(self, section)
162 end
163
164 -- show PID and next update
165 upd = ts:option( DummyValue, "_update",
166         translate("Last Update") .. "<br />" .. translate("Next Update"))
167 upd.template = "ddns/overview_doubleline"
168 function upd.set_one(self, section)     -- fill Last Update
169         -- get/validate last update
170         local uptime   = SYS.uptime()
171         local lasttime = DDNS.get_lastupd(section)
172         if lasttime > uptime then       -- /var might not be linked to /tmp and cleared on reboot
173                 lasttime = 0
174         end
175
176         -- no last update happen
177         if lasttime == 0 then
178                 return translate("never")
179
180         -- we read last update
181         else
182                 -- calc last update
183                 --            os.epoch  - sys.uptime + lastupdate(uptime)
184                 local epoch = os.time() - uptime + lasttime
185                 -- use linux date to convert epoch
186                 return DDNS.epoch2date(epoch)
187         end
188 end
189 function upd.set_two(self, section)     -- fill Next Update
190         -- get enabled state
191         local enabled   = tonumber(self.map:get(section, "enabled") or 0)
192         local datenext  = translate("unknown error")    -- formatted date of next update
193
194         -- get force seconds
195         local force_interval = tonumber(self.map:get(section, "force_interval") or 72)
196         local force_unit = self.map:get(section, "force_unit") or "hours"
197         local force_seconds = DDNS.calc_seconds(force_interval, force_unit)
198
199         -- get last update and get/validate PID
200         local uptime   = SYS.uptime()
201         local lasttime = DDNS.get_lastupd(section)
202         if lasttime > uptime then       -- /var might not be linked to /tmp and cleared on reboot
203                 lasttime = 0
204         end
205         local pid      = DDNS.get_pid(section)
206
207         -- calc next update
208         if lasttime > 0 then
209                 local epoch = os.time() - uptime + lasttime + force_seconds
210                 -- use linux date to convert epoch
211                 datelast = DDNS.epoch2date(epoch)
212         end
213
214         -- process running but update needs to happen
215         if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
216                 datenext = translate("Verify")
217
218         -- run once
219         elseif force_seconds == 0 then
220                 datenext = translate("Run once")
221
222         -- no process running and NOT enabled
223         elseif pid == 0 and enabled == 0 then
224                 datenext  = translate("Disabled")
225
226         -- no process running and NOT
227         elseif pid == 0 and enabled ~= 0 then
228                 datenext = translate("Stopped")
229         end
230
231         return datenext
232 end
233
234 -- start/stop button
235 btn = ts:option( Button, "_startstop",
236         translate("Process ID") .. "<br />" .. translate("Start / Stop") )
237 btn.template = "ddns/overview_startstop"
238 function btn.cfgvalue(self, section)
239         local pid = DDNS.get_pid(section)
240         if pid > 0 then
241                 btn.inputtitle  = "PID: " .. pid
242                 btn.inputstyle  = "reset"
243                 btn.disabled    = false
244         elseif (self.map:get(section, "enabled") or "0") ~= "0" then
245                 btn.inputtitle  = translate("Start")
246                 btn.inputstyle  = "apply"
247                 btn.disabled    = false
248         else
249                 btn.inputtitle  = "----------"
250                 btn.inputstyle  = "button"
251                 btn.disabled    = true
252         end
253         return true
254 end
255
256 return m