cdist/cdist/conf/type/__hostname/gencode-remote

109 lines
3.6 KiB
Bash
Executable File

#!/bin/sh -e
#
# 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.
#
# 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/>.
#
os=$(cat "$__global/explorer/os")
name_running=$(cat "$__global/explorer/hostname")
name_config=$(cat "$__object/explorer/persistent_hostname")
has_hostnamectl=$(cat "$__object/explorer/has_hostnamectl")
if [ -f "$__object/parameter/name" ]; then
name_should="$(cat "$__object/parameter/name")"
else
case $os
in
# RedHat-derivatives and BSDs
centos|fedora|redhat|scientific|freebsd|netbsd|openbsd)
# Hostname is FQDN
name_should="${__target_host}"
;;
*)
# Hostname is only first component of FQDN
name_should="${__target_host%%.*}"
;;
esac
fi
################################################################################
# Check if the hostname is already correct
#
if [ "$name_running" = "$name_should" ] \
&& [ -z "$name_config" -o "$name_config" = "$name_should" ]
then
exit 0
fi
################################################################################
# Setup hostname
#
echo 'changed' >>"$__messages_out"
# 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
in
alpine|archlinux|debian|devuan|ubuntu)
echo "echo '$name_should' >/etc/hostname"
echo "hostname -F /etc/hostname"
;;
coreos|openbmc-phosphor)
# 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'"
;;
macosx)
echo "scutil --set HostName '$name_should'"
;;
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'
;;
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"
;;
esac