From 7e0075584de98f3fbad5694b2ceeffac5b215301 Mon Sep 17 00:00:00 2001
From: Matthias Schiffer <mschiffer@universe-factory.net>
Date: Sat, 27 Aug 2016 15:14:16 +0200
Subject: [PATCH] Use a "pretty" hostname where possible

Fixes #414
---
 package/gluon-config-mode-core/Makefile       |  2 +-
 .../controller/gluon-config-mode/index.lua    |  3 ++-
 .../config-mode/wizard/0100-hostname.lua      |  7 +++--
 .../config-mode/reboot/0100-mesh-vpn.lua      |  4 ++-
 package/gluon-core/Makefile                   |  2 +-
 .../luasrc/lib/gluon/upgrade/030-system       |  3 ++-
 package/gluon-luci-admin/Makefile             |  2 +-
 .../usr/lib/lua/luci/view/admin/info.htm      |  3 ++-
 package/gluon-luci-theme/Makefile             |  2 +-
 .../lib/lua/luci/view/themes/gluon/header.htm |  4 ++-
 package/gluon-respondd/Makefile               |  2 +-
 package/gluon-respondd/src/Makefile           |  2 +-
 package/gluon-respondd/src/respondd.c         | 27 +++++++++++++++----
 13 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/package/gluon-config-mode-core/Makefile b/package/gluon-config-mode-core/Makefile
index c55ebedd8..52a4f0e13 100644
--- a/package/gluon-config-mode-core/Makefile
+++ b/package/gluon-config-mode-core/Makefile
@@ -17,7 +17,7 @@ define Package/gluon-config-mode-core
   SECTION:=gluon
   CATEGORY:=Gluon
   TITLE:=Luci based config mode for user friendly setup of new mesh nodes
-  DEPENDS:=gluon-setup-mode-virtual +gluon-luci-theme +gluon-lock-password $(GLUON_I18N_PACKAGES)
+  DEPENDS:=gluon-setup-mode-virtual +gluon-luci-theme +gluon-lock-password +pretty-hostname $(GLUON_I18N_PACKAGES)
   PROVIDES:=gluon-config-mode-core-virtual
 endef
 
diff --git a/package/gluon-config-mode-core/luasrc/usr/lib/lua/luci/controller/gluon-config-mode/index.lua b/package/gluon-config-mode-core/luasrc/usr/lib/lua/luci/controller/gluon-config-mode/index.lua
index 43c963404..0a6a54950 100644
--- a/package/gluon-config-mode-core/luasrc/usr/lib/lua/luci/controller/gluon-config-mode/index.lua
+++ b/package/gluon-config-mode-core/luasrc/usr/lib/lua/luci/controller/gluon-config-mode/index.lua
@@ -51,6 +51,7 @@ function action_reboot()
   local gluon_luci = require "gluon.luci"
   local fs = require "nixio.fs"
   local util = require "nixio.util"
+  local pretty_hostname = require "pretty_hostname"
 
   local parts_dir = "/lib/gluon/config-mode/reboot/"
   local files = util.consume(fs.dir(parts_dir))
@@ -68,7 +69,7 @@ function action_reboot()
     end
   end
 
-  local hostname = uci:get_first("system", "system", "hostname")
+  local hostname = pretty_hostname.get(uci)
 
   luci.template.render("gluon/config-mode/reboot",
     {
diff --git a/package/gluon-config-mode-hostname/luasrc/lib/gluon/config-mode/wizard/0100-hostname.lua b/package/gluon-config-mode-hostname/luasrc/lib/gluon/config-mode/wizard/0100-hostname.lua
index cf8bbf891..2ffde7237 100644
--- a/package/gluon-config-mode-hostname/luasrc/lib/gluon/config-mode/wizard/0100-hostname.lua
+++ b/package/gluon-config-mode-hostname/luasrc/lib/gluon/config-mode/wizard/0100-hostname.lua
@@ -1,5 +1,6 @@
 local cbi = require "luci.cbi"
 local i18n = require "luci.i18n"
+local pretty_hostname = require "pretty_hostname"
 local uci = luci.model.uci.cursor()
 
 local M = {}
@@ -7,14 +8,12 @@ local M = {}
 function M.section(form)
   local s = form:section(cbi.SimpleSection, nil, nil)
   local o = s:option(cbi.Value, "_hostname", i18n.translate("Node name"))
-  o.value = uci:get_first("system", "system", "hostname")
+  o.value = pretty_hostname.get(uci)
   o.rmempty = false
-  o.datatype = "hostname"
 end
 
 function M.handle(data)
-  uci:set("system", uci:get_first("system", "system"), "hostname", data._hostname)
-  uci:save("system")
+  pretty_hostname.set(uci, data._hostname)
   uci:commit("system")
 end
 
diff --git a/package/gluon-config-mode-mesh-vpn/luasrc/lib/gluon/config-mode/reboot/0100-mesh-vpn.lua b/package/gluon-config-mode-mesh-vpn/luasrc/lib/gluon/config-mode/reboot/0100-mesh-vpn.lua
index da1f64072..993ff2955 100644
--- a/package/gluon-config-mode-mesh-vpn/luasrc/lib/gluon/config-mode/reboot/0100-mesh-vpn.lua
+++ b/package/gluon-config-mode-mesh-vpn/luasrc/lib/gluon/config-mode/reboot/0100-mesh-vpn.lua
@@ -11,8 +11,10 @@ else
   local site = require 'gluon.site_config'
   local sysconfig = require 'gluon.sysconfig'
 
+	local pretty_hostname = require 'pretty_hostname'
+
   local pubkey = util.trim(util.exec("/etc/init.d/fastd show_key " .. "mesh_vpn"))
-  local hostname = uci:get_first("system", "system", "hostname")
+	local hostname = pretty_hostname.get(uci)
   local contact = uci:get_first("gluon-node-info", "owner", "contact")
 
   local msg = i18n.translate('gluon-config-mode:pubkey')
diff --git a/package/gluon-core/Makefile b/package/gluon-core/Makefile
index 10c4dd313..2b9cfb47f 100644
--- a/package/gluon-core/Makefile
+++ b/package/gluon-core/Makefile
@@ -13,7 +13,7 @@ define Package/gluon-core
   SECTION:=gluon
   CATEGORY:=Gluon
   TITLE:=Base files of Gluon
-  DEPENDS:=+gluon-site +libgluonutil +lua-platform-info +lua-hash +luci-base +luci-lib-jsonc +odhcp6c +firewall
+  DEPENDS:=+gluon-site +libgluonutil +lua-platform-info +lua-hash +luci-base +luci-lib-jsonc +odhcp6c +firewall +pretty-hostname
 endef
 
 define Package/gluon-core/description
diff --git a/package/gluon-core/luasrc/lib/gluon/upgrade/030-system b/package/gluon-core/luasrc/lib/gluon/upgrade/030-system
index 0086a5dca..90ad1ba5e 100755
--- a/package/gluon-core/luasrc/lib/gluon/upgrade/030-system
+++ b/package/gluon-core/luasrc/lib/gluon/upgrade/030-system
@@ -1,5 +1,6 @@
 #!/usr/bin/lua
 
+local pretty_hostname = require 'pretty_hostname'
 local sysconfig = require 'gluon.sysconfig'
 
 -- Initial
@@ -10,7 +11,7 @@ if not sysconfig.gluon_version then
 
   local system = uci:get_first('system', 'system')
 
-  uci:set('system', system, 'hostname', (site.hostname_prefix or '') .. util.node_id())
+  pretty_hostname.set(uci, (site.hostname_prefix or '') .. util.node_id())
   uci:set('system', system, 'timezone', site.timezone)
 
   uci:save('system')
diff --git a/package/gluon-luci-admin/Makefile b/package/gluon-luci-admin/Makefile
index 8c3d81549..7b5f632a3 100644
--- a/package/gluon-luci-admin/Makefile
+++ b/package/gluon-luci-admin/Makefile
@@ -18,7 +18,7 @@ define Package/gluon-luci-admin
   SECTION:=gluon
   CATEGORY:=Gluon
   TITLE:=Luci based simple administration interface for mesh nodes
-  DEPENDS:=gluon-config-mode-core-virtual
+  DEPENDS:=gluon-config-mode-core-virtual +pretty-hostname
 endef
 
 define Build/Prepare
diff --git a/package/gluon-luci-admin/files/usr/lib/lua/luci/view/admin/info.htm b/package/gluon-luci-admin/files/usr/lib/lua/luci/view/admin/info.htm
index 3fd4fec01..03f3f2cab 100644
--- a/package/gluon-luci-admin/files/usr/lib/lua/luci/view/admin/info.htm
+++ b/package/gluon-luci-admin/files/usr/lib/lua/luci/view/admin/info.htm
@@ -3,6 +3,7 @@
   local uci = require('luci.model.uci').cursor()
   local util = require 'luci.util'
   local i18n = require 'luci.i18n'
+  local pretty_hostname = require 'pretty_hostname'
 
   local gluon_luci = require "gluon.luci"
   local site = require 'gluon.site_config'
@@ -21,7 +22,7 @@
   }
 
   local values = {
-    hostname = uci:get_first('system', 'system', 'hostname'),
+    hostname = pretty_hostname.get(uci),
     primary_mac = sysconfig.primary_mac,
     model = platform.get_model(),
     version = util.trim(fs.readfile('/lib/gluon/gluon-version')),
diff --git a/package/gluon-luci-theme/Makefile b/package/gluon-luci-theme/Makefile
index 72c92edd1..6e65db3c7 100644
--- a/package/gluon-luci-theme/Makefile
+++ b/package/gluon-luci-theme/Makefile
@@ -16,7 +16,7 @@ define Package/gluon-luci-theme
   SECTION:=gluon
   CATEGORY:=Gluon
   TITLE:=Luci theme for Gluon
-  DEPENDS:=
+  DEPENDS:=+pretty-hostname
 endef
 
 define Package/gluon-luci-theme/description
diff --git a/package/gluon-luci-theme/files/usr/lib/lua/luci/view/themes/gluon/header.htm b/package/gluon-luci-theme/files/usr/lib/lua/luci/view/themes/gluon/header.htm
index 68d114b5c..7e8946035 100644
--- a/package/gluon-luci-theme/files/usr/lib/lua/luci/view/themes/gluon/header.htm
+++ b/package/gluon-luci-theme/files/usr/lib/lua/luci/view/themes/gluon/header.htm
@@ -16,10 +16,12 @@ $Id$
 	local sys  = require "luci.sys"
 	local http = require "luci.http"
 	local disp = require "luci.dispatcher"
+	local uci  = require("luci.model.uci").cursor()
 	local fs   = require "nixio.fs"
 	local gluon_luci = require "gluon.luci"
+	local pretty_hostname = require "pretty_hostname"
 
-	local hostname = sys.hostname()
+	local hostname = pretty_hostname.get(uci)
 	local release = fs.readfile("/lib/gluon/release")
 
 	local request  = disp.context.path
diff --git a/package/gluon-respondd/Makefile b/package/gluon-respondd/Makefile
index 85c718e23..c6cc71e4f 100644
--- a/package/gluon-respondd/Makefile
+++ b/package/gluon-respondd/Makefile
@@ -12,7 +12,7 @@ define Package/gluon-respondd
   SECTION:=gluon
   CATEGORY:=Gluon
   TITLE:=Provides node information to the network
-  DEPENDS:=+gluon-core +libplatforminfo +libgluonutil +respondd
+  DEPENDS:=+gluon-core +libplatforminfo +libgluonutil +libuci +respondd
 endef
 
 define Build/Prepare
diff --git a/package/gluon-respondd/src/Makefile b/package/gluon-respondd/src/Makefile
index eddbe260d..f26b59a21 100644
--- a/package/gluon-respondd/src/Makefile
+++ b/package/gluon-respondd/src/Makefile
@@ -3,4 +3,4 @@ all: respondd.so
 CFLAGS += -Wall
 
 respondd.so: respondd.c
-	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -fPIC -D_GNU_SOURCE -o $@ $^ $(LDLIBS) -lgluonutil -lplatforminfo
+	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -fPIC -D_GNU_SOURCE -o $@ $^ $(LDLIBS) -lgluonutil -lplatforminfo -luci
diff --git a/package/gluon-respondd/src/respondd.c b/package/gluon-respondd/src/respondd.c
index be3c9a7a0..2b7a18f93 100644
--- a/package/gluon-respondd/src/respondd.c
+++ b/package/gluon-respondd/src/respondd.c
@@ -29,12 +29,12 @@
 #include <json-c/json.h>
 #include <libgluonutil.h>
 #include <libplatforminfo.h>
+#include <uci.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 
-#include <sys/utsname.h>
 #include <sys/vfs.h>
 
 
@@ -67,12 +67,29 @@ static struct json_object * get_site_code(void) {
 }
 
 static struct json_object * get_hostname(void) {
-	struct utsname utsname;
+	struct json_object *ret = NULL;
 
-	if (uname(&utsname))
-		return NULL;
+	struct uci_context *ctx = uci_alloc_context();
+	ctx->flags &= ~UCI_FLAG_STRICT;
+
+	char section[] = "system.@system[0]";
+	struct uci_ptr ptr;
+	if (uci_lookup_ptr(ctx, &ptr, section, true))
+		goto error;
+
+	struct uci_section *s = ptr.s;
+
+	const char *hostname = uci_lookup_option_string(ctx, s, "pretty_hostname");
 
-	return gluonutil_wrap_string(utsname.nodename);
+	if (!hostname)
+		hostname = uci_lookup_option_string(ctx, s, "hostname");
+
+	ret = gluonutil_wrap_string(hostname);
+
+error:
+	uci_free_context(ctx);
+
+	return ret;
 }
 
 static struct json_object * respondd_provider_nodeinfo(void) {
-- 
GitLab