Merge pull request #69 from BarbarossaTM/feature/create-destroy

WIP: Update executors to implement create/destroy phase
This commit is contained in:
Ariadne Conill 2020-09-09 16:53:20 -06:00 committed by GitHub
commit 04a65c5c38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 75 additions and 47 deletions

View file

@ -4,6 +4,7 @@ set -e
# Copyright (C) 2012, 2020 Natanael Copa <ncopa@alpinelinux.org> # Copyright (C) 2012, 2020 Natanael Copa <ncopa@alpinelinux.org>
# Copyright (C) 2020 Ariadne Conill <ariadne@dereferenced.org> # Copyright (C) 2020 Ariadne Conill <ariadne@dereferenced.org>
# Copyright (C) 2020 Maximilian Wilhelm <max@sdn.clinic>
# #
# Permission to use, copy, modify, and/or distribute this software for any # Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above # purpose with or without fee is hereby granted, provided that the above
@ -134,8 +135,15 @@ esac
[ -z "$PORTS" ] && ! env | grep -q "^IF_BRIDGE" && exit [ -z "$PORTS" ] && ! env | grep -q "^IF_BRIDGE" && exit
case "$PHASE" in case "$PHASE" in
depend)
echo "$PORTS"
;;
create)
if [ ! -d "/sys/class/net/${IFACE}" ]; then
brctl addbr "${IFACE}"
fi
;;
pre-up) pre-up)
brctl addbr $IFACE || exit 1
wait_ports wait_ports
set_bridge_opts set_bridge_opts
add_ports add_ports
@ -144,6 +152,10 @@ pre-up)
post-down) post-down)
del_ports del_ports
ip link set dev $IFACE down ip link set dev $IFACE down
brctl delbr $IFACE || exit 1 ;;
destroy)
if [ -d "/sys/class/net/${IFACE}" ]; then
brctl delbr "${IFACE}"
fi
;; ;;
esac esac

View file

@ -19,10 +19,10 @@ PARAMS="mode $IF_GRE_MODE local '$IF_GRE_LOCAL' remote '$IF_GRE_REMOTE'"
[ -n "$PARAMS" ] || exit 0 [ -n "$PARAMS" ] || exit 0
case "$PHASE" in case "$PHASE" in
pre-up) create)
${MOCK} eval ip $FAMILY $COMMAND add $IFACE $PARAMS ${MOCK} eval ip $FAMILY $COMMAND add $IFACE $PARAMS
;; ;;
post-down) destroy)
${MOCK} ip $FAMILY $COMMAND del $IFACE ${MOCK} ip $FAMILY $COMMAND del $IFACE
;; ;;
depend) depend)

View file

@ -28,7 +28,7 @@ is_vlan() {
} }
case "$PHASE" in case "$PHASE" in
pre-up) create)
if [ "${IF_LINK_TYPE}" = "dummy" ]; then if [ "${IF_LINK_TYPE}" = "dummy" ]; then
if [ -d "/sys/class/net/${IFACE}" ]; then if [ -d "/sys/class/net/${IFACE}" ]; then
iface_type=$(ip -d link show dev "${IFACE}" | head -n3 | tail -n1 | awk '{ print $1 }') iface_type=$(ip -d link show dev "${IFACE}" | head -n3 | tail -n1 | awk '{ print $1 }')
@ -76,7 +76,7 @@ up)
down) down)
${MOCK} ip link set down dev "${IFACE}" ${MOCK} ip link set down dev "${IFACE}"
;; ;;
post-down) destroy)
if [ "${IF_LINK_TYPE}" = "dummy" ] || is_vlan; then if [ "${IF_LINK_TYPE}" = "dummy" ] || is_vlan; then
if [ -z "${MOCK}" -a ! -d "/sys/class/net/${IFACE}" ]; then if [ -z "${MOCK}" -a ! -d "/sys/class/net/${IFACE}" ]; then
exit 0 exit 0

View file

@ -1,8 +1,16 @@
#!/bin/sh #!/bin/sh
set -e set -e
[ -z "$IF_PPP_PROVIDER" ] && exit 0 [ -z "$IF_PPP_PROVIDER" ] && exit 0
case "$PHASE" in case "$PHASE" in
pre-up) ${MOCK} pon $IF_PPP_PROVIDER ;; create)
post-down) ${MOCK} poff $IF_PPP_PROVIDER ;; ${MOCK} pon $IF_PPP_PROVIDER
depend) echo "$IF_PPP_PHYSDEV" ;; ;;
destroy)
${MOCK} poff $IF_PPP_PROVIDER
;;
depend)
echo "$IF_PPP_PHYSDEV"
;;
esac esac

View file

@ -37,10 +37,10 @@ PARAMS=$(set | sed -E '
[ "$PARAMS" ] || exit 0 [ "$PARAMS" ] || exit 0
case "$PHASE" in case "$PHASE" in
pre-up) create)
${MOCK} eval ip $FAMILY $COMMAND add $IFACE $PARAMS ${MOCK} eval ip $FAMILY $COMMAND add $IFACE $PARAMS
;; ;;
post-down) destroy)
${MOCK} ip $FAMILY $COMMAND del $IFACE ${MOCK} ip $FAMILY $COMMAND del $IFACE
;; ;;
depend) depend)

View file

@ -14,14 +14,14 @@ handle_member() {
[ -n "$VERBOSE" ] && set -x [ -n "$VERBOSE" ] && set -x
case "$PHASE" in case "$PHASE" in
pre-up) create)
[ -n "$IF_VRF_TABLE" ] && handle_init "add" [ -n "$IF_VRF_TABLE" ] && handle_init "add"
[ -n "$IF_VRF_MEMBER" ] && handle_member
exit 0
;; ;;
post-down) pre-up)
[ -n "$IF_VRF_MEMBER" ] && handle_member
;;
destroy)
[ -n "$IF_VRF_TABLE" ] && handle_init "del" [ -n "$IF_VRF_TABLE" ] && handle_init "del"
exit 0
;; ;;
depend) depend)
echo "$IF_VRF_MEMBER" echo "$IF_VRF_MEMBER"

View file

@ -6,11 +6,13 @@ set -e
[ -z "$IF_WIREGUARD_CONFIG_PATH" ] && IF_WIREGUARD_CONFIG_PATH="/etc/wireguard/$IFACE.conf" [ -z "$IF_WIREGUARD_CONFIG_PATH" ] && IF_WIREGUARD_CONFIG_PATH="/etc/wireguard/$IFACE.conf"
case "$PHASE" in case "$PHASE" in
pre-up) create)
${MOCK} ip link add $IFACE type wireguard ${MOCK} ip link add $IFACE type wireguard
;;
pre-up)
${MOCK} wg setconf $IFACE $IF_WIREGUARD_CONFIG_PATH ${MOCK} wg setconf $IFACE $IF_WIREGUARD_CONFIG_PATH
;; ;;
post-down) destroy)
${MOCK} ip link delete dev $IFACE ${MOCK} ip link delete dev $IFACE
;; ;;
esac esac

View file

@ -9,7 +9,7 @@ tests_init \
flags_bringup flags_bringup
basic_bringup_body() { basic_bringup_body() {
export MOCK=echo IFACE=tun0 PHASE=pre-up \ export MOCK=echo IFACE=tun0 PHASE=create \
IF_GRE_LOCAL=1.2.3.4 IF_GRE_REMOTE=5.6.7.8 \ IF_GRE_LOCAL=1.2.3.4 IF_GRE_REMOTE=5.6.7.8 \
IF_GRE_TTL=255 IF_GRE_TTL=255
atf_check -s exit:0 \ atf_check -s exit:0 \
@ -22,7 +22,7 @@ basic_bringup_body() {
} }
basic_teardown_body() { basic_teardown_body() {
export MOCK=echo IFACE=tun0 PHASE=post-down \ export MOCK=echo IFACE=tun0 PHASE=destroy \
IF_GRE_LOCAL=1.2.3.4 IF_GRE_REMOTE=5.6.7.8 \ IF_GRE_LOCAL=1.2.3.4 IF_GRE_REMOTE=5.6.7.8 \
IF_GRE_TTL=255 IF_GRE_TTL=255
atf_check -s exit:0 \ atf_check -s exit:0 \
@ -31,7 +31,7 @@ basic_teardown_body() {
} }
flags_bringup_body() { flags_bringup_body() {
export MOCK=echo IFACE=tun0 PHASE=pre-up \ export MOCK=echo IFACE=tun0 PHASE=create \
IF_GRE_LOCAL=1.2.3.4 IF_GRE_REMOTE=5.6.7.8 \ IF_GRE_LOCAL=1.2.3.4 IF_GRE_REMOTE=5.6.7.8 \
IF_GRE_TTL=255 IF_GRE_FLAGS="nopmtudisc ignore-df" IF_GRE_TTL=255 IF_GRE_FLAGS="nopmtudisc ignore-df"
atf_check -s exit:0 \ atf_check -s exit:0 \

View file

@ -8,13 +8,13 @@ tests_init \
down \ down \
mtu \ mtu \
hwaddress \ hwaddress \
vlan_explicit_pre_up \ vlan_explicit_create \
vlan_explicit_post_down \ vlan_explicit_destroy \
vlan_guessed_pre_up \ vlan_guessed_create \
vlan_guessed_post_down \ vlan_guessed_destroy \
vlan_explicit_depend \ vlan_explicit_depend \
vlan_guessed_depend \ vlan_guessed_depend \
dummy_pre_up dummy_create
up_body() { up_body() {
export IFACE=lo PHASE=up MOCK=echo export IFACE=lo PHASE=up MOCK=echo
@ -40,28 +40,28 @@ hwaddress_body() {
${EXECUTOR} ${EXECUTOR}
} }
vlan_explicit_pre_up_body() { vlan_explicit_create_body() {
export IFACE=servers PHASE=pre-up MOCK=echo \ export IFACE=servers PHASE=create MOCK=echo \
IF_VLAN_RAW_DEVICE="eth0" IF_VLAN_ID="123" IF_VLAN_RAW_DEVICE="eth0" IF_VLAN_ID="123"
atf_check -s exit:0 -o match:'ip link add link eth0 name servers type vlan id 123' \ atf_check -s exit:0 -o match:'ip link add link eth0 name servers type vlan id 123' \
${EXECUTOR} ${EXECUTOR}
} }
vlan_explicit_post_down_body() { vlan_explicit_destroy_body() {
export IFACE=servers PHASE=post-down MOCK=echo \ export IFACE=servers PHASE=destroy MOCK=echo \
IF_VLAN_RAW_DEVICE="eth0" IF_VLAN_ID="123" IF_VLAN_RAW_DEVICE="eth0" IF_VLAN_ID="123"
atf_check -s exit:0 -o match:'ip link del servers' \ atf_check -s exit:0 -o match:'ip link del servers' \
${EXECUTOR} ${EXECUTOR}
} }
vlan_guessed_pre_up_body() { vlan_guessed_create_body() {
export IFACE=eth0.8 PHASE=pre-up MOCK=echo export IFACE=eth0.8 PHASE=create MOCK=echo
atf_check -s exit:0 -o match:'ip link add link eth0 name eth0.8 type vlan id 8' \ atf_check -s exit:0 -o match:'ip link add link eth0 name eth0.8 type vlan id 8' \
${EXECUTOR} ${EXECUTOR}
} }
vlan_guessed_post_down_body() { vlan_guessed_destroy_body() {
export IFACE=eth0.8 PHASE=post-down MOCK=echo export IFACE=eth0.8 PHASE=destroy MOCK=echo
atf_check -s exit:0 -o match:'ip link del eth0.8' \ atf_check -s exit:0 -o match:'ip link del eth0.8' \
${EXECUTOR} ${EXECUTOR}
} }
@ -79,8 +79,8 @@ vlan_guessed_depend_body() {
${EXECUTOR} ${EXECUTOR}
} }
dummy_pre_up_body() { dummy_create_body() {
export IFACE=yolo IF_LINK_TYPE=dummy PHASE=pre-up MOCK=echo export IFACE=yolo IF_LINK_TYPE=dummy PHASE=create MOCK=echo
atf_check -s exit:0 -o match:'ip link add yolo type dummy' \ atf_check -s exit:0 -o match:'ip link add yolo type dummy' \
${EXECUTOR} ${EXECUTOR}
} }

View file

@ -8,14 +8,14 @@ tests_init \
teardown \ teardown \
bringup_body() { bringup_body() {
export MOCK=echo IFACE=ppp0 PHASE=pre-up IF_PPP_PROVIDER=someisp export MOCK=echo IFACE=ppp0 PHASE=create IF_PPP_PROVIDER=someisp
atf_check -s exit:0 \ atf_check -s exit:0 \
-o match:'pon someisp' \ -o match:'pon someisp' \
${EXECUTOR} ${EXECUTOR}
} }
teardown_body() { teardown_body() {
export MOCK=echo IFACE=ppp0 PHASE=post-down IF_PPP_PROVIDER=someisp export MOCK=echo IFACE=ppp0 PHASE=destroy IF_PPP_PROVIDER=someisp
atf_check -s exit:0 \ atf_check -s exit:0 \
-o match:'poff someisp' \ -o match:'poff someisp' \
${EXECUTOR} ${EXECUTOR}

View file

@ -8,7 +8,7 @@ tests_init \
tunnel_teardown tunnel_teardown
tunnel_bringup_body() { tunnel_bringup_body() {
export MOCK=echo IFACE=tun0 PHASE=pre-up IF_TUNNEL_MODE=gre \ export MOCK=echo IFACE=tun0 PHASE=create IF_TUNNEL_MODE=gre \
IF_TUNNEL_LOCAL=1.2.3.4 IF_TUNNEL_REMOTE=5.6.7.8 \ IF_TUNNEL_LOCAL=1.2.3.4 IF_TUNNEL_REMOTE=5.6.7.8 \
IF_TUNNEL_TTL=255 IF_TUNNEL_TTL=255
atf_check -s exit:0 \ atf_check -s exit:0 \
@ -21,7 +21,7 @@ tunnel_bringup_body() {
} }
tunnel_teardown_body() { tunnel_teardown_body() {
export MOCK=echo IFACE=tun0 PHASE=post-down IF_TUNNEL_MODE=gre \ export MOCK=echo IFACE=tun0 PHASE=destroy IF_TUNNEL_MODE=gre \
IF_TUNNEL_LOCAL=1.2.3.4 IF_TUNNEL_REMOTE=5.6.7.8 \ IF_TUNNEL_LOCAL=1.2.3.4 IF_TUNNEL_REMOTE=5.6.7.8 \
IF_TUNNEL_TTL=255 IF_TUNNEL_TTL=255
atf_check -s exit:0 \ atf_check -s exit:0 \

View file

@ -9,7 +9,7 @@ tests_init \
member_bringup member_bringup
leader_bringup_body() { leader_bringup_body() {
export MOCK=echo IFACE=vrf-red PHASE=pre-up IF_VRF_TABLE=1 IF_VRF_MEMBER= export MOCK=echo IFACE=vrf-red PHASE=create IF_VRF_TABLE=1 IF_VRF_MEMBER=
atf_check -s exit:0 \ atf_check -s exit:0 \
-o match:'ip link add vrf-red type vrf table 1' \ -o match:'ip link add vrf-red type vrf table 1' \
-o match:'ip rule add iif vrf-red table 1' \ -o match:'ip rule add iif vrf-red table 1' \
@ -18,7 +18,7 @@ leader_bringup_body() {
} }
leader_teardown_body() { leader_teardown_body() {
export MOCK=echo IFACE=vrf-red PHASE=post-down IF_VRF_TABLE=1 IF_VRF_MEMBER= export MOCK=echo IFACE=vrf-red PHASE=destroy IF_VRF_TABLE=1 IF_VRF_MEMBER=
atf_check -s exit:0 \ atf_check -s exit:0 \
-o match:'ip link del vrf-red type vrf table 1' \ -o match:'ip link del vrf-red type vrf table 1' \
-o match:'ip rule del iif vrf-red table 1' \ -o match:'ip rule del iif vrf-red table 1' \

View file

@ -4,14 +4,21 @@
EXECUTOR="$(atf_get_srcdir)/../../executor-scripts/linux/wireguard" EXECUTOR="$(atf_get_srcdir)/../../executor-scripts/linux/wireguard"
tests_init \ tests_init \
create \
pre_up \ pre_up \
pre_up_specified_config \ pre_up_specified_config \
post_down destroy
create_body() {
export IFACE=wg0 PHASE=create MOCK=echo
atf_check -s exit:0 \
-o match:'ip link add wg0 type wireguard' \
${EXECUTOR}
}
pre_up_body() { pre_up_body() {
export IFACE=wg0 PHASE=pre-up MOCK=echo export IFACE=wg0 PHASE=pre-up MOCK=echo
atf_check -s exit:0 \ atf_check -s exit:0 \
-o match:'ip link add wg0 type wireguard' \
-o match:'wg setconf wg0 /etc/wireguard/wg0.conf' \ -o match:'wg setconf wg0 /etc/wireguard/wg0.conf' \
${EXECUTOR} ${EXECUTOR}
} }
@ -20,13 +27,12 @@ pre_up_specified_config_body() {
export IFACE=wg0 PHASE=pre-up MOCK=echo \ export IFACE=wg0 PHASE=pre-up MOCK=echo \
IF_WIREGUARD_CONFIG_PATH=/etc/wireguard/vpn.conf IF_WIREGUARD_CONFIG_PATH=/etc/wireguard/vpn.conf
atf_check -s exit:0 \ atf_check -s exit:0 \
-o match:'ip link add wg0 type wireguard' \
-o match:'wg setconf wg0 /etc/wireguard/vpn.conf' \ -o match:'wg setconf wg0 /etc/wireguard/vpn.conf' \
${EXECUTOR} ${EXECUTOR}
} }
post_down_body() { destroy_body() {
export IFACE=wg0 PHASE=post-down MOCK=echo export IFACE=wg0 PHASE=destroy MOCK=echo
atf_check -s exit:0 \ atf_check -s exit:0 \
-o match:'ip link delete dev wg0' \ -o match:'ip link delete dev wg0' \
${EXECUTOR} ${EXECUTOR}