forked from ungleich-public/cdist
		
	[__hostname] Support all systemd OSes and prefer hostnamectl
This commit is contained in:
		
					parent
					
						
							
								7981f81dcd
							
						
					
				
			
			
				commit
				
					
						3c9eb7bce7
					
				
			
		
					 3 changed files with 74 additions and 10 deletions
				
			
		
							
								
								
									
										48
									
								
								cdist/conf/type/__hostname/explorer/hostname
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								cdist/conf/type/__hostname/explorer/hostname
									
										
									
									
									
										Normal 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
 | 
			
		||||
| 
						 | 
				
			
			@ -2,6 +2,7 @@
 | 
			
		|||
#
 | 
			
		||||
# 2014-2017 Steven Armstrong (steven-cdist at armstrong.cc)
 | 
			
		||||
# 2014 Nico Schottelius (nico-cdist at schottelius.org)
 | 
			
		||||
# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
 | 
			
		||||
#
 | 
			
		||||
# This file is part of cdist.
 | 
			
		||||
#
 | 
			
		||||
| 
						 | 
				
			
			@ -46,8 +47,9 @@ case "$os" in
 | 
			
		|||
        fi
 | 
			
		||||
    ;;
 | 
			
		||||
    *)
 | 
			
		||||
        echo "Unsupported os: $os" >&2
 | 
			
		||||
        exit 1
 | 
			
		||||
        if [ "$name_running" -a "$name_running" = "$name_should" ]; then
 | 
			
		||||
            exit 0
 | 
			
		||||
        fi
 | 
			
		||||
    ;;
 | 
			
		||||
esac
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -56,23 +58,35 @@ esac
 | 
			
		|||
#
 | 
			
		||||
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
 | 
			
		||||
    archlinux|debian|ubuntu|devuan|centos|coreos|alpine)
 | 
			
		||||
        printf "printf '%%s\\\\n' '$name_should' > /etc/hostname\\n"
 | 
			
		||||
        echo "hostname -F /etc/hostname"
 | 
			
		||||
    ;;
 | 
			
		||||
    freebsd|openbsd)
 | 
			
		||||
        # NOTE: Writing the hostname to file is done in the manifest.
 | 
			
		||||
        echo "hostname '$name_should'"
 | 
			
		||||
    ;;
 | 
			
		||||
    suse)
 | 
			
		||||
        echo "hostname '$name_should'"
 | 
			
		||||
        printf "printf '%%s\\\\n' '$name_should' > /etc/HOSTNAME\\n"
 | 
			
		||||
    ;;
 | 
			
		||||
    *)
 | 
			
		||||
        echo "echo 'Unsupported OS: $os' >&2; exit 1"
 | 
			
		||||
    ;;
 | 
			
		||||
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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,6 +2,7 @@
 | 
			
		|||
#
 | 
			
		||||
# 2012 Steven Armstrong (steven-cdist at armstrong.cc)
 | 
			
		||||
# 2014 Nico Schottelius (nico-cdist at schottelius.org)
 | 
			
		||||
# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
 | 
			
		||||
#
 | 
			
		||||
# This file is part of cdist.
 | 
			
		||||
#
 | 
			
		||||
| 
						 | 
				
			
			@ -63,6 +64,7 @@ case "$os" in
 | 
			
		|||
        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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue