[__hostname] Support all systemd OSes and prefer hostnamectl

This commit is contained in:
Dennis Camera 2019-08-15 16:15:47 +02:00
parent 7981f81dcd
commit 3c9eb7bce7
3 changed files with 74 additions and 10 deletions

View file

@ -0,0 +1,48 @@
#!/bin/sh
#
# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
#
# Retrieve the running hostname
#
# Firstly, check hostnamectl if available.
if command -v hostnamectl >/dev/null
then
hostnamectl --static && exit 0
fi
# Almost any distribution
if [ -f /etc/hostname ]; then
cat /etc/hostname
# SuSE
elif [ -f /etc/HOSTNAME ]; then
cat /etc/HOSTNAME
# OpenBSD
elif [ -f /etc/myname ]; then
cat /etc/myname
# FreeBSD
elif [ "$(uname -s)" == FreeBSD ]; then
(. /etc/rc.conf && echo $hostname)
# Mac OS X
elif [ "$(uname -s)" == Darwin -a -f "$(command -v scutil)" ]; then
scutil --get HostName 2>/dev/null || hostname -s
else
command -v hostname && hostname -s
fi

View file

@ -2,6 +2,7 @@
# #
# 2014-2017 Steven Armstrong (steven-cdist at armstrong.cc) # 2014-2017 Steven Armstrong (steven-cdist at armstrong.cc)
# 2014 Nico Schottelius (nico-cdist at schottelius.org) # 2014 Nico Schottelius (nico-cdist at schottelius.org)
# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
# #
# This file is part of cdist. # This file is part of cdist.
# #
@ -46,8 +47,9 @@ case "$os" in
fi fi
;; ;;
*) *)
echo "Unsupported os: $os" >&2 if [ "$name_running" -a "$name_running" = "$name_should" ]; then
exit 1 exit 0
fi
;; ;;
esac esac
@ -56,23 +58,35 @@ esac
# #
echo changed >> "$__messages_out" echo changed >> "$__messages_out"
# Use the good old way to set the hostname even on machines running systemd.
# First try to set the hostname using hostnamectl, if available.
if [ "$has_hostnamectl" ]; then
# Allow hostnamectl to fail silently.
# Who the fuck invented a tool that needs dbus to set the hostname anyway ...
cat <<EOF
if hostnamectl set-hostname '$name_should'; then
printf '%s\\n' '$name_should' > /etc/hostname
exit 0
fi
EOF
fi
# Use the good old way to set the hostname. Also if hostnamectl fails.
case "$os" in case "$os" in
archlinux|debian|ubuntu|devuan|centos|coreos|alpine) archlinux|debian|ubuntu|devuan|centos|coreos|alpine)
printf "printf '%%s\\\\n' '$name_should' > /etc/hostname\\n" printf "printf '%%s\\\\n' '$name_should' > /etc/hostname\\n"
echo "hostname -F /etc/hostname" echo "hostname -F /etc/hostname"
;; ;;
freebsd|openbsd) freebsd|openbsd)
# NOTE: Writing the hostname to file is done in the manifest.
echo "hostname '$name_should'" echo "hostname '$name_should'"
;; ;;
suse) suse)
echo "hostname '$name_should'" echo "hostname '$name_should'"
printf "printf '%%s\\\\n' '$name_should' > /etc/HOSTNAME\\n" printf "printf '%%s\\\\n' '$name_should' > /etc/HOSTNAME\\n"
;; ;;
*)
echo "echo 'Unsupported OS: $os' >&2; exit 1"
;;
esac esac
if [ "$has_hostnamectl" ]; then
# Allow hostnamectl set-hostname to fail silently.
# Who the fuck invented a tool that needs dbus to set the hostname anyway ...
echo "hostnamectl set-hostname '$name_should' || true"
fi

View file

@ -2,6 +2,7 @@
# #
# 2012 Steven Armstrong (steven-cdist at armstrong.cc) # 2012 Steven Armstrong (steven-cdist at armstrong.cc)
# 2014 Nico Schottelius (nico-cdist at schottelius.org) # 2014 Nico Schottelius (nico-cdist at schottelius.org)
# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
# #
# This file is part of cdist. # This file is part of cdist.
# #
@ -63,6 +64,7 @@ case "$os" in
echo "$name_should" | __file /etc/myname --source - echo "$name_should" | __file /etc/myname --source -
;; ;;
*) *)
not_supported # On other operating systems we fall back to hostnamectl if available…
test -n "$(cat "$__object/explorer/has_hostnamectl")" || not_supported
;; ;;
esac esac