From 2eb893fad05d7bd1a0ac402d7a02777d933d1ec6 Mon Sep 17 00:00:00 2001 From: bitsofsteve Date: Wed, 3 Jul 2024 04:03:09 -0400 Subject: [PATCH] updating script --- scripts/docker-setup.sh | 144 +++++++++------------------------------- 1 file changed, 32 insertions(+), 112 deletions(-) diff --git a/scripts/docker-setup.sh b/scripts/docker-setup.sh index 1de0e89b873..b2899ed60c6 100755 --- a/scripts/docker-setup.sh +++ b/scripts/docker-setup.sh @@ -1,115 +1,3 @@ -# #!/bin/bash - -# # Initialize default values -# VERSION="" -# NODE_NAME="" -# NODE_SIDE_TYPE="high" -# NODE_TYPE="" -# PORT="" - -# # Function to display usage -# usage() { -# echo "Usage: $0 [-v|--version ] [-n|--name ] [-s|--side ] [-t|--type ] [-p|--port ]" -# exit 1 -# } - -# # Parse command line options -# while [[ "$#" -gt 0 ]]; do -# case "$1" in -# -v|--version) -# VERSION="$2" -# shift 2 -# ;; -# -n|--name) -# NODE_NAME="$2" -# shift 2 -# ;; -# -s|--side) -# NODE_SIDE_TYPE="$2" -# shift 2 -# ;; -# -t|--type) -# NODE_TYPE="$2" -# shift 2 -# ;; -# -p|--port) -# PORT="$2" -# shift 2 -# ;; -# *) -# usage -# ;; -# esac -# done - -# # Check if all required options are set -# if [[ -z "$VERSION" || -z "$NODE_NAME" || -z "$NODE_TYPE" || -z "$PORT" ]]; then -# echo "All options are required." -# usage -# fi - - -# [ -f "$TGZ_FILE" ] && rm -f "$TGZ_FILE" -# [ -f "$COMPOSE_FILE" ] && rm -f "$COMPOSE_FILE" - - -# #Use curl to download the fille from azure blob storage -# curl -L -o syft-file.tgz "https://openminedblob.blob.core.windows.net/syft-files/syft-compose-file.tar.gz" - - -# # Unzip the .tgz file -# tar -xzf syft-file.tgz - -# #Change directory to the unzipped folder -# cd syft-compose-files - - -# # Detect OS -# OS="linux" -# case "$(uname)" in -# Darwin) -# OS="mac" -# ;; -# esac - -# # Assuming the .env file is in the current directory after unzipping -# # Update the VERSION, NODE_NAME, NODE_SIDE_TYPE, NODE_TYPE, and PORT values based on the OS -# if [[ "$OS" == "mac" ]]; then -# sed -i '' "s/^VERSION=.*$/VERSION=$VERSION/" .env -# sed -i '' "s/^NODE_NAME=.*$/NODE_NAME=$NODE_NAME/" .env -# sed -i '' "s/^NODE_SIDE_TYPE=.*$/NODE_SIDE_TYPE=$NODE_SIDE_TYPE/" .env -# sed -i '' "s/^NODE_TYPE=.*$/NODE_TYPE=$NODE_TYPE/" .env -# sed -i '' "s/^PORT=.*$/PORT=$PORT/" .env -# else -# sed -i "s/^VERSION=.*$/VERSION=$VERSION/" .env -# sed -i "s/^NODE_NAME=.*$/NODE_NAME=$NODE_NAME/" .env -# sed -i "s/^NODE_SIDE_TYPE=.*$/NODE_SIDE_TYPE=$NODE_SIDE_TYPE/" .env -# sed -i "s/^NODE_TYPE=.*$/NODE_TYPE=$NODE_TYPE/" .env -# sed -i "s/^PORT=.*$/PORT=$PORT/" .env -# fi - - - -# # Modify docker-compose.yml if the version is not 0.8.2-beta.6 -# if [[ "$VERSION" != "0.8.2-beta.6" ]]; then -# if [[ "$OS" == "mac" ]]; then -# sed -i '' '/command: "\/app\/grid\/start.sh"/s/^/#/' docker-compose.yml -# else -# sed -i '/command: "\/app\/grid\/start.sh"/s/^/#/' docker-compose.yml -# fi -# fi - - - -# # Run the docker compose command -# docker compose --env-file ./.env -p "$NODE_NAME" --profile blob-storage --profile frontend --file docker-compose.yml up -d - -# # Change directory out and clean up the downloaded file.tgz -# cd .. && rm -f syft-file.tgz - -#!/bin/bash - -# Initialize default values #!/bin/bash # Initialize default values @@ -127,6 +15,26 @@ usage() { exit 1 } +# Function to check if a port is occupied +is_port_occupied() { + local port=$1 + if lsof -i:"$port" > /dev/null; then + return 0 + else + return 1 + fi +} + +# Function to validate Docker version pattern +is_valid_version() { + local version=$1 + if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-(alpha|beta|rc)\.[0-9]+)?$ ]]; then + return 0 + else + return 1 + fi +} + # Parse command line options while [[ "$#" -gt 0 ]]; do case "$1" in @@ -170,6 +78,18 @@ if [[ -z "$VERSION" || -z "$NODE_NAME" || -z "$NODE_TYPE" || -z "$PORT" ]]; then usage fi +# Validate Docker version pattern +if ! is_valid_version "$VERSION"; then + echo "Invalid version format. Expected format: X.Y.Z or X.Y.Z-suffix.N (e.g., 0.8.2 or 0.8.2-beta.6)" + exit 1 +fi + +# Check if the specified port is occupied +if is_port_occupied "$PORT"; then + echo "Port $PORT is already in use. Please choose a different port." + exit 1 +fi + # Build the Docker run command DOCKER_RUN_CMD="docker run --rm -d \ --name \"$NODE_NAME\" \