From 30b02e6084eebb3ff970a9b443ce39f7c7385ef6 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Wed, 18 Sep 2024 19:35:49 -0400 Subject: [PATCH] Update article --- ...cord-Linux Automated Container Platform.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/markdown/Deep Dive: Discord-Linux Automated Container Platform.md b/markdown/Deep Dive: Discord-Linux Automated Container Platform.md index 326eab1..d4898e8 100644 --- a/markdown/Deep Dive: Discord-Linux Automated Container Platform.md +++ b/markdown/Deep Dive: Discord-Linux Automated Container Platform.md @@ -187,6 +187,48 @@ cmd(`sh /home/scripts/detectIP.sh ` + randNetwork).then(out => { }); ``` +## The detectIP script + +```bash +#!/bin/bash + +# Check if the network name is provided as a parameter +if [ $# -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +# Extract the network name from the parameter +network_name="$1" + +# Get the subnet of the Docker network +subnet=$(docker network inspect "$network_name" -f '{{(index .IPAM.Config 0).Subnet}}') + +if [ -z "$subnet" ]; then + echo "Network '$network_name' not found or doesn't have an assigned subnet." + exit 1 +fi + +# Extract just the subnet prefix (e.g., 172.18.0) +subnet_prefix="${subnet%.*}" + +# Loop through IP addresses in the subnet to find an available one +for ((i=2; i<=254; i++)); do + ip="${subnet_prefix}.$i" + + # Check if the IP address is already in use by a running container + if ! ping -c 1 "$ip" &> /dev/null; then + echo "$ip" + exit 0 + fi +done + +echo "No available IP addresses found in the subnet of network '$network_name'." +exit 1 +``` + +This script checks if exactly one argument (a network name) is provided, and if not, it displays usage instructions and exits. It retrieves the subnet of the specified Docker network, checks if the network exists and has an assigned subnet, and extracts the subnet prefix (e.g., 172.18.0). The script then loops through possible IP addresses within the subnet range, checking if each is available by attempting to ping it. If an available IP is found, it prints the IP and exits. If no available IP is found, it notifies the user and exits. + #### Operating System Selection The script supports multiple Linux distributions, such as Ubuntu, Debian, CentOS, and more. Depending on the command-line arguments passed, the script selects the appropriate operating system for the Docker container. Each OS is associated with specific resource settings (e.g., memory, CPUs) and restart policies, all configurable based on user input.