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-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
|
|
|
|
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%%.*}"
|
|
|
|
;;
|
2016-09-02 15:39:22 +00:00
|
|
|
esac
|
2014-04-24 23:48:58 +00:00
|
|
|
fi
|
|
|
|
|
2019-10-25 00:16:43 +00:00
|
|
|
if test -n "${max_len}" -a "$(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 15:11:35 +00:00
|
|
|
alpine|debian|devuan|ubuntu)
|
|
|
|
echo "$name_should" \
|
2019-10-26 15:25:44 +00:00
|
|
|
| __file /etc/hostname --source -
|
2019-10-26 15:11:35 +00:00
|
|
|
;;
|
|
|
|
macosx)
|
2014-04-24 23:48:58 +00:00
|
|
|
# handled in gencode-remote
|
|
|
|
:
|
|
|
|
;;
|
2019-10-26 15:11:35 +00:00
|
|
|
archlinux)
|
|
|
|
if test -n "$has_hostnamectl"
|
|
|
|
then
|
|
|
|
set_hostname_systemd "$name_should"
|
|
|
|
else
|
|
|
|
# Only for ancient ArchLinux, write to /etc/rc.conf on pre-systemd
|
|
|
|
# versions.
|
|
|
|
__key_value sysconfig-hostname \
|
|
|
|
--file /etc/rc.conf \
|
|
|
|
--delimiter '=' --exact_delimiter \
|
|
|
|
--key 'HOSTNAME' \
|
2019-10-26 15:25:44 +00:00
|
|
|
--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.
|
|
|
|
__key_value sysconfig-hostname \
|
|
|
|
--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-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/conf.d/hostname on OpenRC-based installations.
|
|
|
|
# On systemd use hostnamectl(1) in gencode-remote.
|
|
|
|
__key_value confd-hostname \
|
|
|
|
--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)
|
|
|
|
__key_value rcconf-hostname \
|
|
|
|
--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-05 18:10:52 +00:00
|
|
|
netbsd)
|
|
|
|
__key_value rcconf-hostname \
|
|
|
|
--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-10-05 18:10:52 +00:00
|
|
|
# To avoid confusion, ensure that the hostname is only stored once
|
|
|
|
__file /etc/myname --state absent
|
|
|
|
;;
|
2016-09-02 15:39:22 +00:00
|
|
|
openbsd)
|
2019-10-26 15:11:35 +00:00
|
|
|
echo "$name_should" \
|
2019-10-26 15:25:44 +00:00
|
|
|
| __file /etc/myname --source -
|
2019-10-26 15:11:35 +00:00
|
|
|
;;
|
|
|
|
suse)
|
|
|
|
# We write into /etc/HOSTNAME for backwards-compatibility. Modern SuSE
|
|
|
|
# has hostnamectl anyway and symlinks /etc/HOSTNAME to /etc/hostname.
|
|
|
|
echo "$name_should" \
|
2019-10-26 15:25:44 +00:00
|
|
|
| __file /etc/HOSTNAME --source -
|
2016-09-02 15:39:22 +00:00
|
|
|
;;
|
2014-04-24 23:48:58 +00:00
|
|
|
*)
|
2019-10-26 15:11:35 +00:00
|
|
|
# On other operating systems we fall back to systemd's hostnamectl if available…
|
|
|
|
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
|