Testing env vars in docker compose

This commit is contained in:
Emran Billah 2024-09-03 10:09:03 -03:00
parent 317e4dfaff
commit d5adc2c7db
8 changed files with 155 additions and 93 deletions

12
.env
View File

@ -1,14 +1,14 @@
# Base params
BASE_SUBNET=7.7.7
XBNET_BASE_SUBNET=3.3.3
XBEE_INDEX=1
# Default params
DEFAULT_GATEWAY=7.7.7.1
DEFAULT_IPVLAN_IP=7.7.7.20 # Required only when running ipvlan net (look in docker compose)
DEFAULT_MACVLAN_IP=7.7.7.30 # Required only when running macvlan net (look in docker compose)
XBNET_DEFAULT_GATEWAY=3.3.3.1
XBNET_DEFAULT_IPVLAN_IP=3.3.3.20 # Required only when running ipvlan net (look in docker compose)
XBNET_DEFAULT_MACVLAN_IP=3.3.3.30 # Required only when running macvlan net (look in docker compose)
# Configuration for xbnet
XBEE_PORT=/dev/ttyUSB0
XBEE_BAUDRATE=230400
XBEE_NET_SRC_IP=7.7.7.201 # Set based on interface type
XBEE_NET_IFACE_NAME=xbnet_router_1 # Set based on interface type
XBNET_IP=3.3.3.201 # Set based on interface type
XBNET_INTERFACE_NAME=xbnet_router_1 # Set based on interface type

3
.gitignore vendored
View File

@ -1 +1,2 @@
workspace
workspace
.env

View File

@ -30,6 +30,10 @@ RUN cp target/release/xbnet /usr/local/bin/xbnet
COPY ./scripts/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Copy the set-env-vars.sh script to set env vars
COPY ./scripts/set-env-vars.sh /set-env-vars.sh
RUN chmod +x /set-env-vars.sh
# Copy the health check script
COPY ./scripts/health_check.sh /health_check.sh
RUN chmod +x /health_check.sh

View File

@ -70,6 +70,8 @@ cd services-xbee_net
- Router mode: `sh ./run-single.sh --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=router`
- Gateway mode: `sh ./run-single.sh --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=gateway`
##### NOTE: This setup assumes that you have an XBee device connected to port: /dev/ttyUSB0
#### 3. OR Manually build and Start the Docker Container

View File

@ -67,8 +67,18 @@ services:
build: .
container_name: xbnet_node
privileged: true
env_file: .env
# env_file: .env
environment:
- XBEE_INDEX=1
- XBEE_BAUDRATE=230400
- XBEE_PORT=/dev/ttyUSB0
- XBNET_BASE_SUBNET=7.7.7
- XBNET_INTERFACE_TYPE=router
command: bash /entrypoint.sh
# network_mode: "host"
# command: /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
# command: tail -f /dev/null
# networks:

View File

@ -4,43 +4,36 @@
#
# This script sets up the environment for running a Docker Compose configuration with XBee networks.
# It accepts optional --index=<int>, --subnet=<subnet>, --serial-speed=<speed>, --port=<port>, and --interface-type=<type> arguments
# to specify the XBEE_INDEX, BASE_SUBNET, XBEE_BAUDRATE, XBEE_PORT, and XBEE_INTERFACE_TYPE values.
# If no index is provided, it defaults to 1. If no subnet is provided, it defaults to 192.168.1.
# to specify the XBEE_INDEX, XBNET_BASE_SUBNET, XBEE_BAUDRATE, XBEE_PORT, and XBNET_INTERFACE_TYPE values.
# If no index is provided, it defaults to 1. If no subnet is provided, it defaults to 10.10.10.
# If no serial-speed is provided, it defaults to 230400. If no port is provided, it defaults to /dev/ttyUSB0.
# If no interface-type is provided, it defaults to "router".
#
# Usage:
# ./run-single.sh # Runs with default index (1), default subnet (192.168.1), default speed (230400), and default port (/dev/ttyUSB0)
# ./run-single.sh # Runs with default index (1), default subnet (10.10.10), default speed (230400), and default port (/dev/ttyUSB0)
# ./run-single.sh --index=2 # Runs with index 2 and default values for other options
# ./run-single.sh --subnet=192.168.50 # Runs with default index (1), specified subnet, and default values for other options
# ./run-single.sh --index=3 --subnet=192.168.50 # Runs with specified index, specified subnet, and default values for speed and port
# ./run-single.sh --serial-speed=115200 --port=/dev/ttyUSB1 # Runs with specified serial-speed and port with default values for other options
# ./run-single.sh --interface-type=gateway # Runs with specified interface type as gateway
# IPs assigned:
# BASE_SUBNET: eg.: 10.10.10
# DEFAULT_GATEWAY: ${BASE_SUBNET}.1, eg.: 10.10.10.1
# DEFAULT_IPVLAN_IP: ${BASE_SUBNET}.20 eg.: 10.10.10.20
# DEFAULT_MACVLAN_IP: ${BASE_SUBNET}.30 eg.: 10.10.10.30
# XBEE_NET_SRC_IP: ${BASE_SUBNET}.20${INDEX} eg.: 10.10.10.201 (all Xbnets will be in the 200+ range)
# Default values
INDEX=1
BASE_SUBNET="10.10.10"
XBEE_INDEX=1
XBNET_BASE_SUBNET="10.10.10"
XBEE_BAUDRATE=230400
XBEE_PORT="/dev/ttyUSB0"
XBEE_INTERFACE_TYPE="router" # Options: ["gateway", "router"]
XBNET_INTERFACE_TYPE="router" # Options: ["gateway", "router"]
# Parse command-line arguments
for arg in "$@"
do
case $arg in
--index=*)
INDEX="${arg#*=}"
XBEE_INDEX="${arg#*=}"
shift
;;
--subnet=*)
BASE_SUBNET="${arg#*=}"
XBNET_BASE_SUBNET="${arg#*=}"
shift
;;
--serial-speed=*)
@ -52,7 +45,7 @@ do
shift
;;
--interface-type=*)
XBEE_INTERFACE_TYPE="${arg#*=}"
XBNET_INTERFACE_TYPE="${arg#*=}"
shift
;;
*)
@ -61,30 +54,30 @@ do
done
# Determine the source IP based on interface type
if [ "$XBEE_INTERFACE_TYPE" = "gateway" ]; then
XBEE_NET_SRC_IP="${BASE_SUBNET}.1"
XBEE_NET_IFACE_NAME=xbnet_gateway
if [ "$XBNET_INTERFACE_TYPE" = "gateway" ]; then
XBNET_IP="${XBNET_BASE_SUBNET}.1"
XBNET_INTERFACE_NAME="xbnet_gateway"
else
XBEE_NET_SRC_IP="${BASE_SUBNET}.20${INDEX}"
XBEE_NET_IFACE_NAME=xbnet_router_${INDEX}
XBNET_IP="${XBNET_BASE_SUBNET}.20${XBEE_INDEX}"
XBNET_INTERFACE_NAME="xbnet_router_${XBEE_INDEX}"
fi
# Create a .env file with the specified parameters
cat <<EOF > .env
# Base params
BASE_SUBNET=${BASE_SUBNET}
XBEE_INDEX=${INDEX}
XBNET_BASE_SUBNET=${XBNET_BASE_SUBNET}
XBEE_INDEX=${XBEE_INDEX}
# Default params
DEFAULT_GATEWAY=${BASE_SUBNET}.1
DEFAULT_IPVLAN_IP=${BASE_SUBNET}.20 # Required only when running ipvlan net (look in docker compose)
DEFAULT_MACVLAN_IP=${BASE_SUBNET}.30 # Required only when running macvlan net (look in docker compose)
XBNET_DEFAULT_GATEWAY=${XBNET_BASE_SUBNET}.1
XBNET_DEFAULT_IPVLAN_IP=${XBNET_BASE_SUBNET}.20 # Required only when running ipvlan net (look in docker compose)
XBNET_DEFAULT_MACVLAN_IP=${XBNET_BASE_SUBNET}.30 # Required only when running macvlan net (look in docker compose)
# Configuration for xbnet
XBEE_PORT=${XBEE_PORT}
XBEE_BAUDRATE=${XBEE_BAUDRATE}
XBEE_NET_SRC_IP=${XBEE_NET_SRC_IP} # Set based on interface type
XBEE_NET_IFACE_NAME=${XBEE_NET_IFACE_NAME} # Set based on interface type
XBNET_IP=${XBNET_IP} # Set based on interface type
XBNET_INTERFACE_NAME=${XBNET_INTERFACE_NAME} # Set based on interface type
EOF
# Restart Docker Compose services

View File

@ -12,6 +12,10 @@
echo "Starting entrypoint.sh"
# Set associated env vars
echo "Setting associated environment variables. Calling /set-env-vars.sh"
sh /set-env-vars.sh
# Main loop
loop() {
while true; do
@ -27,13 +31,14 @@ loop() {
# Check if the network interface is up
check_network_state
if [ $? -ne 0 ]; then
echo "Network interface $XBEE_NET_IFACE_NAME not found. Creating TAP interface..."
echo "Network interface $XBNET_INTERFACE_NAME not found. Creating TAP interface..."
create_tap_interface
fi
sleep 0.5
# Log messages sent and received over xbnet
log_xbnet_messages
# log_xbnet_messages
sleep 0.5
done
}
@ -49,7 +54,7 @@ check_device_port() {
# Check if the network interface is up
check_network_state() {
if ip link show $XBEE_NET_IFACE_NAME > /dev/null 2>&1; then
if ip link show $XBNET_INTERFACE_NAME > /dev/null 2>&1; then
return 0
else
return 1
@ -59,7 +64,7 @@ check_network_state() {
# Create a TAP network interface
create_tap_interface() {
# Start xbnet and run it in the background
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 ${XBNET_INTERFACE_NAME} &
# Wait until the interface is created
while ! check_network_state; do
@ -67,93 +72,98 @@ create_tap_interface() {
done
# Create and bring up xbnet interface
ip addr add $XBEE_NET_SRC_IP/24 dev $XBEE_NET_IFACE_NAME
ip link set $XBEE_NET_IFACE_NAME up
ip addr add $XBNET_IP/24 dev $XBNET_INTERFACE_NAME
ip link set $XBNET_INTERFACE_NAME up
echo "-------------------------------------------"
echo "********** Tap interface created **********"
echo "-------------------------------------------"
echo "Name: ${XBEE_NET_IFACE_NAME}"
echo "Name: ${XBNET_INTERFACE_NAME}"
echo "Gateway: ${DEFAULT_GATEWAY}"
echo "IP: ${XBEE_NET_SRC_IP}"
echo "IP: ${XBNET_IP}"
echo "Device port: ${XBEE_PORT}"
echo "Device baudrate: ${XBEE_BAUDRATE}"
echo "-------------------------------------------"
return 0
}
# Log incoming and outgoing messages from Xbees over xbnet
# Function to log messages sent and received over xbnet
log_xbnet_messages() {
echo "Monitoring messages on $XBEE_NET_IFACE_NAME..."
echo "Monitoring messages on $XBNET_INTERFACE_NAME..."
if [ -z "$XBNET_INTERFACE_NAME" ]; then
echo "Error: $XBNET_INTERFACE_NAME is not set."
return 1
fi
# Ensure the interface exists before proceeding
if [ ! -d "/sys/class/net/$XBNET_INTERFACE_NAME" ]; then
echo "Error: Network interface $XBNET_INTERFACE_NAME does not exist."
return 1
fi
# 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)
RX_PACKETS_BEFORE=$(cat /sys/class/net/$XBNET_INTERFACE_NAME/statistics/rx_packets)
TX_PACKETS_BEFORE=$(cat /sys/class/net/$XBNET_INTERFACE_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_PACKETS_AFTER=$(cat /sys/class/net/$XBNET_INTERFACE_NAME/statistics/rx_packets)
TX_PACKETS_AFTER=$(cat /sys/class/net/$XBNET_INTERFACE_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
for packet in $(seq 1 $RX_DIFF); do
SRC_MAC=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/address)
SRC_IP=$(ip -o -4 addr show $XBEE_NET_IFACE_NAME | awk '{print $4}' | cut -d/ -f1)
# Assuming destination info is available
DST_IP=$(ip neighbor show dev $XBEE_NET_IFACE_NAME | grep -oP '((\d+\.){3}\d+)' | head -n 1)
DST_MAC=$(ip neighbor show dev $XBEE_NET_IFACE_NAME | grep -oP '([a-f0-9]{2}:){5}[a-f0-9]{2}' | head -n 1)
# Capture payload data (in hex format for simplicity)
PAYLOAD=$(cat /proc/net/dev_snmp6/$XBEE_NET_IFACE_NAME | hexdump -C | head -n 1)
echo "******************************************************************"
echo "$(date +'%Y-%m-%d %H:%M:%S')"
echo "$packet packets received on $XBEE_NET_IFACE_NAME"
echo "Src: { IP: $SRC_IP, MAC: $SRC_MAC }"
echo "Dst: { IP: $DST_IP, MAC: $DST_MAC }"
echo "Length: $(echo "$PAYLOAD" | wc -c) bytes"
echo "Payload: $PAYLOAD"
echo "******************************************************************"
done
echo "$(date +'%Y-%m-%d %H:%M:%S') - $RX_DIFF packets received on $XBNET_INTERFACE_NAME"
log_packet_info "received"
fi
if [[ $TX_DIFF -gt 0 ]]; then
for packet in $(seq 1 $TX_DIFF); do
SRC_MAC=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/address)
SRC_IP=$(ip -o -4 addr show $XBEE_NET_IFACE_NAME | awk '{print $4}' | cut -d/ -f1)
# Assuming destination info is available
DST_IP=$(ip neighbor show dev $XBEE_NET_IFACE_NAME | grep -oP '((\d+\.){3}\d+)' | head -n 1)
DST_MAC=$(ip neighbor show dev $XBEE_NET_IFACE_NAME | grep -oP '([a-f0-9]{2}:){5}[a-f0-9]{2}' | head -n 1)
# Capture payload data (in hex format for simplicity)
PAYLOAD=$(cat /proc/net/dev_snmp6/$XBEE_NET_IFACE_NAME | hexdump -C | head -n 1)
echo "******************************************************************"
echo "$(date +'%Y-%m-%d %H:%M:%S')"
echo "$packet packets sent on $XBEE_NET_IFACE_NAME"
echo "Src: { IP: $SRC_IP, MAC: $SRC_MAC }"
echo "Dst: { IP: $DST_IP, MAC: $DST_MAC }"
echo "Length: $(echo "$PAYLOAD" | wc -c) bytes"
echo "Payload: $PAYLOAD"
echo "******************************************************************"
done
echo "$(date +'%Y-%m-%d %H:%M:%S') - $TX_DIFF packets sent on $XBNET_INTERFACE_NAME"
log_packet_info "sent"
fi
done
}
# Function to log detailed packet information
log_packet_info() {
local direction=$1
local ip_info
# Example: Check /proc/net/arp for the associated IP and MAC addresses
ip_info=$(ip -br addr show $XBNET_INTERFACE_NAME)
src_ip=$(echo "$ip_info" | awk '{print $3}' | cut -d '/' -f 1)
src_mac=$(cat /sys/class/net/$XBNET_INTERFACE_NAME/address)
dst_info=$(ip neigh show dev $XBNET_INTERFACE_NAME | grep -v REACHABLE | head -n 1)
dst_ip=$(echo "$dst_info" | awk '{print $1}')
dst_mac=$(echo "$dst_info" | awk '{print $5}')
echo "******************************************************************"
echo "$(date +'%Y-%m-%d %H:%M:%S') - Packet $direction on $XBNET_INTERFACE_NAME"
echo "Src: { IP: $src_ip, MAC: $src_mac }"
echo "Dst: { IP: $dst_ip, MAC: $dst_mac }"
# Log packet length and a placeholder for payload (real payload extraction requires deep packet inspection)
length=$(cat /sys/class/net/$XBNET_INTERFACE_NAME/statistics/tx_bytes)
echo "Length: ${length} bytes"
# Placeholder for payload (not feasible to extract without raw packet inspection)
echo "Payload: [Payload extraction requires raw packet inspection tools]"
echo "******************************************************************"
}
# Clean up resources and exit the script
cleanup() {
echo "Cleaning up resources..."
pkill -f "xbnet -d --serial-speed $XBEE_BAUDRATE $XBEE_PORT tap"
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
if ip link show $XBNET_INTERFACE_NAME > /dev/null 2>&1; then
ip link set $XBNET_INTERFACE_NAME down
ip link delete $XBNET_INTERFACE_NAME
fi
}

42
scripts/set-env-vars.sh Executable file
View File

@ -0,0 +1,42 @@
#!/bin/bash
# set-env-vars.sh
#
# This script sets up the environment for running a Docker Compose configuration with XBee networks.
# Export the values set in the Docker Compose environment
export XBEE_INDEX=${XBEE_INDEX}
export XBEE_BAUDRATE=${XBEE_BAUDRATE}
export XBEE_PORT=${XBEE_PORT}
export XBNET_BASE_SUBNET=${XBNET_BASE_SUBNET}
export XBNET_INTERFACE_TYPE=${XBNET_INTERFACE_TYPE} # Options: ["gateway", "router"]
# Determine the source IP based on interface type
if [ "$XBNET_INTERFACE_TYPE" = "gateway" ]; then
XBNET_IP="${XBNET_BASE_SUBNET}.1"
XBNET_INTERFACE_NAME="xbnet_gateway"
else
XBNET_IP="${XBNET_BASE_SUBNET}.20${XBEE_INDEX}"
XBNET_INTERFACE_NAME="xbnet_router_${XBEE_INDEX}"
fi
# Export the renamed variables
export XBNET_DEFAULT_GATEWAY="${XBNET_BASE_SUBNET}.1"
export XBNET_DEFAULT_IPVLAN_IP="${XBNET_BASE_SUBNET}.20"
export XBNET_DEFAULT_MACVLAN_IP="${XBNET_BASE_SUBNET}.30"
export XBNET_IP
export XBNET_INTERFACE_NAME
# Print out values of environment variables
echo "***********************************************************"
echo "Environment variables set. "
echo "XBEE_INDEX : ${XBEE_INDEX}"
echo "XBEE_BAUDRATE : ${XBEE_BAUDRATE}"
echo "XBEE_PORT : ${XBEE_PORT}"
echo "XBNET_BASE_SUBNET : ${XBNET_BASE_SUBNET}"
echo "XBNET_IP : ${XBNET_IP}"
echo "XBNET_INTERFACE_NAME : ${XBNET_INTERFACE_NAME}"
echo "XBNET_DEFAULT_IPVLAN_IP : ${XBNET_DEFAULT_IPVLAN_IP}"
echo "XBNET_DEFAULT_MACVLAN_IP : ${XBNET_DEFAULT_MACVLAN_IP}"
echo "***********************************************************"