From 3c9eb7bce769e7a902e437cdb411be9f5179fe27 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Thu, 15 Aug 2019 16:15:47 +0200 Subject: [PATCH] [__hostname] Support all systemd OSes and prefer hostnamectl --- cdist/conf/type/__hostname/explorer/hostname | 48 ++++++++++++++++++++ cdist/conf/type/__hostname/gencode-remote | 32 +++++++++---- cdist/conf/type/__hostname/manifest | 4 +- 3 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 cdist/conf/type/__hostname/explorer/hostname diff --git a/cdist/conf/type/__hostname/explorer/hostname b/cdist/conf/type/__hostname/explorer/hostname new file mode 100644 index 00000000..aaaa3a30 --- /dev/null +++ b/cdist/conf/type/__hostname/explorer/hostname @@ -0,0 +1,48 @@ +#!/bin/sh +# +# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch) +# +# 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 . +# +# +# Retrieve the running hostname +# + +# Firstly, check hostnamectl if available. +if command -v hostnamectl >/dev/null +then + hostnamectl --static && exit 0 +fi + + +# Almost any distribution +if [ -f /etc/hostname ]; then + cat /etc/hostname +# SuSE +elif [ -f /etc/HOSTNAME ]; then + cat /etc/HOSTNAME +# OpenBSD +elif [ -f /etc/myname ]; then + cat /etc/myname +# FreeBSD +elif [ "$(uname -s)" == FreeBSD ]; then + (. /etc/rc.conf && echo $hostname) +# Mac OS X +elif [ "$(uname -s)" == Darwin -a -f "$(command -v scutil)" ]; then + scutil --get HostName 2>/dev/null || hostname -s +else + command -v hostname && hostname -s +fi diff --git a/cdist/conf/type/__hostname/gencode-remote b/cdist/conf/type/__hostname/gencode-remote index 8b5797dd..20de7078 100755 --- a/cdist/conf/type/__hostname/gencode-remote +++ b/cdist/conf/type/__hostname/gencode-remote @@ -2,6 +2,7 @@ # # 2014-2017 Steven Armstrong (steven-cdist at armstrong.cc) # 2014 Nico Schottelius (nico-cdist at schottelius.org) +# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch) # # This file is part of cdist. # @@ -46,8 +47,9 @@ case "$os" in fi ;; *) - echo "Unsupported os: $os" >&2 - exit 1 + if [ "$name_running" -a "$name_running" = "$name_should" ]; then + exit 0 + fi ;; esac @@ -56,23 +58,35 @@ esac # echo changed >> "$__messages_out" -# Use the good old way to set the hostname even on machines running systemd. + +# 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 < /etc/hostname + exit 0 +fi +EOF +fi + +# Use the good old way to set the hostname. Also if hostnamectl fails. 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) + # NOTE: Writing the hostname to file is done in the manifest. echo "hostname '$name_should'" ;; suse) echo "hostname '$name_should'" printf "printf '%%s\\\\n' '$name_should' > /etc/HOSTNAME\\n" ;; + *) + echo "echo 'Unsupported OS: $os' >&2; exit 1" + ;; 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 diff --git a/cdist/conf/type/__hostname/manifest b/cdist/conf/type/__hostname/manifest index 8f1adf12..5ea3b8ff 100755 --- a/cdist/conf/type/__hostname/manifest +++ b/cdist/conf/type/__hostname/manifest @@ -2,6 +2,7 @@ # # 2012 Steven Armstrong (steven-cdist at armstrong.cc) # 2014 Nico Schottelius (nico-cdist at schottelius.org) +# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch) # # This file is part of cdist. # @@ -63,6 +64,7 @@ case "$os" in echo "$name_should" | __file /etc/myname --source - ;; *) - not_supported + # On other operating systems we fall back to hostnamectl if available… + test -n "$(cat "$__object/explorer/has_hostnamectl")" || not_supported ;; esac