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