[type/__interface] Partial implementation of type for ifupdown.d

This commit is contained in:
Dennis Camera 2020-06-23 12:59:29 +02:00
parent e3e78ae373
commit 6ef242004c
7 changed files with 157 additions and 40 deletions

View file

@ -0,0 +1,52 @@
#!/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/>.
#
set -- --state "${__object}/parameter/state"
if test -f "${__object}/parameter/onboot"
then
set -- "$@" --auto
fi
if test -f "${__object}/parameter/hotplug"
then
set -- "$@" --hotplug
fi
# Generate appropriate parameters for backend type
bootproto=$(cat "${__object}/parameter/bootproto")
case $bootproto
in
(dhcp)
# TODO
;;
(static)
# TODO
;;
(manual)
# TODO
;;
(*)
exit 1
;;
esac
# Instantiate backend type
__interface_ifupdown.d "@"

View file

@ -10,6 +10,8 @@ DESCRIPTION
-----------
This cdist type allows you to manage network interfaces on the target.
It dispatches the actual work to the interface management system specific types.
Only TCP/IP is supported. For other protocol stacks, use the specific types for
your OS.
REQUIRED PARAMETERS
@ -19,12 +21,54 @@ None.
OPTIONAL PARAMETERS
-------------------
None.
state
'present', 'absent', 'exists' or 'pre-exists', defaults to 'present' where:
present
the file is exactly the one from source
absent
the file does not exist
exists
the file from source but only if it doesn't already exist
pre-exists
check that the file exists and is a regular file, but do not
create or modify it
name
The name of the physical or logical network device to configure.
Defaults to __object_id.
bootproto
The boot protocol to use.
Acceptable values are 'dhcp', 'static', 'manual'.
Defaults to 'dhcp'.
address
The IP address to assign to the network interface.
Can be repeated to assign multiple IP addresses.
gateway
The default gateway to assign to this interface (optional).
netmask
The netmask.
comment
A comment to be stored in the configuration file.
type
The backend to use to store the interface configuration.
Default is to auto detect.
extra-config
Other options to be passed to the implementation type verbatim.
Using this option makes the configuration non-portable to other backends.
If this option is used extensively, it is recommended to use the respective
backend type directly.
BOOLEAN PARAMETERS
------------------
None.
onboot
Whether to bring the interface up on boot
hotplug
Allow/disallow hotplug support for this interface
nodns
Do not configure nameservers in /etc/resolv.conf.
noroute
Do not set default route.
EXAMPLES

View file

@ -1,4 +1,5 @@
#!/bin/sh -e
# -*- mode: sh; indent-tabs-mode: t -*-
#
# 2020 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
#
@ -18,51 +19,67 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
die() { echo "$*" >&2; exit 1; }
dief() { die "$(printf "$@")"; }
invalid_param() { die "$@"; }
invalid_paramf() { dief "$@"; }
os=$(cat "${__global}/explorer/os")
# Parameters
state=$(cat "${__object}/parameter/state")
comment=$(cat "${__object}/parameter/comment")
bootproto=$(cat "${__object}/parameter/bootproto")
case $bootproto
in
(dhcp)
# Check parameters
! test -f "${__object}/parameter/address" \
|| invalid_param '--address is invalid for --bootproto dhcp'
! test -f "${__object}/parameter/gateway" \
|| invalid_param '--gateway is invalid for --bootproto dhcp'
! test -f "${__object}/parameter/netmask" \
|| invalid_param '--netmask is invalid for --bootproto dhcp'
;;
(static)
# TODO
;;
(manual)
# TODO
;;
(*)
invalid_paramf 'Unknown --bootproto: %s\n' "${bootproto}"
;;
esac
if test -f "${__object}/parameter/type"
then
type=$(cat "${__object}/parameter/type")
impl_type=$(cat "${__object}/parameter/type")
else
# Guess the type based on the operating system
case $os
in
debian)
(debian)
os_major=$(grep -o '^[0-9][0-9]*' "${__global}/explorer/os_version")
if test "${os_major}" -ge 7
then
type=ifupdown.d
else
echo 'Debian versions older than 7 (wheezy) are not (currently) supported' >&2
exit 1
fi
test "${os_major}" -ge 7 \
|| die 'Debian versions older than 7 (wheezy) are not supported'
impl_type=ifupdown.d
;;
devuan)
type=ifupdown.d
(devuan)
impl_type=ifupdown.d
;;
*)
echo "Don't know how to manage interfaces on: ${os}" >&2
exit 1
(*)
die "Don't know how to manage interfaces on: ${os}"
;;
esac
fi
state=$(cat "${__object}/parameter/state")
# Hand over to backend-specific implementation
manifest_file="${__type}/files/backends/${impl_type}/manifest"
test -x "${manifest_file}" || dief 'Unknown type: %s\n' "${impl_type}"
set -- "$@" "${__object_id}" --state "${state}"
cd "${__object}/parameter"
for param in *
do
if [ "${param}" != 'type' ] && [ "${param}" != 'state' ]
then
set -- "$@" --"${param}"
if ! grep -q "^${param}$" "${__type}/parameter/boolean"
then
set -- "$@" "$(cat "${param}")"
fi
fi
done
__interface_$type "$@"
# Run backend-specific script
"${manifest_file}" "$@"

View file

@ -1,4 +1,4 @@
hotplug
no-auto
no-auto-down
no-scripts
nodns
noroute
onboot

View file

@ -0,0 +1 @@
dhcp

View file

@ -1,5 +1,7 @@
family
method
bootproto
comment
gateway
name
netmask
state
type

View file

@ -1 +1,2 @@
option
address
extra-config