117 lines
2.9 KiB
Bash
Executable file
117 lines
2.9 KiB
Bash
Executable file
#!/bin/sh -e
|
|
# -*- mode: sh; indent-tabs-mode: t -*-
|
|
#
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
#
|
|
|
|
opt_format() {
|
|
printf '%s=%s\n' "${1:?'option name missing'}" "${2:?'option value missing'}"
|
|
}
|
|
|
|
opt_from_param() {
|
|
# Only first line will be read.
|
|
opt_format \
|
|
"${1:?'option name missing'}" \
|
|
"$(head -n 1 "${__object:?}/parameter/${2:?'parameter name missing'}")"
|
|
}
|
|
|
|
|
|
set -- --state "$(cat "${__object:?}/parameter/state")"
|
|
|
|
# Boolean flags
|
|
test -f "${__object:?}/parameter/onboot" || {
|
|
echo "WARNING: ${__type##*/}: The systemd backend only supports --onboot." >&2
|
|
}
|
|
test -f "${__object:?}/parameter/hotplug" || {
|
|
echo "WARNING: ${__type##*/}: The systemd backend only supports --hotplug." >&2
|
|
}
|
|
|
|
|
|
if test -f "${__object:?}/parameter/name"
|
|
then
|
|
set -- "$@" --name "$(cat "${__object:?}/parameter/name")"
|
|
fi
|
|
|
|
|
|
# Generate appropriate parameters based on bootproto
|
|
bootproto=$(cat "${__object:?}/parameter/bootproto")
|
|
case $bootproto
|
|
in
|
|
(dhcp*)
|
|
if test "${bootproto}" = dhcp
|
|
then
|
|
set -- "$@" --option "$(opt_format Network.DHCP yes)"
|
|
# TODO: Implement IPv6 support
|
|
# else
|
|
# set -- "$@" --option "$(opt_format Network.DHCP "${bootproto}")"
|
|
fi
|
|
;;
|
|
(static)
|
|
set -- "$@" --option 'Network.DHCP=false'
|
|
|
|
if test -s "${__object:?}/parameter/address"
|
|
then
|
|
while read -r _value
|
|
do
|
|
set -- "$@" --option "$(opt_format Network.Address "${_value}")"
|
|
done <"${__object:?}/parameter/address"
|
|
unset _value
|
|
fi
|
|
|
|
if test -s "${__object:?}/parameter/gateway"
|
|
then
|
|
set -- "$@" --option "$(opt_from_param Network.Gateway gateway)"
|
|
fi
|
|
;;
|
|
(manual)
|
|
set -- "$@" --option 'Network.DHCP=false'
|
|
;;
|
|
(*)
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
if test -s "${__object:?}/parameter/comment"
|
|
then
|
|
set -- "$@" --option "$(opt_from_param Network.Description comment)"
|
|
fi
|
|
|
|
case $(cat "${__object:?}/parameter/onchange")
|
|
in
|
|
(leave)
|
|
;;
|
|
(refresh)
|
|
set -- "$@" --auto-reload
|
|
;;
|
|
(*)
|
|
echo 'The --onchange parameter for type systemd only accepts "leave" or "refresh".' >&2
|
|
echo 'systemd-networkd automatically reconfigures all interfaces.' >&2
|
|
exit 1
|
|
esac
|
|
|
|
if test -s "${__object:?}/parameter/extra-config"
|
|
then
|
|
while read -r _opt
|
|
do
|
|
set -- "$@" --option "${_opt}"
|
|
done <"${__object:?}/parameter/extra-config"
|
|
unset _opt
|
|
fi
|
|
|
|
# Instantiate backend type
|
|
__systemd_networkd_network "${__object_id:?}" "$@"
|