From 23b30416b582cfbecea5e21eb4e9b7ece3c53c53 Mon Sep 17 00:00:00 2001 From: Emran Billah Date: Wed, 4 Sep 2024 14:26:59 -0300 Subject: [PATCH] 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 --- .env | 13 +++--- Dockerfile | 6 +-- README.md | 46 ++++++++++++------- ...yml => docker-compose-run-multiple-dev.yml | 0 ...e.yml => docker-compose-run-single-dev.yml | 9 +--- docker-compose.yml | 15 ++++++ ...ate_tap_device.sh => create-tap-device.sh} | 0 ...oy_tap_device.sh => destroy-tap-device.sh} | 0 ...host_macvlan.sh => enable-host-macvlan.sh} | 0 .../debug/{host_setup.sh => host-setup.sh} | 0 scripts/entrypoint.sh | 36 ++++++++------- ...ifi_info.sh => get-connected-wifi-info.sh} | 0 scripts/{health_check.sh => health-check.sh} | 0 scripts/set-env-vars.sh | 32 +++++++------ 14 files changed, 95 insertions(+), 62 deletions(-) rename docker-compose-run-multiple.yml => docker-compose-run-multiple-dev.yml (100%) rename docker-compose-run-single.yml => docker-compose-run-single-dev.yml (92%) create mode 100644 docker-compose.yml rename scripts/debug/{create_tap_device.sh => create-tap-device.sh} (100%) rename scripts/debug/{destroy_tap_device.sh => destroy-tap-device.sh} (100%) rename scripts/debug/{enable_host_macvlan.sh => enable-host-macvlan.sh} (100%) rename scripts/debug/{host_setup.sh => host-setup.sh} (100%) rename scripts/{get_connected_wifi_info.sh => get-connected-wifi-info.sh} (100%) rename scripts/{health_check.sh => health-check.sh} (100%) diff --git a/.env b/.env index 23de14a..b686547 100755 --- a/.env +++ b/.env @@ -1,14 +1,15 @@ # Base params -XBNET_BASE_SUBNET=3.3.3 +XBNET_BASE_SUBNET=2.2.2 XBEE_INDEX=1 +XBNET_PROTO=tap # Default params -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) +XBNET_DEFAULT_GATEWAY=2.2.2.1 +XBNET_DEFAULT_IPVLAN_IP=2.2.2.20 # Required only when running ipvlan 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 XBEE_PORT=/dev/ttyUSB0 XBEE_BAUDRATE=230400 -XBNET_IP=3.3.3.201 # Set based on interface type -XBNET_INTERFACE_NAME=xbnet_router_1 # 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 diff --git a/Dockerfile b/Dockerfile index 29a0361..fbcaeff 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,8 +35,8 @@ 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 +COPY ./scripts/health-check.sh /health-check.sh +RUN chmod +x /health-check.sh # Add healthcheck -HEALTHCHECK CMD /health_check.sh || exit 1 +HEALTHCHECK CMD /health-check.sh || exit 1 diff --git a/README.md b/README.md index 2808ab1..374ee76 100644 --- a/README.md +++ b/README.md @@ -65,19 +65,30 @@ git clone https://git.spirirobotics.com/Spiri/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` - 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: ```bash @@ -91,6 +102,14 @@ cd services-xbee_net 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: ```bash @@ -109,22 +128,15 @@ cd services-xbee_net # Default Gateway (REQUIRED: part of minimum concifg to run a single xbnet node) 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. -#### 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 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.\ Verify the network interfaces using ip a inside the container.\ diff --git a/docker-compose-run-multiple.yml b/docker-compose-run-multiple-dev.yml similarity index 100% rename from docker-compose-run-multiple.yml rename to docker-compose-run-multiple-dev.yml diff --git a/docker-compose-run-single.yml b/docker-compose-run-single-dev.yml similarity index 92% rename from docker-compose-run-single.yml rename to docker-compose-run-single-dev.yml index 75c8583..1b1539a 100644 --- a/docker-compose-run-single.yml +++ b/docker-compose-run-single-dev.yml @@ -67,13 +67,8 @@ services: build: . container_name: xbnet_node privileged: true - # env_file: .env - environment: - - XBEE_INDEX=1 - - XBEE_BAUDRATE=230400 - - XBEE_PORT=/dev/ttyUSB0 - - XBNET_BASE_SUBNET=7.7.7 - - XBNET_INTERFACE_TYPE=router + env_file: + - .env command: bash /entrypoint.sh # network_mode: "host" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e18ddf3 --- /dev/null +++ b/docker-compose.yml @@ -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 \ No newline at end of file diff --git a/scripts/debug/create_tap_device.sh b/scripts/debug/create-tap-device.sh similarity index 100% rename from scripts/debug/create_tap_device.sh rename to scripts/debug/create-tap-device.sh diff --git a/scripts/debug/destroy_tap_device.sh b/scripts/debug/destroy-tap-device.sh similarity index 100% rename from scripts/debug/destroy_tap_device.sh rename to scripts/debug/destroy-tap-device.sh diff --git a/scripts/debug/enable_host_macvlan.sh b/scripts/debug/enable-host-macvlan.sh similarity index 100% rename from scripts/debug/enable_host_macvlan.sh rename to scripts/debug/enable-host-macvlan.sh diff --git a/scripts/debug/host_setup.sh b/scripts/debug/host-setup.sh similarity index 100% rename from scripts/debug/host_setup.sh rename to scripts/debug/host-setup.sh diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index d502308..18b346b 100755 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -14,10 +14,11 @@ echo "Starting entrypoint.sh" # Set associated env vars echo "Setting associated environment variables. Calling /set-env-vars.sh" -sh /set-env-vars.sh +source /set-env-vars.sh # Main loop loop() { + echo "Starting loop" while true; do # Check if the XBee device is connected check_device_port @@ -26,7 +27,6 @@ loop() { cleanup continue fi - sleep 0.5 # Check if the network interface is up check_network_state @@ -34,11 +34,10 @@ loop() { 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 - + sleep 0.5 done } @@ -63,8 +62,9 @@ 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 ${XBNET_INTERFACE_NAME} & + # Start xbnet and run it in the background + # - 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 while ! check_network_state; do @@ -75,15 +75,19 @@ create_tap_interface() { 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: ${XBNET_INTERFACE_NAME}" - echo "Gateway: ${DEFAULT_GATEWAY}" - echo "IP: ${XBNET_IP}" - echo "Device port: ${XBEE_PORT}" - echo "Device baudrate: ${XBEE_BAUDRATE}" - echo "-------------------------------------------" + echo "---------------------------------------------------------" + echo "***************** Tap interface created *****************" + echo "---------------------------------------------------------" + 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_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 } @@ -107,7 +111,7 @@ log_xbnet_messages() { 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 + sleep 0.5 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) diff --git a/scripts/get_connected_wifi_info.sh b/scripts/get-connected-wifi-info.sh similarity index 100% rename from scripts/get_connected_wifi_info.sh rename to scripts/get-connected-wifi-info.sh diff --git a/scripts/health_check.sh b/scripts/health-check.sh similarity index 100% rename from scripts/health_check.sh rename to scripts/health-check.sh diff --git a/scripts/set-env-vars.sh b/scripts/set-env-vars.sh index 4571acb..7526cc5 100755 --- a/scripts/set-env-vars.sh +++ b/scripts/set-env-vars.sh @@ -9,7 +9,8 @@ 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"] +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 if [ "$XBNET_INTERFACE_TYPE" = "gateway" ]; then @@ -27,16 +28,21 @@ 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 "***********************************************************" +# Print out environment variables +print_env_vars() { + 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 "***********************************************************" +} + +# print_env_vars +