258 lines
6.1 KiB
Bash
Executable File
258 lines
6.1 KiB
Bash
Executable File
#!/bin/sh -e
|
|
#
|
|
# 2014 Steven Armstrong (steven-cdist at armstrong.cc)
|
|
# 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/>.
|
|
#
|
|
|
|
yesno() { test $? -eq 0 && echo yes || echo no; }
|
|
opt_format() {
|
|
printf '%s="%s"\n' "${1:?'option name missing'}" "${2:?'option value missing'}"
|
|
}
|
|
prefix2subnet() {
|
|
python3 -c 'for addr in __import__("sys").argv[1:]: print(__import__("ipaddress").ip_network(addr, strict=False).netmask)' "$@"
|
|
}
|
|
param2var() {
|
|
echo "${1:?'param name missing'}" | tr '[:lower:]-' '[:upper:]_'
|
|
}
|
|
ipversion() {
|
|
python3 -c 'for addr in __import__("sys").argv[1:]: print(__import__("ipaddress").ip_address(addr).version)' "$@"
|
|
}
|
|
|
|
os=$(cat "${__global:?}/explorer/os")
|
|
|
|
case $os
|
|
in
|
|
(centos|redhat|scientific)
|
|
NETWORK_SCRIPTS_DIR='/etc/sysconfig/network-scripts'
|
|
systype=redhat
|
|
;;
|
|
(suse)
|
|
NETWORK_SCRIPTS_DIR='/etc/sysconfig/network'
|
|
systype=suse
|
|
;;
|
|
(*)
|
|
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
|
|
|
|
if test -s "${__object:?}/parameter/name"
|
|
then
|
|
name=$(cat "${__object:?}/parameter/name")
|
|
else
|
|
name=${__object_id:?}
|
|
fi
|
|
state_should=$(cat "${__object:?}/parameter/state")
|
|
bootproto=$(cat "${__object:?}/parameter/bootproto")
|
|
onchange=$(cat "${__object:?}/parameter/onchange")
|
|
iftype=$(cat "${__object:?}/parameter/type")
|
|
|
|
ifcfg_file="${NETWORK_SCRIPTS_DIR:?}/ifcfg-${__object_id:?}"
|
|
|
|
|
|
onchange_action() {
|
|
case $onchange
|
|
in
|
|
(refresh)
|
|
printf "ifdown '%s' || true ; ifup '%s'\n" "${name}" "${name}"
|
|
;;
|
|
(up)
|
|
printf "ifup '%s'\n" "${name}"
|
|
;;
|
|
(down)
|
|
printf "ifdown '%s' || true\n" "${name}"
|
|
;;
|
|
(leave)
|
|
# ignore
|
|
;;
|
|
(*)
|
|
printf 'Invalid --onchange: %s\n' "${onchange}" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
|
|
{
|
|
cat <<-EOF
|
|
# Created by cdist ${__type##*/}
|
|
# Do not change. Changes will be overwritten.
|
|
#
|
|
|
|
EOF
|
|
|
|
if test -f "${__object:?}/parameter/comment"
|
|
then
|
|
awk '{ print "# " $0 }' <"${__object:?}/parameter/comment"
|
|
echo
|
|
fi
|
|
|
|
opt_format DEVICE "${name}"
|
|
opt_format NM_CONTROLLED no
|
|
|
|
case $systype
|
|
in
|
|
(redhat)
|
|
opt_format TYPE "${iftype}"
|
|
|
|
for _param in onboot hotplug
|
|
do
|
|
_key=$(param2var "${_param}")
|
|
opt_format "${_key}" "$(test -f "${__object}/parameter/${_param}" | yesno)"
|
|
done
|
|
unset _param _key
|
|
;;
|
|
(suse)
|
|
iftype=$(echo "${iftype}" | tr '[:upper:]' '[:lower:]')
|
|
if test "${iftype}" = 'loopback' -o "${iftype}" = 'dummy'
|
|
then
|
|
# Only use INTERFACETYPE for lo/dummy.
|
|
# cf. /usr/share/YaST2/modules/NetworkInterfaces.rb
|
|
opt_format INTERFACETYPE "${iftype}"
|
|
fi
|
|
|
|
_startmode=manual
|
|
|
|
if test -f "${__object:?}/parameter/onboot"
|
|
then
|
|
_startmode=auto
|
|
fi
|
|
if test -f "${__object:?}/parameter/hotplug"
|
|
then
|
|
_startmode=ifplugd
|
|
fi
|
|
|
|
opt_format STARTMODE "${_startmode}"
|
|
;;
|
|
esac
|
|
|
|
ignored_parameters=
|
|
|
|
# manually_handled_parameters are handled in their own code block, not by
|
|
# the generic accumulator
|
|
manually_handled_parameters='bootproto comment extra-config hotplug name onboot onchange state'
|
|
|
|
case $bootproto
|
|
in
|
|
(dhcp)
|
|
opt_format BOOTPROTO dhcp
|
|
ignored_parameters='address broadcast gateway'
|
|
;;
|
|
(static|none)
|
|
opt_format BOOTPROTO none
|
|
;;
|
|
(*)
|
|
printf 'Invalid --bootproto: %s\n' "${bootproto}" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
_bonding_opts=
|
|
|
|
for _param in "${__object:?}"/parameter/*
|
|
do
|
|
_key=$(param2var "${_param}")
|
|
|
|
echo "${ignored_parameters}" | grep -q -w "${_param}" && continue
|
|
echo "${manually_handled_parameters}" | grep -q -w "${_param}" && continue
|
|
|
|
case $_param
|
|
in
|
|
(bond-master)
|
|
opt_format SLAVE yes
|
|
opt_format MASTER "$(cat "${__object:?}/parameter/${_param}")"
|
|
;;
|
|
(bond-*)
|
|
_key=$(echo "${_param#bond-}" | tr '-' '_')
|
|
_value=$(cat "${__object:?}/parameter/${_param}")
|
|
|
|
if test "${_key}" = 'arp_ip_target'
|
|
then
|
|
_value=$(echo "${_value}" | awk -v RS= -v FS='\n' -v OFS=',' '$1=$1')
|
|
fi
|
|
|
|
_bonding_opts="${_bonding_opts} $(opt_format "${_key}" "${_value}")"
|
|
;;
|
|
(*)
|
|
case $_param
|
|
in
|
|
(address)
|
|
n=0
|
|
case $systype
|
|
in
|
|
(redhat)
|
|
# the fucked-up config
|
|
while read -r _addr
|
|
do
|
|
if test "$(ipversion "${_addr}")" -eq 6
|
|
then
|
|
if ! ${_ipv6_enabled:-false}
|
|
then
|
|
opt_format IPV6INIT yes
|
|
opt_format IPV6ADDR "${_addr}"
|
|
_ipv6_enabled=true
|
|
else
|
|
_ipv6_sec="${_ipv6_sec} ${_addr}"
|
|
fi
|
|
else
|
|
opt_format "IPADDR${n}" "${_addr%%/*}"
|
|
opt_format "NETMASK${n}" "$(prefix2subnet "${_addr##*/}")"
|
|
: $((n+=1))
|
|
fi
|
|
done
|
|
|
|
if test -n "${_ipv6_sec}"
|
|
then
|
|
opt_format IPV6ADDR_SECONDARIES "${_ipv6_sec# }"
|
|
fi
|
|
unset _ipv6_enabled _ipv6_sec
|
|
;;
|
|
(suse)
|
|
# the sane config
|
|
while read -r _addr
|
|
do
|
|
opt_format "IPADDR${n}" "${_addr}"
|
|
: $((n+=1))
|
|
done
|
|
;;
|
|
esac <"${__object:?}/parameter/address"
|
|
unset n _addr
|
|
;;
|
|
(*)
|
|
opt_format "${_key}" "$(cat "${__object:?}/parameter/${_param}")"
|
|
;;
|
|
esac
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if test -n "${_bonding_opts}"
|
|
then
|
|
# strip leading space
|
|
opt_format BONDING_OPTS "${_bonding_opts# }"
|
|
fi
|
|
|
|
if test -f "${__object:?}/parameter/extra-config"
|
|
then
|
|
cat "${__object:?}/parameter/extra-config"
|
|
fi
|
|
} | __file "${ifcfg_file}" \
|
|
--state "${state_should}" --owner root --group root --mode 0644 \
|
|
--onchange "$(onchange_action)" --source -
|