Merge pull request #1818 from dibdot/lxc_fix
[project/luci.git] / applications / luci-app-ddns / luasrc / model / cbi / ddns / overview.lua
1 -- Copyright 2014-2018 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 chk_sec  = DDNS.calc_seconds(
126                                 tonumber(self.map:get(section, "check_interval")) or 10,
127                                 self.map:get(section, "check_unit") or "minutes" )
128         local ip = DDNS.get_regip(section, chk_sec)
129         if ip == "NOFILE" then
130                 local lookup_host = self.map:get(section, "lookup_host") or ""
131                 if lookup_host == "" then return "" end
132                 local dnsserver = self.map:get(section, "dnsserver") or ""
133                 local use_ipv6 = tonumber(self.map:get(section, "use_ipv6") or 0)
134                 local force_ipversion = tonumber(self.map:get(section, "force_ipversion") or 0)
135                 local force_dnstcp = tonumber(self.map:get(section, "force_dnstcp") or 0)
136                 local is_glue = tonumber(self.map:get(section, "is_glue") or 0)
137                 local command = CTRL.luci_helper .. [[ -]]
138                 if (use_ipv6 == 1) then command = command .. [[6]] end
139                 if (force_ipversion == 1) then command = command .. [[f]] end
140                 if (force_dnstcp == 1) then command = command .. [[t]] end
141                 if (is_glue == 1) then command = command .. [[g]] end
142                 command = command .. [[l ]] .. lookup_host
143                 command = command .. [[ -S ]] .. section
144                 if (#dnsserver > 0) then command = command .. [[ -d ]] .. dnsserver end
145                 command = command .. [[ -- get_registered_ip]]
146                 ip = SYS.exec(command)
147         end
148         if ip == "" then ip = translate("no data") end
149         return ip
150 end
151
152 -- enabled
153 ena = ts:option( Flag, "enabled",
154         translate("Enabled"))
155 ena.template = "ddns/overview_enabled"
156 ena.rmempty = false
157
158 -- show PID and next update
159 upd = ts:option( DummyValue, "_update",
160         translate("Last Update") .. "<br />" .. translate("Next Update"))
161 upd.template = "ddns/overview_doubleline"
162 function upd.set_one(self, section)     -- fill Last Update
163         -- get/validate last update
164         local uptime   = SYS.uptime()
165         local lasttime = DDNS.get_lastupd(section)
166         if lasttime > uptime then       -- /var might not be linked to /tmp and cleared on reboot
167                 lasttime = 0
168         end
169
170         -- no last update happen
171         if lasttime == 0 then
172                 return translate("never")
173
174         -- we read last update
175         else
176                 -- calc last update
177                 --            os.epoch  - sys.uptime + lastupdate(uptime)
178                 local epoch = os.time() - uptime + lasttime
179                 -- use linux date to convert epoch
180                 return DDNS.epoch2date(epoch)
181         end
182 end
183 function upd.set_two(self, section)     -- fill Next Update
184         -- get enabled state
185         local enabled   = tonumber(self.map:get(section, "enabled") or 0)
186         local datenext  = translate("unknown error")    -- formatted date of next update
187
188         -- get force seconds
189         local force_interval = tonumber(self.map:get(section, "force_interval") or 72)
190         local force_unit = self.map:get(section, "force_unit") or "hours"
191         local force_seconds = DDNS.calc_seconds(force_interval, force_unit)
192
193         -- get last update and get/validate PID
194         local uptime   = SYS.uptime()
195         local lasttime = DDNS.get_lastupd(section)
196         if lasttime > uptime then       -- /var might not be linked to /tmp and cleared on reboot
197                 lasttime = 0
198         end
199         local pid      = DDNS.get_pid(section)
200
201         -- calc next update
202         if lasttime > 0 then
203                 local epoch = os.time() - uptime + lasttime + force_seconds
204                 -- use linux date to convert epoch
205                 datelast = DDNS.epoch2date(epoch)
206         end
207
208         -- process running but update needs to happen
209         if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
210                 datenext = translate("Verify")
211
212         -- run once
213         elseif force_seconds == 0 then
214                 datenext = translate("Run once")
215
216         -- no process running and NOT enabled
217         elseif pid == 0 and enabled == 0 then
218                 datenext  = translate("Disabled")
219
220         -- no process running and NOT
221         elseif pid == 0 and enabled ~= 0 then
222                 datenext = translate("Stopped")
223         end
224
225         return datenext
226 end
227
228 -- start/stop button
229 btn = ts:option( Button, "_startstop",
230         translate("Process ID") .. "<br />" .. translate("Start / Stop") )
231 btn.template = "ddns/overview_startstop"
232 function btn.cfgvalue(self, section)
233         local pid = DDNS.get_pid(section)
234         if pid > 0 then
235                 btn.inputtitle  = "PID: " .. pid
236                 btn.inputstyle  = "reset"
237                 btn.disabled    = false
238         elseif (self.map:get(section, "enabled") or "0") ~= "0" then
239                 btn.inputtitle  = translate("Start")
240                 btn.inputstyle  = "apply"
241                 btn.disabled    = false
242         else
243                 btn.inputtitle  = "----------"
244                 btn.inputstyle  = "button"
245                 btn.disabled    = true
246         end
247         return true
248 end
249
250 return m