2017-06-28 11:19:32 +00:00
|
|
|
#!/bin/sh -e
|
2014-01-20 12:47:45 +00:00
|
|
|
#
|
|
|
|
# 2012 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/>.
|
|
|
|
#
|
|
|
|
|
2019-10-24 17:44:51 +00:00
|
|
|
not_supported() {
|
2019-10-26 15:11:35 +00:00
|
|
|
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
|
|
|
|
exit 1
|
2019-10-24 17:44:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 15:11:35 +00:00
|
|
|
set_hostname_systemd() {
|
2019-10-26 15:25:44 +00:00
|
|
|
echo "$1" | __file /etc/hostname --source -
|
2019-10-26 15:11:35 +00:00
|
|
|
}
|
2019-10-24 17:44:51 +00:00
|
|
|
|
2014-01-20 12:47:45 +00:00
|
|
|
os=$(cat "$__global/explorer/os")
|
2019-11-17 13:46:37 +00:00
|
|
|
os_version=$(cat "$__global/explorer/os_version")
|
2019-11-22 16:56:11 +00:00
|
|
|
os_major=$(echo "$os_version" | grep -o '^[0-9][0-9]*' || true)
|
2019-11-17 13:46:37 +00:00
|
|
|
|
2019-10-24 23:41:26 +00:00
|
|
|
max_len=$(cat "$__object/explorer/max_len")
|
2019-10-26 15:11:35 +00:00
|
|
|
has_hostnamectl=$(cat "$__object/explorer/has_hostnamectl")
|
2019-10-24 23:41:26 +00:00
|
|
|
|
2019-10-26 15:11:35 +00:00
|
|
|
if test -s "$__object/parameter/name"
|
|
|
|
then
|
|
|
|
name_should=$(cat "$__object/parameter/name")
|
2014-04-24 23:48:58 +00:00
|
|
|
else
|
2019-10-06 16:36:43 +00:00
|
|
|
case $os
|
|
|
|
in
|
2019-10-06 16:23:56 +00:00
|
|
|
# RedHat-derivatives and BSDs
|
2019-10-27 14:14:09 +00:00
|
|
|
centos|fedora|redhat|scientific|freebsd|netbsd|openbsd|slackware)
|
2019-10-06 16:23:56 +00:00
|
|
|
# Hostname is FQDN
|
|
|
|
name_should="${__target_host}"
|
|
|
|
;;
|
2019-11-17 13:46:37 +00:00
|
|
|
suse|opensuse-leap)
|
|
|
|
# Classic SuSE stores the FQDN in /etc/HOSTNAME, while
|
|
|
|
# systemd does not. The running hostname is the first
|
|
|
|
# component in both cases.
|
|
|
|
# In versions before 15.x, the FQDN is stored in /etc/hostname.
|
|
|
|
if test -n "$has_hostnamectl" && test "$os_major" -ge 15 \
|
|
|
|
&& test "$os_major" -ne 42
|
|
|
|
then
|
|
|
|
name_should="${__target_host%%.*}"
|
|
|
|
else
|
|
|
|
name_should="${__target_host}"
|
|
|
|
fi
|
|
|
|
;;
|
2019-10-06 16:23:56 +00:00
|
|
|
*)
|
2019-11-17 13:46:37 +00:00
|
|
|
# Hostname is only first component of FQDN on all other systems.
|
2019-10-06 16:23:56 +00:00
|
|
|
name_should="${__target_host%%.*}"
|
|
|
|
;;
|
2016-09-02 15:39:22 +00:00
|
|
|
esac
|
2014-04-24 23:48:58 +00:00
|
|
|
fi
|
|
|
|
|
2019-11-16 13:58:30 +00:00
|
|
|
if test -n "$max_len" && test "$(printf '%s' "$name_should" | wc -c)" -gt "$max_len"
|
2019-10-24 17:44:51 +00:00
|
|
|
then
|
2019-10-26 15:11:35 +00:00
|
|
|
printf "Host name too long. Up to %u characters allowed.\n" "${max_len}" >&2
|
|
|
|
exit 1
|
2019-10-24 17:44:51 +00:00
|
|
|
fi
|
2014-01-20 12:47:45 +00:00
|
|
|
|
2019-10-06 16:36:43 +00:00
|
|
|
case $os
|
|
|
|
in
|
2019-10-26 20:17:08 +00:00
|
|
|
alpine|debian|devuan|ubuntu|void)
|
2019-10-27 12:16:05 +00:00
|
|
|
echo "$name_should" | __file /etc/hostname --source -
|
2014-04-24 23:48:58 +00:00
|
|
|
;;
|
2019-10-26 15:11:35 +00:00
|
|
|
archlinux)
|
|
|
|
if test -n "$has_hostnamectl"
|
|
|
|
then
|
|
|
|
set_hostname_systemd "$name_should"
|
|
|
|
else
|
2019-11-16 22:31:58 +00:00
|
|
|
echo 'Ancient ArchLinux variants without hostnamectl are not supported.' >&2
|
|
|
|
exit 1
|
2019-10-26 15:11:35 +00:00
|
|
|
# Only for ancient ArchLinux, write to /etc/rc.conf on pre-systemd
|
2019-10-27 19:12:10 +00:00
|
|
|
# versions. There are some versions which use /etc/hostname but not
|
2019-11-16 22:31:58 +00:00
|
|
|
# systemd. It is unclear which ones these are.
|
|
|
|
|
|
|
|
# __key_value '/etc/rc.conf:HOSTNAME' \
|
|
|
|
# --file /etc/rc.conf \
|
|
|
|
# --delimiter '=' --exact_delimiter \
|
|
|
|
# --key 'HOSTNAME' \
|
|
|
|
# --value "\"$name_should\""
|
2019-10-26 15:11:35 +00:00
|
|
|
fi
|
|
|
|
;;
|
2019-10-06 15:56:37 +00:00
|
|
|
centos|fedora|redhat|scientific)
|
2019-10-26 15:11:35 +00:00
|
|
|
if test -z "$has_hostnamectl"
|
2019-10-12 16:47:27 +00:00
|
|
|
then
|
|
|
|
# Only write to /etc/sysconfig/network on non-systemd versions.
|
|
|
|
# On systemd-based versions this entry is ignored.
|
2019-10-27 12:16:05 +00:00
|
|
|
__key_value '/etc/sysconfig/network:HOSTNAME' \
|
2019-10-12 16:47:27 +00:00
|
|
|
--file /etc/sysconfig/network \
|
|
|
|
--delimiter '=' --exact_delimiter \
|
|
|
|
--key HOSTNAME \
|
2019-10-26 15:25:44 +00:00
|
|
|
--value "\"$name_should\""
|
2019-10-26 15:11:35 +00:00
|
|
|
else
|
|
|
|
set_hostname_systemd "$name_should"
|
2019-10-12 16:47:27 +00:00
|
|
|
fi
|
2014-04-24 23:48:58 +00:00
|
|
|
;;
|
2019-10-06 21:34:59 +00:00
|
|
|
gentoo)
|
2019-11-16 13:58:30 +00:00
|
|
|
# Only write to /etc/conf.d/hostname on OpenRC-based installations.
|
|
|
|
# On systemd use hostnamectl(1) in gencode-remote.
|
2019-10-26 15:11:35 +00:00
|
|
|
if test -z "$has_hostnamectl"
|
2019-10-12 16:47:27 +00:00
|
|
|
then
|
2019-10-27 12:16:05 +00:00
|
|
|
__key_value '/etc/conf.d/hostname:hostname' \
|
2019-10-12 16:47:27 +00:00
|
|
|
--file /etc/conf.d/hostname \
|
|
|
|
--delimiter '=' --exact_delimiter \
|
|
|
|
--key 'hostname' \
|
|
|
|
--value "\"$name_should\""
|
2019-10-26 15:11:35 +00:00
|
|
|
else
|
|
|
|
set_hostname_systemd "$name_should"
|
2019-10-12 16:47:27 +00:00
|
|
|
fi
|
2019-10-06 21:34:59 +00:00
|
|
|
;;
|
2018-03-26 06:53:31 +00:00
|
|
|
freebsd)
|
2019-10-27 12:16:05 +00:00
|
|
|
__key_value '/etc/rc.conf:hostname' \
|
2018-03-26 06:53:31 +00:00
|
|
|
--file /etc/rc.conf \
|
2019-10-06 16:01:58 +00:00
|
|
|
--delimiter '=' --exact_delimiter \
|
2018-03-26 06:53:31 +00:00
|
|
|
--key 'hostname' \
|
2019-10-26 15:25:44 +00:00
|
|
|
--value "\"$name_should\""
|
2018-03-26 06:53:31 +00:00
|
|
|
;;
|
2019-10-27 12:16:05 +00:00
|
|
|
macosx)
|
|
|
|
# handled in gencode-remote
|
|
|
|
:
|
|
|
|
;;
|
2019-10-05 18:10:52 +00:00
|
|
|
netbsd)
|
2019-10-27 12:16:05 +00:00
|
|
|
__key_value '/etc/rc.conf:hostname' \
|
2019-10-05 18:10:52 +00:00
|
|
|
--file /etc/rc.conf \
|
2019-10-06 16:01:58 +00:00
|
|
|
--delimiter '=' --exact_delimiter \
|
2019-10-05 18:10:52 +00:00
|
|
|
--key 'hostname' \
|
2019-10-26 15:25:44 +00:00
|
|
|
--value "\"$name_should\""
|
2019-10-26 15:11:35 +00:00
|
|
|
|
2019-11-17 13:46:37 +00:00
|
|
|
# To avoid confusion, ensure that the hostname is only stored once.
|
2019-10-05 18:10:52 +00:00
|
|
|
__file /etc/myname --state absent
|
|
|
|
;;
|
2016-09-02 15:39:22 +00:00
|
|
|
openbsd)
|
2019-10-27 12:16:05 +00:00
|
|
|
echo "$name_should" | __file /etc/myname --source -
|
2019-10-26 15:11:35 +00:00
|
|
|
;;
|
2019-10-27 14:14:09 +00:00
|
|
|
slackware)
|
2019-11-17 13:46:37 +00:00
|
|
|
# We write the FQDN into /etc/HOSTNAME. But /etc/rc.d/rc.M will only
|
|
|
|
# read the first component from this file and set it as the running
|
|
|
|
# hostname on boot.
|
2019-10-27 14:14:09 +00:00
|
|
|
echo "$name_should" | __file /etc/HOSTNAME --source -
|
|
|
|
;;
|
2019-11-16 13:57:27 +00:00
|
|
|
solaris)
|
|
|
|
echo "$name_should" | __file /etc/nodename --source -
|
|
|
|
;;
|
2019-11-17 13:46:37 +00:00
|
|
|
suse|opensuse-leap)
|
|
|
|
# Modern SuSE provides /etc/HOSTNAME as a symlink for
|
|
|
|
# backwards-compatibility. Unfortunately it cannot be used
|
|
|
|
# here as __file does not follow the symlink.
|
|
|
|
# Therefore, we use the presence of the hostnamectl binary as
|
|
|
|
# an indication of which file to use. This unfortunately does
|
|
|
|
# not work correctly on openSUSE 12.x which provides
|
|
|
|
# hostnamectl but not /etc/hostname.
|
|
|
|
|
|
|
|
if test -n "$has_hostnamectl" -a "$os_major" -gt 12
|
|
|
|
then
|
|
|
|
hostname_file='/etc/hostname'
|
|
|
|
else
|
|
|
|
hostname_file='/etc/HOSTNAME'
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$name_should" | __file "$hostname_file" --source -
|
2016-09-02 15:39:22 +00:00
|
|
|
;;
|
2014-04-24 23:48:58 +00:00
|
|
|
*)
|
2019-11-17 13:46:37 +00:00
|
|
|
# On other operating systems we fall back to systemd's
|
|
|
|
# hostnamectl if available…
|
2019-10-26 15:11:35 +00:00
|
|
|
if test -n "$has_hostnamectl"
|
|
|
|
then
|
|
|
|
set_hostname_systemd "$name_should"
|
|
|
|
else
|
|
|
|
not_supported
|
|
|
|
fi
|
2014-04-24 23:48:58 +00:00
|
|
|
;;
|
2014-01-20 12:47:45 +00:00
|
|
|
esac
|