From e3e78ae3733410d68715d28146df563fac31d315 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Wed, 29 Jan 2020 18:12:53 +0100 Subject: [PATCH] [type/__interface_ifupdown.d] Major refactor --- .../__interface_ifupdown.d/files/interfaces | 11 ++ .../__interface_ifupdown.d/gencode-remote | 20 --- .../conf/type/__interface_ifupdown.d/man.rst | 69 +++++++- .../conf/type/__interface_ifupdown.d/manifest | 160 +++++++++++------- .../__interface_ifupdown.d/parameter/boolean | 2 +- .../parameter/default/onchange | 1 + .../__interface_ifupdown.d/parameter/optional | 3 + 7 files changed, 181 insertions(+), 85 deletions(-) create mode 100644 cdist/conf/type/__interface_ifupdown.d/files/interfaces delete mode 100755 cdist/conf/type/__interface_ifupdown.d/gencode-remote create mode 100644 cdist/conf/type/__interface_ifupdown.d/parameter/default/onchange diff --git a/cdist/conf/type/__interface_ifupdown.d/files/interfaces b/cdist/conf/type/__interface_ifupdown.d/files/interfaces new file mode 100644 index 00000000..084df231 --- /dev/null +++ b/cdist/conf/type/__interface_ifupdown.d/files/interfaces @@ -0,0 +1,11 @@ +# Managed by cdist +# Changes will be overwritten. + +# This file describes the network interfaces available on your system +# and how to activate them. For more information, see interfaces(5). + +source /etc/network/interfaces.d/* + +# The loopback network interface +auto lo +iface lo inet loopback diff --git a/cdist/conf/type/__interface_ifupdown.d/gencode-remote b/cdist/conf/type/__interface_ifupdown.d/gencode-remote deleted file mode 100755 index 77ad9d25..00000000 --- a/cdist/conf/type/__interface_ifupdown.d/gencode-remote +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh -e -# -# 2020 Dennis Camera (dennis.camera@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 . -# - diff --git a/cdist/conf/type/__interface_ifupdown.d/man.rst b/cdist/conf/type/__interface_ifupdown.d/man.rst index f297f1da..3050b7c0 100644 --- a/cdist/conf/type/__interface_ifupdown.d/man.rst +++ b/cdist/conf/type/__interface_ifupdown.d/man.rst @@ -17,17 +17,70 @@ NOTE: Only interfaces declared by cdist are removed when using --state absent. REQUIRED PARAMETERS ------------------- -None. +family + The name of the address family that the interface uses. + Available options are: 'inet', 'ipx', 'inet6', 'can'. +method + The name of the method used to configure the interface. + Available options depend on the address family and are, for + inet: + 'loopback', 'static', 'manual', 'dhcp', 'bootp', 'tunnel', 'ppp', + 'wvdial', and 'ip4ll'. + ipx: + 'static', and 'dynamic'. + inet6: + 'auto', 'loopback', 'static', 'manual', 'dhcp', 'v4tunnel', '6to4'. + can: + 'static'. OPTIONAL PARAMETERS ------------------- -None. +name + The name of the logical interface to configure (usually equivalent to the + physical interface). + Defaults to __object_id. +comment + If supplied, the value will be inserted at the top of the configuration file + as a comment. + If comment is '-', what was written to stdin is used as the comment. +rename + If supplied, the given interface renaming spefication will be added to the + config file. + This option can e.g. be used to generate predictable interface names based on + the interface's MAC address. +state + Either present or absent. Defaults to present. +option + Additional option that is added to the generated interface configuration + verbatim. +onchange + The action to perform if the interface configuration has changed. + Available options are: + leave (default) + Do nothing. + up + Bring the interface up if it is down. + down + Bring the interface down if it is up. + refresh + Refresh the interface (down && up). BOOLEAN PARAMETERS ------------------ -None. +auto + If supplied, the interface will be marked "auto" and brought up during boot. +hotplug + Allow hotplug support for this interface. + The interface will be brought up when udev detects it, this can be during + boot or later. +no-auto-down + If supplied, the interface will not be brought down by the command + "ifdown -a". +no-scripts + If supplied, scripts in '/etc/network/if-*.d/' will not be run when this + interface is brought up or down. EXAMPLES @@ -35,13 +88,17 @@ EXAMPLES .. code-block:: sh - # TODO - __interface_ifupdown.d + # Configure interface eth0 to use DHCP. + __interface_ifupdown.d eth0 --family inet --method dhcp + + # Rename interface with MAC 00:11:22:33:44:55 to eth1 and use DHCP. + __interface_ifupdown.d eth1 --rename mac/00:11:22:33:44:55=eth1 \ + --family inet --method dhcp SEE ALSO -------- -:strong:`TODO`\ (7) +:strong:`interfaces`\ (5) AUTHORS diff --git a/cdist/conf/type/__interface_ifupdown.d/manifest b/cdist/conf/type/__interface_ifupdown.d/manifest index 4180db93..91ac3e82 100755 --- a/cdist/conf/type/__interface_ifupdown.d/manifest +++ b/cdist/conf/type/__interface_ifupdown.d/manifest @@ -21,10 +21,35 @@ INTERFACES_FILE=/etc/network/interfaces INTERFACES_D_PATH=/etc/network/interfaces.d -os=$(cat "$__global/explorer/os") +os=$(cat "${__global}/explorer/os") +name=$(test -s "${__object}/parameter/name" && cat "${__object}/parameter/name" || echo "${__object_id}") state=$(cat "${__object}/parameter/state") + +onchange_action() { + case $(cat "${__object}/parameter/onchange") + in + refresh) + printf "ifdown --force '%s' ; ifup '%s'\n" "${name}" "${name}" + ;; + up) + printf "ifup '%s'\n" "${name}" + ;; + down) + printf "ifdown --force '%s'\n" "${name}" + ;; + leave) + : # ignore + ;; + *) + printf 'Invalid option "%s" for --onchange!' "$(cat "${__object}/parameter/onchange")" >&2 + exit 1 + ;; + esac +} + + case $os in debian) @@ -57,70 +82,89 @@ in esac -name=$(test -s "${__object}/parameter/name" && cat "${__object}/parameter/name" || echo "${__object_id}") -addrfam=$(cat "${__object}/parameter/family") -method=$(cat "${__object}/parameter/method") - -options=$(test -s "${__object}/parameter/option" && cat "${__object}/parameter/option" || true) - -file_path="${INTERFACES_D_PATH}/${name}" - - if test "${state}" = 'present' then - # If state is present, ensure that the interfaces.d directory is sourced in - # the main interfaces file - __line "${INTERFACES_FILE}:source_interfaces.d" --state present \ - --file "${INTERFACES_FILE}" \ - --before '^#?[:blank:]*(auto|no-auto-down|no-scripts|allow-|iface|mapping|rename|source|source-directory)|^#[[:blank:]]*The .* interface$' \ - --line "source ${INTERFACES_D_PATH}/*" + if test "${IFUPDOWND_LEAVE_EXISTING_CONFIG:-0}" -eq 0 + then + # The /etc/network/interfaces file is overwritten with a basic example. + __file "${INTERFACES_FILE}" --state present --owner root --mode 0644 \ + --source "${__type}/files/interfaces" + else + # If state is present and IFUPDOWND_LEAVE_EXISTING_CONFIG is set, + # ensure that the interfaces.d directory is sourced in the main + # interfaces file, being as minimally invasive as possible. + __line "${INTERFACES_FILE}:source_interfaces.d" --state present \ + --file "${INTERFACES_FILE}" \ + --before '^#?[:blank:]*(auto|no-auto-down|no-scripts|allow-|iface|mapping|rename|source|source-directory)|^#[[:blank:]]*The .* interface$' \ + --line "source ${INTERFACES_D_PATH}/*" + fi # FIXME: State should be --pre-exists... - __directory "${INTERFACES_D_PATH}" --owner root --mode 0644 --state present - - # NOTE: No file ending is used not to break the source-directory stanza (if - # used and at least for normal interface names) - # FIXME: The file will be left there when --state absent. It should be - # deleted if empty (could be non-empty if user has manual - # configuration) - require=__directory/"${INTERFACES_D_PATH}" - __file "${file_path}" --state exists --owner root --mode 0644 - export require="__file/${file_path}" + __directory "${INTERFACES_D_PATH}" --owner root --mode 0755 --state present + export require="__directory/${INTERFACES_D_PATH}" fi -# Put together the "allow" lines (these are the line(s) usually preceeding the -# iface line in the config) -allow='' -if ! test -f "${__object}/parameter/no-auto" -then - allow="${allow}auto ${name} -" -fi -if test -f "${__object}/parameter/hotplug" -then - allow="${allow}allow-hotplug ${name} -" -fi -if test -f "${__object}/parameter/no-auto-down" -then - allow="${allow}no-auto-down ${name} -" -fi -if test -f "${__object}/parameter/no-scripts" -then - allow="${allow}no-scripts ${name} -" -fi +# Construct interface config file +( + cat <<-EOF + # Managed by cdist (${__type##*/}) + # Do not change. Changes will be overwritten. -# Construct file + EOF -__block "${__object_id}" --state "${state}" \ - --file "${file_path}" \ - --prefix "#cdist:${__type##*/}/${__object_id}" \ - --suffix "#/cdist:${__type##*/}/${__object_id}" \ - --text - <