From 876a74364b752ba73e47a3eadca338fc5d7ab88f Mon Sep 17 00:00:00 2001 From: timqiu <9145422+cocoonwind@user.noreply.gitee.com> Date: Thu, 20 Mar 2025 18:16:49 +0800 Subject: [PATCH] Update for redis connection issue and gitea data access issue and frontend code change issue --- devbox/cli/devbox | 971 ++++++++++++++++++++------------- devbox/cli/docker-compose.yaml | 2 +- 2 files changed, 590 insertions(+), 383 deletions(-) diff --git a/devbox/cli/devbox b/devbox/cli/devbox index 8deaaed..77922be 100644 --- a/devbox/cli/devbox +++ b/devbox/cli/devbox @@ -1,6 +1,16 @@ #!/usr/bin/env bash # Modifying it manually is not recommended +log_info() { + echo "[INFO] $(date '+%Y-%m-%d %H:%M:%S') $*" +} +log_warn() { + echo "[WARN] $(date '+%Y-%m-%d %H:%M:%S') $*" +} +log_error() { + echo "[ERROR] $(date '+%Y-%m-%d %H:%M:%S') $*" >&2 +} + # :wrapper.bash3_bouncer if [[ "${BASH_VERSINFO:-0}" -lt 3 ]]; then printf "bash version 3 or higher is required\n" >&2 @@ -26,11 +36,15 @@ exit_with_message() { local message="$1" local code="${2:-1}" echo - echo "============================================" + echo "============================================================================" echo - echo "$message" >&2 + if [[ $code -eq 0 ]]; then + echo "[INFO] $message" + else + echo "[ERROR] $message" >&2 + fi echo - echo "============================================" + echo "============================================================================" echo exit $code } @@ -84,7 +98,7 @@ devbox_init_guidance() { printf " 2. Use custom repository\n" read -p "Enter your choice (1 or 2): " choice else - echo "Your will start with init product $1 develop environment" + log_info "Your will start with init product $1 develop environment" product_id=$1 case "$product_id" in freeleaps) @@ -142,7 +156,7 @@ devbox_init_guidance() { # Test the repository connection if ! git ls-remote "$use_custom_repository"; then - exit_with_message "[ERROR] Failed to connect to the repository. Please check your username and password." 1 + exit_with_message " Failed to connect to the repository. Please check your username and password." 1 fi printf "Repository connection successfully.\n" @@ -400,7 +414,7 @@ inspect_args() { if [ ${#args_keys[@]} -gt 0 ]; then # 利用 printf 和 sort 对键进行排序 sorted_keys=$(printf "%s\n" "${args_keys[@]}" | sort) - echo "args:" + log_info "args:" for key in $sorted_keys; do value="" # Find the value based on the key @@ -413,14 +427,14 @@ inspect_args() { done else - echo "args: none" + log_info "args: none" fi # Check and output the simulated deps associative array (using deps_keys and deps_values) if [ ${#deps_keys[@]} -gt 0 ]; then sorted_keys=$(printf "%s\n" "${deps_keys[@]}" | sort) echo - echo "deps:" + log_info "deps:" for key in $sorted_keys; do value="" for i in `seq 0 $((${#deps_keys[@]} - 1))`; do @@ -429,7 +443,7 @@ inspect_args() { break fi done - echo "- \$deps[$key] = $value" + log_info "- \$deps[$key] = $value" done fi @@ -437,21 +451,21 @@ inspect_args() { if [ ${#env_var_names[@]} -gt 0 ]; then sorted_names=$(printf "%s\n" "${env_var_names[@]}" | sort) echo - echo "environment variables:" + log_info "environment variables:" for name in $sorted_names; do # Look up the value based on the name - echo "- \$${name} = ${!name:-}" + log_info "- \$${name} = ${!name:-}" done fi } install_docker() { - echo "==> Installing Docker..." + log_info "==> Installing Docker..." # Check if Docker is already installed if command -v docker &>/dev/null; then - echo "==> Docker is already installed." + log_info "==> Docker is already installed." return 0 fi @@ -522,60 +536,60 @@ install_docker() { return 0 fi - echo "ERROR: Unable to install Docker automatically. Please install Docker manually." + log_info "ERROR: Unable to install Docker automatically. Please install Docker manually." return 1 } check_docker_running() { - echo "==> Checking if Docker service is running..." + log_info "==> Checking if Docker service is running..." # if Docker CLI is installed and Docker daemon is running if docker info >/dev/null 2>&1; then - echo "==> Docker is running." + log_info "==> Docker is running." return 0 fi # if running on WSL, check for Docker socket if grep -qi microsoft /proc/version; then - echo "[INFO] Detected WSL environment. Verifying /var/run/docker.sock..." + log_info " Detected WSL environment. Verifying /var/run/docker.sock..." if [ -S /var/run/docker.sock ]; then - echo "==> Docker socket found. Docker should be available via Docker Desktop." + log_info "==> Docker socket found. Docker should be available via Docker Desktop." return 0 else - echo "[ERROR] Docker socket not found in WSL environment." + log_error " Docker socket not found in WSL environment." return 1 fi fi - echo "==> Docker is not running. Attempting to start it..." + log_info "==> Docker is not running. Attempting to start it..." # Start Docker service using systemctl or service command if command -v systemctl &>/dev/null; then if systemctl list-units --type=service | grep -q "docker.service"; then - echo "==> Starting Docker with systemctl..." - sudo systemctl start docker && echo "==> Docker started successfully." && return 0 + log_info "==> Starting Docker with systemctl..." + sudo systemctl start docker && log_info "==> Docker started successfully." && return 0 fi fi if command -v service &>/dev/null; then if service --status-all | grep -q "docker"; then - echo "==> Starting Docker with service..." - sudo service docker start && echo "==> Docker started successfully." && return 0 + log_info "==> Starting Docker with service..." + sudo service docker start && log_info "==> Docker started successfully." && return 0 fi fi if command -v snap &>/dev/null && snap list | grep -q "docker"; then - echo "==> Starting Docker with snap..." - sudo snap start docker && echo "==> Docker started successfully." && return 0 + log_info "==> Starting Docker with snap..." + sudo snap start docker && log_info "==> Docker started successfully." && return 0 fi - echo "ERROR: Unable to start Docker automatically. Please start it manually." + log_error " Unable to start Docker automatically. Please start it manually." return 1 } build_local_image() { local dockerfile_path="$1" - echo "==> [Build] use Dockerfile: $(grep '^FROM' "$dockerfile_path")" + log_info "==> [Build] use Dockerfile: $(grep '^FROM' "$dockerfile_path")" # Check if the image already exists docker rmi -f $devbox_full_image 2>/dev/null || true @@ -588,12 +602,12 @@ build_local_image() { -t $devbox_full_image \ -f "$dockerfile_path" . 2>&1 | tee "$WORKING_HOME/build.log"; then - exit_with_message "[ERROR] Image build failed, please check the log file: $WORKING_HOME/build.log" 1 + exit_with_message " 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 - exit_with_message "[ERROR] Image build failed, please check the log file: $WORKING_HOME/build.log" 1 + exit_with_message " Image build failed, please check the log file: $WORKING_HOME/build.log" 1 fi } @@ -623,7 +637,7 @@ get_port() { # Build the local image build_local_image() { local dockerfile_path="$1" - echo "==> [BUILD] Building local image..." + log_info "==> [BUILD] Building local image..." docker buildx build \ --platform linux/amd64 \ --build-arg BUILDARCH="x86-64-v3" \ @@ -638,7 +652,7 @@ build_local_image() { init_compile_env() { docker exec -i "$DEVBOX_NAME" bash < [INIT] Starting DevBox initialization..." + echo "[INIT] \$(date '+%Y-%m-%d %H:%M:%S') Starting DevBox initialization..." # Export environment variables export WORKING_HOME="${WORKING_HOME}" @@ -648,11 +662,11 @@ docker exec -i "$DEVBOX_NAME" bash < [INIT] USE_LOCAL_COMPONENT_VAL: ${USE_LOCAL_COMPONENT_VAL}" + echo "[INIT] \$(date '+%Y-%m-%d %H:%M:%S') USE_LOCAL_COMPONENT_VAL: ${USE_LOCAL_COMPONENT_VAL}" # Check if the working home directory exists, if not, create it if [[ "$USE_LOCAL_COMPONENT_VAL" == "true" ]]; then # Local component environment variables - echo "==> [INIT] Use local component dev environment." + echo "[INIT] \$(date '+%Y-%m-%d %H:%M:%S') Use local component dev environment." cat << 'EOFinner' > /home/devbox/freeleaps/apps/.env export MONGODB_NAME=freeleaps2 export MONGODB_URI=mongodb://freeleaps2-mongodb:27017/ @@ -662,6 +676,7 @@ if [[ "$USE_LOCAL_COMPONENT_VAL" == "true" ]]; then export RABBITMQ_HOST=freeleaps2-rabbitmq export RABBITMQ_PORT=5672 export FREELEAPS_ENV=dev + export REDIS_URL=redis://freeleaps2-redis:6379/0 export STRIPE_API_KEY=sk_test_51Ogsw5B0IyqaSJBrwczlr820jnmvA1qQQGoLZ2XxOsIzikpmXo4pRLjw4XVMTEBR8DdVTYySiAv1XX53Zv5xqynF00GfMqttFd export STRIPE_WEBHOOK_SECRET=whsec_S6ZWjSAdR5Cpsn2USH6ZRBqbdBIENjTC export STRIPE_ACCOUNT_WEBHOOK_SECRET=whsec_PgPnkWGhEUiQfnV8aIb5Wmruz7XETJLm @@ -679,7 +694,7 @@ if [[ "$USE_LOCAL_COMPONENT_VAL" == "true" ]]; then EOFinner else # Online component environment variables - echo "==> [INIT] Use online component dev environment." + echo "[INIT] \$(date '+%Y-%m-%d %H:%M:%S') Use online component dev environment." cat << 'EOFinner' > /home/devbox/freeleaps/apps/.env export MONGODB_NAME=freeleaps2 export MONGODB_PORT=27017 @@ -702,6 +717,7 @@ else export KAFKA_SERVER_URL='' export EMAIL_FROM=freeleaps@freeleaps.com export JWT_SECRET_KEY=ea84edf152976b2fcec12b78aa8e45bc26a5cf0ef61bf16f5c317ae33b3fd8b0 + export REDIS_URL='' EOFinner fi @@ -729,21 +745,21 @@ if true ; then ##################################### # Initialize the backend environment, including Python3.11 and venv ##################################### - echo "==> [INIT] Check Python3.11 environment..." + echo "[INIT] \$(date '+%Y-%m-%d %H:%M:%S') Check Python3.11 environment..." sudo apt update sudo apt install python3.11 python3.11-venv -y if ! command -v python3.11 &>/dev/null; then echo echo "============================================" echo - echo "ERROR: Python3.11 is failed to install, please check the log." + echo "[ERROR] Python3.11 is failed to install, please check the log." echo echo "============================================" echo exit 1 fi - echo "==> [INIT] Upgrade pip and ensurepip..." + echo " [INIT] Upgrade pip and ensurepip..." python3.11 -m ensurepip --upgrade python3.11 -m pip install --upgrade pip @@ -751,8 +767,8 @@ if true ; then pushd /home/devbox/freeleaps/apps > /dev/null if [ ! -d "venv_t" ]; then - echo "==> [INIT] Create Python3.11 virtual environment..." - python3.11 -m venv venv_t || { echo "ERROR: Python3.11 virtual environment creation failed."; exit 1; } + echo "[INIT] \$(date '+%Y-%m-%d %H:%M:%S') Create Python3.11 virtual environment..." + python3.11 -m venv venv_t || { echo "[ERROR] Python3.11 virtual environment creation failed."; exit 1; } sleep 5 fi popd > /dev/null @@ -760,7 +776,7 @@ if true ; then # Install pipreqs for generating requirements.txt python3.11 -m pip install pipreqs - echo "==> [INIT] Backend environment initialization completed." + echo "[INIT] \$(date '+%Y-%m-%d %H:%M:%S') Backend environment initialization completed." ##################################### # Initialize the frontend environment, including Node.js and npm @@ -777,16 +793,17 @@ if true ; then pnpm --version # 3️⃣ Clean up old dependencies + if [ -f "pnpm-lock.yaml" ]; then + mv pnpm-lock.yaml /tmp/pnpm-lock.yaml.bak + fi + rm -rf node_modules pnpm-lock.yaml # 4️⃣ Install dependencies (ensuring lockfile updates) pnpm install --no-frozen-lockfile - # 5️⃣ Build the project - npm run build - - # 6️⃣ Format the code (Optional) - npm run format + # 4️⃣ Build the frontend + pnpm run build echo "==> [INIT] Backend and frontend environment initialization completed." @@ -796,38 +813,37 @@ EOF } stop_backend_service() { - echo "==> [BACKEND] Stopping backend service..." + echo "[BACKEND] $(date '+%Y-%m-%d %H:%M:%S') Stopping backend service..." devbox_container_id_file_path="${WORKING_HOME}/.devbox-instance" DEVBOX_NAME=$(cat "$devbox_container_id_file_path") docker exec -i "$DEVBOX_NAME" bash < [BACKEND] Stopping backend service..." + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Stopping backend service..." if [ -f /home/devbox/.backend.pid ]; then BACKEND_PID=\$(cat /home/devbox/.backend.pid) - echo "==> [BACKEND] Killing backend service PID: \$BACKEND_PID" + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Killing backend service PID: \$BACKEND_PID" kill -9 \$BACKEND_PID rm -f /home/devbox/.backend.pid else - echo "==> [BACKEND] Backend service is not running." + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Backend service is not running." fi EOF } stop_frontend_service() { - echo "==> [FRONTEND] Stopping frontend service..." devbox_container_id_file_path="${WORKING_HOME}/.devbox-instance" DEVBOX_NAME=$(cat "$devbox_container_id_file_path") docker exec -i "$DEVBOX_NAME" bash < [FRONTEND] Stopping frontend service..." + echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Stopping frontend service..." if [ -f /home/devbox/.frontend.pid ]; then FRONTEND_PID=\$(cat /home/devbox/.frontend.pid) - echo "==> [FRONTEND] Killing frontend service PID: \$FRONTEND_PID" + echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Killing frontend service PID: \$FRONTEND_PID" kill -9 \$FRONTEND_PID rm -f /home/devbox/.frontend.pid else - echo "==> [FRONTEND] Frontend service is not running." + echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Frontend service is not running." fi EOF @@ -839,7 +855,7 @@ EOF ############################################### compile_backend_service() { -echo "==> [BACKEND] start backend service at home path $WORKING_HOME." +echo "[BACKEND] $(date '+%Y-%m-%d %H:%M:%S') Start backend service at home path $WORKING_HOME." devbox_container_id_file_path="${WORKING_HOME}/.devbox-instance" DEVBOX_NAME=$(cat "$devbox_container_id_file_path") @@ -847,33 +863,37 @@ DEVBOX_NAME=$(cat "$devbox_container_id_file_path") devbox_backend_port_file_path="${WORKING_HOME}/.devbox-backend-port" DEVBOX_BACKEND_PORT=$(cat "$devbox_backend_port_file_path") -echo "==> [BACKEND] start backend service from $DEVBOX_NAME." +echo "[BACKEND] $(date '+%Y-%m-%d %H:%M:%S') Start backend service from $DEVBOX_NAME." docker exec -i "$DEVBOX_NAME" bash < /dev/null; then - 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 + # Check if /home/devbox/.backend.pid exits + if [ -f /home/devbox/.backend.pid ]; then + backend_pid=\$(cat /home/devbox/.backend.pid) + if [ -n "\$backend_pid" ]; then + # Check if the backend service is running + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Backend service is running with PID: \$backend_pid" + if ps -p "\$backend_pid" > /dev/null; then + echo + echo "============================================================================================" + echo + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') [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 fi - echo "==> [BACKEND] Starting backend compilation and startup..." + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Starting backend compilation and startup..." pushd /home/devbox/freeleaps/apps > /dev/null # Record the git status baseline before compilation baseline_backend=\$(mktemp) + git config --global --add safe.directory /home/devbox/freeleaps git status -s > "\$baseline_backend" - echo "==> [BACKEND] Recorded baseline before compilation: \$baseline_backend" + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Recorded baseline before compilation: \$baseline_backend" # CHeck if the virtual environment is created @@ -881,27 +901,27 @@ docker exec -i "$DEVBOX_NAME" bash < [BACKEND] Virtual environment activate: \$VIRTUAL_ENV" + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Virtual environment activate: \$VIRTUAL_ENV" else echo echo "============================================" echo - echo "[ERROR] The virtual environment cannot be startup \$VIRTUAL_ENV, please check the log for more information." + echo "[BACKEND] [ERROR] The virtual environment cannot be startup \$VIRTUAL_ENV, please check the log for more information." echo echo "============================================" echo @@ -910,102 +930,147 @@ docker exec -i "$DEVBOX_NAME" bash < [BACKEND] Install backend dependencies..." + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Install backend dependencies..." pip install -r /home/devbox/freeleaps/apps/requirements.txt if ! pip show async_timeout; then - echo "==> [BACKEND] async_timeout is missing. Installing..." + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') async_timeout is missing. Installing..." pip install async_timeout fi - # Backup the requirements.txt file - cp /home/devbox/freeleaps/apps/requirements.txt /home/devbox/freeleaps/apps/requirements.txt.bak + # Generate /home/devbox/tmp/requirements.txt + mkdir -p /home/devbox/tmp + + ## Backup the requirements.txt file + cp /home/devbox/freeleaps/apps/requirements.txt /home/devbox/tmp/requirements.txt.bak - # Check by pipreqs to generate requirements.txt - echo - echo "1============================================================================" - echo - pip install --upgrade pipreqs - - echo - echo "2============================================================================" - echo - - pipreqs /home/devbox/freeleaps/apps --ignore venv_t --force --use-local --savepath /home/devbox/freeleaps/apps/requirements.txt - echo - echo "3============================================================================" - echo - - - # Check if requirements.txt and requirements.txt.bak are different - if ! diff /home/devbox/freeleaps/apps/requirements.txt /home/devbox/freeleaps/apps/requirements.txt.bak; then - echo "==> [BACKEND] requirements.txt has changed. Reinstalling dependencies..." - pip install -r /home/devbox/freeleaps/apps/requirements.txt - - rm /home/devbox/freeleaps/apps/requirements.txt.bak - - else - # Restore the requirements.txt file - mv /home/devbox/freeleaps/apps/requirements.txt.bak /home/devbox/freeleaps/apps/requirements.txt + ORIGINAL_REQ="/home/devbox/freeleaps/apps/requirements.txt" + NEW_REQ="/home/devbox/tmp/requirements.txt" + + # Check if /home/devbox/tmp/requirements.txt exists, if yes, remove it + if [ -f "/home/devbox/tmp/requirements.txt" ]; then + rm /home/devbox/tmp/requirements.txt fi + # Generate /home/devbox/tmp/requirements.txt + pipreqs /home/devbox/freeleaps/apps --ignore venv_t --force --use-local --savepath /home/devbox/tmp/requirements.txt + + if [ ! -f "\$ORIGINAL_REQ" ]; then + mv "\$NEW_REQ" "\$ORIGINAL_REQ" + else + IS_NEW_REQ_ADDED=0 + while IFS= read -r line; do + # Revome the version number from the line + pkg=\$(echo "\$line" | cut -d '=' -f 1 | tr -d ' ') + # Check if the package is already in the requirements.txt file + if ! grep -i -E "^\${pkg}([=]|$)" "\$ORIGINAL_REQ" > /dev/null; then + echo "\$line" >> "\$ORIGINAL_REQ" + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Added package: \${pkg}" + IS_NEW_REQ_ADDED=1 + else + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Package \${pkg} already exists in requirements.txt" + fi + done < "\$NEW_REQ" + fi + + + + if [ \$IS_NEW_REQ_ADDED -eq 1 ]; then + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Reinstalling dependencies..." + pip install -r /home/devbox/freeleaps/apps/requirements.txt + fi + # Undo update for /home/devbox/freeleaps/apps/requirements.txt + rm /home/devbox/freeleaps/apps/requirements.txt + mv /home/devbox/tmp/requirements.txt.bak /home/devbox/freeleaps/apps/requirements.txt + touch /home/devbox/.backend_deps_installed - echo "==> [BACKEND] Run backend service..." + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Run backend service..." ./start_webapi.sh > /home/devbox/logs/backend.logs 2>&1 & else - echo "==> [BACKEND] Backend dependencies already installed. Skipping installation." + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Backend dependencies already installed. Skipping installation." # Check if all dependencies are installed, if not, install them if ! pip check; then - echo "==> [BACKEND] Some dependencies are missing. Reinstalling..." + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Some dependencies are missing. Reinstalling..." pip install -r /home/devbox/freeleaps/apps/requirements.txt fi + # pip install async_timeout if not installed - if ! pip show async_timeout; then - echo "==> [BACKEND] async_timeout is missing. Installing..." - pip install async_timeout - fi - - ## Backup the requirements.txt file - cp /home/devbox/freeleaps/apps/requirements.txt /home/devbox/freeleaps/apps/requirements.txt.bak - - # Check by pipreqs to generate requirements.txt - pip install --upgrade pipreqs - pipreqs /home/devbox/freeleaps/apps --ignore venv_t --force --use-local --savepath /home/devbox/freeleaps/apps/requirements.txt - - # Check if requirements.txt and requirements.txt.bak are different - if ! diff /home/devbox/freeleaps/apps/requirements.txt /home/devbox/freeleaps/apps/requirements.txt.bak; then - echo "==> [BACKEND] requirements.txt has changed. Reinstalling dependencies..." - pip install -r /home/devbox/freeleaps/apps/requirements.txt - - rm /home/devbox/freeleaps/apps/requirements.txt.bak - - else - # Restore the requirements.txt file - mv /home/devbox/freeleaps/apps/requirements.txt.bak /home/devbox/freeleaps/apps/requirements.txt + if ! pip show async_timeout; then + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') async_timeout is missing. Installing..." + pip install async_timeout fi + + # Generate /home/devbox/tmp/requirements.txt + mkdir -p /home/devbox/tmp + + ## Backup the requirements.txt file + cp /home/devbox/freeleaps/apps/requirements.txt /home/devbox/tmp/requirements.txt.bak + + + ORIGINAL_REQ="/home/devbox/freeleaps/apps/requirements.txt" + NEW_REQ="/home/devbox/tmp/requirements.txt" + + # Check if /home/devbox/tmp/requirements.txt exists, if yes, remove it + if [ -f "/home/devbox/tmp/requirements.txt" ]; then + rm /home/devbox/tmp/requirements.txt + fi + + pipreqs /home/devbox/freeleaps/apps --ignore venv_t --force --use-local --savepath /home/devbox/tmp/requirements.txt + if [ ! -f "\$ORIGINAL_REQ" ]; then + mv "\$NEW_REQ" "\$ORIGINAL_REQ" + else + IS_NEW_REQ_ADDED=0 + while IFS= read -r line; do + # Revome the version number from the line + pkg=\$(echo "\$line" | cut -d '=' -f 1 | tr -d ' ') + # Check if the package is already in the requirements.txt file + if ! grep -i -E "^\${pkg}([=]|$)" "\$ORIGINAL_REQ" > /dev/null; then + echo "\$line" >> "\$ORIGINAL_REQ" + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Added package: \${pkg}" + IS_NEW_REQ_ADDED=1 + else + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Package \${pkg} already exists in requirements.txt" + fi + done < "\$NEW_REQ" + fi + + if [ \$IS_NEW_REQ_ADDED -eq 1 ]; then + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Reinstalling dependencies..." + pip install -r /home/devbox/freeleaps/apps/requirements.txt + fi + + # Undo update for /home/devbox/freeleaps/apps/requirements.txt + rm /home/devbox/freeleaps/apps/requirements.txt + mv /home/devbox/tmp/requirements.txt.bak /home/devbox/freeleaps/apps/requirements.txt + # Check if the backend service is already running SERVICE_API_ACCESS_PORT=\$(cat /home/devbox/.devbox-backend-port) uvicorn freeleaps.webapi.main:app --reload --host 0.0.0.0 --port \$SERVICE_API_ACCESS_PORT > /home/devbox/logs/backend.logs 2>&1 & fi + # Remove tempory file /home/devbox/tmp/requirements.txt if it exists + if [ -f "/home/devbox/tmp/requirements.txt" ]; then + rm /home/devbox/tmp/requirements.txt + fi + # Check the health of the backend service: poll to detect HTTP status MAX_ATTEMPTS=30 ATTEMPT=0 - echo "==> [BACKEND] Checking backend service startup..." + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Checking backend service startup..." while [ \$ATTEMPT -lt \$MAX_ATTEMPTS ]; do - echo "==> [BACKEND] backend url http://localhost:${DEVBOX_BACKEND_PORT}/docs" + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Backend url http://localhost:${DEVBOX_BACKEND_PORT}/docs" HTTP_CODE=\$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:${DEVBOX_BACKEND_PORT}/docs") if [ "\$HTTP_CODE" -eq 200 ]; then - echo "==> [BACKEND] Service started successfully (HTTP \$HTTP_CODE)" + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Service started successfully (HTTP \$HTTP_CODE)" # Get the backend service PID by checking the process port with netstat -tulnp | grep ${DEVBOX_BACKEND_PORT} BACKEND_PID=\$(netstat -tulnp 2>/dev/null | grep "${DEVBOX_BACKEND_PORT}" | awk '{print \$7}' | awk -F'/' '{print \$1}') echo "\$BACKEND_PID" > /home/devbox/.backend.pid break else - echo "==> [BACKEND] Waiting for backend service... Attempt \$((ATTEMPT+1)) with HTTP \$HTTP_CODE" + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Waiting for backend service... Attempt \$((ATTEMPT+1)) with HTTP \$HTTP_CODE" ATTEMPT=\$((ATTEMPT+1)) sleep 5 fi @@ -1015,7 +1080,7 @@ docker exec -i "$DEVBOX_NAME" bash < "\$current_backend" git config --global --add safe.directory /home/devbox/freeleaps - echo "==> [BACKEND] Checking git changes after compilation..." + git status -s > "\$current_backend" + + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Checking git changes after compilation..." while read -r line; do file=\$(echo "\$line" | awk '{print \$2}') if ! grep -q "[[:space:]]\${file}$" "\$baseline_backend"; then - echo "==> [BACKEND] Restore file \$file" + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Restore file \$file" git reset HEAD "\$file" git checkout -- "\$file" fi @@ -1039,61 +1105,87 @@ docker exec -i "$DEVBOX_NAME" bash < /dev/null - echo "==> [BACKEND] Backend compilation and startup completed." + echo "[BACKEND] \$(date '+%Y-%m-%d %H:%M:%S') Backend compilation and startup completed." EOF } - ############################################### # Frontend compilation and startup logic ############################################### compile_frontend_service() { -echo "==> [FRONTEND] start frontend service at home path $WORKING_HOME." +echo "[FRONTEND] $(date '+%Y-%m-%d %H:%M:%S') start frontend service at home path $WORKING_HOME." devbox_container_id_file_path="${WORKING_HOME}/.devbox-instance" +if [ ! -f "$devbox_container_id_file_path" ]; then + # Check if devbox container exists by checking the container name of devbox + if ! docker ps -a --format "{{.Names}}" | grep -q "devbox"; then + exit_with_message "DevBox container is not running. Please start the DevBox container first." 1 + fi +fi + DEVBOX_NAME=$(cat "$devbox_container_id_file_path") DEVBOX_FRONTEND_PORT=$(cat "$WORKING_HOME/.devbox-frontend-port") docker exec -i "$DEVBOX_NAME" bash < /dev/null; then - 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 + + # Check if /home/devbox/.frontend.pid exits + if [ -f /home/devbox/.frontend.pid ]; then + frontend_pid=\$(cat /home/devbox/.frontend.pid) + if [ -n "\$frontend_pid" ]; then + # Check if the frontend service is running + if ps -p "\$frontend_pid" > /dev/null; then + echo + echo "============================================================================================" + echo + echo "[FRONTEND] [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 fi - echo "==> [FRONTEND] Starting frontend compilation and startup..." + echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Starting frontend compilation and startup..." pushd /home/devbox/freeleaps/frontend > /dev/null # Record the git status baseline before compilation baseline_frontend=\$(mktemp) + git config --global --add safe.directory /home/devbox/freeleaps git status -s > "\$baseline_frontend" - echo "==> [FRONTEND] Recorded baseline before compilation: \$baseline_frontend" + echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Recorded baseline before compilation: \$baseline_frontend" # Check if the frontend service is already running according to the package.json and pnpm-lock.yaml files timestamps - if [[ ! -d "node_modules" || "package.json" -nt "node_modules" || "pnpm-lock.yaml" -nt "node_modules" ]]; then - echo "==> Installing/Updating frontend dependencies..." + + # Get the timestamps of the package.json and pnpm-lock.yaml files + lock_time=\$(stat -c "%Y" pnpm-lock.yaml) + modules_time=\$(stat -c "%Y" node_modules) + + # Calculate the time difference between the lock file and the modules file + time_diff=\$(( lock_time - modules_time )) + + # Set the threshold for the time difference + threshold=150 + + if [[ ! -d "node_modules" || "package.json" -nt "node_modules" || \$time_diff -gt \$threshold ]]; then + echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Installing/Updating frontend dependencies..." # Clean up old dependencies rm -rf node_modules + # Backup the pnpm-lock.yaml file + if [ -f "pnpm-lock.yaml" ]; then + mv pnpm-lock.yaml /tmp/pnpm-lock.yaml.bak + fi + # Install dependencies - pnpm install --prefer-offline || { + pnpm install --no-frozen-lockfile || { echo echo "============================================================================================" echo - echo " [ERROR] Failed to install dependencies. Please check the logs for more information." + echo "[FRONTEND] [ERROR] Failed to install dependencies. Please check the logs for more information." echo echo "============================================================================================" echo @@ -1101,18 +1193,45 @@ docker exec -i "$DEVBOX_NAME" bash < [FRONTEND] Start frontend service..." - nohup npm run dev > /home/devbox/logs/frontend.logs 2>&1 & + echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Start frontend service..." + nohup pnpm run dev > /home/devbox/logs/frontend.logs 2>&1 & # Check the health of the frontend service: poll to detect HTTP status MAX_ATTEMPTS=30 ATTEMPT=0 - echo "==> [FRONTEND] Checking frontend service startup..." + echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Checking frontend service startup..." while [ \$ATTEMPT -lt \$MAX_ATTEMPTS ]; do HTTP_CODE=\$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:${DEVBOX_FRONTEND_PORT}/") if [ "\$HTTP_CODE" -eq 200 ]; then - echo "==> [FRONTEND] Service started successfully (HTTP \$HTTP_CODE)" + echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Service started successfully (HTTP \$HTTP_CODE)" # Get the backend service PID by checking the process port with netstat -tulnp | grep ${DEVBOX_FRONTEND_PORT} FRONTEND_PID=\$(netstat -tulnp 2>/dev/null | grep "${DEVBOX_FRONTEND_PORT}" | awk '{print \$7}' | awk -F'/' '{print \$1}') @@ -1120,7 +1239,7 @@ docker exec -i "$DEVBOX_NAME" bash < /home/devbox/.frontend.pid break else - echo "==> [FRONTEND] Waiting for frontend service... Attempt \$((ATTEMPT+1))" + echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Waiting for frontend service... Attempt \$((ATTEMPT+1))" ATTEMPT=\$((ATTEMPT+1)) sleep 10 fi @@ -1129,7 +1248,7 @@ docker exec -i "$DEVBOX_NAME" bash < "\$current_frontend" git config --global --add safe.directory /home/devbox/freeleaps - echo "==> [FRONTEND] Checking git changes after compilation..." + git status -s > "\$current_frontend" + echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Checking git changes after compilation..." while read -r line; do file=\$(echo "\$line" | awk '{print \$2}') if ! grep -q "[[:space:]]\${file}$" "\$baseline_frontend"; then - echo "==> [FRONTEND] Restore file \$file" + echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Restore file \$file" git reset HEAD "\$file" git checkout -- "\$file" fi @@ -1154,14 +1282,17 @@ docker exec -i "$DEVBOX_NAME" bash < /dev/null - echo "==> [FRONTEND] Frontend compilation and startup completed." + + echo + echo "[FRONTEND] \$(date '+%Y-%m-%d %H:%M:%S') Frontend compilation and startup completed." + echo EOF } # :command.command_functions # :command.function devbox_init_command() { - echo "==> [INIT] Starting DevBox environment initialization..." + log_info " [INIT] Starting DevBox environment initialization..." echo # ------------------------------------------------------------------- @@ -1241,7 +1372,7 @@ devbox_init_command() { fi done if [ "$found" = false ]; then - exit_with_message "[ERROR] Invalid component: $component, please check the component name." 1 + exit_with_message " Invalid component: $component, please check the component name." 1 fi done @@ -1250,35 +1381,35 @@ devbox_init_command() { start_components=("${components[@]}") fi - echo "init arch value is : $ARCH " + log_info "init arch value is : $ARCH " # If is_pull_all_components is true, then pull all components if [[ "$is_pull_all_components" == true ]]; then start_components=("${components[@]}") - echo "==> Pulling all components..." - echo "==> start components: ${start_components[@]}" + log_info "==> Pulling all components..." + log_info "==> start components: ${start_components[@]}" fi # Remove duplicated components start_components=($(echo "${start_components[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) - echo " ===================================================== " - echo "Parameters:" - echo " OS = $OS" - echo " ARCH = $ARCH" - echo " DEVBOX_NAME = $DEVBOX_NAME" - echo " DEVBOX_PORT = $DEVBOX_PORT" - echo " DEVBOX_FRONTEND_PORT = $DEVBOX_FRONTEND_PORT" - echo " DEVBOX_BACKEND_PORT = $DEVBOX_BACKEND_PORT" - echo " DEVBOX_REPO = $DEVBOX_REPO" - echo " DEVBOX_IMAGE = $DEVBOX_IMAGE" - echo " DEVBOX_TAG = $DEVBOX_TAG" - echo " WORKING_HOME = $WORKING_HOME" - echo " FREELEAPS_USERNAME= $FREELEAPS_USERNAME" - echo " (FREELEAPS_PASSWORD is hidden for security)" - echo " USE_LOCAL_COMPONENT= $USE_LOCAL_COMPONENT" - echo " FREELEAPS_COMPONENTS= ${start_components[@]}" - echo " FORCE_INIT = $FORCE_INIT" + echo "===================================================== " + log_info "Parameters:" + log_info " OS = $OS" + log_info " ARCH = $ARCH" + log_info " DEVBOX_NAME = $DEVBOX_NAME" + log_info " DEVBOX_PORT = $DEVBOX_PORT" + log_info " DEVBOX_FRONTEND_PORT = $DEVBOX_FRONTEND_PORT" + log_info " DEVBOX_BACKEND_PORT = $DEVBOX_BACKEND_PORT" + log_info " DEVBOX_REPO = $DEVBOX_REPO" + log_info " DEVBOX_IMAGE = $DEVBOX_IMAGE" + log_info " DEVBOX_TAG = $DEVBOX_TAG" + log_info " WORKING_HOME = $WORKING_HOME" + log_info " FREELEAPS_USERNAME= $FREELEAPS_USERNAME" + log_info " (FREELEAPS_PASSWORD is hidden for security)" + log_info " USE_LOCAL_COMPONENT= $USE_LOCAL_COMPONENT" + log_info " FREELEAPS_COMPONENTS= ${start_components[@]}" + log_info " FORCE_INIT = $FORCE_INIT" echo # ------------------------------------------------------------------- @@ -1286,18 +1417,18 @@ devbox_init_command() { # (if using auto, detect current system, here just show simple check) # ------------------------------------------------------------------- if [[ "$OS" != "auto" && "$OS" != "linux" && "$OS" != "darwin" && "$OS" != "wsl2" ]]; then - exit_with_message "[ERROR] Unsupported OS: $OS, please check the OS." 1 + exit_with_message " Unsupported OS: $OS, please check the OS." 1 fi # Auto detected $OS if [[ "$OS" == "auto" ]]; then - echo "==> Auto detecting OS..." + log_info "==> Auto detecting OS..." OS="$(detect_os)" - echo "==> Detected OS: $OS" + log_info "==> Detected OS: $OS" fi if [[ "$ARCH" != "auto" && "$ARCH" != "amd64" && "$ARCH" != "arm64" ]]; then - exit_with_message "[ERROR] Unsupported architecture: $ARCH, please check the architecture." 1 + exit_with_message " Unsupported architecture: $ARCH, please check the architecture." 1 fi # Check ARCH match current device @@ -1309,7 +1440,7 @@ devbox_init_command() { if grep -q avx2 /proc/cpuinfo; then ARCH="amd64" ARCH_MICRO="v3" - echo "==> Detected AMD64 architecture." + log_info "==> Detected AMD64 architecture." else ARCH="amd64" fi @@ -1335,11 +1466,11 @@ devbox_init_command() { # 3.1.1.install docker and check docker running # ------------------------------------------------------------------- if ! install_docker; then - exit_with_message "[ERROR] Failed to install Docker or Docker service is not running. Please install Docker and start Docker service." 1 + exit_with_message " Failed to install Docker or Docker service is not running. Please install Docker and start Docker service." 1 fi if ! check_docker_running; then - exit_with_message "[ERROR] Docker service is not running. Please start Docker service." 1 + exit_with_message " Docker service is not running. Please start Docker service." 1 fi sudo usermod -aG docker $USER @@ -1354,38 +1485,41 @@ devbox_init_command() { free_space_kb="$(df -Pk "$HOME" | awk 'END{print $4}')" # 若无法获取或小于 10GB (10485760 KB),报错 if [[ -z "$free_space_kb" || $free_space_kb -lt 10485760 ]]; then - exit_with_message "[ERROR] Insufficient disk space (need >10GB). Please free up some space." 1 + exit_with_message " 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 - exit_with_message "[ERROR] Can't create or write to WORKING_HOME: $WORKING_HOME, please check the path." 1 + exit_with_message " 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 - exit_with_message "[ERROR] Network to Docker.com is not available. Please check your network connection." 1 + exit_with_message " 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 if [[ -f "$WORKING_HOME/.devbox-instance" && -z "$FORCE_INIT" ]]; then + echo read -p "DevBox instance already exists. Do you want to force remove it? (y/N): " ans case "$ans" in [Yy]* ) FORCE_INIT=true ;; * ) - exit_with_message "[ERROR]DevBox instance already exists. Use --force to remove it." 1 + exit_with_message "DevBox instance already exists. Use --force to remove it." 1 ;; esac fi - + + echo # Check if any component is running on the host when force init is not set if [ -z "$FORCE_INIT" ]; then running_containers=$(docker ps --format '{{.Names}}') 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 + echo read -p "Container '$comp' is already running. Do you want to force update it? (y/N): " ans case "$ans" in [Yy]* ) @@ -1393,19 +1527,20 @@ devbox_init_command() { break ;; * ) - exit_with_message "[ERROR] Container '$comp' is already running. Use --force to override." 1 + exit_with_message " Container '$comp' is already running. Use --force to override." 1 ;; esac fi done fi + echo - echo "Current OS is $OS" + log_info "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..." + log_info "==> Installing net-tools package..." if [[ "$OS" == "darwin" ]]; then brew install net-tools elif [[ "$OS" == "wsl2" ]]; then @@ -1415,55 +1550,55 @@ devbox_init_command() { 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 + exit_with_message " Failed install net-tools package on OS: $OS, please install it manually." 1 fi else - echo "==> net-tools package already installed." + log_info "==> net-tools package already installed." fi fi - echo "==> Checking if the ports are in use..." + log_info "==> 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 + exit_with_message " 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 + exit_with_message " 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 + exit_with_message " 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 + exit_with_message " 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 + exit_with_message " redis port 6379 is already in use, please stop the service." 1 fi - echo "==> Checking if the ports are in use... Done." + log_info "==> Checking if the ports are in use... Done." local devbox_full_image="${DEVBOX_REPO}/${DEVBOX_IMAGE}:${DEVBOX_TAG}" # Check local and remote version. User doesn’t need to rebuild devbox if local version is consistent with remote version if [[ -n "$DEVBOX_REPO" && -n "$DEVBOX_IMAGE" && -n "$DEVBOX_TAG" ]]; then if docker images --format '{{.Repository}}:{{.Tag}}' | grep -q "^${DEVBOX_REPO}/${DEVBOX_IMAGE}:${DEVBOX_TAG}\$"; then - echo "==> DevBox image $devbox_full_image already exists." + log_info "==> DevBox image $devbox_full_image already exists." else - echo "==> Pulling DevBox image $devbox_full_image..." + log_info "==> Pulling DevBox image $devbox_full_image..." docker pull "$devbox_full_image" fi else - exit_with_message "[ERROR] DevBox image repository, name, or tag is not specified, please check the parameters." 1 + exit_with_message " DevBox image repository, name, or tag is not specified, please check the parameters." 1 fi # If container with same name exists, remove it if docker ps -a --format '{{.Names}}' | grep -q "^${DEVBOX_NAME}\$"; then if [[ -n "$FORCE_INIT" ]]; then - echo "==> Removing existing container named $DEVBOX_NAME ..." + log_info "==> Removing existing container named $DEVBOX_NAME ..." docker stop "$DEVBOX_NAME" &>/dev/null || true docker rm "$DEVBOX_NAME" &>/dev/null || true @@ -1474,30 +1609,30 @@ devbox_init_command() { rm -f "$WORKING_HOME/.backend.pid" rm -f "$WORKING_HOME/.frontend.pid" else - exit_with_message "[ERROR] Container named $DEVBOX_NAME already exists. Use --force to remove it." 1 + exit_with_message " Container named $DEVBOX_NAME already exists. Use --force to remove it." 1 fi 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" - echo '==> [INIT] Starting DevBox environment initialization...' + log_info ' Starting DevBox environment initialization...' # Check if docker network create devbox_freeleaps2-network if ! docker network ls | grep -q "$DEVBOX_FREELEAPS2_NETWORK"; then - echo "==> Creating Docker network: $DEVBOX_FREELEAPS2_NETWORK" + log_info "==> Creating Docker network: $DEVBOX_FREELEAPS2_NETWORK" docker network create "$DEVBOX_FREELEAPS2_NETWORK" else - echo "==> Docker network devbox_freeleaps2-network already exists." + log_info "==> Docker network devbox_freeleaps2-network already exists." fi # Check if use custom repository if [[ -n "$USE_CUSTOM_REPOSITORY" ]]; then - echo "==> [INIT] Using custom repository." + log_info " [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 + exit_with_message " Username or password is missing. Please provide a valid username and password for freeleaps.com repository." 1 fi - echo '==> [INIT] Starting DevBox container...' + log_info ' [INIT] Starting DevBox container...' # Create and start DevBox container local container_id @@ -1514,8 +1649,7 @@ devbox_init_command() { )" if [[ -z "$container_id" ]]; then - echo "ERROR: Failed to create DevBox container." - exit 1 + exit_with_message " Failed to create DevBox container." 1 fi # record container id, DEVBOX_FRONTEND_PORT, DEVBOX_BACKEND_PORT @@ -1523,9 +1657,6 @@ devbox_init_command() { echo "$DEVBOX_FRONTEND_PORT" > "$WORKING_HOME/.devbox-frontend-port" echo "$DEVBOX_BACKEND_PORT" > "$WORKING_HOME/.devbox-backend-port" - - - DOVBOX_CLI_DIR=$(pwd) ECHO_USE_CUSTOM_REPOSITORY="" @@ -1535,9 +1666,9 @@ devbox_init_command() { ENCODEING_FREELEAPS_USERNAME=$(url_encode "$FREELEAPS_USERNAME") ENCODEING_FREELEAPS_PASSWORD=$(url_encode "$FREELEAPS_PASSWORD") # Test if the user can access the freeleaps.com repository - echo "==> Testing access to freeleaps.com repository..." + log_info "==> 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 - exit_with_message "[ERROR] Failed to access freeleaps.com repository. Please check your username and password." 1 + exit_with_message " Failed to access freeleaps.com repository. Please check your username and password." 1 fi FREELEAPS_DIR="$WORKING_HOME/freeleaps" @@ -1545,10 +1676,9 @@ devbox_init_command() { # Check if freeleaps2-frontend exists, if not git clone it if [ ! -d "$FREELEAPS_DIR" ]; then pushd "$WORKING_HOME" > /dev/null - echo "Git cloning freeleaps.com:3443/products/freeleaps.git 1" + log_info "Git cloning freeleaps.com:3443/products/freeleaps.git to $FREELEAPS_DIR" git clone --depth 5 $FRONTEND_GIT_URL else - echo "Git pulling 2" pushd "$FREELEAPS_DIR" > /dev/null # Check $WORKING_HOME/freeleaps exists and it is a git repository, if not git clone it if ! git rev-parse --is-inside-work-tree &>/dev/null; then @@ -1557,11 +1687,14 @@ devbox_init_command() { rmdir "$FREELEAPS_DIR" # Remove $FREELEAPS_DIR # Git clone freeleaps.com:3443/products/freeleaps.git - echo "Cloning repository again: $FRONTEND_GIT_URL" - sudo chown -R "$OWNER_GROUP" "$WORKING_HOME" + log_info "Cloning repository again: $FRONTEND_GIT_URL" + uid=$(id -u) + gid=$(id -g) + sudo chown -R ${uid}:${gid} "$WORKING_HOME" + git clone --depth 5 "$FRONTEND_GIT_URL" else - echo "Git pulling freeleaps.com:3443/products/freeleaps.git" + log_info "Git pulling freeleaps.com:3443/products/freeleaps.git" git pull fi @@ -1570,14 +1703,14 @@ devbox_init_command() { else if ! echo "$USE_CUSTOM_REPOSITORY" | grep -Eq '^(https:\/\/|git@|git:\/\/|file:\/\/\/)[^ ]+\.git$'; then - exit_with_message "[ERROR] Invalid custom repository URL. Please provide a valid URL." 1 + exit_with_message " 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..." + log_info "==> Testing access to custom repository..." if ! git ls-remote "$USE_CUSTOM_REPOSITORY" &>/dev/null; then - exit_with_message "[ERROR] Failed to access custom repository. Please check the repository URL." 1 + exit_with_message " Failed to access custom repository. Please check the repository URL." 1 fi ECHO_USE_CUSTOM_REPOSITORY=$(echo "$USE_CUSTOM_REPOSITORY" | sed 's/\(https:\/\/[^:]*\):[^@]*@/\1:****@/') @@ -1586,7 +1719,7 @@ devbox_init_command() { CUSTOM_DIR="$WORKING_HOME/$CUSTOM_FOLDER_NAME" if [ ! -d "$CUSTOM_DIR" ]; then pushd "$WORKING_HOME" > /dev/null - echo "Git cloning custom repository: $ECHO_USE_CUSTOM_REPOSITORY" + log_info "Git cloning custom repository: $ECHO_USE_CUSTOM_REPOSITORY" git clone --depth 5 "$USE_CUSTOM_REPOSITORY" else pushd "$CUSTOM_DIR" > /dev/null @@ -1597,11 +1730,14 @@ devbox_init_command() { rmdir "$CUSTOM_DIR" # Remove $CUSTOM_DIR # Git clone custom repository - echo "Cloning repository again: $ECHO_USE_CUSTOM_REPOSITORY" - sudo chown -R "$OWNER_GROUP" "$WORKING_HOME" + log_info "Cloning repository again: $ECHO_USE_CUSTOM_REPOSITORY" + uid=$(id -u) + gid=$(id -g) + sudo chown -R ${uid}:${gid} "$WORKING_HOME" + git clone --depth 5 "$USE_CUSTOM_REPOSITORY" else - echo "Git pulling custom repository" + log_info "Git pulling custom repository" git pull fi @@ -1615,7 +1751,7 @@ devbox_init_command() { # 6. linbwang: pull and start other components # ------------------------------------------------------------------- -echo "==> [INIT] Starting Freeleaps services... Use Local component $USE_LOCAL_COMPONENT_VAL" +log_info "==> [INIT] Starting Freeleaps services... Use Local component $USE_LOCAL_COMPONENT_VAL" export ARCH="$ARCH" export WORKING_HOME="$WORKING_HOME" @@ -1632,10 +1768,10 @@ 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." + log_info "==> [INIT] Custom repository initialization completed." + log_info "==> Custom repository is located at: ${WORKING_HOME}/${CUSTOM_FOLDER_NAME}" + log_info "==> Custom repository URL: $ECHO_USE_CUSTOM_REPOSITORY" + log_info "==> Custom repository is ready for use." echo echo "===========================================================" echo @@ -1644,7 +1780,7 @@ if [[ -n "$USE_CUSTOM_REPOSITORY" ]]; then fi # Check if docker-compose command exists -echo "==> Cehck if docker-compose command exists" +log_info "==> 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 @@ -1654,15 +1790,15 @@ elif docker compose version >/dev/null 2>&1; then DC_CMD="docker compose" else DC_CMD="" - echo "ERROR: docker-compose is not installed." + log_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 + exit_with_message "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.' + log_info ' ===> Using local components for Freeleaps services.' export DEVSVC_IMAGE_TAG="$component_tag" export CONTENT_IMAGE_TAG="$component_tag" @@ -1673,7 +1809,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 - exit_with_message "[ERROR] gitea_data_backup.tar.gz not found. Please make sure it exists in the current directory." 1 + exit_with_message " 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 @@ -1681,12 +1817,10 @@ 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 - exit_with_message "[ERROR] Failed to extract gitea data backup, please check the backup file." 1 + exit_with_message " Failed to extract gitea data backup, please check the backup file." 1 fi - # Echo OWNER_GROUP - echo "OWNER_GROUP: $OWNER_GROUP" - + # Copy gitea data to the gitea container GITEA_HOST_DIR="${WORKING_HOME}/freeleaps2-gitea" @@ -1700,13 +1834,15 @@ if [[ $USE_LOCAL_COMPONENT_VAL == true ]]; then sudo mv data/gitea ${GITEA_HOST_DIR}/ sudo mv data/ssh ${GITEA_HOST_DIR}/ - # Change the owner group of the gitea data directories - sudo chown -R "${OWNER_GROUP}" ${GITEA_HOST_DIR} - echo "Gitea data copying is done" + # Change the current user of the gitea data directories + uid=$(id -u) + gid=$(id -g) + sudo chown -R ${uid}:${gid} ${GITEA_HOST_DIR} + log_info "Gitea data copying is done" # Check if gitea data directories exist in the gitea container if [[ ! -d "${GITEA_HOST_DIR}/gitea" ]]; then - exit_with_message "[ERROR] Failed to copy gitea data, please check the data directories." 1 + exit_with_message " Failed to copy gitea data, please check the data directories." 1 fi @@ -1720,7 +1856,7 @@ if [[ $USE_LOCAL_COMPONENT_VAL == true ]]; then done # Start Gitea, MongoDB, RabbitMQ and other components containers - echo "===> start Gitea, MongoDB, RabbitMQ and other components containers" + log_info "===> start Gitea, MongoDB, RabbitMQ and other components containers" $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}}") @@ -1742,24 +1878,17 @@ if [[ $USE_LOCAL_COMPONENT_VAL == true ]]; then echo "$tmp_container_id" > "$WORKING_HOME/.${component}-instance" done - echo "${component} container created: $component_container_id" - - # Get the owner group of the WORKING_HOME - if [[ "$(uname)" == "Darwin" ]]; then - OWNER_GROUP=$(stat -f "%Su:%Sg" "${WORKING_HOME}") - else - OWNER_GROUP=$(stat -c "%U:%G" "${WORKING_HOME}") - fi + log_info "${component} container created: $component_container_id" # Check all components are started for component in "${start_components[@]}"; do if [[ -z "$(docker ps -a --format '{{.Names}}' | grep "^$component\$")" ]]; then - exit_with_message "[ERROR] Failed to start $component container. Please check the logs for more information." 1 + exit_with_message " Failed to start $component container. Please check the logs for more information." 1 fi done else echo '============================================' - echo ' ===> Using online components for Freeleaps services.' + log_info ' ===> Using online components for Freeleaps services.' echo '============================================' # Start Gitea, MongoDB, RabbitMQ containers $DC_CMD -f docker-compose.yaml up -d mongodb rabbitmq redis @@ -1785,7 +1914,9 @@ IS_START_FRONTEND=false # Make a user input (Y/N) to continue pull freeleaps.com code and start if N then exit +echo read -p "Do you want to continue to pull freeleaps.com code and start the services? (Y/N): " user_input +echo # Initialize the compile environment init_compile_env @@ -1795,10 +1926,10 @@ if [[ "$user_input" == "N" || "$user_input" == "n" ]]; then # Echo as init job completed and exit echo echo "===========================================================" - echo "DevBox init completed successfully!" - echo "DevBox Environment Details:" - echo " DevBox container name: $DEVBOX_NAME" - echo " DevBox container ID: $WORKING_HOME/.devbox-instance" + log_info "DevBox init completed successfully!" + log_info "DevBox Environment Details:" + log_info " DevBox container name: $DEVBOX_NAME" + log_info " DevBox container ID: $WORKING_HOME/.devbox-instance" echo "===========================================================" echo exit 0 @@ -1812,7 +1943,7 @@ compile_backend_service compile_frontend_service docker exec -i "$DEVBOX_NAME" bash < Reset git changes caused by compilation" +echo "[INFO] \$(date '+%Y-%m-%d %H:%M:%S') Reset git changes caused by compilation" pushd /home/devbox/freeleaps git config --global --add safe.directory /home/devbox/freeleaps git reset --hard @@ -1823,16 +1954,16 @@ EOF # ------------------------------------------------------------------- echo echo "===========================================================" - echo "DevBox init completed successfully!" - echo "DevBox Environment Details:" - echo "1. Code repository is located at: ${WORKING_HOME}/freeleaps" - echo "2. Open up the frontend by visiting: http://localhost:${DEVBOX_FRONTEND_PORT}" - echo "3. Log files can be viewed at:" - echo " - Backend logs: ${WORKING_HOME}/logs/backend.logs" - echo " - Frontend logs: ${WORKING_HOME}/logs/frontend.logs" - echo " DevBox container ID: $WORKING_HOME/.devbox-instance" - echo " Backend PID: $WORKING_HOME/.backend.pid" - echo " Frontend PID: $WORKING_HOME/.frontend.pid" + log_info "DevBox init completed successfully!" + log_info "DevBox Environment Details:" + log_info "1. Code repository is located at: ${WORKING_HOME}/freeleaps" + log_info "2. Open up the frontend by visiting: http://localhost:${DEVBOX_FRONTEND_PORT}" + log_info "3. Log files can be viewed at:" + log_info " - Backend logs: ${WORKING_HOME}/logs/backend.logs" + log_info " - Frontend logs: ${WORKING_HOME}/logs/frontend.logs" + log_info " DevBox container ID: $WORKING_HOME/.devbox-instance" + log_info " Backend PID: $WORKING_HOME/.backend.pid" + log_info " Frontend PID: $WORKING_HOME/.frontend.pid" echo "===========================================================" echo } @@ -1841,28 +1972,30 @@ EOF devbox_deinit_command() { # src/deinit_command.sh - echo "# It contains the implementation for the 'devbox deinit' command." + log_info " It contains the implementation for the 'devbox deinit' command." local WORKING_HOME="$(get_arg '--working-home' "${HOME}/devbox")" local CLEAR_LOGS="$(get_arg '--clear-logs')" local CLEAR_REPO="$(get_arg '--clear-repo')" local CLEAR_ALL="$(get_arg '--clear-all')" # print the parameters - echo "==> Deinitialization parameters:" - echo " WORKING_HOME = $WORKING_HOME" - echo " CLEAR_LOGS = $CLEAR_LOGS" - echo " CLEAR_REPO = $CLEAR_REPO" - echo " CLEAR_ALL = $CLEAR_ALL" + log_info "==> Deinitialization parameters:" + log_info " WORKING_HOME = $WORKING_HOME" + log_info " CLEAR_LOGS = $CLEAR_LOGS" + log_info " CLEAR_REPO = $CLEAR_REPO" + log_info " CLEAR_ALL = $CLEAR_ALL" - echo "==> Starting DevBox deinitialization..." + log_info "==> Starting DevBox deinitialization..." # Stop and remove DevBox container + if [[ -f "$WORKING_HOME/.devbox-instance" ]]; then local container_id container_id=$(cat "$WORKING_HOME/.devbox-instance") - echo "==> Stopping and removing DevBox container: $container_id" + log_info "==> Stopping and removing DevBox container: $container_id" docker stop "$container_id" &>/dev/null || true docker rm "$container_id" &>/dev/null || true + rm -f "$WORKING_HOME/.devbox-instance" # Remove the frontend and backend ports if the file exists @@ -1872,14 +2005,33 @@ devbox_deinit_command() { # Remove the backend and frontend process IDs rm -f "$WORKING_HOME/.backend.pid" rm -f "$WORKING_HOME/.frontend.pid" + else + echo "==> DevBox container is not running." + if [[ -n "$(docker ps -a --format '{{.Names}}' | grep "^devbox\$")" ]]; then + # Get the container ID of the DevBox container + echo "==> DevBox container is stopped." + local container_id + container_id=$(docker ps -a --format '{{.Names}}' | grep "^devbox\$") + + log_info "==> Stopping and removing DevBox container: $container_id" + docker stop "$container_id" &>/dev/null || true + docker rm "$container_id" &>/dev/null || true + fi fi if [[ -f "$WORKING_HOME/.gitea-instance" ]]; then local gitea_container_id gitea_container_id=$(cat "$WORKING_HOME/.gitea-instance") - echo "==> Stopping and removing Gitea container: $gitea_container_id" + log_info "==> Stopping and removing Gitea container: $gitea_container_id" docker stop "$gitea_container_id" &>/dev/null || true docker rm "$gitea_container_id" &>/dev/null || true + + # Check if the Gitea container is still running, then use docker compose down to stop and remove the container + if [[ -n "$(docker ps -a --format '{{.Names}}' | grep "^freeleaps2-gitea\$")" ]]; then + log_info "==> Using docker-compose down to stop and remove Gitea container." + $DC_CMD -f docker-compose.yaml down gitea + fi + rm -f "$WORKING_HOME/.gitea-instance" fi @@ -1887,18 +2039,32 @@ devbox_deinit_command() { if [[ -f "$WORKING_HOME/.mongodb-instance" ]]; then local mongodb_container_id mongodb_container_id=$(cat "$WORKING_HOME/.mongodb-instance") - echo "==> Stopping and removing MongoDB container: $mongodb_container_id" + log_info "==> Stopping and removing MongoDB container: $mongodb_container_id" docker stop "$mongodb_container_id" &>/dev/null || true docker rm "$mongodb_container_id" &>/dev/null || true + + # Check if the MongoDB container is still running, then use docker compose down to stop and remove the container + if [[ -n "$(docker ps -a --format '{{.Names}}' | grep "^freeleaps2-mongodb\$")" ]]; then + log_info "==> Using docker-compose down to stop and remove MongoDB container." + $DC_CMD -f docker-compose.yaml down mongodb + fi + rm -f "$WORKING_HOME/.mongodb-instance" fi if [[ -f "$WORKING_HOME/.redis-instance" ]]; then local redis_container_id redis_container_id=$(cat "$WORKING_HOME/.redis-instance") - echo "==> Stopping and removing Redis container: $redis_container_id" + log_info "==> Stopping and removing Redis container: $redis_container_id" docker stop "$redis_container_id" &>/dev/null || true docker rm "$redis_container_id" &>/dev/null || true + + # Check if the Redis container is still running, then use docker compose down to stop and remove the container + if [[ -n "$(docker ps -a --format '{{.Names}}' | grep "^freeleaps2-redis\$")" ]]; then + log_info "==> Using docker-compose down to stop and remove Redis container." + $DC_CMD -f docker-compose.yaml down redis + fi + rm -f "$WORKING_HOME/.redis-instance" fi @@ -1906,9 +2072,16 @@ devbox_deinit_command() { if [[ -f "$WORKING_HOME/.rabbitmq-instance" ]]; then local rabbitmq_container_id rabbitmq_container_id=$(cat "$WORKING_HOME/.rabbitmq-instance") - echo "==> Stopping and removing RabbitMQ container: $rabbitmq_container_id" + log_info "==> Stopping and removing RabbitMQ container: $rabbitmq_container_id" docker stop "$rabbitmq_container_id" &>/dev/null || true docker rm "$rabbitmq_container_id" &>/dev/null || true + + # Check if the RabbitMQ container is still running, then use docker compose down to stop and remove the container + if [[ -n "$(docker ps -a --format '{{.Names}}' | grep "^freeleaps2-rabbitmq\$")" ]]; then + log_info "==> Using docker-compose down to stop and remove RabbitMQ container." + $DC_CMD -f docker-compose.yaml down rabbitmq + fi + rm -f "$WORKING_HOME/.rabbitmq-instance" fi @@ -1918,9 +2091,16 @@ devbox_deinit_command() { if [[ -f "$WORKING_HOME/.${component}-instance" ]]; then local component_container_id component_container_id=$(cat "$WORKING_HOME/.${component}-instance") - echo "==> Stopping and removing ${component} container: $component_container_id" + log_info "==> Stopping and removing ${component} container: $component_container_id" docker stop "$component_container_id" &>/dev/null || true docker rm "$component_container_id" &>/dev/null || true + + # Check if the component is still running, then use docker compose down to stop and remove the container + if [[ -n "$(docker ps -a --format '{{.Names}}' | grep "^$component\$")" ]]; then + log_info "==> Using docker-compose down to stop and remove $component container." + $DC_CMD -f docker-compose.yaml down "$component" + fi + rm -f "$WORKING_HOME/.${component}-instance" fi done @@ -1928,53 +2108,61 @@ devbox_deinit_command() { # Clear the DevBox container logs if [[ "$CLEAR_LOGS" == "true" ]]; then - echo "==> Clearing logs in $WORKING_HOME/logs..." + log_info "==> Clearing logs in $WORKING_HOME/logs..." if [[ -d "$WORKING_HOME/logs" ]]; then - sudo chown -R $(whoami) "$WORKING_HOME/logs" + uid=$(id -u) + gid=$(id -g) + sudo chown -R ${uid}:${gid} "$WORKING_HOME/logs" rm -rf "$WORKING_HOME/logs" 2>/dev/null || true mkdir -p "$WORKING_HOME/logs" 2>/dev/null || true fi else - echo "==> Skipping log clearing." + log_info "==> Skipping log clearing." fi # 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) "$WORKING_HOME/freeleaps" + log_info "==> Deleting source repository at $WORKING_HOME/freeleaps" + uid=$(id -u) + gid=$(id -g) + sudo chown -R ${uid}:${gid} "$WORKING_HOME/freeleaps" rm -rf "$WORKING_HOME/freeleaps" 2>/dev/null || true rmdir "$WORKING_HOME/freeleaps" 2>/dev/null || true else - echo "==> Skipping repository deletion." + log_info "==> Skipping repository deletion." fi if [[ "$CLEAR_LOGS" == "true" ]]; then # Check if logs directory is removed if [[ -d "$WORKING_HOME/logs" ]]; then - echo "[WARNING] $WORKING_HOME/logs still exists after removal." + log_warn " $WORKING_HOME/logs still exists after removal." rm -rf "$WORKING_HOME/logs" else - echo "==> Logs directory removed successfully." + log_info "==> Logs directory removed successfully." fi fi if [[ "$CLEAR_ALL" == "true" ]]; then # Check Y/N to remove the working home directory + echo read -p "Do you want to delete the working home directory? This will permanently remove all your local code and environment. (Y/N): " user_input + echo if [[ "$user_input" == "Y" || "$user_input" == "y" ]]; then REMOVE_WORKING_HOME=true # Remove the working home directory - echo "==> Removing working home directory: $WORKING_HOME" + log_info "==> Removing working home directory: $WORKING_HOME" if [[ -d "$WORKING_HOME" ]]; then - sudo chown -R $(whoami) "$WORKING_HOME" + uid=$(id -u) + gid=$(id -g) + sudo chown -R ${uid}:${gid} "$WORKING_HOME" rm -rf "$WORKING_HOME" 2>/dev/null || true rmdir "$WORKING_HOME" 2>/dev/null || true fi - echo "==> Working home directory removed successfully." + log_info "==> Working home directory removed successfully." else REMOVE_WORKING_HOME=false - echo "==> Skipping working home directory removal." + log_info "==> Skipping working home directory removal." fi fi @@ -1984,10 +2172,12 @@ devbox_deinit_command() { sleep 1 done + echo + # Remove the use-local-component file rm -f "$WORKING_HOME/.use-local-component" - echo "==> DevBox deinitialization completed." + log_info "==> DevBox deinitialization completed." } # :command.function @@ -2014,7 +2204,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 - exit_with_message "[ERROR] DevBox container is not running. Please run 'devbox init' first." 1 + exit_with_message " DevBox container is not running. Please run 'devbox init' first." 1 fi local devbox_container_id=$(cat "$devbox_container_id_file_path") @@ -2027,10 +2217,10 @@ devbox_start_command() { fi if [[ "$(lower "$USE_LOCAL_COMPONENT")" == "true" ]]; then - echo "==> Using local components for Freeleaps services." + log_info "==> Using local components for Freeleaps services." USE_LOCAL_COMPONENT="true" else - echo "==> Using online components for Freeleaps services." + log_info "==> Using online components for Freeleaps services." USE_LOCAL_COMPONENT="false" fi @@ -2058,28 +2248,28 @@ devbox_start_command() { for comp in "${COMPONENTS[@]}"; do case "$comp" in "gitea" | "mongodb" | "rabbitmq" | "devbox" | "devsvc" | "notification" | "content" | "central_storage" | "chat" | "authentication") - echo "==> Starting $comp service..." + log_info "==> Starting $comp service..." # Check if the component container file exists local component_container_id_file_path="${WORKING_HOME}/.${comp}-instance" if [[ ! -f "$component_container_id_file_path" ]]; then - echo "ERROR: $comp container is not running. Please run 'devbox init' first." + log_error " $comp container is not running. Please run 'devbox init' first." else local component_container_id=$(cat "$component_container_id_file_path") if ! docker ps --no-trunc --format '{{.ID}}' | grep -q "^${component_container_id}\$"; then - echo "==> $comp container is not running, starting container..." + log_info "==> $comp container is not running, starting container..." # Start the container if ! docker start "${component_container_id}"; then - echo "ERROR: Failed to start $comp container." + log_error " Failed to start $comp container." else - echo "==> $comp container started successfully." + log_info "==> $comp container started successfully." fi else - echo "==> $comp container is already running." + log_info "==> $comp container is already running." fi fi ;; *) - exit_with_message "[ERROR] Unknown component: $comp, please check the component name." 1 + exit_with_message " Unknown component: $comp, please check the component name." 1 ;; esac done @@ -2097,7 +2287,7 @@ devbox_start_command() { done if docker ps --no-trunc --format '{{.ID}}' | grep -q "^${devbox_container_id}\$"; then - echo "==> Starting Freeleaps frontend and backend services..." + log_info "==> Starting Freeleaps frontend and backend services..." # Check if start backend service if [[ "${START_BACKEND}" == "true" ]]; then compile_backend_service @@ -2108,11 +2298,23 @@ devbox_start_command() { compile_frontend_service fi else - exit_with_message "[ERROR] DevBox container is not running, please run 'devbox init' first." 1 + exit_with_message " DevBox container is not running, please run 'devbox init' first." 1 fi fi - exit_with_message "[INFO] DevBox services started successfully." 0 + success_message="==> Freeleaps services started successfully. " + + frontend_port=$(cat "${WORKING_HOME}/.devbox-frontend-port") + if [[ "${START_FRONTEND}" == "true" && "${START_BACKEND}" == "true" ]]; then + success_message=" Frontend and backend services started successfully. Open up the frontend by visiting: http://localhost:${frontend_port}" + elif [[ "${START_FRONTEND}" == "true" ]]; then + success_message=" Frontend service started successfully. Open up the frontend by visiting: http://localhost:${frontend_port}" + elif [[ "${START_BACKEND}" == "true" ]]; then + success_message=" Backend service started successfully." + fi + + exit_with_message "$success_message" 0 + } # :command.function @@ -2142,7 +2344,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 - exit_with_message "[ERROR] DevBox container is not running, please run 'devbox init' first." 1 + exit_with_message " DevBox container is not running, please run 'devbox init' first." 1 fi # Check if STOP_BACKEND is true, stop the backend service @@ -2150,7 +2352,7 @@ devbox_stop_command() { if [[ -f "${WORKING_HOME}/.backend.pid" ]]; then stop_backend_service else - echo "==> Backend service is not running." + log_info "==> Backend service is not running." fi fi @@ -2159,7 +2361,7 @@ devbox_stop_command() { if [[ -f "${WORKING_HOME}/.frontend.pid" ]]; then stop_frontend_service else - echo "==> Frontend service is not running." + log_info "==> Frontend service is not running." fi fi fi @@ -2185,7 +2387,7 @@ devbox_stop_command() { fi - exit_message="[INFO] Stopped Freeleaps $stoped_freeleaps_service_names services successfully." + exit_message="Stopped Freeleaps $stoped_freeleaps_service_names services successfully." exit_with_message "$exit_message" 0 fi @@ -2205,16 +2407,16 @@ devbox_stop_command() { local container_id container_id=$(cat "${WORKING_HOME}/.devbox-instance") if docker ps --no-trunc --format '{{.ID}}' | grep -q "^${container_id}\$"; then - echo "==> Stopping devbox..." + log_info "==> Stopping devbox..." docker stop "$container_id" &>/dev/null || true # Remove the frontend and backend pid files rm -f "${WORKING_HOME}/.backend.pid" rm -f "${WORKING_HOME}/.frontend.pid" else - echo "==> DevBox container is not running." + log_info "==> DevBox container is not running." fi else - echo "==> Backend service is not running." + log_info "==> Backend service is not running." fi ;; @@ -2223,42 +2425,42 @@ devbox_stop_command() { local container_id container_id=$(cat "${WORKING_HOME}/.${comp}-instance") if docker ps --no-trunc --format '{{.ID}}' | grep -q "^${container_id}\$"; then - echo "==> Stopping $comp service..." + log_info "==> Stopping $comp service..." docker stop "$container_id" &>/dev/null || true else - echo "==> $comp service is not running." + log_info "==> $comp service is not running." fi else - echo "==> $comp service is not running." + log_info "==> $comp service is not running." fi ;; *) - exit_with_message "[ERROR] Unknown component: $comp, please check the component name." 1 + exit_with_message " Unknown component: $comp, please check the component name." 1 ;; esac done - exit_with_message "[INFO] Stopped Freeleaps services successfully." 0 + exit_with_message "[INFO] Stopped Freeleaps services successfully. " 0 } # :command.function devbox_status_command() { - echo "==> Checking DevBox services status..." + log_info "==> Checking DevBox services status..." local COMPONENT="$(get_arg '--component')" local WORKING_HOME="$(get_arg '--working-home' "${HOME}/devbox")" # Check if .devbox-instance file exists if [[ ! -f "${WORKING_HOME}/.devbox-instance" ]]; then - exit_with_message "[ERROR] DevBox container is not running. Please run 'devbox init' first." 1 + exit_with_message " 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 - exit_with_message "[ERROR] DevBox container is not running. Please run 'devbox init' first." 1 + exit_with_message " DevBox container is not running. Please run 'devbox init' first." 1 fi # If no component is specified, check all components @@ -2272,76 +2474,76 @@ devbox_status_command() { for comp in "${COMPONENTS[@]}"; do case "$comp" in "mongodb") - echo "==> Checking MongoDB status..." + log_info "==> Checking MongoDB status..." if [[ -f "${WORKING_HOME}/.mongodb-instance" ]]; then local container_id container_id=$(cat "${WORKING_HOME}/.mongodb-instance") if docker ps --no-trunc --format '{{.ID}}' | grep -q "^${container_id}\$"; then - echo "[RESULT]: MongoDB container is running." + log_info "[RESULT]: MongoDB container is running." else - echo "[RESULT]: MongoDB container is not running." + log_info "[RESULT]: MongoDB container is not running." fi else - echo "[RESULT]: MongoDB container is not running." + log_info "[RESULT]: MongoDB container is not running." fi ;; "rabbitmq") - echo "==> Checking RabbitMQ status..." + log_info "==> Checking RabbitMQ status..." if [[ -f "${WORKING_HOME}/.rabbitmq-instance" ]]; then local container_id container_id=$(cat "${WORKING_HOME}/.rabbitmq-instance") if docker ps --no-trunc --format '{{.ID}}' | grep -q "^${container_id}\$"; then - echo "[RESULT]: RabbitMQ container is running." + log_info "[RESULT]: RabbitMQ container is running." else - echo "[RESULT]: RabbitMQ container is not running." + log_info "[RESULT]: RabbitMQ container is not running." fi else - echo "[RESULT]: RabbitMQ container is not running." + log_info "[RESULT]: RabbitMQ container is not running." fi ;; "devbox") - echo "==> Checking devbox service status..." + log_info "==> Checking devbox service status..." if [[ -f "${WORKING_HOME}/.devbox-instance" ]]; then local container_id container_id=$(cat "${WORKING_HOME}/.devbox-instance") if docker ps --no-trunc --format '{{.ID}}' | grep -q "^${container_id}\$"; then - echo "[RESULT]: devbox container is running." + log_info "[RESULT]: devbox container is running." else - echo "[RESULT]: devbox container is not running." + log_info "[RESULT]: devbox container is not running." fi else - echo "[RESULT]: devbox container is not running." + log_info "[RESULT]: devbox container is not running." fi ;; "devsvc" | "notification" | "content" | "central_storage" | "chat" | "authentication") - echo "==> Checking $comp service status..." + log_info "==> Checking $comp service status..." if [[ -f "${WORKING_HOME}/.${comp}-instance" ]]; then local container_id container_id=$(cat "${WORKING_HOME}/.${comp}-instance") if docker ps --no-trunc --format '{{.ID}}' | grep -q "^${container_id}\$"; then - echo "[RESULT]: $comp service is running." + log_info "[RESULT]: $comp service is running." else - echo "[RESULT]: $comp service is not running." + log_info "[RESULT]: $comp service is not running." fi else - echo "[RESULT]: $comp service is not running." + log_info "[RESULT]: $comp service is not running." fi ;; *) - exit_with_message "[ERROR] Unknown component: $comp, please check the component name." 1 + exit_with_message " Unknown component: $comp, please check the component name." 1 ;; esac done - echo "==> DevBox services status checked successfully." + exit_with_message "==> DevBox services status checked successfully." 0 } # :command.function devbox_restart_command() { - echo "==> Restarting DevBox services..." + log_info "==> Restarting DevBox services..." local COMPONENT="$(get_arg '--component')" local WORKING_HOME="$(get_arg '--working-home' "${HOME}/devbox")" local FREELEAPS_ENDPOINT="$(get_arg '--freeleaps-endpoint')" @@ -2370,9 +2572,9 @@ devbox_restart_command() { if [[ "$FREELEAPS_ENDPOINT" != "" ]]; then devbox_container_id=$(cat "${WORKING_HOME}/.devbox-instance") - echo "docker ps --no-trunc --format ${devbox_container_id} " + log_info "docker ps --no-trunc --format ${devbox_container_id} " if docker ps --no-trunc --format '{{.ID}}' | grep -q "${devbox_container_id}\$"; then - echo "==> Starting Freeleaps frontend and backend services..." + log_info "==> Starting Freeleaps frontend and backend services..." # Check if start backend service if [[ "${START_BACKEND}" == "true" ]]; then stop_backend_service @@ -2385,16 +2587,21 @@ devbox_restart_command() { compile_frontend_service fi else - exit_with_message "[ERROR] DevBox container is not running." 1 + exit_with_message " DevBox container is not running." 1 fi - exit_with_message "[INFO] Freeleaps $restart_services services restarted successfully." 0 + frontend_port=$(cat "${WORKING_HOME}/.devbox-frontend-port") + success_message="Freeleaps $restart_services services restarted successfully." + if [[ "${START_FRONTEND}" == "true" ]]; then + success_message+=" Open up the frontend by visiting: http://localhost:${frontend_port}" + fi + exit_with_message "$success_message" 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 - exit_with_message "[ERROR] DevBox container is not running. Please run 'devbox init' first." 1 + exit_with_message " DevBox container is not running. Please run 'devbox init' first." 1 fi local devbox_container_id=$(cat "$devbox_container_id_file_path") @@ -2402,19 +2609,19 @@ devbox_restart_command() { # Check if current environment is using local components USE_LOCAL_COMPONENT=$(cat "${WORKING_HOME}/.use-local-component" 2>/dev/null || true) if [[ "$USE_LOCAL_COMPONENT" == "true" ]]; then - echo "==> Using local components..." + log_info "==> Using local components..." if [[ -z "$COMPONENT" ]]; then COMPONENTS=("gitea" "mongodb" "rabbitmq" "devbox" "devsvc" "notification" "content" "central_storage" "chat" "authentication") else COMPONENTS=("$COMPONENT") fi else - echo "==> Using remote components..." + log_info "==> Using remote components..." if [[ -z "$COMPONENT" ]]; then COMPONENTS=("mongodb" "rabbitmq" "devbox") else if [[ "$COMPONENT" == "devsvc" || "$COMPONENT" == "notification" || "$COMPONENT" == "content" || "$COMPONENT" == "central_storage" || "$COMPONENT" == "chat" || "$COMPONENT" == "authentication" ]]; then - exit_with_message "[ERROR] Remote component $COMPONENT cannot be restarted, please use local components." 1 + exit_with_message " Remote component $COMPONENT cannot be restarted, please use local components." 1 fi COMPONENTS=("$COMPONENT") @@ -2428,14 +2635,14 @@ devbox_restart_command() { if [[ -f "${WORKING_HOME}/.devbox-instance" ]]; then local container_id container_id=$(cat "${WORKING_HOME}/.devbox-instance") - echo "==> Stopping devbox service..." + log_info "==> Stopping devbox service..." docker stop "$container_id" &>/dev/null || true # Remove the frontend and backend pid files rm -f "${WORKING_HOME}/.backend.pid" rm -f "${WORKING_HOME}/.frontend.pid" else - echo "==> Devbox is not running." + log_info "==> Devbox is not running." fi ;; @@ -2444,14 +2651,14 @@ devbox_restart_command() { if [[ -f "${WORKING_HOME}/.${comp}-instance" ]]; then local container_id container_id=$(cat "${WORKING_HOME}/.${comp}-instance") - echo "==> Stopping $comp service..." + log_info "==> Stopping $comp service..." docker stop "$container_id" &>/dev/null || true else - echo "==> $comp service is not running." + log_info "==> $comp service is not running." fi ;; *) - exit_with_message "[ERROR] Unknown component: $comp, please check the component name." 1 + exit_with_message " Unknown component: $comp, please check the component name." 1 ;; esac done @@ -2460,22 +2667,22 @@ devbox_restart_command() { for comp in "${COMPONENTS[@]}"; do case "$comp" in "gitea" | "mongodb" | "rabbitmq" | "devbox" | "devsvc" | "notification" | "content" | "central_storage" | "chat" | "authentication") - echo "==> Restarting $comp service..." + log_info "==> Restarting $comp service..." if [[ -f "${WORKING_HOME}/.${comp}-instance" ]]; then local container_id container_id=$(cat "${WORKING_HOME}/.${comp}-instance") docker start "$container_id" &>/dev/null || true else - echo "==> $comp service is not running." + log_info "==> $comp service is not running." fi ;; *) - exit_with_message "[ERROR] Unknown component: $comp, please check the component name." 1 + exit_with_message " Unknown component: $comp, please check the component name." 1 ;; esac done - echo "==> DevBox services restarted successfully." + exit_with_message "==> DevBox services restarted successfully." 0 } # :command.parse_requirements diff --git a/devbox/cli/docker-compose.yaml b/devbox/cli/docker-compose.yaml index 8ffa650..272421e 100644 --- a/devbox/cli/docker-compose.yaml +++ b/devbox/cli/docker-compose.yaml @@ -11,7 +11,7 @@ services: - DISABLE_REGISTRATION=true - REQUIRE_SIGNIN_VIEW=true volumes: - - ${WORKING_HOME}/freeleaps2-gitea:/data:Z + - ${WORKING_HOME}/freeleaps2-gitea:/data networks: - devbox_freeleaps2-network healthcheck: