2017-06-28 11:19:32 +00:00
|
|
|
#!/bin/sh -e
|
2014-01-20 12:47:45 +00:00
|
|
|
#
|
2017-08-29 20:50:47 +00:00
|
|
|
# 2014-2017 Steven Armstrong (steven-cdist at armstrong.cc)
|
2014-01-20 12:50:42 +00:00
|
|
|
# 2014 Nico Schottelius (nico-cdist at schottelius.org)
|
2019-08-15 14:15:47 +00:00
|
|
|
# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
|
2014-01-20 12:47:45 +00:00
|
|
|
#
|
|
|
|
# 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")
|
2014-01-20 13:26:22 +00:00
|
|
|
name_running=$(cat "$__global/explorer/hostname")
|
2019-10-04 18:35:14 +00:00
|
|
|
name_config=$(cat "$__object/explorer/persistent_hostname")
|
2014-01-20 13:26:22 +00:00
|
|
|
has_hostnamectl=$(cat "$__object/explorer/has_hostnamectl")
|
2014-01-20 12:47:45 +00:00
|
|
|
|
2019-10-06 16:23:56 +00:00
|
|
|
|
|
|
|
if [ -f "$__object/parameter/name" ]; then
|
|
|
|
name_should="$(cat "$__object/parameter/name")"
|
|
|
|
else
|
2019-10-06 16:36:43 +00:00
|
|
|
case $os
|
|
|
|
in
|
2019-10-06 16:23:56 +00:00
|
|
|
# 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
|
|
|
|
|
2014-01-20 13:26:22 +00:00
|
|
|
################################################################################
|
2019-10-03 19:41:51 +00:00
|
|
|
# Check if the hostname is already correct
|
|
|
|
#
|
2019-10-06 16:36:43 +00:00
|
|
|
if [ "$name_running" = "$name_should" ] \
|
|
|
|
&& [ -z "$name_config" -o "$name_config" = "$name_should" ]
|
|
|
|
then
|
2019-10-04 18:35:14 +00:00
|
|
|
exit 0
|
2019-10-03 19:41:51 +00:00
|
|
|
fi
|
2014-01-20 13:26:22 +00:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Setup hostname
|
|
|
|
#
|
2019-10-06 16:36:43 +00:00
|
|
|
echo 'changed' >> "$__messages_out"
|
2014-01-20 12:47:45 +00:00
|
|
|
|
2019-08-15 14:15:47 +00:00
|
|
|
|
|
|
|
# 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.
|
2019-10-06 16:36:43 +00:00
|
|
|
case $os
|
|
|
|
in
|
2019-10-06 15:56:37 +00:00
|
|
|
archlinux|debian|ubuntu|devuan|coreos|alpine)
|
2019-10-06 19:03:49 +00:00
|
|
|
echo "printf '%s\\n' '$name_should' > /etc/hostname"
|
2017-08-29 20:50:47 +00:00
|
|
|
echo "hostname -F /etc/hostname"
|
|
|
|
;;
|
2019-10-06 15:56:37 +00:00
|
|
|
centos|fedora|redhat|scientific|freebsd|openbsd)
|
2019-08-15 14:15:47 +00:00
|
|
|
# NOTE: Writing the hostname to file is done in the manifest.
|
2017-08-29 20:50:47 +00:00
|
|
|
echo "hostname '$name_should'"
|
|
|
|
;;
|
2019-08-15 14:16:38 +00:00
|
|
|
macosx)
|
|
|
|
echo "scutil --set HostName '$name_should'"
|
|
|
|
;;
|
2019-10-06 19:03:49 +00:00
|
|
|
suse)
|
|
|
|
# We write into /etc/HOSTNAME for backwards-compatibility. Modern SuSE
|
|
|
|
# has hostnamectl anyway and symlinks /etc/HOSTNAME to /etc/hostname.
|
|
|
|
echo "printf '%s\\n' '$name_should' > /etc/HOSTNAME"
|
|
|
|
echo 'hostname -F /etc/HOSTNAME'
|
|
|
|
;;
|
2019-08-15 14:15:47 +00:00
|
|
|
*)
|
|
|
|
echo "echo 'Unsupported OS: $os' >&2; exit 1"
|
|
|
|
;;
|
2017-08-29 20:50:47 +00:00
|
|
|
esac
|