diff --git a/devbox/devbox.local/devbox b/devbox/devbox.local/devbox index 3e9be69..f123934 100644 --- a/devbox/devbox.local/devbox +++ b/devbox/devbox.local/devbox @@ -446,43 +446,6 @@ get_port() { echo "$port" } -check_jq() { - # 从参数中获取 OS 和 ARCH,默认值分别为 auto - local target_os target_arch - target_os="$(get_arg '--os' 'auto')" - target_arch="$(get_arg '--arch' 'auto')" - - if ! command -v jq >/dev/null 2>&1; then - echo "[INFO] 'jq' is not installed. Installing jq..." - case "$target_os" in - darwin) - if command -v brew >/dev/null 2>&1; then - brew install jq - else - echo "[ERROR] brew not found. Please install jq manually." - exit 1 - fi - ;; - linux | wsl2 | auto) - if command -v apt-get >/dev/null 2>&1; then - sudo apt-get update && sudo apt-get install -y jq - elif command -v yum >/dev/null 2>&1; then - sudo yum install -y epel-release && sudo yum install -y jq - else - echo "[ERROR] apt-get or yum not found. Please install jq manually." - exit 1 - fi - ;; - *) - echo "[ERROR] Unsupported OS: $target_os" - exit 1 - ;; - esac - else - echo "[INFO] 'jq' is already installed." - fi -} - # :command.command_functions # :command.function devbox_init_command() { @@ -852,14 +815,8 @@ if [[ $USE_LOCAL_COMPONENT_VAL == true ]]; then exit 1 fi - - # Check if jq is installed, if not install it - check_jq - # Get Gitea data volume mount point - GITEA_DATA=$(docker volume inspect devbox_freeleaps2-gitea-data | jq -r '.[0].Mountpoint') - echo "Gitea data volume mount point: $GITEA_DATA" echo "==> Starting Gitea, MongoDB, RabbitMQ containers..." # Start local components by docker compose file and start up specified services. docker compose file is in the same directory as the script (docker-compose.dev.arm64.new.yaml) @@ -889,43 +846,49 @@ if [[ $USE_LOCAL_COMPONENT_VAL == true ]]; then echo "${component} container created: $component_container_id" - # Check if devbox_freeleaps2-gitea-data exists, if not create a docker volume for gitea data, if exists then remove it and create a new one - if docker volume ls | grep -q "devbox_freeleaps2-gitea-data"; then - docker volume rm devbox_freeleaps2-gitea-data - fi - - docker volume create devbox_freeleaps2-gitea-data || { - echo "ERROR: Failed to create volume devbox_freeleaps2-gitea-data." - exit 1 - } - - # Check volume created successfully - if ! docker volume ls | grep -q "devbox_freeleaps2-gitea-data"; then - echo "ERROR: Failed to create volume devbox_freeleaps2-gitea-data." - exit 1 - fi - - if [[ -d "${GITEA_DATA}/gitea" ]]; then - echo "Gitea data exist, skipping..." + # Get the owner group of the WORKING_HOME + if [[ "$(uname)" == "Darwin" ]]; then + OWNER_GROUP=$(stat -f "%Su:%Sg" "${WORKING_HOME}") else - echo "Gitea data not exist, copying..." - sudo rm -rf ${WORKING_HOME}/freeleaps2-gitea/git - sudo rm -rf ${WORKING_HOME}/freeleaps2-gitea/gitea - sudo rm -rf ${WORKING_HOME}/freeleaps2-gitea/ssh - sudo mv data/git ${WORKING_HOME}/freeleaps2-gitea/ - sudo mv data/gitea ${WORKING_HOME}/freeleaps2-gitea/ - sudo mv data/ssh ${WORKING_HOME}/freeleaps2-gitea/ - sudo chown -R yaojing:staff ${WORKING_HOME}/freeleaps2-gitea - # sudo chmod -R 750 ${WORKING_HOME}/freeleaps2-gitea - echo "Gitea data copying is done" - # Check if gitea data copied successfully - if [[ ! -d "${WORKING_HOME}/freeleaps2-gitea/gitea" ]]; then - echo "ERROR: Failed to copy gitea data." - exit 1 - fi + OWNER_GROUP=$(stat -c "%U:%G" "${WORKING_HOME}") + fi - # restart gitea container - docker-compose -f docker-compose.dev.arm64.new.yaml restart gitea + # Echo OWNER_GROUP + echo "OWNER_GROUP: $OWNER_GROUP" + + # Copy gitea data to the gitea container + GITEA_HOST_DIR="${WORKING_HOME}/freeleaps2-gitea" + + # Remove existing data directories + sudo rm -rf ${GITEA_HOST_DIR}/git + sudo rm -rf ${GITEA_HOST_DIR}/gitea + sudo rm -rf ${GITEA_HOST_DIR}/ssh + + # Move data directories to the gitea container + sudo mv data/git ${GITEA_HOST_DIR}/ + 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" + + # 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 + fi + + # restart gitea container + docker-compose -f docker-compose.dev.arm64.new.yaml restart rabbitmq + sleep 10 + + docker-compose -f docker-compose.dev.arm64.new.yaml restart gitea + sleep 5 + + # restart notification if it is in the start_components + if [[ " ${start_components[@]} " =~ "notification" ]]; then + docker-compose -f docker-compose.dev.arm64.new.yaml restart notification fi else echo '============================================' @@ -957,6 +920,7 @@ echo "$USE_LOCAL_COMPONENT" > "$WORKING_HOME/.use-local-component" pushd $WORKING_HOME + IS_START_FRONTEND=false # Make a user input (Y/N) to continue pull freeleaps.com code and start if N then exit @@ -1509,29 +1473,36 @@ devbox_start_command() { # Start the backend and frontend services echo "Starting backend and frontend services..." -# Check if /home/.devbox/.backend.pid exists -if [ ! -f /home/.devbox/.backend.pid ]; then - echo "ERROR: Backend service is not running. Please run 'devbox init' first." - exit 1 -fi - -# Check if /home/.devbox/.frontend.pid exists -if [ ! -f /home/.devbox/.frontend.pid ]; then - echo "ERROR: Frontend service is not running. Please run 'devbox init' first." - exit 1 -fi - # Start the backend service echo '============================================' -echo ' Start to run start_webapi.sh' +echo ' Start to run webapi.main:app ' echo '============================================' - pushd /home/.devbox/freeleaps/apps +# CHeck if the virtual environment is created +if [ ! -f "venv_t/bin/activate" ]; then + echo "ERROR: The virtual environment cannot be created" + exit 1 +fi + +echo '============================================' +echo ' Start to activate virtual environment' +echo '============================================' +source venv_t/bin/activate +source /home/.devbox/freeleaps/apps/.env + +# Verify the virtual environment is activated +if [[ "\$VIRTUAL_ENV" != "" ]]; then + echo "Virtual environment activate: \$VIRTUAL_ENV" +else + echo "ERROR: The virtual environment cannot be startup \$VIRTUAL_ENV" + exit 1 +fi + # Check if the backend service is already running -SERVICE_API_ACCESS_PORT=\$(cat /home/.devbox/.devbox-frontend-port) -uvicorn webapi.main:app --reload --host 0.0.0.0 --port \$SERVICE_API_ACCESS_PORT > /home/.devbox/logs/backend.logs 2>&1 & +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 & BACKEND_PID=\$! # Save BACKEND_PID to a file \${WORKING_HOME}/.backend.pid: Stores the process id of backend process. @@ -1544,6 +1515,28 @@ if ! ps -p "\$BACKEND_PID" &>/dev/null; then exit 1 fi +# Test backend and frontend services +echo "Testing backend and frontend services..." + +# Test the backend service +echo "Testing backend service..." +attempt=0 +max_attempts=10 +while [ \$attempt -lt \$max_attempts ]; do + http_code=\$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:\$SERVICE_API_ACCESS_PORT/docs") + if [ "\$http_code" -eq 200 ]; then + break + fi + attempt=\$((attempt+1)) + sleep 5 +done + +if [ \$attempt -eq \$max_attempts ]; then + echo "ERROR: Backend service is not available after \$max_attempts attempts." + exit 1 +fi + + # Start the frontend service echo '============================================' @@ -1563,23 +1556,29 @@ if ! ps -p "\$FRONTEND_PID" &>/dev/null; then exit 1 fi -# Test backend and frontend services -echo "Testing backend and frontend services..." - -# Test the backend service -echo "Testing backend service..." -curl -s -o /dev/null -w "%{http_code}" "http://localhost:\$SERVICE_API_ACCESS_PORT/docs" -if [ "\$?" -ne 0 ]; then - echo "ERROR: Backend service is not available." - exit 1 -fi # Test the frontend service -echo "Testing frontend service..." -curl -s -o /dev/null -w "%{http_code}" "http://localhost:5173/" -if [ "\$?" -ne 0 ]; then - echo "ERROR: Frontend service is not available." +WEB_APP_ACCESS_PORT=\$(cat /home/.devbox/.devbox-frontend-port) + +echo "Testing frontend service... PORT: \$WEB_APP_ACCESS_PORT" +attempt=0 +max_attempts=10 +while [ \$attempt -lt \$max_attempts ]; do + HTTP_CODE=\$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:\$WEB_APP_ACCESS_PORT/") + # Check HTTP Code 200 + if [ "\$HTTP_CODE" -eq 200 ]; then + echo "Frontend is available (HTTP \$HTTP_CODE)" + break + else + echo "Attempt \$((attempt+1)): Frontend not available (HTTP \$HTTP_CODE). Waiting..." + attempt=\$((attempt+1)) + sleep 10 + fi +done + +if [ \$attempt -eq \$max_attempts ]; then + echo "ERROR: Frontend service is not available after \$max_attempts attempts." exit 1 fi @@ -1863,29 +1862,38 @@ if [[ "$FREELEAPS_ENDPOINT" == "true" ]]; then # Start the backend and frontend services echo "Starting backend and frontend services..." -# Check if /home/.devbox/.backend.pid exists -if [ ! -f /home/.devbox/.backend.pid ]; then - echo "ERROR: Backend service is not running. Please run 'devbox init' first." - exit 1 -fi - -# Check if /home/.devbox/.frontend.pid exists -if [ ! -f /home/.devbox/.frontend.pid ]; then - echo "ERROR: Frontend service is not running. Please run 'devbox init' first." - exit 1 -fi - # Start the backend service echo '============================================' -echo ' Start to run start_webapi.sh' +echo ' Start to run webapi.main:app' echo '============================================' pushd /home/.devbox/freeleaps/apps + +# CHeck if the virtual environment is created +if [ ! -f "venv_t/bin/activate" ]; then + echo "ERROR: The virtual environment cannot be created" + exit 1 +fi + +echo '============================================' +echo ' Start to activate virtual environment' +echo '============================================' +source venv_t/bin/activate +source /home/.devbox/freeleaps/apps/.env + +# Verify the virtual environment is activated +if [[ "\$VIRTUAL_ENV" != "" ]]; then + echo "Virtual environment activate: \$VIRTUAL_ENV" +else + echo "ERROR: The virtual environment cannot be startup \$VIRTUAL_ENV" + exit 1 +fi + # Check if the backend service is already running -SERVICE_API_ACCESS_PORT=\$(cat /home/.devbox/.devbox-frontend-port) -uvicorn webapi.main:app --reload --host 0.0.0.0 --port \$SERVICE_API_ACCESS_PORT > /home/.devbox/logs/backend.logs 2>&1 & +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 & BACKEND_PID=\$! # Save BACKEND_PID to a file \${WORKING_HOME}/.backend.pid: Stores the process id of backend process. @@ -1898,6 +1906,28 @@ if ! ps -p "\$BACKEND_PID" &>/dev/null; then exit 1 fi +# Test backend and frontend services +echo "Testing backend and frontend services..." + +# Test the backend service +echo "Testing backend service..." +attempt=0 +max_attempts=10 +while [ \$attempt -lt \$max_attempts ]; do + http_code=\$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:\$SERVICE_API_ACCESS_PORT/docs") + if [ "\$http_code" -eq 200 ]; then + break + fi + attempt=\$((attempt+1)) + sleep 5 +done + +if [ \$attempt -eq \$max_attempts ]; then + echo "ERROR: Backend service is not available after \$max_attempts attempts." + exit 1 +fi + + # Start the frontend service echo '============================================' @@ -1917,23 +1947,27 @@ if ! ps -p "\$FRONTEND_PID" &>/dev/null; then exit 1 fi -# Test backend and frontend services -echo "Testing backend and frontend services..." - -# Test the backend service -echo "Testing backend service..." -curl -s -o /dev/null -w "%{http_code}" "http://localhost:\$SERVICE_API_ACCESS_PORT/docs" -if [ "\$?" -ne 0 ]; then - echo "ERROR: Backend service is not available." - exit 1 -fi # Test the frontend service +WEB_APP_ACCESS_PORT=\$(cat /home/.devbox/.devbox-frontend-port) echo "Testing frontend service..." -curl -s -o /dev/null -w "%{http_code}" "http://localhost:5173/" -if [ "\$?" -ne 0 ]; then - echo "ERROR: Frontend service is not available." +attempt=0 +max_attempts=10 +while [ \$attempt -lt \$max_attempts ]; do + http_code=\$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:\$WEB_APP_ACCESS_PORT/") + if [ "\$http_code" -eq 200 ]; then + echo "Frontend service is available (HTTP \$http_code)" + break + else + echo "Attempt \$((attempt+1)): Frontend not available (HTTP \$http_code). Waiting..." + attempt=\$((attempt+1)) + sleep 10 + fi +done + +if [ \$attempt -eq \$max_attempts ]; then + echo "ERROR: Frontend service is not available after \$max_attempts attempts." exit 1 fi