Xbnet working in good state. Removed supervisor

This commit is contained in:
Emran Billah 2024-08-28 14:19:36 -03:00
parent fab1d2a805
commit 9005578876
4 changed files with 39 additions and 221 deletions

View File

@ -8,7 +8,6 @@ RUN apt-get update && apt-get install -y \
net-tools \ net-tools \
bridge-utils \ bridge-utils \
iptables \ iptables \
supervisor \
traceroute \ traceroute \
nmap \ nmap \
tcpdump \ tcpdump \
@ -29,15 +28,9 @@ RUN cp target/release/xbnet /usr/local/bin/xbnet
COPY ./scripts/entrypoint.sh /entrypoint.sh COPY ./scripts/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh RUN chmod +x /entrypoint.sh
# Copy the supervisor config file
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# Copy the health check script # Copy the health check script
COPY ./scripts/health_check.sh /health_check.sh COPY ./scripts/health_check.sh /health_check.sh
RUN chmod +x /health_check.sh RUN chmod +x /health_check.sh
# # Start supervisord as the main command
# CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# Add healthcheck # Add healthcheck
HEALTHCHECK CMD /health_check.sh || exit 1 HEALTHCHECK CMD /health_check.sh || exit 1

View File

@ -68,7 +68,8 @@ services:
container_name: xbnet_node container_name: xbnet_node
privileged: true privileged: true
env_file: .env env_file: .env
command: /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf command: bash /entrypoint.sh
# command: /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
# command: tail -f /dev/null # command: tail -f /dev/null
# networks: # networks:
# xbee_net: # xbee_net:

View File

@ -1,179 +1,3 @@
# #!/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
# # # Add the main network interface (e.g., eth0) to the bridge
# # if ip link show eth0 > /dev/null 2>&1; then
# # brctl addif br0 eth0
# # else
# # echo "Warning: eth0 not found. Skipping adding eth0 to bridge."
# # fi
# 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 -s ${DEFAULT_SUBNET}.0/24 -o $HOST_WIFI_IFACE -j MASQUERADE
# # iptables -A FORWARD -i $HOST_WIFI_IFACE -o docker0 -j ACCEPT
# # iptables -A FORWARD -i docker0 -o $HOST_WIFI_IFACE -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
#############################################
#!/bin/bash #!/bin/bash
# entrypoint.sh # entrypoint.sh
@ -207,8 +31,12 @@ loop() {
create_tap_interface create_tap_interface
fi fi
sleep 0.5
# Log messages sent and received over xbnet # Log messages sent and received over xbnet
# log_xbnet_messages log_xbnet_messages
sleep 0.5
done done
} }
@ -234,7 +62,6 @@ check_network_state() {
# Function to create a TAP network interface # Function to create a TAP network interface
create_tap_interface() { create_tap_interface() {
# Start xbnet and run it in the background # Start xbnet and run it in the background
echo "Triggering xbnet cmd"
xbnet -d --serial-speed ${XBEE_BAUDRATE} ${XBEE_PORT} tap --iface-name ${XBEE_NET_IFACE_NAME} & xbnet -d --serial-speed ${XBEE_BAUDRATE} ${XBEE_PORT} tap --iface-name ${XBEE_NET_IFACE_NAME} &
# Wait until the interface is created # Wait until the interface is created
@ -242,21 +69,49 @@ create_tap_interface() {
sleep 1 sleep 1
done done
echo "Triggering ip addr add cmd" # Create and bring up xbnet interface
ip addr add $XBEE_NET_SRC_IP/24 dev $XBEE_NET_IFACE_NAME ip addr add $XBEE_NET_SRC_IP/24 dev $XBEE_NET_IFACE_NAME
echo "Triggering ip link set cmd"
ip link set $XBEE_NET_IFACE_NAME up ip link set $XBEE_NET_IFACE_NAME up
echo "Tap interface created." echo "-------------------------------------------"
echo "********** Tap interface created **********"
echo "-------------------------------------------"
echo "Name: ${XBEE_NET_IFACE_NAME}" echo "Name: ${XBEE_NET_IFACE_NAME}"
echo "Gateway: ${DEFAULT_GATEWAY}" echo "Gateway: ${DEFAULT_GATEWAY}"
echo "IP: ${XBEE_NET_SRC_IP}" echo "IP: ${XBEE_NET_SRC_IP}"
echo "Device port: ${XBEE_PORT}" echo "Device port: ${XBEE_PORT}"
echo "Device baudrate: ${XBEE_BAUDRATE}" echo "Device baudrate: ${XBEE_BAUDRATE}"
echo "-------------------------------------------"
return 0 return 0
} }
# Function to log messages sent and received over xbnet using netstat
log_xbnet_messages() {
echo "Monitoring messages on $XBEE_NET_IFACE_NAME..."
# Continuously monitor the interface for packet statistics
while true; do
RX_PACKETS_BEFORE=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/rx_packets)
TX_PACKETS_BEFORE=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/tx_packets)
sleep 1
RX_PACKETS_AFTER=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/rx_packets)
TX_PACKETS_AFTER=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/tx_packets)
RX_DIFF=$((RX_PACKETS_AFTER - RX_PACKETS_BEFORE))
TX_DIFF=$((TX_PACKETS_AFTER - TX_PACKETS_BEFORE))
if [[ $RX_DIFF -gt 0 ]]; then
echo "$(date +'%Y-%m-%d %H:%M:%S') - $RX_DIFF packets received on $XBEE_NET_IFACE_NAME"
fi
if [[ $TX_DIFF -gt 0 ]]; then
echo "$(date +'%Y-%m-%d %H:%M:%S') - $TX_DIFF packets sent on $XBEE_NET_IFACE_NAME"
fi
done
}
# Function to clean up resources and exit the script # Function to clean up resources and exit the script
cleanup() { cleanup() {
echo "Cleaning up resources..." echo "Cleaning up resources..."

View File

@ -1,31 +0,0 @@
[supervisord]
nodaemon=true
[program:xbnet]
command=/bin/bash /entrypoint.sh
autostart=true
autorestart=true
startretries=3
stderr_logfile=/var/log/supervisor/xbnet.err.log
stdout_logfile=/var/log/supervisor/xbnet.out.log
user=root
environment=HOME="/root",USER="root",PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Cmd line tool to check supervisor status
# Usage:
# eg. supervisorctl -c /etc/supervisor/conf.d/supervisord.conf status
# supervisorctl -c /etc/supervisor/conf.d/supervisord.conf restart
# supervisorctl -c /etc/supervisor/conf.d/supervisord.conf stop
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock
# HTTP server used to monitor supervisorctl
# Usage:
# eg. curl --unix-socket /var/run/supervisor.sock http://localhost
[unix_http_server]
file=/var/run/supervisor.sock
chmod=0700
# RPC interface required for supervisorctl to communciate with supervisor
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface