wireguard-inject/example-provisioner/provisioner.sh
2024-09-07 16:59:01 +01:00

70 lines
1.7 KiB
Bash
Executable File

#!/bin/bash -e
###########################################################################
PROVISIONER_NAME='provisioner'
CONFIG_PATH='/path/to/wireguard/config/files'
# find router PID
ROUTER_PID=$(incus info --project my-project router 2>/dev/null | \
grep '^PID:' | cut -d' ' -f2)
echo "* my-project/router PID: $ROUTER_PID"
###########################################################################
function list_config_files
{
find "$CONFIG_PATH" -name '*.conf' -type f -print
}
###########################################################################
function list_actual_interfaces
{
if [ -n "$ROUTER_PID" ]
then
nsenter -t "$ROUTER_PID" -n wg show interfaces
fi
}
###########################################################################
function add_tunnel
{
local iface="$1"
local config="$2"
if [ -n "$ROUTER_PID" ]
then
ip link add dev "$iface" type wireguard
wg setconf "$iface" "$config"
ip link set dev "$iface" netns "$ROUTER_PID"
nsenter -t "$ROUTER_PID" -n ip link set "$iface" up
else
echo >&2 'Unable to add tunnel, router PID not found'
fi
}
###########################################################################
function remove_tunnel
{
local iface="$1"
if [ -n "$ROUTER_PID" ]
then
nsenter -t "$ROUTER_PID" -n ip link del "$iface"
else
echo >&2 'Unable to delete tunnel, router PID not found'
fi
}
###########################################################################
function all_done
{
touch "${CONFIG_PATH}/inject.complete"
}
###########################################################################
# end of file