Update for fix start and stop issue and add some friendly log message for user to look the backend and frontend logs

This commit is contained in:
timqiu 2025-03-18 01:57:09 +08:00
parent 387a96d08f
commit 054c54c5b4

View File

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