Added production quality docker compose file with environment vars set in the docker compose file, without using env vars, updated docs, separated out run-single and run-multiple

This commit is contained in:
Emran Billah 2024-09-04 14:26:59 -03:00
parent d5adc2c7db
commit 23b30416b5
14 changed files with 95 additions and 62 deletions

11
.env
View File

@ -1,14 +1,15 @@
# Base params # Base params
XBNET_BASE_SUBNET=3.3.3 XBNET_BASE_SUBNET=2.2.2
XBEE_INDEX=1 XBEE_INDEX=1
XBNET_PROTO=tap
# Default params # Default params
XBNET_DEFAULT_GATEWAY=3.3.3.1 XBNET_DEFAULT_GATEWAY=2.2.2.1
XBNET_DEFAULT_IPVLAN_IP=3.3.3.20 # Required only when running ipvlan net (look in docker compose) XBNET_DEFAULT_IPVLAN_IP=2.2.2.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) XBNET_DEFAULT_MACVLAN_IP=2.2.2.30 # Required only when running macvlan net (look in docker compose)
# Configuration for xbnet # Configuration for xbnet
XBEE_PORT=/dev/ttyUSB0 XBEE_PORT=/dev/ttyUSB0
XBEE_BAUDRATE=230400 XBEE_BAUDRATE=230400
XBNET_IP=3.3.3.201 # Set based on interface type XBNET_IP=2.2.2.201 # Set based on interface type
XBNET_INTERFACE_NAME=xbnet_router_1 # Set based on interface type XBNET_INTERFACE_NAME=xbnet_router_1 # Set based on interface type

View File

@ -35,8 +35,8 @@ COPY ./scripts/set-env-vars.sh /set-env-vars.sh
RUN chmod +x /set-env-vars.sh RUN chmod +x /set-env-vars.sh
# 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
# Add healthcheck # Add healthcheck
HEALTHCHECK CMD /health_check.sh || exit 1 HEALTHCHECK CMD /health-check.sh || exit 1

View File

@ -65,19 +65,30 @@ git clone https://git.spirirobotics.com/Spiri/services-xbee_net
cd services-xbee_net cd services-xbee_net
``` ```
#### 2. Run a single xbnet node using the following cmd: #### 2. To run the project in `PRODUCTION` mode, use the following cmd:
- `docker-compose -f docker-compose.yml up --build`
- Configure the project for user specs, modify the `docker-compose.yml` file for the following parameters:
```bash
environment:
- XBEE_INDEX=1
- XBEE_BAUDRATE=230400
- XBEE_PORT=/dev/ttyUSB0
- XBNET_BASE_SUBNET=2.2.2
- XBNET_INTERFACE_TYPE=router # ["router" | "gateway"]
- XBNET_PROTO=tap # ["tap" | "tun"]
```
#### 3. To run a single xbnet node in `DEVELOPEMENT` mode, use the following cmd:
- Router mode: `sh ./run-single.sh --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=router` - 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` - 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 #### OR Manually build and Start the Docker Container
NOTE: This will require the config `.env` to be manually modified, based on user needs.
#### 3. OR Manually build and Start the Docker Container
##### NOTE: This will require the config `.env` to be manually modified, based on user needs.
---
- Configure a single xbnet net service using: - Configure a single xbnet net service using:
```bash ```bash
@ -91,6 +102,14 @@ cd services-xbee_net
DEFAULT_GATEWAY=192.168.1.1 DEFAULT_GATEWAY=192.168.1.1
``` ```
#### 4. To run a multiple xbnet nodes in `DEVELOPEMENT` mode, use the following cmd:
- Router 1: `sh ./run-multiple.sh --index=1 --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=router`
- Router 2: `sh ./run-multiple.sh --index=2 --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=router`
- Gateway: `sh ./run-multiple.sh --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=gateway`
NOTE: This will require the config `.env` to be manually modified, based on user needs.
- Configure multiple xbnet net services using: - Configure multiple xbnet net services using:
```bash ```bash
@ -109,22 +128,15 @@ cd services-xbee_net
# Default Gateway (REQUIRED: part of minimum concifg to run a single xbnet node) # Default Gateway (REQUIRED: part of minimum concifg to run a single xbnet node)
DEFAULT_GATEWAY=192.168.1.1 DEFAULT_GATEWAY=192.168.1.1
``` ```
---
To build the Docker image and start the container, run:
```bash
docker-compose -f docker-compose.yml up --build
```
This will build the Docker image and start the container with the XBee network and the required setup. This will build the Docker image and start the container with the XBee network and the required setup.
#### 4. Health Check and Container Management #### 5. Health Check and Container Management
The container includes a health check that pings the XBee network interface to ensure it is functioning properly. \ The container includes a health check that pings the XBee network interface to ensure it is functioning properly. \
The container is configured to restart automatically if the health check fails. The container is configured to restart automatically if the health check fails.
#### 5. Troubleshooting #### 6. Troubleshooting
Ensure the XBee devices are connected to the correct serial ports.\ Ensure the XBee devices are connected to the correct serial ports.\
Verify the network interfaces using ip a inside the container.\ Verify the network interfaces using ip a inside the container.\

View File

@ -67,13 +67,8 @@ services:
build: . build: .
container_name: xbnet_node container_name: xbnet_node
privileged: true privileged: true
# env_file: .env env_file:
environment: - .env
- XBEE_INDEX=1
- XBEE_BAUDRATE=230400
- XBEE_PORT=/dev/ttyUSB0
- XBNET_BASE_SUBNET=7.7.7
- XBNET_INTERFACE_TYPE=router
command: bash /entrypoint.sh command: bash /entrypoint.sh
# network_mode: "host" # network_mode: "host"

15
docker-compose.yml Normal file
View File

@ -0,0 +1,15 @@
version: '3.8'
services:
xbnet-node:
build: .
container_name: xbnet_node
privileged: true
environment:
- XBEE_INDEX=1
- XBEE_BAUDRATE=230400
- XBEE_PORT=/dev/ttyUSB0
- XBNET_BASE_SUBNET=2.2.2
- XBNET_INTERFACE_TYPE=router
- XBNET_PROTO=tap
command: bash /entrypoint.sh

View File

@ -14,10 +14,11 @@ echo "Starting entrypoint.sh"
# Set associated env vars # Set associated env vars
echo "Setting associated environment variables. Calling /set-env-vars.sh" echo "Setting associated environment variables. Calling /set-env-vars.sh"
sh /set-env-vars.sh source /set-env-vars.sh
# Main loop # Main loop
loop() { loop() {
echo "Starting loop"
while true; do while true; do
# Check if the XBee device is connected # Check if the XBee device is connected
check_device_port check_device_port
@ -26,7 +27,6 @@ loop() {
cleanup cleanup
continue continue
fi fi
sleep 0.5
# Check if the network interface is up # Check if the network interface is up
check_network_state check_network_state
@ -34,7 +34,6 @@ loop() {
echo "Network interface $XBNET_INTERFACE_NAME not found. Creating TAP interface..." echo "Network interface $XBNET_INTERFACE_NAME not found. Creating TAP interface..."
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
@ -64,7 +63,8 @@ check_network_state() {
# Create a TAP network interface # 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
xbnet -d --serial-speed ${XBEE_BAUDRATE} ${XBEE_PORT} tap --iface-name ${XBNET_INTERFACE_NAME} & # - eg. xbnet -d --serial-speed 230400 /dev/ttyUSB0 tap --iface-name=xbnet_router_1
xbnet -d --serial-speed ${XBEE_BAUDRATE} ${XBEE_PORT} ${XBNET_PROTO} --iface-name ${XBNET_INTERFACE_NAME} &
# Wait until the interface is created # Wait until the interface is created
while ! check_network_state; do while ! check_network_state; do
@ -75,15 +75,19 @@ create_tap_interface() {
ip addr add $XBNET_IP/24 dev $XBNET_INTERFACE_NAME ip addr add $XBNET_IP/24 dev $XBNET_INTERFACE_NAME
ip link set $XBNET_INTERFACE_NAME up ip link set $XBNET_INTERFACE_NAME up
echo "-------------------------------------------" echo "---------------------------------------------------------"
echo "********** Tap interface created **********" echo "***************** Tap interface created *****************"
echo "-------------------------------------------" echo "---------------------------------------------------------"
echo "Name: ${XBNET_INTERFACE_NAME}" echo "XBEE_INDEX : ${XBEE_INDEX}"
echo "Gateway: ${DEFAULT_GATEWAY}" echo "XBEE_BAUDRATE : ${XBEE_BAUDRATE}"
echo "IP: ${XBNET_IP}" echo "XBEE_PORT : ${XBEE_PORT}"
echo "Device port: ${XBEE_PORT}" echo "XBNET_BASE_SUBNET : ${XBNET_BASE_SUBNET}"
echo "Device baudrate: ${XBEE_BAUDRATE}" echo "XBNET_IP : ${XBNET_IP}"
echo "-------------------------------------------" echo "XBNET_INTERFACE_NAME : ${XBNET_INTERFACE_NAME}"
echo "XBNET_DEFAULT_GATEWAY : ${XBNET_DEFAULT_GATEWAY}"
echo "XBNET_DEFAULT_IPVLAN_IP : ${XBNET_DEFAULT_IPVLAN_IP}"
echo "XBNET_DEFAULT_MACVLAN_IP : ${XBNET_DEFAULT_MACVLAN_IP}"
echo "---------------------------------------------------------"
return 0 return 0
} }
@ -107,7 +111,7 @@ log_xbnet_messages() {
RX_PACKETS_BEFORE=$(cat /sys/class/net/$XBNET_INTERFACE_NAME/statistics/rx_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) TX_PACKETS_BEFORE=$(cat /sys/class/net/$XBNET_INTERFACE_NAME/statistics/tx_packets)
sleep 1 sleep 0.5
RX_PACKETS_AFTER=$(cat /sys/class/net/$XBNET_INTERFACE_NAME/statistics/rx_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) TX_PACKETS_AFTER=$(cat /sys/class/net/$XBNET_INTERFACE_NAME/statistics/tx_packets)

View File

@ -10,6 +10,7 @@ export XBEE_BAUDRATE=${XBEE_BAUDRATE}
export XBEE_PORT=${XBEE_PORT} export XBEE_PORT=${XBEE_PORT}
export XBNET_BASE_SUBNET=${XBNET_BASE_SUBNET} export XBNET_BASE_SUBNET=${XBNET_BASE_SUBNET}
export XBNET_INTERFACE_TYPE=${XBNET_INTERFACE_TYPE} # Options: ["gateway", "router"] export XBNET_INTERFACE_TYPE=${XBNET_INTERFACE_TYPE} # Options: ["gateway", "router"]
export XBNET_PROTO=tap # Options: ["tap", "tun"]
# Determine the source IP based on interface type # Determine the source IP based on interface type
if [ "$XBNET_INTERFACE_TYPE" = "gateway" ]; then if [ "$XBNET_INTERFACE_TYPE" = "gateway" ]; then
@ -27,7 +28,8 @@ export XBNET_DEFAULT_MACVLAN_IP="${XBNET_BASE_SUBNET}.30"
export XBNET_IP export XBNET_IP
export XBNET_INTERFACE_NAME export XBNET_INTERFACE_NAME
# Print out values of environment variables # Print out environment variables
print_env_vars() {
echo "***********************************************************" echo "***********************************************************"
echo "Environment variables set. " echo "Environment variables set. "
echo "XBEE_INDEX : ${XBEE_INDEX}" echo "XBEE_INDEX : ${XBEE_INDEX}"
@ -39,4 +41,8 @@ echo "XBNET_INTERFACE_NAME : ${XBNET_INTERFACE_NAME}"
echo "XBNET_DEFAULT_IPVLAN_IP : ${XBNET_DEFAULT_IPVLAN_IP}" echo "XBNET_DEFAULT_IPVLAN_IP : ${XBNET_DEFAULT_IPVLAN_IP}"
echo "XBNET_DEFAULT_MACVLAN_IP : ${XBNET_DEFAULT_MACVLAN_IP}" echo "XBNET_DEFAULT_MACVLAN_IP : ${XBNET_DEFAULT_MACVLAN_IP}"
echo "***********************************************************" echo "***********************************************************"
}
# print_env_vars