#!/bin/zsh # or use #!/usr/bin/env zsh # Function to display usage information usage() { echo "Usage: source $0 [version]" echo " environment: The environment to use (dev, prod, test, integration, bugfix)" echo " version : (Optional) Specific release version to deploy" echo " If not specified, uses 'latest' (except for dev environment)" } # Replace the existing check at the beginning of docker_env_switch.sh # Check if the script is sourced if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then # Script is being executed directly from terminal echo "Error: This script must be sourced, not executed directly." echo "Please run: source $0 [version]" exit 1 fi # If we reach here, script is being sourced (either by terminal or another script) # Check if an environment is provided if [ $# -eq 0 ]; then usage return 1 fi ENVIRONMENT=$1 VERSION=${2:-latest} # Default to latest if not specified # Set variables based on the environment case $ENVIRONMENT in dev) DOCKER_CONTEXT="default" COMPOSE_FILE="compose_dev.yaml" VERSION="latest" # Always use latest for dev ;; prod) DOCKER_CONTEXT="mxz536.stackhero-network.com" COMPOSE_FILE="compose_stackhero.yaml" ;; test) DOCKER_CONTEXT="test-environment" # Change to your actual test Docker context COMPOSE_FILE="compose_test.yaml" ;; integration) DOCKER_CONTEXT="integration-environment" # Change to your actual integration Docker context COMPOSE_FILE="compose_integration.yaml" ;; bugfix) DOCKER_CONTEXT="bugfix-environment" # Change to your actual bugfix Docker context COMPOSE_FILE="compose_bugfix.yaml" ;; *) echo "Invalid environment: $ENVIRONMENT" usage return 1 ;; esac # Set Docker account DOCKER_ACCOUNT="josakola" # Check if Docker context exists if ! docker context ls --format '{{.Name}}' | grep -q "^$DOCKER_CONTEXT$"; then echo "Warning: Docker context '$DOCKER_CONTEXT' does not exist." # Prompt user if they want to create the context if [[ "$DOCKER_CONTEXT" != "default" ]]; then echo "Do you want to set up this context now? (y/n): " read CREATE_CONTEXT if [[ "$CREATE_CONTEXT" == "y" || "$CREATE_CONTEXT" == "Y" ]]; then # You would add here the specific code to create each context type # For example, for remote contexts you might need SSH settings echo "Please specify the Docker host URL (e.g., ssh://user@remote_host or tcp://remote_host:2375):" read DOCKER_HOST docker context create "$DOCKER_CONTEXT" --docker "host=$DOCKER_HOST" if [ $? -ne 0 ]; then echo "Failed to create Docker context. Please create it manually." return 1 fi else echo "Using default context instead." DOCKER_CONTEXT="default" fi fi fi # Check if compose file exists if [ ! -f "$COMPOSE_FILE" ]; then echo "Warning: Compose file '$COMPOSE_FILE' does not exist." echo "Do you want to create it based on compose_dev.yaml? (y/n): " read CREATE_FILE if [[ "$CREATE_FILE" == "y" || "$CREATE_FILE" == "Y" ]]; then # Create new compose file based on compose_dev.yaml with version variables sed 's/\(image: josakola\/[^:]*\):latest/\1:${EVEAI_VERSION:-latest}/g' compose_dev.yaml > "$COMPOSE_FILE" echo "Created $COMPOSE_FILE with version placeholders." else echo "Cannot proceed without a valid compose file." return 1 fi fi # Switch Docker context echo "Switching to Docker context: $DOCKER_CONTEXT" docker context use $DOCKER_CONTEXT # Set environment variables export COMPOSE_FILE=$COMPOSE_FILE export EVEAI_VERSION=$VERSION export DOCKER_ACCOUNT=$DOCKER_ACCOUNT echo "Set COMPOSE_FILE to $COMPOSE_FILE" echo "Set EVEAI_VERSION to $VERSION" echo "Set DOCKER_ACCOUNT to $DOCKER_ACCOUNT" # Define aliases for common Docker commands alias docker-compose="docker compose -f $COMPOSE_FILE" alias dc="docker compose -f $COMPOSE_FILE" alias dcup="docker compose -f $COMPOSE_FILE up -d --remove-orphans" alias dcdown="docker compose -f $COMPOSE_FILE down" alias dcps="docker compose -f $COMPOSE_FILE ps" alias dclogs="docker compose -f $COMPOSE_FILE logs" alias dcpull="docker compose -f $COMPOSE_FILE pull" alias dcrefresh="docker compose -f $COMPOSE_FILE pull && docker compose -f $COMPOSE_FILE up -d --remove-orphans" echo "Docker environment switched to $ENVIRONMENT with version $VERSION" echo "You can now use 'docker-compose', 'dc', 'dcup', 'dcdown', 'dcps', 'dclogs', 'dcpull' or 'dcrefresh' commands"