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