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