diff --git a/devbox/cli/devbox b/devbox/cli/devbox index d8e52f7..582315f 100644 --- a/devbox/cli/devbox +++ b/devbox/cli/devbox @@ -22,6 +22,31 @@ lower() { echo "$1" | tr '[:upper:]' '[:lower:]' } +exit_with_message() { + local message="$1" + local code="${2:-1}" + echo + echo "============================================" + echo + echo "$message" >&2 + echo + echo "============================================" + echo + exit $code +} + +detect_os() { + if [[ "$OSTYPE" == "darwin"* ]]; then + echo "darwin" + elif [[ "$OSTYPE" == "linux-gnu"* ]]; then + echo "linux" + elif grep -qi microsoft /proc/version; then + echo "wsl2" + else + echo "unknown" + fi +} + # Add a key-value pair to the args array add_arg() { @@ -99,8 +124,7 @@ devbox_init_guidance() { # Ask user for product_id read -p "Enter your product_id: " product_id if [[ -z $product_id ]]; then - printf "Product ID is required.\n" - exit 1 + exit_with_message "Product ID is required, please provide a valid product ID." 1 fi fi @@ -118,8 +142,7 @@ devbox_init_guidance() { # Test the repository connection if ! git ls-remote "$use_custom_repository"; then - printf "Failed to connect to the repository. Please check your username and password.\n" - exit 1 + exit_with_message "[ERROR] Failed to connect to the repository. Please check your username and password." 1 fi printf "Repository connection successfully.\n" @@ -127,8 +150,7 @@ devbox_init_guidance() { add_arg "--use-custom-repository" "$use_custom_repository" ;; *) - printf "Invalid choice. Exiting.\n" - exit 1 + exit_with_message "Invalid choice. Please enter 1 or 2." 1 ;; esac } @@ -565,14 +587,13 @@ build_local_image() { --no-cache \ -t $devbox_full_image \ -f "$dockerfile_path" . 2>&1 | tee "$WORKING_HOME/build.log"; then - echo "ERROR: Image build failed: $WORKING_HOME/build.log" - exit 1 + + exit_with_message "[ERROR] Image build failed, please check the log file: $WORKING_HOME/build.log" 1 fi # Check if the image is built successfully if ! docker inspect $devbox_full_image | grep -q 'amd64'; then - echo "ERROR:" - exit 1 + exit_with_message "[ERROR] Image build failed, please check the log file: $WORKING_HOME/build.log" 1 fi } @@ -712,7 +733,13 @@ if true ; then sudo apt update sudo apt install python3.11 python3.11-venv -y if ! command -v python3.11 &>/dev/null; then - echo "ERROR: Python3.11 is failed to install." + echo + echo "============================================" + echo + echo "ERROR: Python3.11 is failed to install, please check the log." + echo + echo "============================================" + echo exit 1 fi @@ -823,7 +850,13 @@ docker exec -i "$DEVBOX_NAME" bash < /dev/null; then - echo "==> [BACKEND] Backend service is running with PID: \$backend_pid, if you want to restart, please stop it first or run devbox restart -e backend." + echo + echo "============================================================================================" + echo + echo " [WARNING] Backend service is running with PID: \$backend_pid, if you want to restart, please stop it first or run devbox restart -e backend." + echo + echo "============================================================================================" + echo exit 0 fi fi @@ -840,7 +873,13 @@ docker exec -i "$DEVBOX_NAME" bash < [BACKEND] Virtual environment activate: \$VIRTUAL_ENV" else - echo "==> [BACKEND] ERROR: The virtual environment cannot be startup \$VIRTUAL_ENV" + echo + echo "============================================" + echo + echo "[ERROR] The virtual environment cannot be startup \$VIRTUAL_ENV, please check the log for more information." + echo + echo "============================================" + echo exit 1 fi # Check if it's the first time by verifying if the backend dependencies have been installed - if [ ! -f ".backend_deps_installed" ]; then + if [ ! -f "/home/devbox/.backend_deps_installed" ]; then echo "==> [BACKEND] Install backend dependencies..." pip install -r /home/devbox/freeleaps/apps/requirements.txt - touch .backend_deps_installed + + touch /home/devbox/.backend_deps_installed echo "==> [BACKEND] Run backend service..." ./start_webapi.sh > /home/devbox/logs/backend.logs 2>&1 & else @@ -900,7 +946,15 @@ docker exec -i "$DEVBOX_NAME" bash < /dev/null; then - echo "==> [FRONTEND] Frontend service is running with PID: \$frontend_pid, if you want to restart, please stop it first or run devbox restart -e frontend." - exit 0 + echo + echo "============================================================================================" + echo + echo " [WARNING] Frontend service is running with PID: \$frontend_pid, if you want to restart, please stop it first or run devbox restart -e frontend." + echo + echo "============================================================================================" + echo + exit 1 fi fi @@ -981,7 +1041,14 @@ docker exec -i "$DEVBOX_NAME" bash < Auto detecting OS..." + OS="$(detect_os)" + echo "==> Detected OS: $OS" fi if [[ "$ARCH" != "auto" && "$ARCH" != "amd64" && "$ARCH" != "arm64" ]]; then - echo "ERROR: Unsupported architecture: $ARCH" - exit 1 + exit_with_message "[ERROR] Unsupported architecture: $ARCH, please check the architecture." 1 fi # Check ARCH match current device @@ -1158,7 +1228,7 @@ devbox_init_command() { else ARCH="amd64" fi - elif [[ "$ARCH" == "aarch64" ]]; then + elif [[ "$ARCH" == "aarch64" ]] || [[ "$ARCH" == "arm64" ]]; then ARCH="arm64" fi fi @@ -1180,41 +1250,36 @@ devbox_init_command() { # 3.1.1.install docker and check docker running # ------------------------------------------------------------------- if ! install_docker; then - echo "ERROR: Failed to install Docker or Docker service is not running." - exit 1 + exit_with_message "[ERROR] Failed to install Docker or Docker service is not running. Please install Docker and start Docker service." 1 fi if ! check_docker_running; then - echo "ERROR: Docker service is not running." - exit 1 + exit_with_message "[ERROR] Docker service is not running. Please start Docker service." 1 fi sudo usermod -aG docker $USER sudo apt-get update -y sudo apt-get install docker-compose -y - fi + # 3.2 Check disk space local free_space_kb free_space_kb="$(df -Pk "$HOME" | awk 'END{print $4}')" # 若无法获取或小于 10GB (10485760 KB),报错 if [[ -z "$free_space_kb" || $free_space_kb -lt 10485760 ]]; then - echo "ERROR: Insufficient disk space (need >10GB)." - exit 1 + exit_with_message "[ERROR] Insufficient disk space (need >10GB). Please free up some space." 1 fi # 3.3 WORKING_HOME Check if ! mkdir -p "$WORKING_HOME" 2>/dev/null; then - echo "ERROR: Can't create or write to WORKING_HOME: $WORKING_HOME" - exit 1 + exit_with_message "[ERROR] Can't create or write to WORKING_HOME: $WORKING_HOME, please check the path." 1 fi # 3.4 Network to docker.com(sample:ping docker.com) if ! ping -c 1 docker.com &>/dev/null; then - echo "ERROR: Network unreachable." - exit 1 + exit_with_message "[ERROR] Network to Docker.com is not available. Please check your network connection." 1 fi # 3.5 Check if the user has permission to write to WORKING_HOME @@ -1225,11 +1290,9 @@ devbox_init_command() { FORCE_INIT=true ;; * ) - echo "Aborting initialization." - exit 1 + exit_with_message "[ERROR]DevBox instance already exists. Use --force to remove it." 1 ;; esac - fi # Check if any component is running on the host when force init is not set @@ -1238,19 +1301,65 @@ devbox_init_command() { components_to_check=("devbox" "freeleaps2-gitea" "freeleaps2-mongodb" "freeleaps2-rabbitmq" "freeleaps2-redis" "devsvc" "notification" "content" "central_storage" "chat" "authentication") for comp in "${components_to_check[@]}"; do if echo "$running_containers" | grep -qx "$comp"; then - read -p "Container '$comp' is already running. Do you want to force remove it? (y/N): " ans + read -p "Container '$comp' is already running. Do you want to force update it? (y/N): " ans case "$ans" in [Yy]* ) FORCE_INIT=true + break ;; * ) - echo "ERROR: Container '$comp' is already running. Use --force to override." - exit 1 + exit_with_message "[ERROR] Container '$comp' is already running. Use --force to override." 1 ;; esac fi - done + done fi + + + echo "Current OS is $OS" + # Check if Installed the net-tools netstat under MacOS, WSL2 and Linux OS + if [[ "$OS" == "darwin" || "$OS" == "wsl2" || "$OS" == "linux" ]]; then + if ! command -v netstat &>/dev/null; then + echo "==> Installing net-tools package..." + if [[ "$OS" == "darwin" ]]; then + brew install net-tools + elif [[ "$OS" == "wsl2" ]]; then + sudo apt-get update -y + sudo apt-get install net-tools -y + elif [[ "$OS" == "linux" ]]; then + sudo apt-get update -y + sudo apt-get install net-tools -y + else + exit_with_message "[ERROR] Failed install net-tools package on OS: $OS, please install it manually." 1 + fi + else + echo "==> net-tools package already installed." + fi + fi + + echo "==> Checking if the ports are in use..." + # Check if the gittea, mongodb, rabbitmq, redis ports are in use + if netstat -tuln | grep -q ":3000"; then + exit_with_message "[ERROR] gitea port 3000 is already in use, please stop the service." 1 + fi + + if netstat -tuln | grep -q ":27017"; then + exit_with_message "[ERROR] mongodb port 27017 is already in use, please stop the service." 1 + fi + + if netstat -tuln | grep -q ":5672"; then + exit_with_message "[ERROR] rabbitmq port 5672 is already in use, please stop the service." 1 + fi + + if netstat -tuln | grep -q ":15672"; then + exit_with_message "[ERROR] rabbitmq port 15672 is already in use, please stop the service." 1 + fi + + if netstat -tuln | grep -q ":6379"; then + exit_with_message "[ERROR] redis port 6379 is already in use, please stop the service." 1 + fi + + echo "==> Checking if the ports are in use... Done." local devbox_full_image="${DEVBOX_REPO}/${DEVBOX_IMAGE}:${DEVBOX_TAG}" @@ -1263,8 +1372,7 @@ devbox_init_command() { docker pull "$devbox_full_image" fi else - echo "ERROR: DevBox image repository, name, or tag is not specified." - exit 1 + exit_with_message "[ERROR] DevBox image repository, name, or tag is not specified, please check the parameters." 1 fi # If container with same name exists, remove it @@ -1281,25 +1389,10 @@ devbox_init_command() { rm -f "$WORKING_HOME/.backend.pid" rm -f "$WORKING_HOME/.frontend.pid" else - echo "ERROR: Container named $DEVBOX_NAME already exists. Use --force to remove it." - exit 1 + exit_with_message "[ERROR] Container named $DEVBOX_NAME already exists. Use --force to remove it." 1 fi fi - # If force init is set, remove the existing container - if [[ -n "$FORCE_INIT" ]]; then - echo "==> Removing existing container named $DEVBOX_NAME ..." - docker stop "$DEVBOX_NAME" &>/dev/null || true - docker rm "$DEVBOX_NAME" &>/dev/null || true - - # Remove .devbox-instance file - rm -f "$WORKING_HOME/.devbox-instance" - - # Remove .backend.pid .frontend.pid - rm -f "$WORKING_HOME/.backend.pid" - rm -f "$WORKING_HOME/.frontend.pid" - fi - # Create Docker network for DevBox container. TODO: if the network need to be configured in the docker-compose file add some logic to load it from the file DEVBOX_FREELEAPS2_NETWORK="devbox_freeleaps2-network" @@ -1312,6 +1405,13 @@ devbox_init_command() { echo "==> Docker network devbox_freeleaps2-network already exists." fi + # Check if use custom repository + if [[ -n "$USE_CUSTOM_REPOSITORY" ]]; then + echo "==> [INIT] Using custom repository." + elif [[ -z "$FREELEAPS_USERNAME" || -z "$FREELEAPS_PASSWORD" ]]; then + exit_with_message "[ERROR] Username or password is missing. Please provide a valid username and password for freeleaps.com repository." 1 + fi + echo '==> [INIT] Starting DevBox container...' # Create and start DevBox container @@ -1339,14 +1439,7 @@ devbox_init_command() { echo "$DEVBOX_BACKEND_PORT" > "$WORKING_HOME/.devbox-backend-port" - # Check if use custom repository - if [[ -n "$USE_CUSTOM_REPOSITORY" ]]; then - echo "==> [INIT] Using custom repository." - elif [[ -z "$FREELEAPS_USERNAME" || -z "$FREELEAPS_PASSWORD" ]]; then - echo "Warining: Username and password are required to pull freeleaps.com code." - echo "==> [INIT] DevBox environment initialization completed." - exit 1 - fi + DOVBOX_CLI_DIR=$(pwd) @@ -1359,12 +1452,9 @@ devbox_init_command() { # Test if the user can access the freeleaps.com repository echo "==> Testing access to freeleaps.com repository..." if ! git ls-remote "https://$ENCODEING_FREELEAPS_USERNAME:$ENCODEING_FREELEAPS_PASSWORD@freeleaps.com:3443/products/freeleaps.git" &>/dev/null; then - echo "ERROR: Failed to access freeleaps.com repository. Please check your username and password." - echo "==> [INIT] DevBox environment initialization completed successfully, but access to the freeleaps.com repository failed." - exit 1 + exit_with_message "[ERROR] Failed to access freeleaps.com repository. Please check your username and password." 1 fi - FREELEAPS_DIR="$WORKING_HOME/freeleaps" FRONTEND_GIT_URL="https://$ENCODEING_FREELEAPS_USERNAME:$ENCODEING_FREELEAPS_PASSWORD@freeleaps.com:3443/products/freeleaps.git" # Check if freeleaps2-frontend exists, if not git clone it @@ -1395,17 +1485,14 @@ devbox_init_command() { else if ! echo "$USE_CUSTOM_REPOSITORY" | grep -Eq '^(https:\/\/|git@|git:\/\/|file:\/\/\/)[^ ]+\.git$'; then - echo "ERROR: Invalid custom repository URL. Please provide a valid URL." - exit 1 + exit_with_message "[ERROR] Invalid custom repository URL. Please provide a valid URL." 1 fi # Check if the custom repository is a git repository # Test if the user can access the custom repository echo "==> Testing access to custom repository..." if ! git ls-remote "$USE_CUSTOM_REPOSITORY" &>/dev/null; then - echo "ERROR: Failed to access custom repository. Please check the repository URL." - echo "==> [INIT] DevBox environment initialization completed successfully, but access to the custom repository failed." - exit 1 + exit_with_message "[ERROR] Failed to access custom repository. Please check the repository URL." 1 fi ECHO_USE_CUSTOM_REPOSITORY=$(echo "$USE_CUSTOM_REPOSITORY" | sed 's/\(https:\/\/[^:]*\):[^@]*@/\1:****@/') @@ -1459,21 +1546,39 @@ if [[ -n "$USE_CUSTOM_REPOSITORY" ]]; then echo echo "===========================================================" + echo echo "==> [INIT] Custom repository initialization completed." echo "==> Custom repository is located at: ${WORKING_HOME}/${CUSTOM_FOLDER_NAME}" echo "==> Custom repository URL: $ECHO_USE_CUSTOM_REPOSITORY" echo "==> Custom repository is ready for use." + echo echo "===========================================================" echo exit 0 fi +# Check if docker-compose command exists +echo "==> Cehck if docker-compose command exists" +# Check if docker-compose is installed +local DC_CMD +if command -v docker-compose >/dev/null 2>&1; then + DC_CMD="docker-compose" +# 如果没有找到 docker-compose,再检查 docker compose(v2 插件) +elif docker compose version >/dev/null 2>&1; then + DC_CMD="docker compose" +else + DC_CMD="" + echo "ERROR: docker-compose is not installed." +fi +if [[ "$DC_CMD" == "" ]]; then + exit_with_message "[ERROR] Please install docker-compose or docker compose (v2) and try again." 1 +fi + # If USE_LOCAL_COMPONENT is true, then use local components if [[ $USE_LOCAL_COMPONENT_VAL == true ]]; then - echo ' ===> Using local components for Freeleaps services.' - + export DEVSVC_IMAGE_TAG="$component_tag" export CONTENT_IMAGE_TAG="$component_tag" export CENTRAL_STORAGE_IMAGE_TAG="$component_tag" @@ -1483,8 +1588,7 @@ if [[ $USE_LOCAL_COMPONENT_VAL == true ]]; then # Check if gitea_data_backup.tar.gz exists at current script directory, if not exit if [[ ! -f "gitea_data_backup.tar.gz" ]]; then - echo "ERROR: gitea_data_backup.tar.gz not found. Please make sure it exists in the current directory." - exit 1 + exit_with_message "[ERROR] gitea_data_backup.tar.gz not found. Please make sure it exists in the current directory." 1 fi # Sudo force sudo tar -xzvf gitea_data_backup.tar.gz @@ -1492,8 +1596,7 @@ if [[ $USE_LOCAL_COMPONENT_VAL == true ]]; then # Check if data/git, data/gitea, data/ssh directories exist after extracting gitea_data_backup.tar.gz if [[ ! -d "data/git" || ! -d "data/gitea" || ! -d "data/ssh" ]]; then - echo "ERROR: Failed to extract gitea data backup." - exit 1 + exit_with_message "[ERROR] Failed to extract gitea data backup, please check the backup file." 1 fi # Echo OWNER_GROUP @@ -1518,8 +1621,7 @@ if [[ $USE_LOCAL_COMPONENT_VAL == true ]]; then # Check if gitea data directories exist in the gitea container if [[ ! -d "${GITEA_HOST_DIR}/gitea" ]]; then - echo "ERROR: Failed to copy gitea data." - exit 1 + exit_with_message "[ERROR] Failed to copy gitea data, please check the data directories." 1 fi @@ -1534,7 +1636,7 @@ if [[ $USE_LOCAL_COMPONENT_VAL == true ]]; then # Start Gitea, MongoDB, RabbitMQ and other components containers echo "===> start Gitea, MongoDB, RabbitMQ and other components containers" - docker-compose -f docker-compose.yaml up -d mongodb rabbitmq gitea redis "${start_components[@]}" + $DC_CMD -f docker-compose.yaml up -d mongodb rabbitmq gitea redis "${start_components[@]}" gitea_container_id=$(docker ps --no-trunc -a --filter "name=^freeleaps2-gitea$" --format "{{.ID}}") echo "$gitea_container_id" > "$WORKING_HOME/.gitea-instance" @@ -1567,8 +1669,7 @@ if [[ $USE_LOCAL_COMPONENT_VAL == true ]]; then # Check all components are started for component in "${start_components[@]}"; do if [[ -z "$(docker ps -a --format '{{.Names}}' | grep "^$component\$")" ]]; then - echo "ERROR: Failed to start $component container." - exit 1 + exit_with_message "[ERROR] Failed to start $component container. Please check the logs for more information." 1 fi done else @@ -1576,7 +1677,7 @@ else echo ' ===> Using online components for Freeleaps services.' echo '============================================' # Start Gitea, MongoDB, RabbitMQ containers - docker-compose -f docker-compose.yaml up -d mongodb rabbitmq redis + $DC_CMD -f docker-compose.yaml up -d mongodb rabbitmq redis # Save MongoDB and RabbitMQ container ids to .mongodb-instance and .rabbitmq-instance mongo_container_id=$(docker ps -a --format '{{.Names}}' | grep "^freeleaps2-mongodb\$") @@ -1649,10 +1750,8 @@ EOF echo " Frontend PID: $WORKING_HOME/.frontend.pid" echo "===========================================================" echo - } - # :command.function devbox_deinit_command() { @@ -1746,7 +1845,7 @@ devbox_deinit_command() { if [[ "$CLEAR_LOGS" == "true" ]]; then echo "==> Clearing logs in $WORKING_HOME/logs..." if [[ -d "$WORKING_HOME/logs" ]]; then - sudo chown -R $(whoami):$(whoami) "$WORKING_HOME/logs" + sudo chown -R $(whoami) "$WORKING_HOME/logs" rm -rf "$WORKING_HOME/logs" 2>/dev/null || true mkdir -p "$WORKING_HOME/logs" 2>/dev/null || true fi @@ -1757,7 +1856,7 @@ devbox_deinit_command() { # Clear the source repository if [[ "$CLEAR_REPO" == "true" && -d "$WORKING_HOME/freeleaps" ]]; then echo "==> Deleting source repository at $WORKING_HOME/freeleaps" - sudo chown -R $(whoami):$(whoami) "$WORKING_HOME/freeleaps" + sudo chown -R $(whoami) "$WORKING_HOME/freeleaps" rm -rf "$WORKING_HOME/freeleaps" 2>/dev/null || true rmdir "$WORKING_HOME/freeleaps" 2>/dev/null || true else @@ -1783,7 +1882,7 @@ devbox_deinit_command() { # Remove the working home directory echo "==> Removing working home directory: $WORKING_HOME" if [[ -d "$WORKING_HOME" ]]; then - sudo chown -R $(whoami):$(whoami) "$WORKING_HOME" + sudo chown -R $(whoami) "$WORKING_HOME" rm -rf "$WORKING_HOME" 2>/dev/null || true rmdir "$WORKING_HOME" 2>/dev/null || true fi @@ -1830,8 +1929,7 @@ devbox_start_command() { # Check if the DevBox container is running local devbox_container_id_file_path="${WORKING_HOME}/.devbox-instance" if [[ ! -f "$devbox_container_id_file_path" ]]; then - echo "ERROR: DevBox container is not running. Please run 'devbox init' first." - exit 1 + exit_with_message "[ERROR] DevBox container is not running. Please run 'devbox init' first." 1 fi local devbox_container_id=$(cat "$devbox_container_id_file_path") @@ -1864,8 +1962,7 @@ devbox_start_command() { COMPONENTS=("mongodb" "rabbitmq" "devbox") else if [[ "$COMPONENT" == "devsvc" || "$COMPONENT" == "notification" || "$COMPONENT" == "content" || "$COMPONENT" == "central_storage" || "$COMPONENT" == "chat" || "$COMPONENT" == "authentication" ]]; then - echo "ERROR: Remote component $COMPONENT cannot be restarted." - exit 1 + exit_with_message "ERROR: Remote component $COMPONENT cannot be restarted, please use local components." 1 fi COMPONENTS=("$COMPONENT") @@ -1897,8 +1994,7 @@ devbox_start_command() { fi ;; *) - echo "ERROR: Unknown component: $comp" - exit 1 + exit_with_message "[ERROR] Unknown component: $comp, please check the component name." 1 ;; esac done @@ -1927,12 +2023,11 @@ devbox_start_command() { compile_frontend_service fi else - echo "ERROR: DevBox container is not running." - exit 1 + exit_with_message "[ERROR] DevBox container is not running, please run 'devbox init' first." 1 fi fi - echo "==> DevBox services started successfully." + exit_with_message "[INFO] DevBox services started successfully." 0 } # :command.function @@ -1962,8 +2057,7 @@ devbox_stop_command() { # If the DevBox container is not running, exit if ! docker ps --no-trunc --format '{{.ID}}' | grep -q "^${devbox_container_id}\$"; then - echo "==> DevBox container is not running." - exit 1 + exit_with_message "[ERROR] DevBox container is not running, please run 'devbox init' first." 1 fi # Check if STOP_BACKEND is true, stop the backend service @@ -1997,9 +2091,17 @@ devbox_stop_command() { stoped_freeleaps_service_names+=("backend") fi - echo "==> Stopped Freeleaps services: ${stoped_freeleaps_service_names[@]} successfully." - exit 0 + # Combine the stoped_freeleaps_service_names array to a string with "and" if there are more than one service + if [[ "${#stoped_freeleaps_service_names[@]}" -gt 1 ]]; then + stoped_freeleaps_service_names="frontend and backend" + else + stoped_freeleaps_service_names="${stoped_freeleaps_service_names[0]}" + fi + + + exit_message="[INFO] Stopped Freeleaps $stoped_freeleaps_service_names services successfully." + exit_with_message "$exit_message" 0 fi @@ -2046,12 +2148,13 @@ devbox_stop_command() { fi ;; *) - echo "ERROR: Unknown component: $comp" - exit 1 + exit_with_message "[ERROR] Unknown component: $comp, please check the component name." 1 ;; esac done - echo "==> All conponent services stopped successfully." + + exit_with_message "[INFO] Stopped Freeleaps services successfully." 0 + } # :command.function @@ -2063,16 +2166,14 @@ devbox_status_command() { # Check if .devbox-instance file exists if [[ ! -f "${WORKING_HOME}/.devbox-instance" ]]; then - echo "==> DevBox container is not running." - exit 1 + exit_with_message "[ERROR] DevBox container is not running. Please run 'devbox init' first." 1 fi local devbox_container_id=$(cat "${WORKING_HOME}/.devbox-instance") # If the DevBox container devbox_container_id is not running, exit if ! docker ps --no-trunc --format '{{.ID}}' | grep -q "^${devbox_container_id}\$"; then - echo "==> DevBox container is not running." - exit 1 + exit_with_message "[ERROR] DevBox container is not running. Please run 'devbox init' first." 1 fi # If no component is specified, check all components @@ -2146,8 +2247,7 @@ devbox_status_command() { ;; *) - echo "ERROR: Unknown component: $comp" - exit 1 + exit_with_message "[ERROR] Unknown component: $comp, please check the component name." 1 ;; esac done @@ -2200,19 +2300,16 @@ devbox_restart_command() { compile_frontend_service fi else - echo "ERROR: DevBox container is not running." - exit 1 + exit_with_message "[ERROR] DevBox container is not running." 1 fi - echo "==> Freeleaps $restart_services services restarted successfully." - exit 0 + exit_with_message "[INFO] Freeleaps $restart_services services restarted successfully." 0 fi # Check devbox container file path local devbox_container_id_file_path="${WORKING_HOME}/.devbox-instance" if [[ ! -f "$devbox_container_id_file_path" ]]; then - echo "ERROR: DevBox container is not running. Please run 'devbox init' first." - exit 1 + exit_with_message "[ERROR] DevBox container is not running. Please run 'devbox init' first." 1 fi local devbox_container_id=$(cat "$devbox_container_id_file_path") @@ -2232,8 +2329,7 @@ devbox_restart_command() { COMPONENTS=("mongodb" "rabbitmq" "devbox") else if [[ "$COMPONENT" == "devsvc" || "$COMPONENT" == "notification" || "$COMPONENT" == "content" || "$COMPONENT" == "central_storage" || "$COMPONENT" == "chat" || "$COMPONENT" == "authentication" ]]; then - echo "ERROR: Remote component $COMPONENT cannot be restarted." - exit 1 + exit_with_message "[ERROR] Remote component $COMPONENT cannot be restarted, please use local components." 1 fi COMPONENTS=("$COMPONENT") @@ -2270,8 +2366,7 @@ devbox_restart_command() { fi ;; *) - echo "ERROR: Unknown component: $comp" - exit 1 + exit_with_message "[ERROR] Unknown component: $comp, please check the component name." 1 ;; esac done @@ -2290,8 +2385,7 @@ devbox_restart_command() { fi ;; *) - echo "ERROR: Unknown component: $comp" - exit 1 + exit_with_message "[ERROR] Unknown component: $comp, please check the component name." 1 ;; esac done