[type/__interface_ifupdown.d] Major refactor

This commit is contained in:
Dennis Camera 2020-01-29 18:12:53 +01:00
parent 6b63b3c369
commit e3e78ae373
7 changed files with 181 additions and 85 deletions

View file

@ -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

View file

@ -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 <http://www.gnu.org/licenses/>.
#

View file

@ -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

View file

@ -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 - <<EOF
${allow}iface ${name} ${addrfam} ${method}
$(echo "$options" | while read -r opt ; do printf '\t%s %s\n' "${opt%%=*}" "${opt#*=}" ; done)
EOF
if test -f "${__object}/parameter/comment"
then
# Prefix lines with # and append trailing empty line.
comment_file="${__object}/parameter/comment"
if test "$(cat "${comment_file}")" = '-'
then
comment_file="${__object}/stdin"
fi
# shellcheck disable=SC2016
sed -e 's/^/# /;$G' "${comment_file}"
unset comment_file
fi
# Put together rename line
if test -s "${__object}/parameter/rename"
then
printf 'rename %s\n' "$(cat "${__object}/parameter/rename")"
fi
# Put together the "allow" lines (these are the line(s) usually preceeding
# the iface line in the config).
if test -f "${__object}/parameter/onboot"
then
printf 'auto %s\n' "$name"
fi
if test -f "${__object}/parameter/hotplug"
then
printf 'allow-hotplug %s\n' "$name"
fi
if test -f "${__object}/parameter/no-auto-down"
then
printf 'no-auto-down %s\n' "$name"
fi
if test -f "${__object}/parameter/no-scripts"
then
printf 'no-scripts %s\n' "$name"
fi
addrfam=$(cat "${__object}/parameter/family")
method=$(cat "${__object}/parameter/method")
printf 'iface %s %s %s\n' "$name" "$addrfam" "$method"
while read -r opt
do
printf '\t%s\n' "$opt"
done <"${__object}/parameter/option"
) | \
# NOTE: No file ending is used not to break the source-directory stanza (if
# used and at least for normal interface names)
__file "${INTERFACES_D_PATH}/${__object_id}" --state "$state" \
--owner root --mode 0644 --source - \
--onchange "$(onchange_action)"

View file

@ -1,4 +1,4 @@
no-auto
auto
hotplug
no-auto-down
no-scripts

View file

@ -0,0 +1 @@
leave

View file

@ -1,2 +1,5 @@
comment
name
rename
state
onchange