From 95b5fcbdc4d27badcbce4623eda77b621351646d Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Mon, 29 Jun 2020 11:09:59 +0200 Subject: [PATCH] [type/__interface_ifcfg] Add type with basic support for RedHat --- cdist/conf/type/__interface_ifcfg/man.rst | 68 +++++++ cdist/conf/type/__interface_ifcfg/manifest | 184 ++++++++++++++++++ .../type/__interface_ifcfg/parameter/boolean | 2 + .../parameter/default/onchange | 1 + .../__interface_ifcfg/parameter/default/state | 1 + .../type/__interface_ifcfg/parameter/optional | 4 + .../type/__interface_ifcfg/parameter/required | 1 + 7 files changed, 261 insertions(+) create mode 100644 cdist/conf/type/__interface_ifcfg/man.rst create mode 100755 cdist/conf/type/__interface_ifcfg/manifest create mode 100644 cdist/conf/type/__interface_ifcfg/parameter/boolean create mode 100644 cdist/conf/type/__interface_ifcfg/parameter/default/onchange create mode 100644 cdist/conf/type/__interface_ifcfg/parameter/default/state create mode 100644 cdist/conf/type/__interface_ifcfg/parameter/optional create mode 100644 cdist/conf/type/__interface_ifcfg/parameter/required diff --git a/cdist/conf/type/__interface_ifcfg/man.rst b/cdist/conf/type/__interface_ifcfg/man.rst new file mode 100644 index 00000000..72b50350 --- /dev/null +++ b/cdist/conf/type/__interface_ifcfg/man.rst @@ -0,0 +1,68 @@ +cdist-type__interface_ifcfg(7) +=============================== + +NAME +---- +cdist-type__interface_ifcfg - Manage network interfaces using ifcfg network +scripts. + + +DESCRIPTION +----------- +This type allows to configure network interfaces using the ifcfg network scripts +on RedHat and SuSE distributions. + + +REQUIRED PARAMETERS +------------------- +bootproto + ... + + +OPTIONAL PARAMETERS +------------------- +address + +comment + If supplied, the value will be inserted at the top of the configuration file + as a comment. +option + ... + Can be used multiple times. + + +BOOLEAN PARAMETERS +------------------ +onboot + +hotplug + + + +EXAMPLES +-------- + +.. code-block:: sh + + # TODO + __interface_ifcfg ... + + +SEE ALSO +-------- +:strong:`cdist-type__interface`\ (7) + + +AUTHORS +------- +Steven Armstrong +Dennis Camera + + +COPYING +------- +Copyright \(C) 2014--2020 Steven Armstrong and Dennis Camera. + +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. diff --git a/cdist/conf/type/__interface_ifcfg/manifest b/cdist/conf/type/__interface_ifcfg/manifest new file mode 100755 index 00000000..c1ebfd55 --- /dev/null +++ b/cdist/conf/type/__interface_ifcfg/manifest @@ -0,0 +1,184 @@ +#!/bin/sh -e +# +# 2014 Steven Armstrong (steven-cdist at armstrong.cc) +# 2020 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 . +# + +yesno() { test $? -eq 0 && echo yes || echo no; } +opt_format() { + printf '%s="%s"\n' "${1:?'option name missing'}" "${2:?'option value missing'}" +} +prefix2subnet() { + python3 -c 'for addr in __import__("sys").argv[1:]: print(__import__("ipaddress").ip_network(addr, strict=False).netmask)' "$@" +} + +os=$(cat "${__global}/explorer/os") + +case $os +in + (centos|redhat|scientific) + NETWORK_SCRIPTS_DIR='/etc/sysconfig/network-scripts' + ;; + (suse) + NETWORK_SCRIPTS_DIR='/etc/sysconfig/network' + ;; + (*) + printf "Your operating system (%s) is currently not supported by this type (%s)\n" "${os}" "${__type##*/}" >&2 + printf "Please contribute an implementation for it if you can.\n" >&2 + exit 1 + ;; +esac + +if test -s "${__object}/parameter/name" +then + name=$(cat "${__object}/parameter/name") +else + name=$__object_id +fi +state_should=$(cat "${__object}/parameter/state") +bootproto=$(cat "${__object}/parameter/bootproto") +onchange=$(cat "${__object}/parameter/onchange") + +ifcfg_file="${NETWORK_SCRIPTS_DIR:?}/ifcfg-${__object_id}" + + +onchange_action() { + case $onchange + in + (refresh) + printf "ifdown '%s' || true ; ifup '%s'\n" "${name}" "${name}" + ;; + (up) + printf "ifup '%s'\n" "${name}" + ;; + (down) + printf "ifdown '%s' || true\n" "${name}" + ;; + (leave) + # ignore + ;; + (*) + printf 'Invalid --onchange: %s\n' "${onchange}" >&2 + exit 1 + ;; + esac +} + + +{ + cat <<-EOF + # Created by cdist ${__type##*/} + # Do not change. Changes will be overwritten. + # + + EOF + + if test -f "$__object/parameter/comment" + then + awk '{ print "# " $0 }' <"$__object/parameter/comment" + echo + fi + + opt_format DEVICE "${name}" + opt_format NM_CONTROLLED no + + ignored_parameters= + + # manually_handled_parameters are handled in their own code block, not by + # the generic accumulator + manually_handled_parameters='bootproto comment name extra-config state onboot onchange hotplug' + + case $bootproto + in + (dhcp) + opt_format BOOTPROTO dhcp + ignored_parameters='address broadcast gateway' + ;; + (static|manual) + opt_format BOOTPROTO none + ;; + (*) + printf 'Invalid --bootproto: %s\n' "${bootproto}" >&2 + exit 1 + ;; + esac + + _bonding_opts= + + for _param in "${__object}"/parameter/* + do + _key=$(echo "${_param}" | tr '[:lower:]' '[:upper:]' | tr '-' '_') + + echo "${ignored_parameters}" | grep -q -w "${_param}" && continue + echo "${manually_handled_parameters}" | grep -q -w "${_param}" && continue + + case $_param + in + (onboot|hotplug) + # boolean options + opt_format "${_key}" "$(test -f "${__object}/parameter/${_param}" | yesno)" + ;; + (bond-master) + opt_format SLAVE yes + opt_format MASTER "$(cat "${__object}/parameter/${_param}")" + ;; + (bond-*) + _key=$(echo "${_param#bond-}" | tr '-' '_') + _value=$(cat "${__object}/parameter/${_param}") + + if test "${_key}" = 'arp_ip_target' + then + _value=$(echo "${_value}" | awk -v RS= -v FS='\n' -v OFS=',' '$1=$1') + fi + + _bonding_opts="${_bonding_opts} $(opt_format "${_key}" "${_value}")" + ;; + (*) + case $_param + in + (address) + n=0 + while read -r _addr + do + opt_format "IPADDR${n}" "${_addr%%/*}" + opt_format "NETMASK${n}" "$(prefix2subnet "${_addr##*/}")" + : $((n+=1)) + done <"${__object}/parameter/address" + unset n _addr + ;; + (*) + opt_format "${_key}" "$(cat "${__object}/parameter/${_param}")" + ;; + esac + ;; + esac + done + + if test -n "${_bonding_opts}" + then + # strip leading space + opt_format BONDING_OPTS "${_bonding_opts# }" + fi + + if test -f "${__object}/parameter/extra-config" + then + cat "${__object}/parameter/extra-config" + fi +} | __file "${ifcfg_file}" \ + --state "${state_should}" --owner root --group root --mode 0644 \ + --onchange "$(onchange_action)" --source - diff --git a/cdist/conf/type/__interface_ifcfg/parameter/boolean b/cdist/conf/type/__interface_ifcfg/parameter/boolean new file mode 100644 index 00000000..14b5e789 --- /dev/null +++ b/cdist/conf/type/__interface_ifcfg/parameter/boolean @@ -0,0 +1,2 @@ +onboot +hotplug diff --git a/cdist/conf/type/__interface_ifcfg/parameter/default/onchange b/cdist/conf/type/__interface_ifcfg/parameter/default/onchange new file mode 100644 index 00000000..e8f9c907 --- /dev/null +++ b/cdist/conf/type/__interface_ifcfg/parameter/default/onchange @@ -0,0 +1 @@ +leave diff --git a/cdist/conf/type/__interface_ifcfg/parameter/default/state b/cdist/conf/type/__interface_ifcfg/parameter/default/state new file mode 100644 index 00000000..e7f6134f --- /dev/null +++ b/cdist/conf/type/__interface_ifcfg/parameter/default/state @@ -0,0 +1 @@ +present diff --git a/cdist/conf/type/__interface_ifcfg/parameter/optional b/cdist/conf/type/__interface_ifcfg/parameter/optional new file mode 100644 index 00000000..f8e8477d --- /dev/null +++ b/cdist/conf/type/__interface_ifcfg/parameter/optional @@ -0,0 +1,4 @@ +comment +name +onchange +state diff --git a/cdist/conf/type/__interface_ifcfg/parameter/required b/cdist/conf/type/__interface_ifcfg/parameter/required new file mode 100644 index 00000000..507318ee --- /dev/null +++ b/cdist/conf/type/__interface_ifcfg/parameter/required @@ -0,0 +1 @@ +bootproto