From 99b5982593ce0c004e85f01a81394e4d94bd1275 Mon Sep 17 00:00:00 2001 From: zeus Date: Wed, 8 Nov 2023 13:46:14 +0000 Subject: [PATCH] swarmlab-linux --- swarmlab-linux | 178 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 swarmlab-linux diff --git a/swarmlab-linux b/swarmlab-linux new file mode 100644 index 0000000..1bc7996 --- /dev/null +++ b/swarmlab-linux @@ -0,0 +1,178 @@ +#!/bin/bash + +# Default values for options +scale=1 +DOCKERLINUX=/home/${USER}/appl/docker/linux + +txturl='\033[1;36m' +txturl1='\033[0;36m' +txtg='\033[0;32m' +NOCOLOR='\033[0m' + +# Function to print usage information +function print_usage { + #echo "Usage: $0 [OPTION]..." + echo "Usage: swarmlab-linux [OPTION]..." + echo "Start, stop, scale, help, or connect to a service." + echo "" + echo "Options:" + echo " start Start the service." + echo " stop Stop the service." + echo " scale=N Scale the service to N instances (default: 1)." + echo " help Display this help message." + echo " connect Connect to the service." + echo "" + echo -e ${txtg}" ****************************************** " + echo -e "INFO: Sharing Data between Docker Containers " + echo -e " -OR- " + echo -e " Sharing Data between Host and Docker Containers " + echo -e " " + echo -e "HOST: /home/${USER}/appl/docker/linux/data" + echo -e "Container: /data" + echo -e " ****************************************** ${NOCOLOR}" +} + +# +## Function to generate autocompletion options +#function generate_options { +# local options="start stop scale help connect" +# for i in $(seq 1 $(( $# - 1 )) ); do +# options=$(echo "$options" | tr " " "\n" | grep "^${!i}" | tr "\n" " ") +# done +# echo "$options" +#} +# +## Bash autocompletion +#if [[ -n $COMP_LINE ]]; then +# COMPREPLY=($(generate_options ${COMP_WORDS[@]})) +#else +# complete -W "start stop scale help connect" $0 +# exit 0 +#fi +# + + + +# Parse command line options +if [[ $# -eq 0 ]]; then + print_usage + exit 1 +fi + +while [[ $# -gt 0 ]]; do + case $1 in + start) + echo "Starting service..." + scale=1 + ;; + stop) + echo "Stopping service..." + scale=0 +if [ -d $DOCKERLINUX ] +then + #echo "exists" + cd $DOCKERLINUX + docker compose down +else + #echo "no exists" + mkdir -p $DOCKERLINUX + mkdir -p $DOCKERLINUX/data + cd $DOCKERLINUX + +cat << EOF > docker-compose.yml +version: "3" +services: + ubuntu: + #container_name: ubuntu + volumes: + - "${DOCKERLINUX}/data:/data:rw" + image: ubuntu + restart: on-failure + command: ["sleep","infinity"] +EOF + + docker compose down + +fi + + ;; + scale=*) + scale=${1#*=} + if ! [[ $scale =~ ^[0-9]+$ ]] || [[ $scale -lt 1 ]]; then + echo "Error: scale must be a positive integer." >&2 + exit 1 + fi + ;; + scale) + if [[ -z $2 ]] || ! [[ $2 =~ ^[0-9]+$ ]] || [[ $2 -lt 1 ]]; then + echo "Error: scale must be followed by a positive integer." >&2 + exit 1 + fi + scale=$2 + shift + ;; + help) + scale=0 + print_usage + exit 0 + ;; + connect) + cd $DOCKERLINUX + scale=0 + echo "Connecting to service..." + echo "if there is no current Linux instance, then start one with start or scale=num" + echo "Ctrl-c to escape" + echo "-----------------------" + #docker ps --format "Linux: {{.Names}}" | grep linux-ubuntu | cut -d'-' -f3 | sort + rin=$(docker ps --format "Linux: {{.Names}}" | grep linux-ubuntu | cut -d'-' -f3 | sort -n | sed -e ':a;N;s/\n/ /;ba') + echo "ID: ${rin}" + read -p "Which Linux instance do you want to connect to? Input Selection:" ins + docker exec -it linux-ubuntu-${ins} /bin/bash + ;; + *) + echo "Error: invalid option $1." >&2 + print_usage >&2 + exit 1 + ;; + esac + shift +done + +# Start services +#echo "Scale: $scale" +if [ ! "$scale" == 0 ] +then + if [ -d $DOCKERLINUX ] + then + #echo "exists" + cd $DOCKERLINUX + docker compose up -d --no-recreate --scale ubuntu=$scale + echo "Running Linux Instances..." + rin=$(docker ps --format "Linux: {{.Names}}" | grep linux-ubuntu | cut -d'-' -f3 | sort -n | sed -e ':a;N;s/\n/ /;ba') + echo "ID: ${rin}" + else + #echo "no exists" + mkdir -p $DOCKERLINUX + mkdir -p $DOCKERLINUX/data + cd $DOCKERLINUX + +cat << EOF > docker-compose.yml +version: "3" +services: + ubuntu: + #container_name: ubuntu + volumes: + - "${DOCKERLINUX}/data:/data:rw" + image: ubuntu + restart: on-failure + command: ["sleep","infinity"] +EOF + + docker compose up -d --no-recreate --scale ubuntu=$scale + echo "Running Linux:" + #docker ps --format "Linux: {{.Names}}" | grep linux-ubuntu | cut -d'-' -f3 | sort -n | sed -e ':a;N;s/\n/ /;ba' + rin=$(docker ps --format "Linux: {{.Names}}" | grep linux-ubuntu | cut -d'-' -f3 | sort -n | sed -e ':a;N;s/\n/ /;ba') + echo "ID: ${rin}" + + fi +fi