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

79 lines
2.5 KiB
Bash
Executable File

#!/bin/sh -e
#
# 2014-2017 Steven Armstrong (steven-cdist at armstrong.cc)
# 2014 Nico Schottelius (nico-cdist at schottelius.org)
#
# 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/>.
#
if [ -f "$__object/parameter/name" ]; then
name_should="$(cat "$__object/parameter/name")"
else
name_should="${__target_host%%.*}"
fi
os=$(cat "$__global/explorer/os")
name_running=$(cat "$__global/explorer/hostname")
name_config=$(cat "$__object/explorer/hostname_file")
name_sysconfig=$(cat "$__object/explorer/hostname_sysconfig")
has_hostnamectl=$(cat "$__object/explorer/has_hostnamectl")
################################################################################
# If everything is ok -> exit
#
case "$os" in
archlinux|debian|suse|ubuntu|devuan|coreos|alpine)
if [ "$name_config" = "$name_should" ] && [ "$name_running" = "$name_should" ]; then
exit 0
fi
;;
scientific|centos|freebsd|openbsd)
if [ "$name_sysconfig" = "$name_should" ] && [ "$name_running" = "$name_should" ]; then
exit 0
fi
;;
*)
echo "Unsupported os: $os" >&2
exit 1
;;
esac
################################################################################
# Setup hostname
#
echo changed >> "$__messages_out"
# Use the good old way to set the hostname even on machines running systemd.
case "$os" in
archlinux|debian|ubuntu|devuan|centos|coreos|alpine)
printf "printf '%%s\\\\n' '$name_should' > /etc/hostname\\n"
echo "hostname -F /etc/hostname"
;;
freebsd|openbsd)
echo "hostname '$name_should'"
;;
suse)
echo "hostname '$name_should'"
printf "printf '%%s\\\\n' '$name_should' > /etc/HOSTNAME\\n"
;;
esac
if [ "$has_hostnamectl" ]; then
# Allow hostnamectl set-hostname to fail silently.
# Who the fuck invented a tool that needs dbus to set the hostname anyway ...
echo "hostnamectl set-hostname '$name_should' || true"
fi