cdist/cdist/conf/type/__interface_ifupdown.d/manifest

185 lines
5.0 KiB
Bash
Executable File

#!/bin/sh -e
#
# 2020-2021 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/>.
#
INTERFACES_FILE=/etc/network/interfaces
INTERFACES_D_PATH=/etc/network/interfaces.d
os=$(cat "${__global}/explorer/os")
if test -s "${__object}/parameter/name"
then
name=$(cat "${__object}/parameter/name")
else
name=$__object_id
fi
onchange_action=$(cat "${__object}/parameter/onchange")
state=$(cat "${__object}/parameter/state")
# NOTE: No file ending is used not to break the source-directory stanza
# (if used and at least for normal interface names)
iface_file="${INTERFACES_D_PATH}/${__object_id}"
onchange_action() {
case $onchange_action
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 --onchange: %s\n' "${onchange_action}" >&2
exit 1
;;
esac
}
case $os
in
(debian)
os_major=$(grep -o '^[0-9][0-9]*' "${__global}/explorer/os_version")
if test "${os_major}" -lt 7
then
# Old versions do not support "source"
printf 'Debian versions older than 7 (wheezy) are not supported by this type (%s)\n' "${__type##*/}" >&2
exit 1
fi
if test "${state}" = 'present'
then
__package ifupdown --state present
export require=__package/ifupdown
fi
;;
(devuan)
if test "${state}" = 'present'
then
__package ifupdown --state present
export require=__package/ifupdown
fi
;;
(*)
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
case $state
in
(present)
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"
export require="__file${INTERFACES_FILE}"
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}/*"
export require="__line${INTERFACES_FILE}:source_interfaces.d"
fi
__directory "${INTERFACES_D_PATH}" --state pre-exists \
--owner root --mode 0755
export require="__directory/${INTERFACES_D_PATH}"
# Construct interface config file
(
cat <<-EOF
# Managed by cdist (${__type##*/})
# Do not change. Changes will be overwritten.
EOF
if test -s "${__object}/parameter/comment"
then
# Prefix lines with # and append trailing empty line.
# shellcheck disable=SC2016
{
if test "$(cat "${__object}/parameter/comment")" = '-'
then
cat "${__object}/stdin"
else
cat "${__object}/parameter/comment"
fi
} | sed -e 's/^/# /;$G'
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/auto"
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"
unset _opt
) | __file "${iface_file}" --state present --owner root --mode 0644 \
--onchange "$(onchange_action)" --source -
;;
(absent)
__file "${iface_file}" --state absent \
--onchange "$(onchange_action)"
;;
(*)
printf 'Invalid --state: %s\n' "${state}" >&2
exit 1
;;
esac