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/>.
|
|
|
|
#
|
|
|
|
|
2020-11-11 13:09:26 +00:00
|
|
|
os=$(cat "${__global:?}/explorer/os")
|
|
|
|
name_running=$(cat "${__global:?}/explorer/hostname")
|
|
|
|
has_hostnamectl=$(cat "${__object:?}/explorer/has_hostnamectl")
|
2014-01-20 12:47:45 +00:00
|
|
|
|
2019-10-06 16:23:56 +00:00
|
|
|
|
2020-11-11 13:09:26 +00:00
|
|
|
if test -s "${__object:?}/parameter/name"
|
2019-10-26 15:11:35 +00:00
|
|
|
then
|
2020-11-11 13:09:26 +00:00
|
|
|
name_should=$(cat "${__object:?}/parameter/name")
|
2019-10-06 16:23:56 +00:00
|
|
|
else
|
2020-11-11 13:09:26 +00:00
|
|
|
case ${os}
|
2019-10-06 16:36:43 +00:00
|
|
|
in
|
2019-10-06 16:23:56 +00:00
|
|
|
# RedHat-derivatives and BSDs
|
2020-11-11 13:09:26 +00:00
|
|
|
(centos|fedora|redhat|scientific|freebsd|macosx|netbsd|openbsd)
|
2019-10-06 16:23:56 +00:00
|
|
|
# Hostname is FQDN
|
2020-11-11 13:09:26 +00:00
|
|
|
name_should=${__target_host:?}
|
|
|
|
;;
|
|
|
|
(*)
|
2019-10-06 16:23:56 +00:00
|
|
|
# Hostname is only first component of FQDN
|
2020-11-11 13:09:26 +00:00
|
|
|
name_should=${__target_host:?}
|
|
|
|
name_should=${name_should%%.*}
|
|
|
|
;;
|
2019-10-06 16:23:56 +00:00
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
2019-10-26 15:11:35 +00:00
|
|
|
|
2014-01-20 13:26:22 +00:00
|
|
|
################################################################################
|
2019-10-26 15:11:35 +00:00
|
|
|
# Check if the (running) hostname is already correct
|
2019-10-03 19:41:51 +00:00
|
|
|
#
|
2020-11-11 13:09:26 +00:00
|
|
|
test "${name_running}" != "${name_should}" || exit 0
|
2019-10-26 15:11:35 +00:00
|
|
|
|
2014-01-20 13:26:22 +00:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Setup hostname
|
|
|
|
#
|
2020-11-11 13:09:26 +00:00
|
|
|
echo 'changed' >>"${__messages_out:?}"
|
2014-01-20 12:47:45 +00:00
|
|
|
|
2019-10-26 15:11:35 +00:00
|
|
|
# Use the good old way to set the hostname.
|
2020-11-11 13:09:26 +00:00
|
|
|
case ${os}
|
2019-10-06 16:36:43 +00:00
|
|
|
in
|
2020-11-11 13:09:26 +00:00
|
|
|
(alpine|debian|devuan|ubuntu)
|
2019-10-26 15:11:35 +00:00
|
|
|
echo 'hostname -F /etc/hostname'
|
2020-11-11 13:09:26 +00:00
|
|
|
;;
|
|
|
|
(archlinux)
|
2019-10-26 19:01:54 +00:00
|
|
|
echo 'command -v hostnamectl >/dev/null 2>&1' \
|
2020-11-11 13:09:26 +00:00
|
|
|
"&& hostnamectl set-hostname '${name_should}'" \
|
|
|
|
"|| hostname '${name_should}'"
|
|
|
|
;;
|
|
|
|
(centos|fedora|redhat|scientific|freebsd|netbsd|openbsd|gentoo|void)
|
|
|
|
echo "hostname '${name_should}'"
|
|
|
|
;;
|
|
|
|
(openwrt)
|
|
|
|
echo "echo '${name_should}' >/proc/sys/kernel/hostname"
|
|
|
|
;;
|
|
|
|
(macosx)
|
|
|
|
echo "scutil --set HostName '${name_should}'"
|
|
|
|
;;
|
|
|
|
(solaris)
|
|
|
|
echo "uname -S '${name_should}'"
|
|
|
|
;;
|
2020-11-11 13:15:02 +00:00
|
|
|
(slackware|suse)
|
2019-11-17 13:46:37 +00:00
|
|
|
# We do not read from /etc/HOSTNAME, because the running
|
|
|
|
# hostname is the first component only while the file contains
|
|
|
|
# the FQDN.
|
2020-11-11 13:09:26 +00:00
|
|
|
echo "hostname '${name_should}'"
|
|
|
|
;;
|
|
|
|
(*)
|
2019-10-26 15:11:35 +00:00
|
|
|
# Fall back to set the hostname using hostnamectl, if available.
|
2020-11-11 13:09:26 +00:00
|
|
|
if test -n "${has_hostnamectl}"
|
2019-10-26 15:11:35 +00:00
|
|
|
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
|
2019-11-17 13:46:37 +00:00
|
|
|
# exit with non-zero when it fails (e.g. hostname too long,
|
|
|
|
# D-Bus failure, etc.).
|
2019-10-26 15:11:35 +00:00
|
|
|
|
|
|
|
echo "hostnamectl set-hostname \"\$(cat /etc/hostname)\""
|
2019-11-17 13:46:37 +00:00
|
|
|
echo "test \"\$(hostname)\" = \"\$(cat /etc/hostname)\"" \
|
|
|
|
" || hostname -F /etc/hostname"
|
2019-10-26 15:11:35 +00:00
|
|
|
else
|
2020-11-11 13:09:26 +00:00
|
|
|
printf "echo 'Unsupported OS: %s' >&2\n" "${os}"
|
|
|
|
printf 'exit 1\n'
|
2019-10-26 15:11:35 +00:00
|
|
|
fi
|
2020-11-11 13:09:26 +00:00
|
|
|
;;
|
2017-08-29 20:50:47 +00:00
|
|
|
esac
|