libs/sys: extend luci.sys.hostname() to allow setting a new name
authorJo-Philipp Wich <jow@openwrt.org>
Sun, 1 Feb 2009 17:08:40 +0000 (17:08 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Sun, 1 Feb 2009 17:08:40 +0000 (17:08 +0000)
modules/admin-{full,mini}: update the hostname at runtime

libs/sys/luasrc/sys.lua
modules/admin-full/luasrc/model/cbi/admin_system/system.lua
modules/admin-mini/luasrc/model/cbi/mini/system.lua

index a2c9a31..3e2ce7f 100644 (file)
@@ -129,10 +129,16 @@ end
 -- @return             Table containing all variables if no variable name is given
 getenv = posix.getenv
 
 -- @return             Table containing all variables if no variable name is given
 getenv = posix.getenv
 
---- Determine the current hostname.
+--- Get or set the current hostname.
+-- @param              String containing a new hostname to set (optional)
 -- @return             String containing the system hostname
 -- @return             String containing the system hostname
-function hostname()
-       return posix.uname("%n")
+function hostname(newname)
+       if type(newname) == "string" and #newname > 0 then
+               luci.fs.writefile( "/proc/sys/kernel/hostname", newname .. "\n" )
+               return newname
+       else
+               return posix.uname("%n")
+       end
 end
 
 --- Returns the contents of a documented referred by an URL.
 end
 
 --- Returns the contents of a documented referred by an URL.
index 9820cce..6814680 100644 (file)
@@ -28,10 +28,10 @@ s:option(DummyValue, "_system", translate("system")).value = system
 s:option(DummyValue, "_cpu", translate("m_i_processor")).value = model
 
 local load1, load5, load15 = luci.sys.loadavg()
 s:option(DummyValue, "_cpu", translate("m_i_processor")).value = model
 
 local load1, load5, load15 = luci.sys.loadavg()
-s:option(DummyValue, "_la", translate("load")).value = 
+s:option(DummyValue, "_la", translate("load")).value =
  string.format("%.2f, %.2f, %.2f", load1, load5, load15)
  string.format("%.2f, %.2f, %.2f", load1, load5, load15)
-s:option(DummyValue, "_memtotal", translate("m_i_memory")).value = 
+
+s:option(DummyValue, "_memtotal", translate("m_i_memory")).value =
  string.format("%.2f MB (%.0f%% %s, %.0f%% %s, %.0f%% %s)",
   tonumber(memtotal) / 1024,
   100 * memcached / memtotal,
  string.format("%.2f MB (%.0f%% %s, %.0f%% %s, %.0f%% %s)",
   tonumber(memtotal) / 1024,
   100 * memcached / memtotal,
@@ -43,11 +43,17 @@ s:option(DummyValue, "_memtotal", translate("m_i_memory")).value =
 
 s:option(DummyValue, "_systime", translate("m_i_systemtime")).value =
  os.date("%c")
 
 s:option(DummyValue, "_systime", translate("m_i_systemtime")).value =
  os.date("%c")
-s:option(DummyValue, "_uptime", translate("m_i_uptime")).value = 
+
+s:option(DummyValue, "_uptime", translate("m_i_uptime")).value =
  luci.tools.webadmin.date_format(tonumber(uptime))
 
  luci.tools.webadmin.date_format(tonumber(uptime))
 
-s:option(Value, "hostname", translate("hostname"))
+hn = s:option(Value, "hostname", translate("hostname"))
+
+function hn.write(self, section, value)
+       Value.write(self, section, value)
+       luci.sys.hostname(value)
+end
+
 
 tz = s:option(ListValue, "zonename", translate("timezone"))
 tz:value("UTC")
 
 tz = s:option(ListValue, "zonename", translate("timezone"))
 tz:value("UTC")
index 5c06a0f..fa23b7c 100644 (file)
@@ -31,10 +31,10 @@ s:option(DummyValue, "_system", translate("system")).value = system
 s:option(DummyValue, "_cpu", translate("m_i_processor")).value = model
 
 local load1, load5, load15 = luci.sys.loadavg()
 s:option(DummyValue, "_cpu", translate("m_i_processor")).value = model
 
 local load1, load5, load15 = luci.sys.loadavg()
-s:option(DummyValue, "_la", translate("load")).value = 
+s:option(DummyValue, "_la", translate("load")).value =
  string.format("%.2f, %.2f, %.2f", load1, load5, load15)
  string.format("%.2f, %.2f, %.2f", load1, load5, load15)
-s:option(DummyValue, "_memtotal", translate("m_i_memory")).value = 
+
+s:option(DummyValue, "_memtotal", translate("m_i_memory")).value =
  string.format("%.2f MB (%.0f%% %s, %.0f%% %s, %.0f%% %s)",
   tonumber(memtotal) / 1024,
   100 * memcached / memtotal,
  string.format("%.2f MB (%.0f%% %s, %.0f%% %s, %.0f%% %s)",
   tonumber(memtotal) / 1024,
   100 * memcached / memtotal,
@@ -46,11 +46,17 @@ s:option(DummyValue, "_memtotal", translate("m_i_memory")).value =
 
 s:option(DummyValue, "_systime", translate("m_i_systemtime")).value =
  os.date("%c")
 
 s:option(DummyValue, "_systime", translate("m_i_systemtime")).value =
  os.date("%c")
-s:option(DummyValue, "_uptime", translate("m_i_uptime")).value = 
+
+s:option(DummyValue, "_uptime", translate("m_i_uptime")).value =
  luci.tools.webadmin.date_format(tonumber(uptime))
  luci.tools.webadmin.date_format(tonumber(uptime))
-s:option(Value, "hostname", translate("hostname"))
+
+hn = s:option(Value, "hostname", translate("hostname"))
+
+function hn.write(self, section, value)
+       Value.write(self, section, value)
+       luci.sys.hostname(value)
+end
+
 
 tz = s:option(ListValue, "zonename", translate("timezone"))
 tz:value("UTC")
 
 tz = s:option(ListValue, "zonename", translate("timezone"))
 tz:value("UTC")