services-xbee_net/scripts/entrypoint.sh

161 lines
5.2 KiB
Bash

#!/bin/bash
# entrypoint.sh
#
# This script is responsible for setting up and maintaining an XBee network interface within a Docker container.
# It performs several key tasks, including checking for root privileges, setting up the XBee network interface,
# configuring network bridges, and monitoring both the XBee device and Wi-Fi interface for connectivity.
# The script runs in a loop to ensure continuous operation, retrying the setup process if any steps fail.
#
# Usage:
# - This script is intended to be run as the entrypoint for a Docker container.
# - Ensure that the script has executable permissions (`chmod +x entrypoint.sh`) before running it.
# Source the get_connected_wifi_info.sh script
source /scripts/get_connected_wifi_info.sh
# Check if the script is running with root privileges
check_root() {
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root. Exiting..."
exit 1
fi
}
# Clean up resources
# - Kill the background process used to create the xbnet interface
# - Clean up any resources created or copied over. This includes:
# - Bridge interface
# - xbnet interface
cleanup() {
echo "Cleaning up resources..."
pkill -f "xbnet -d --serial-speed $XBEE_BAUDRATE $XBEE_PORT tap"
if ip link show br0 > /dev/null 2>&1; then
ip link set br0 down
brctl delbr br0
fi
if ip link show $XBEE_NET_IFACE_NAME > /dev/null 2>&1; then
ip link set $XBEE_NET_IFACE_NAME down
ip link delete $XBEE_NET_IFACE_NAME
fi
}
# Function to check if the XBee device is connected
# - Ensures that the XBee device is present at the specified port before proceeding
check_xbee_device() {
if [ -e "$XBEE_PORT" ]; then
echo "XBee device found at $XBEE_PORT. Proceeding with setup..."
return 0
else
echo "Error: No XBee device found at $XBEE_PORT. Please connect the device."
return 1
fi
}
# Start the xbnet interface
# - Attempts to start the xbnet interface in tap mode
# - Verifies if the interface is successfully created and returns the appropriate status code
start_xbnet_interface() {
echo "Starting XBee network interface..."
xbnet -d --serial-speed $XBEE_BAUDRATE $XBEE_PORT tap &
# Wait until the xbnet interface is created
while [ ! -d "/sys/class/net/$XBEE_NET_IFACE_NAME" ]; do
echo "Waiting for interface $XBEE_NET_IFACE_NAME to be created..."
sleep 1
done
# Get the xbnet interface name (e.g., xbnet0) automatically
XBEE_NET_IFACE_NAME=$(ls /sys/class/net | grep 'xbnet')
# Check if the xbnet interface is found
if [ -z "$XBEE_NET_IFACE_NAME" ]; then
echo "Error: No XBee network interface found. Retrying setup..."
cleanup
return 1
else
echo "XBee network interface $XBEE_NET_IFACE_NAME created successfully."
return 0
fi
}
# Configure the network interface and bridge
# - Creates the network interface with the name specified by XBEE_NET_IFACE_NAME in .env file
# - Creates a bridge interface and attaches the xbnet interface to it
# - Brings up the bridge interface
configure_network_and_bridge() {
ip addr add $XBEE_NET_SRC_IP/24 dev $XBEE_NET_IFACE_NAME
ip link set dev $XBEE_NET_IFACE_NAME up
brctl addbr br0
brctl addif br0 $XBEE_NET_IFACE_NAME
ip link set dev br0 up
}
# Monitor and bridge Wi-Fi
# - Continuously checks for a connected Wi-Fi interface
# - Bridges the Wi-Fi interface with the xbnet interface when found
# - If no Wi-Fi interface is found, continues checking every 5 seconds
monitor_and_bridge_wifi() {
while true; do
HOST_WIFI_IFACE=$(get_connected_wifi_device)
if [ -n "$HOST_WIFI_IFACE" ]; then
echo "Wi-Fi interface found: $HOST_WIFI_IFACE. Bridging with $XBEE_NET_IFACE_NAME..."
brctl addif br0 $HOST_WIFI_IFACE
iptables -t nat -A POSTROUTING -o $HOST_WIFI_IFACE -j MASQUERADE
iptables -A FORWARD -i br0 -o $HOST_WIFI_IFACE -j ACCEPT
iptables -A FORWARD -i $HOST_WIFI_IFACE -o br0 -j ACCEPT
echo "Bridge configured. Monitoring for changes..."
break
else
echo "No Wi-Fi interface found. Rechecking in 5 seconds..."
sleep 5
fi
done
}
# Function to monitor the XBee device and restart setup if disconnected
# - Periodically checks if the XBee device is still connected
# - If the device is disconnected, triggers cleanup and restarts the setup process
monitor_xbee_device() {
while [ -e "$XBEE_PORT" ]; do
sleep 5
done
echo "XBee device at $XBEE_PORT was removed. Cleaning up..."
cleanup
}
# Main function to set up the XBee network
# - Runs the setup process in a loop to ensure continuous operation
# - If any step fails, it retries the entire process after a short delay
setup_xbee_network() {
while true; do
check_xbee_device
if [ $? -ne 0 ]; then
sleep 5
continue
fi
start_xbnet_interface
if [ $? -ne 0 ]; then
cleanup
sleep 5
continue
fi
configure_network_and_bridge
monitor_and_bridge_wifi
monitor_xbee_device
done
}
# Run the setup only if the script is executed as root
check_root
setup_xbee_network