luci-mod-admin-full: switch to nixio.fs
[project/luci.git] / modules / luci-mod-admin-full / luasrc / model / cbi / admin_system / system.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 local sys   = require "luci.sys"
17 local zones = require "luci.sys.zoneinfo"
18 local fs    = require "nixio.fs"
19 local conf  = require "luci.config"
20
21 local m, s, o
22 local has_ntpd = fs.access("/usr/sbin/ntpd")
23
24 m = Map("system", translate("System"), translate("Here you can configure the basic aspects of your device like its hostname or the timezone."))
25 m:chain("luci")
26
27
28 s = m:section(TypedSection, "system", translate("System Properties"))
29 s.anonymous = true
30 s.addremove = false
31
32 s:tab("general",  translate("General Settings"))
33 s:tab("logging",  translate("Logging"))
34 s:tab("language", translate("Language and Style"))
35
36
37 --
38 -- System Properties
39 --
40
41 o = s:taboption("general", DummyValue, "_systime", translate("Local Time"))
42 o.template = "admin_system/clock_status"
43
44
45 o = s:taboption("general", Value, "hostname", translate("Hostname"))
46 o.datatype = "hostname"
47
48 function o.write(self, section, value)
49         Value.write(self, section, value)
50         sys.hostname(value)
51 end
52
53
54 o = s:taboption("general", ListValue, "zonename", translate("Timezone"))
55 o:value("UTC")
56
57 for i, zone in ipairs(zones.TZ) do
58         o:value(zone[1])
59 end
60
61 function o.write(self, section, value)
62         local function lookup_zone(title)
63                 for _, zone in ipairs(zones.TZ) do
64                         if zone[1] == title then return zone[2] end
65                 end
66         end
67
68         AbstractValue.write(self, section, value)
69         local timezone = lookup_zone(value) or "GMT0"
70         self.map.uci:set("system", section, "timezone", timezone)
71         fs.writefile("/etc/TZ", timezone .. "\n")
72 end
73
74
75 --
76 -- Logging
77 --
78
79 o = s:taboption("logging", Value, "log_size", translate("System log buffer size"), "kiB")
80 o.optional    = true
81 o.placeholder = 16
82 o.datatype    = "uinteger"
83
84 o = s:taboption("logging", Value, "log_ip", translate("External system log server"))
85 o.optional    = true
86 o.placeholder = "0.0.0.0"
87 o.datatype    = "ip4addr"
88
89 o = s:taboption("logging", Value, "log_port", translate("External system log server port"))
90 o.optional    = true
91 o.placeholder = 514
92 o.datatype    = "port"
93
94 o = s:taboption("logging", ListValue, "conloglevel", translate("Log output level"))
95 o:value(8, translate("Debug"))
96 o:value(7, translate("Info"))
97 o:value(6, translate("Notice"))
98 o:value(5, translate("Warning"))
99 o:value(4, translate("Error"))
100 o:value(3, translate("Critical"))
101 o:value(2, translate("Alert"))
102 o:value(1, translate("Emergency"))
103
104 o = s:taboption("logging", ListValue, "cronloglevel", translate("Cron Log Level"))
105 o.default = 8
106 o:value(5, translate("Debug"))
107 o:value(8, translate("Normal"))
108 o:value(9, translate("Warning"))
109
110
111 --
112 -- Langauge & Style
113 --
114
115 o = s:taboption("language", ListValue, "_lang", translate("Language"))
116 o:value("auto")
117
118 local i18ndir = luci.i18n.i18ndir .. "base."
119 for k, v in luci.util.kspairs(conf.languages) do
120         local file = i18ndir .. k:gsub("_", "-")
121         if k:sub(1, 1) ~= "." and fs.access(file .. ".lmo") then
122                 o:value(k, v)
123         end
124 end
125
126 function o.cfgvalue(...)
127         return m.uci:get("luci", "main", "lang")
128 end
129
130 function o.write(self, section, value)
131         m.uci:set("luci", "main", "lang", value)
132 end
133
134
135 o = s:taboption("language", ListValue, "_mediaurlbase", translate("Design"))
136 for k, v in pairs(conf.themes) do
137         if k:sub(1, 1) ~= "." then
138                 o:value(v, k)
139         end
140 end
141
142 function o.cfgvalue(...)
143         return m.uci:get("luci", "main", "mediaurlbase")
144 end
145
146 function o.write(self, section, value)
147         m.uci:set("luci", "main", "mediaurlbase", value)
148 end
149
150
151 --
152 -- NTP
153 --
154
155 if has_ntpd then
156
157         -- timeserver setup was requested, create section and reload page
158         if m:formvalue("cbid.system._timeserver._enable") then
159                 m.uci:section("system", "timeserver", "ntp",
160                         {
161                         server = { "0.openwrt.pool.ntp.org", "1.openwrt.pool.ntp.org", "2.openwrt.pool.ntp.org", "3.openwrt.pool.ntp.org" }
162                         }
163                 )
164
165                 m.uci:save("system")
166                 luci.http.redirect(luci.dispatcher.build_url("admin/system", arg[1]))
167                 return
168         end
169
170         local has_section = false
171         m.uci:foreach("system", "timeserver", 
172                 function(s) 
173                         has_section = true 
174                         return false
175         end)
176
177         if not has_section then
178
179                 s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
180                 s.anonymous   = true
181                 s.cfgsections = function() return { "_timeserver" } end
182
183                 x = s:option(Button, "_enable")
184                 x.title      = translate("Time Synchronization is not configured yet.")
185                 x.inputtitle = translate("Set up Time Synchronization")
186                 x.inputstyle = "apply"
187
188         else
189                 
190                 s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
191                 s.anonymous = true
192                 s.addremove = false
193
194                 o = s:option(Flag, "enable", translate("Enable NTP client"))
195                 o.rmempty = false
196
197                 function o.cfgvalue(self)
198                         return sys.init.enabled("sysntpd")
199                                 and self.enabled or self.disabled
200                 end
201
202                 function o.write(self, section, value)
203                         if value == self.enabled then
204                                 sys.init.enable("sysntpd")
205                                 sys.call("env -i /etc/init.d/sysntpd start >/dev/null")
206                         else
207                                 sys.call("env -i /etc/init.d/sysntpd stop >/dev/null")
208                                 sys.init.disable("sysntpd")
209                         end
210                 end
211
212
213                 o = s:option(Flag, "enable_server", translate("Provide NTP server"))
214                 o:depends("enable", "1")
215
216
217                 o = s:option(DynamicList, "server", translate("NTP server candidates"))
218                 o.datatype = "host"
219                 o:depends("enable", "1")
220
221                 -- retain server list even if disabled
222                 function o.remove() end
223
224         end
225 end
226
227 return m