pretty-hostname: add package to manage a "pretty" hostname
This commit is contained in:
parent
7327bfdde5
commit
be83ca57e4
|
@ -0,0 +1,35 @@
|
||||||
|
include $(TOPDIR)/rules.mk
|
||||||
|
|
||||||
|
PKG_NAME:=pretty-hostname
|
||||||
|
PKG_VERSION:=1
|
||||||
|
|
||||||
|
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
|
||||||
|
|
||||||
|
include $(INCLUDE_DIR)/package.mk
|
||||||
|
|
||||||
|
define Package/pretty-hostname
|
||||||
|
SECTION:=admin
|
||||||
|
CATEGORY:=Administration
|
||||||
|
DEPENDS:=+uci
|
||||||
|
TITLE:=Manage a "pretty" hostname in addition to the actual one
|
||||||
|
endef
|
||||||
|
|
||||||
|
define Build/Prepare
|
||||||
|
mkdir -p $(PKG_BUILD_DIR)
|
||||||
|
endef
|
||||||
|
|
||||||
|
define Build/Configure
|
||||||
|
endef
|
||||||
|
|
||||||
|
define Build/Compile
|
||||||
|
endef
|
||||||
|
|
||||||
|
define Package/pretty-hostname/install
|
||||||
|
$(INSTALL_DIR) $(1)/bin
|
||||||
|
$(INSTALL_BIN) ./files/pretty-hostname $(1)/bin/
|
||||||
|
|
||||||
|
$(INSTALL_DIR) $(1)/usr/lib/lua
|
||||||
|
$(INSTALL_DATA) ./files/pretty_hostname.lua $(1)/usr/lib/lua/
|
||||||
|
endef
|
||||||
|
|
||||||
|
$(eval $(call BuildPackage,pretty-hostname))
|
|
@ -0,0 +1,34 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
case $# in
|
||||||
|
0)
|
||||||
|
uci -q get 'system.@system[0].pretty_hostname' || uci get 'system.@system[0].hostname'
|
||||||
|
;;
|
||||||
|
1)
|
||||||
|
local pretty_hostname="$1"
|
||||||
|
# Remove invalid characters and leading/trailing spaces
|
||||||
|
local hostname="$(echo -n "$pretty_hostname" | sed -r -e 's/[^a-zA-Z0-9-]//g' -e 's/-+/-/g' -e 's/^-//')"
|
||||||
|
hostname="${hostname:0:63}"
|
||||||
|
hostname="$(echo -n "$hostname" | sed -e 's/-$//')"
|
||||||
|
|
||||||
|
if [ -z "$hostname" ]; then
|
||||||
|
hostname='localhost'
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$hostname" = "$pretty_hostname" ]; then
|
||||||
|
uci -q delete "system.@system[0].pretty_hostname"
|
||||||
|
else
|
||||||
|
uci set "system.@system[0].pretty_hostname=$pretty_hostname"
|
||||||
|
fi
|
||||||
|
|
||||||
|
uci set "system.@system[0].hostname=$hostname"
|
||||||
|
uci commit system
|
||||||
|
|
||||||
|
echo "$hostname" > /proc/sys/kernel/hostname
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo 'Usage: pretty-hostname [ hostname ]'
|
||||||
|
exit 1
|
||||||
|
esac
|
|
@ -0,0 +1,43 @@
|
||||||
|
local assert = assert
|
||||||
|
local string = string
|
||||||
|
|
||||||
|
module 'pretty_hostname'
|
||||||
|
|
||||||
|
local function get_system(uci)
|
||||||
|
local system
|
||||||
|
uci:foreach('system', 'system',
|
||||||
|
function(s)
|
||||||
|
system = s
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
)
|
||||||
|
return assert(system, 'unable to find system section')
|
||||||
|
end
|
||||||
|
|
||||||
|
function get(uci)
|
||||||
|
local system = get_system(uci)
|
||||||
|
|
||||||
|
return system.pretty_hostname or system.hostname
|
||||||
|
end
|
||||||
|
|
||||||
|
function set(uci, pretty_hostname)
|
||||||
|
local system = get_system(uci)['.name']
|
||||||
|
|
||||||
|
local hostname = string.gsub(pretty_hostname, '[^a-zA-Z0-9%-]', '')
|
||||||
|
hostname = string.gsub(hostname, '%-+', '-')
|
||||||
|
hostname = string.gsub(hostname, '^%-', '')
|
||||||
|
hostname = string.sub(hostname, 1, 63)
|
||||||
|
hostname = string.gsub(hostname, '%-$', '')
|
||||||
|
|
||||||
|
if hostname == '' then
|
||||||
|
hostname = 'localhost'
|
||||||
|
end
|
||||||
|
|
||||||
|
if hostname == pretty_hostname then
|
||||||
|
uci:delete('system', system, 'pretty_hostname')
|
||||||
|
else
|
||||||
|
uci:set('system', system, 'pretty_hostname', pretty_hostname)
|
||||||
|
end
|
||||||
|
|
||||||
|
uci:set('system', system, 'hostname', hostname)
|
||||||
|
end
|
Loading…
Reference in New Issue