forked from ungleich-public/cdist
		
	[__hostame] Simplify and revert hostnamectl to be a last resort
This commit is contained in:
		
					parent
					
						
							
								b65ceba569
							
						
					
				
			
			
				commit
				
					
						6eec5f87a4
					
				
			
		
					 3 changed files with 90 additions and 127 deletions
				
			
		| 
						 | 
					@ -1,66 +0,0 @@
 | 
				
			||||||
#!/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 persistent hostname
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Firstly, check hostnamectl if available.
 | 
					 | 
				
			||||||
if command -v hostnamectl >/dev/null 2>&1
 | 
					 | 
				
			||||||
then
 | 
					 | 
				
			||||||
	hostnamectl status --static && exit 0
 | 
					 | 
				
			||||||
fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# In case hostnamectl is missing or failed:
 | 
					 | 
				
			||||||
# Fall back to alternative OS-specific approaches.
 | 
					 | 
				
			||||||
case $("$__explorer/os")
 | 
					 | 
				
			||||||
in
 | 
					 | 
				
			||||||
	alpine|archlinux|coreos|debian|devuan|ubuntu)
 | 
					 | 
				
			||||||
		test -f /etc/hostname && cat /etc/hostname
 | 
					 | 
				
			||||||
		;;
 | 
					 | 
				
			||||||
	centos|fedora|redhat|scientific)
 | 
					 | 
				
			||||||
		test -f /etc/sysconfig/network \
 | 
					 | 
				
			||||||
			&& (. /etc/sysconfig/network && echo "$HOSTNAME")
 | 
					 | 
				
			||||||
		;;
 | 
					 | 
				
			||||||
	freebsd)
 | 
					 | 
				
			||||||
		(. /etc/rc.conf && echo "$hostname")
 | 
					 | 
				
			||||||
		;;
 | 
					 | 
				
			||||||
	gentoo)
 | 
					 | 
				
			||||||
		(. /etc/conf.d/hostname && echo "$hostname")
 | 
					 | 
				
			||||||
		;;
 | 
					 | 
				
			||||||
	macosx)
 | 
					 | 
				
			||||||
		scutil --get HostName 2>/dev/null
 | 
					 | 
				
			||||||
		;;
 | 
					 | 
				
			||||||
	netbsd)
 | 
					 | 
				
			||||||
		if grep -q '^hostname=' /etc/rc.conf
 | 
					 | 
				
			||||||
		then
 | 
					 | 
				
			||||||
			(. /etc/rc.conf && echo "$hostname")
 | 
					 | 
				
			||||||
		elif [ -f /etc/myname ]
 | 
					 | 
				
			||||||
		then
 | 
					 | 
				
			||||||
			# Fall back to /etc/myname file
 | 
					 | 
				
			||||||
			cat /etc/myname
 | 
					 | 
				
			||||||
		fi
 | 
					 | 
				
			||||||
		;;
 | 
					 | 
				
			||||||
	openbsd)
 | 
					 | 
				
			||||||
		cat /etc/myname
 | 
					 | 
				
			||||||
		;;
 | 
					 | 
				
			||||||
	suse)
 | 
					 | 
				
			||||||
		cat /etc/HOSTNAME 2>/dev/null
 | 
					 | 
				
			||||||
		;;
 | 
					 | 
				
			||||||
esac
 | 
					 | 
				
			||||||
| 
						 | 
					@ -22,17 +22,17 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
os=$(cat "$__global/explorer/os")
 | 
					os=$(cat "$__global/explorer/os")
 | 
				
			||||||
name_running=$(cat "$__global/explorer/hostname")
 | 
					name_running=$(cat "$__global/explorer/hostname")
 | 
				
			||||||
name_config=$(cat "$__object/explorer/persistent_hostname")
 | 
					 | 
				
			||||||
has_hostnamectl=$(cat "$__object/explorer/has_hostnamectl")
 | 
					has_hostnamectl=$(cat "$__object/explorer/has_hostnamectl")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if [ -f "$__object/parameter/name" ]; then
 | 
					if test -s "$__object/parameter/name"
 | 
				
			||||||
    name_should="$(cat "$__object/parameter/name")"
 | 
					then
 | 
				
			||||||
 | 
					    name_should=$(cat "$__object/parameter/name")
 | 
				
			||||||
else
 | 
					else
 | 
				
			||||||
    case $os
 | 
					    case $os
 | 
				
			||||||
    in
 | 
					    in
 | 
				
			||||||
        # RedHat-derivatives and BSDs
 | 
					        # RedHat-derivatives and BSDs
 | 
				
			||||||
        centos|fedora|redhat|scientific|freebsd|netbsd|openbsd)
 | 
					        centos|fedora|redhat|scientific|freebsd|macosx|netbsd|openbsd)
 | 
				
			||||||
            # Hostname is FQDN
 | 
					            # Hostname is FQDN
 | 
				
			||||||
            name_should="${__target_host}"
 | 
					            name_should="${__target_host}"
 | 
				
			||||||
        ;;
 | 
					        ;;
 | 
				
			||||||
| 
						 | 
					@ -43,66 +43,49 @@ else
 | 
				
			||||||
    esac
 | 
					    esac
 | 
				
			||||||
fi
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
################################################################################
 | 
					################################################################################
 | 
				
			||||||
# Check if the hostname is already correct
 | 
					# Check if the (running) hostname is already correct
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
if [ "$name_running" = "$name_should" ] \
 | 
					test "$name_running" != "$name_should" || exit 0
 | 
				
			||||||
  && [ -z "$name_config" -o "$name_config" = "$name_should" ]
 | 
					
 | 
				
			||||||
then
 | 
					 | 
				
			||||||
    exit 0
 | 
					 | 
				
			||||||
fi
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
################################################################################
 | 
					################################################################################
 | 
				
			||||||
# Setup hostname
 | 
					# Setup hostname
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
echo 'changed' >>"$__messages_out"
 | 
					echo 'changed' >>"$__messages_out"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Use the good old way to set the hostname.
 | 
				
			||||||
# First try to set the hostname using hostnamectl, if available.
 | 
					 | 
				
			||||||
if [ -n "$has_hostnamectl" ]; then
 | 
					 | 
				
			||||||
    # Allow hostnamectl to fail silently.
 | 
					 | 
				
			||||||
    # Who the fuck invented a tool that needs dbus to set the hostname anyway…
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # NOTE: We write the static hostname to /etc/hostname first in case
 | 
					 | 
				
			||||||
    #       hostnamectl fails. Fallback-code below will then adjust the running
 | 
					 | 
				
			||||||
    #       hostname below.
 | 
					 | 
				
			||||||
    echo "echo '$name_should' >/etc/hostname"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    echo "hostnamectl set-hostname '$name_should' && exit 0"
 | 
					 | 
				
			||||||
fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Use the good old way to set the hostname. Also if hostnamectl fails.
 | 
					 | 
				
			||||||
case $os
 | 
					case $os
 | 
				
			||||||
in
 | 
					in
 | 
				
			||||||
    alpine|archlinux|debian|devuan|ubuntu)
 | 
					    alpine|debian|devuan|ubuntu)
 | 
				
			||||||
        echo "echo '$name_should' >/etc/hostname"
 | 
					        echo 'hostname -F /etc/hostname'
 | 
				
			||||||
        echo "hostname -F /etc/hostname"
 | 
					 | 
				
			||||||
    ;;
 | 
					    ;;
 | 
				
			||||||
    coreos|openbmc-phosphor)
 | 
					    archlinux|centos|fedora|redhat|scientific|freebsd|netbsd|gentoo)
 | 
				
			||||||
        # NOTE: systemd-only distros have the hostname already written above.
 | 
					 | 
				
			||||||
        #       But since hostamectl failed, we update the running hostname
 | 
					 | 
				
			||||||
        #       manually.
 | 
					 | 
				
			||||||
        echo "hostname -F /etc/hostname"
 | 
					 | 
				
			||||||
        ;;
 | 
					 | 
				
			||||||
    gentoo)
 | 
					 | 
				
			||||||
        # NOTE: Writing the hostname to file is done in the manifest for OpenRC.
 | 
					 | 
				
			||||||
        #       For systemd hostnamectl should take care of that.
 | 
					 | 
				
			||||||
        echo "hostname '$name_should'"
 | 
					        echo "hostname '$name_should'"
 | 
				
			||||||
    ;;
 | 
					    ;;
 | 
				
			||||||
    macosx)
 | 
					    macosx)
 | 
				
			||||||
        echo "scutil --set HostName '$name_should'"
 | 
					        echo "scutil --set HostName '$name_should'"
 | 
				
			||||||
    ;;
 | 
					    ;;
 | 
				
			||||||
 | 
					    openbsd)
 | 
				
			||||||
 | 
					        echo 'hostname -F /etc/myname'
 | 
				
			||||||
 | 
					    ;;
 | 
				
			||||||
    suse)
 | 
					    suse)
 | 
				
			||||||
        # We write into /etc/HOSTNAME for backwards-compatibility.  Modern SuSE
 | 
					 | 
				
			||||||
        # has hostnamectl anyway and symlinks /etc/HOSTNAME to /etc/hostname.
 | 
					 | 
				
			||||||
        echo "echo '$name_should' >/etc/HOSTNAME"
 | 
					 | 
				
			||||||
        echo 'hostname -F /etc/HOSTNAME'
 | 
					        echo 'hostname -F /etc/HOSTNAME'
 | 
				
			||||||
    ;;
 | 
					    ;;
 | 
				
			||||||
    centos|fedora|freebsd|netbsd|openbsd|redhat|scientific)
 | 
					 | 
				
			||||||
        # NOTE: Writing the hostname to file is done in the manifest.
 | 
					 | 
				
			||||||
        echo "hostname '$name_should'"
 | 
					 | 
				
			||||||
    ;;
 | 
					 | 
				
			||||||
    *)
 | 
					    *)
 | 
				
			||||||
        echo "echo 'Unsupported OS: $os' >&2; exit 1"
 | 
					        # Fall back to set the hostname using hostnamectl, if available.
 | 
				
			||||||
 | 
					        if test -n "$has_hostnamectl"
 | 
				
			||||||
 | 
					        then
 | 
				
			||||||
 | 
					            # Don't use hostnamectl as the primary means to set the hostname for
 | 
				
			||||||
 | 
					            # systemd systems, because it cannot be trusted to work reliably and
 | 
				
			||||||
 | 
					            # exit with non-zero when it fails.
 | 
				
			||||||
 | 
					            # Who invented a tool that needs dbus to set the hostname anyway…
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            echo "hostnamectl set-hostname \"\$(cat /etc/hostname)\""
 | 
				
			||||||
 | 
					            echo "test \"\$(hostname)\" = \"\$(cat /etc/hostname)\" || hostname -F /etc/hostname"
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					            printf "echo 'Unsupported OS: %s' >&2\nexit 1\n" "$os"
 | 
				
			||||||
 | 
					        fi
 | 
				
			||||||
    ;;
 | 
					    ;;
 | 
				
			||||||
esac
 | 
					esac
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,18 +20,25 @@
 | 
				
			||||||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
					# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					onchange_cmd="echo changed >>'$__messages_out'"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
not_supported() {
 | 
					not_supported() {
 | 
				
			||||||
    echo "Your operating system ($os) is currently not supported by this type (${__type##*/})." >&2
 | 
					    echo "Your operating system ($os) is currently not supported by this type (${__type##*/})." >&2
 | 
				
			||||||
    echo "Please contribute an implementation for it if you can." >&2
 | 
					    echo "Please contribute an implementation for it if you can." >&2
 | 
				
			||||||
    exit 1
 | 
					    exit 1
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					set_hostname_systemd() {
 | 
				
			||||||
 | 
					    echo "$1" | __file /etc/hostname --source - --onchange "$onchange_cmd"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
os=$(cat "$__global/explorer/os")
 | 
					os=$(cat "$__global/explorer/os")
 | 
				
			||||||
max_len=$(cat "$__object/explorer/max_len")
 | 
					max_len=$(cat "$__object/explorer/max_len")
 | 
				
			||||||
 | 
					has_hostnamectl=$(cat "$__object/explorer/has_hostnamectl")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if [ -f "$__object/parameter/name" ]; then
 | 
					if test -s "$__object/parameter/name"
 | 
				
			||||||
    name_should="$(cat "$__object/parameter/name")"
 | 
					then
 | 
				
			||||||
 | 
					    name_should=$(cat "$__object/parameter/name")
 | 
				
			||||||
else
 | 
					else
 | 
				
			||||||
    case $os
 | 
					    case $os
 | 
				
			||||||
    in
 | 
					    in
 | 
				
			||||||
| 
						 | 
					@ -55,12 +62,31 @@ fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
case $os
 | 
					case $os
 | 
				
			||||||
in
 | 
					in
 | 
				
			||||||
    archlinux|debian|suse|ubuntu|devuan|coreos|alpine|macosx)
 | 
					    alpine|debian|devuan|ubuntu)
 | 
				
			||||||
 | 
					        echo "$name_should" \
 | 
				
			||||||
 | 
					            | __file /etc/hostname --source - --onchange "$onchange_cmd"
 | 
				
			||||||
 | 
					    ;;
 | 
				
			||||||
 | 
					    macosx)
 | 
				
			||||||
        # handled in gencode-remote
 | 
					        # handled in gencode-remote
 | 
				
			||||||
        :
 | 
					        :
 | 
				
			||||||
    ;;
 | 
					    ;;
 | 
				
			||||||
 | 
					    archlinux)
 | 
				
			||||||
 | 
					        if test -n "$has_hostnamectl"
 | 
				
			||||||
 | 
					        then
 | 
				
			||||||
 | 
					            set_hostname_systemd "$name_should"
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					            # Only for ancient ArchLinux, write to /etc/rc.conf on pre-systemd
 | 
				
			||||||
 | 
					            # versions.
 | 
				
			||||||
 | 
					            __key_value sysconfig-hostname \
 | 
				
			||||||
 | 
					                --file /etc/rc.conf \
 | 
				
			||||||
 | 
					                --delimiter '=' --exact_delimiter \
 | 
				
			||||||
 | 
					                --key 'HOSTNAME' \
 | 
				
			||||||
 | 
					                --value "\"$name_should\"" \
 | 
				
			||||||
 | 
					                --onchange "$onchange_cmd"
 | 
				
			||||||
 | 
					        fi
 | 
				
			||||||
 | 
					        ;;
 | 
				
			||||||
    centos|fedora|redhat|scientific)
 | 
					    centos|fedora|redhat|scientific)
 | 
				
			||||||
        if test -z "$(cat "$__object/explorer/has_hostnamectl")"
 | 
					        if test -z "$has_hostnamectl"
 | 
				
			||||||
        then
 | 
					        then
 | 
				
			||||||
            # Only write to /etc/sysconfig/network on non-systemd versions.
 | 
					            # Only write to /etc/sysconfig/network on non-systemd versions.
 | 
				
			||||||
            # On systemd-based versions this entry is ignored.
 | 
					            # On systemd-based versions this entry is ignored.
 | 
				
			||||||
| 
						 | 
					@ -68,11 +94,14 @@ in
 | 
				
			||||||
                --file /etc/sysconfig/network \
 | 
					                --file /etc/sysconfig/network \
 | 
				
			||||||
                --delimiter '=' --exact_delimiter \
 | 
					                --delimiter '=' --exact_delimiter \
 | 
				
			||||||
                --key HOSTNAME \
 | 
					                --key HOSTNAME \
 | 
				
			||||||
                --value "\"$name_should\""
 | 
					                --value "\"$name_should\"" \
 | 
				
			||||||
 | 
					                --onchange "$onchange_cmd"
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					            set_hostname_systemd "$name_should"
 | 
				
			||||||
        fi
 | 
					        fi
 | 
				
			||||||
    ;;
 | 
					    ;;
 | 
				
			||||||
    gentoo)
 | 
					    gentoo)
 | 
				
			||||||
        if test -z "$(cat "$__object/explorer/has_hostnamectl")"
 | 
					        if test -z "$has_hostnamectl"
 | 
				
			||||||
        then
 | 
					        then
 | 
				
			||||||
            # Only write to /etc/conf.d/hostname on OpenRC-based installations.
 | 
					            # Only write to /etc/conf.d/hostname on OpenRC-based installations.
 | 
				
			||||||
            # On systemd use hostnamectl(1) in gencode-remote.
 | 
					            # On systemd use hostnamectl(1) in gencode-remote.
 | 
				
			||||||
| 
						 | 
					@ -81,6 +110,8 @@ in
 | 
				
			||||||
                --delimiter '=' --exact_delimiter \
 | 
					                --delimiter '=' --exact_delimiter \
 | 
				
			||||||
                --key 'hostname' \
 | 
					                --key 'hostname' \
 | 
				
			||||||
                --value "\"$name_should\""
 | 
					                --value "\"$name_should\""
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					            set_hostname_systemd "$name_should"
 | 
				
			||||||
        fi
 | 
					        fi
 | 
				
			||||||
    ;;
 | 
					    ;;
 | 
				
			||||||
    freebsd)
 | 
					    freebsd)
 | 
				
			||||||
| 
						 | 
					@ -88,22 +119,37 @@ in
 | 
				
			||||||
            --file /etc/rc.conf \
 | 
					            --file /etc/rc.conf \
 | 
				
			||||||
            --delimiter '=' --exact_delimiter \
 | 
					            --delimiter '=' --exact_delimiter \
 | 
				
			||||||
            --key 'hostname' \
 | 
					            --key 'hostname' \
 | 
				
			||||||
            --value "\"$name_should\""
 | 
					            --value "\"$name_should\"" \
 | 
				
			||||||
 | 
					            --onchange "$onchange_cmd"
 | 
				
			||||||
    ;;
 | 
					    ;;
 | 
				
			||||||
    netbsd)
 | 
					    netbsd)
 | 
				
			||||||
        __key_value rcconf-hostname \
 | 
					        __key_value rcconf-hostname \
 | 
				
			||||||
            --file /etc/rc.conf \
 | 
					            --file /etc/rc.conf \
 | 
				
			||||||
            --delimiter '=' --exact_delimiter \
 | 
					            --delimiter '=' --exact_delimiter \
 | 
				
			||||||
            --key 'hostname' \
 | 
					            --key 'hostname' \
 | 
				
			||||||
            --value "\"$name_should\""
 | 
					            --value "\"$name_should\"" \
 | 
				
			||||||
 | 
					            --onchange "$onchange_cmd"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # To avoid confusion, ensure that the hostname is only stored once
 | 
					        # To avoid confusion, ensure that the hostname is only stored once
 | 
				
			||||||
        __file /etc/myname --state absent
 | 
					        __file /etc/myname --state absent
 | 
				
			||||||
    ;;
 | 
					    ;;
 | 
				
			||||||
    openbsd)
 | 
					    openbsd)
 | 
				
			||||||
        echo "$name_should" | __file /etc/myname --source -
 | 
					        echo "$name_should" \
 | 
				
			||||||
 | 
					            | __file /etc/myname --source - --onchange "$onchange_cmd"
 | 
				
			||||||
 | 
					    ;;
 | 
				
			||||||
 | 
					    suse)
 | 
				
			||||||
 | 
					        # We write into /etc/HOSTNAME for backwards-compatibility.  Modern SuSE
 | 
				
			||||||
 | 
					        # has hostnamectl anyway and symlinks /etc/HOSTNAME to /etc/hostname.
 | 
				
			||||||
 | 
					        echo "$name_should" \
 | 
				
			||||||
 | 
					            | __file /etc/HOSTNAME --source - --onchange "$onchange_cmd"
 | 
				
			||||||
    ;;
 | 
					    ;;
 | 
				
			||||||
    *)
 | 
					    *)
 | 
				
			||||||
        # On other operating systems we fall back to hostnamectl if available…
 | 
					        # On other operating systems we fall back to systemd's hostnamectl if available…
 | 
				
			||||||
        test -n "$(cat "$__object/explorer/has_hostnamectl")" || not_supported
 | 
					        if test -n "$has_hostnamectl"
 | 
				
			||||||
 | 
					        then
 | 
				
			||||||
 | 
					            set_hostname_systemd "$name_should"
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					            not_supported
 | 
				
			||||||
 | 
					        fi
 | 
				
			||||||
    ;;
 | 
					    ;;
 | 
				
			||||||
esac
 | 
					esac
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue