From b6bae508a8641ee6a6bba0751bf57b2d8113b4bf Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Mon, 20 Jan 2014 14:26:22 +0100 Subject: [PATCH] use hostnamectl, if available, else use /etc/hostname and hostname Signed-off-by: Nico Schottelius --- .../type/__hostname/explorer/has_hostnamectl | 24 ++++++++++++++ .../type/__hostname/explorer/hostname_file | 26 +++++++++++++++ cdist/conf/type/__hostname/gencode-remote | 33 ++++++++++++------- cdist/conf/type/__hostname/man.text | 5 +++ 4 files changed, 77 insertions(+), 11 deletions(-) create mode 100755 cdist/conf/type/__hostname/explorer/has_hostnamectl create mode 100755 cdist/conf/type/__hostname/explorer/hostname_file diff --git a/cdist/conf/type/__hostname/explorer/has_hostnamectl b/cdist/conf/type/__hostname/explorer/has_hostnamectl new file mode 100755 index 00000000..9040023d --- /dev/null +++ b/cdist/conf/type/__hostname/explorer/has_hostnamectl @@ -0,0 +1,24 @@ +#!/bin/sh +# +# 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 . +# +# +# Check whether system has hostnamectl +# + +command -v hostnamectl || true diff --git a/cdist/conf/type/__hostname/explorer/hostname_file b/cdist/conf/type/__hostname/explorer/hostname_file new file mode 100755 index 00000000..ed28c8a8 --- /dev/null +++ b/cdist/conf/type/__hostname/explorer/hostname_file @@ -0,0 +1,26 @@ +#!/bin/sh +# +# 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 . +# +# +# Retrieve the contents of /etc/hostname +# + +if [ -f /etc/hostname ]; then + cat /etc/hostname +fi diff --git a/cdist/conf/type/__hostname/gencode-remote b/cdist/conf/type/__hostname/gencode-remote index 6760507f..3d208cbe 100755 --- a/cdist/conf/type/__hostname/gencode-remote +++ b/cdist/conf/type/__hostname/gencode-remote @@ -20,20 +20,31 @@ # if [ -f "$__object/parameter/name" ]; then - name="$(cat "$__object/parameter/name")" + name_should="$(cat "$__object/parameter/name")" else - name="$(echo "${__target_host%%.*}")" + name_should="$(echo "${__target_host%%.*}")" fi os=$(cat "$__global/explorer/os") +name_running=$(cat "$__global/explorer/hostname") +name_config=$(cat "$__object/explorer/hostname_file") +has_hostnamectl=$(cat "$__object/explorer/has_hostnamectl") -echo "printf '%s\n' '$name' > /etc/hostname" +################################################################################ +# If everything is ok -> exit +# +if [ "$name_config" = "$name_should" -a "$name_running" = "$name_should" ]; then + exit 0 +fi -case "$os" in - debian|ubuntu) - echo "hostname \"$name\"" - ;; - archlinux) - echo "hostnamectl set-hostname \"$name\"" - ;; -esac +################################################################################ +# Setup hostname +# +echo changed >> "$__messages_out" + +if [ "$has_hostnamectl" ]; then + echo "hostnamectl set-hostname '$name_should'" +else + echo "hostname '$name_should'" + echo "printf '%s\n' '$name_should' > /etc/hostname" +fi diff --git a/cdist/conf/type/__hostname/man.text b/cdist/conf/type/__hostname/man.text index 07ec1083..ac44d426 100644 --- a/cdist/conf/type/__hostname/man.text +++ b/cdist/conf/type/__hostname/man.text @@ -24,6 +24,11 @@ name:: (${__target_host%%.*}) +MESSAGES +-------- +changed:: + Changed the hostname + EXAMPLES --------