Use a "pretty" hostname where possible

Fixes #414
This commit is contained in:
Matthias Schiffer 2016-08-27 15:14:16 +02:00
parent 6df141e7a6
commit 7e0075584d
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C
13 changed files with 43 additions and 20 deletions

View File

@ -17,7 +17,7 @@ define Package/gluon-config-mode-core
SECTION:=gluon SECTION:=gluon
CATEGORY:=Gluon CATEGORY:=Gluon
TITLE:=Luci based config mode for user friendly setup of new mesh nodes 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 PROVIDES:=gluon-config-mode-core-virtual
endef endef

View File

@ -51,6 +51,7 @@ function action_reboot()
local gluon_luci = require "gluon.luci" local gluon_luci = require "gluon.luci"
local fs = require "nixio.fs" local fs = require "nixio.fs"
local util = require "nixio.util" local util = require "nixio.util"
local pretty_hostname = require "pretty_hostname"
local parts_dir = "/lib/gluon/config-mode/reboot/" local parts_dir = "/lib/gluon/config-mode/reboot/"
local files = util.consume(fs.dir(parts_dir)) local files = util.consume(fs.dir(parts_dir))
@ -68,7 +69,7 @@ function action_reboot()
end end
end end
local hostname = uci:get_first("system", "system", "hostname") local hostname = pretty_hostname.get(uci)
luci.template.render("gluon/config-mode/reboot", luci.template.render("gluon/config-mode/reboot",
{ {

View File

@ -1,5 +1,6 @@
local cbi = require "luci.cbi" local cbi = require "luci.cbi"
local i18n = require "luci.i18n" local i18n = require "luci.i18n"
local pretty_hostname = require "pretty_hostname"
local uci = luci.model.uci.cursor() local uci = luci.model.uci.cursor()
local M = {} local M = {}
@ -7,14 +8,12 @@ local M = {}
function M.section(form) function M.section(form)
local s = form:section(cbi.SimpleSection, nil, nil) local s = form:section(cbi.SimpleSection, nil, nil)
local o = s:option(cbi.Value, "_hostname", i18n.translate("Node name")) 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.rmempty = false
o.datatype = "hostname"
end end
function M.handle(data) function M.handle(data)
uci:set("system", uci:get_first("system", "system"), "hostname", data._hostname) pretty_hostname.set(uci, data._hostname)
uci:save("system")
uci:commit("system") uci:commit("system")
end end

View File

@ -11,8 +11,10 @@ else
local site = require 'gluon.site_config' local site = require 'gluon.site_config'
local sysconfig = require 'gluon.sysconfig' 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 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 contact = uci:get_first("gluon-node-info", "owner", "contact")
local msg = i18n.translate('gluon-config-mode:pubkey') local msg = i18n.translate('gluon-config-mode:pubkey')

View File

@ -13,7 +13,7 @@ define Package/gluon-core
SECTION:=gluon SECTION:=gluon
CATEGORY:=Gluon CATEGORY:=Gluon
TITLE:=Base files of 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 endef
define Package/gluon-core/description define Package/gluon-core/description

View File

@ -1,5 +1,6 @@
#!/usr/bin/lua #!/usr/bin/lua
local pretty_hostname = require 'pretty_hostname'
local sysconfig = require 'gluon.sysconfig' local sysconfig = require 'gluon.sysconfig'
-- Initial -- Initial
@ -10,7 +11,7 @@ if not sysconfig.gluon_version then
local system = uci:get_first('system', 'system') 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:set('system', system, 'timezone', site.timezone)
uci:save('system') uci:save('system')

View File

@ -18,7 +18,7 @@ define Package/gluon-luci-admin
SECTION:=gluon SECTION:=gluon
CATEGORY:=Gluon CATEGORY:=Gluon
TITLE:=Luci based simple administration interface for mesh nodes TITLE:=Luci based simple administration interface for mesh nodes
DEPENDS:=gluon-config-mode-core-virtual DEPENDS:=gluon-config-mode-core-virtual +pretty-hostname
endef endef
define Build/Prepare define Build/Prepare

View File

@ -3,6 +3,7 @@
local uci = require('luci.model.uci').cursor() local uci = require('luci.model.uci').cursor()
local util = require 'luci.util' local util = require 'luci.util'
local i18n = require 'luci.i18n' local i18n = require 'luci.i18n'
local pretty_hostname = require 'pretty_hostname'
local gluon_luci = require "gluon.luci" local gluon_luci = require "gluon.luci"
local site = require 'gluon.site_config' local site = require 'gluon.site_config'
@ -21,7 +22,7 @@
} }
local values = { local values = {
hostname = uci:get_first('system', 'system', 'hostname'), hostname = pretty_hostname.get(uci),
primary_mac = sysconfig.primary_mac, primary_mac = sysconfig.primary_mac,
model = platform.get_model(), model = platform.get_model(),
version = util.trim(fs.readfile('/lib/gluon/gluon-version')), version = util.trim(fs.readfile('/lib/gluon/gluon-version')),

View File

@ -16,7 +16,7 @@ define Package/gluon-luci-theme
SECTION:=gluon SECTION:=gluon
CATEGORY:=Gluon CATEGORY:=Gluon
TITLE:=Luci theme for Gluon TITLE:=Luci theme for Gluon
DEPENDS:= DEPENDS:=+pretty-hostname
endef endef
define Package/gluon-luci-theme/description define Package/gluon-luci-theme/description

View File

@ -16,10 +16,12 @@ $Id$
local sys = require "luci.sys" local sys = require "luci.sys"
local http = require "luci.http" local http = require "luci.http"
local disp = require "luci.dispatcher" local disp = require "luci.dispatcher"
local uci = require("luci.model.uci").cursor()
local fs = require "nixio.fs" local fs = require "nixio.fs"
local gluon_luci = require "gluon.luci" 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 release = fs.readfile("/lib/gluon/release")
local request = disp.context.path local request = disp.context.path

View File

@ -12,7 +12,7 @@ define Package/gluon-respondd
SECTION:=gluon SECTION:=gluon
CATEGORY:=Gluon CATEGORY:=Gluon
TITLE:=Provides node information to the network TITLE:=Provides node information to the network
DEPENDS:=+gluon-core +libplatforminfo +libgluonutil +respondd DEPENDS:=+gluon-core +libplatforminfo +libgluonutil +libuci +respondd
endef endef
define Build/Prepare define Build/Prepare

View File

@ -3,4 +3,4 @@ all: respondd.so
CFLAGS += -Wall CFLAGS += -Wall
respondd.so: respondd.c 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

View File

@ -29,12 +29,12 @@
#include <json-c/json.h> #include <json-c/json.h>
#include <libgluonutil.h> #include <libgluonutil.h>
#include <libplatforminfo.h> #include <libplatforminfo.h>
#include <uci.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/utsname.h>
#include <sys/vfs.h> #include <sys/vfs.h>
@ -67,12 +67,29 @@ static struct json_object * get_site_code(void) {
} }
static struct json_object * get_hostname(void) { static struct json_object * get_hostname(void) {
struct utsname utsname; struct json_object *ret = NULL;
if (uname(&utsname)) struct uci_context *ctx = uci_alloc_context();
return NULL; ctx->flags &= ~UCI_FLAG_STRICT;
return gluonutil_wrap_string(utsname.nodename); 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");
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) { static struct json_object * respondd_provider_nodeinfo(void) {