Update for component init checking logic

This commit is contained in:
Tianyong Qiu 2025-02-12 16:36:07 +08:00
parent 3f3580e25e
commit 137c9936b0

View File

@ -178,18 +178,18 @@ devbox_init_usage() {
echo
# :flag.usage freeleaps username
printf " %s\n" "--freeleaps-username FREELEAPS_USERNAME (required)"
printf " Specifies the Freeleaps.com repository username (Required).\n"
printf " %s\n" "--freeleaps-username FREELEAPS_USERNAME (optional)"
printf " Specifies the Freeleaps.com repository username (Optional).\n"
echo
# :flag.usage freeleaps password
printf " %s\n" "--freeleaps-password FREELEAPS_PASSWORD (required)"
printf " Specifies the Freeleaps.com password repository (Required).\n"
printf " %s\n" "--freeleaps-password FREELEAPS_PASSWORD (optional)"
printf " Specifies the Freeleaps.com password repository (Optional).\n"
echo
# :flag.usage use local component
printf " %s\n" "--use-local-component USE_LOCAL_COMPONENT"
printf " Check if use local component or use online dev environment. (Default: false, use online service) (Optional)\n"
printf " Check if use local component or use online dev environment. (Optional, default=false)\n"
echo
# :flag.usage devsvc image repo
@ -204,7 +204,7 @@ devbox_init_usage() {
# :flag.usage devsvc image tag
printf " %s\n" "--devsvc-image-tag DEVSVC_IMAGE_TAG"
printf " Specifies the image tag for devsvc component. (Optional, default=latest-)\n"
printf " Specifies the image tag for devsvc component. (Optional, default=latest-linux-arm64)\n"
printf " %s\n" "Default: latest"
echo
@ -220,7 +220,7 @@ devbox_init_usage() {
# :flag.usage notification image tag
printf " %s\n" "--notification-image-tag NOTIFICATION_IMAGE_TAG"
printf " Specifies the image tag for notification component. (Optional, default=latest)\n"
printf " Specifies the image tag for notification component. (Optional, default=latest-linux-arm64)\n"
printf " %s\n" "Default: latest"
echo
@ -236,7 +236,7 @@ devbox_init_usage() {
# :flag.usage content image tag
printf " %s\n" "--content-image-tag CONTENT_IMAGE_TAG"
printf " Specifies the image tag for content component. (Optional, default=latest)\n"
printf " Specifies the image tag for content component. (Optional, default=latest-linux-arm64)\n"
printf " %s\n" "Default: latest"
echo
@ -252,7 +252,7 @@ devbox_init_usage() {
# :flag.usage central storage image tag
printf " %s\n" "--central_storage-image-tag CENTRAL_STORAGE_IMAGE_TAG"
printf " Specifies the image tag for central_storage component. (Optional, default=latest)\n"
printf " Specifies the image tag for central_storage component. (Optional, default=latest-linux-arm64)\n"
printf " %s\n" "Default: latest"
echo
@ -268,7 +268,7 @@ devbox_init_usage() {
# :flag.usage authentication image tag
printf " %s\n" "--authentication-image-tag AUTHENTICATION_IMAGE_TAG"
printf " Specifies the image tag for authentication component. (Optional, default=latest)\n"
printf " Specifies the image tag for authentication component. (Optional, default=latest-linux-arm64)\n"
printf " %s\n" "Default: latest"
echo
@ -729,7 +729,6 @@ devbox_init_command() {
local FREELEAPS_USERNAME="$args_freeleaps_username" # --freeleaps-username
local FREELEAPS_PASSWORD="$args_freeleaps_password" # --freeleaps-password
local USE_LOCAL_COMPONENT="$args_use_local_component" # --use-local-component
local DEVSVC_REPO="$args_devsvc_image_repo" # --devsvc-image-repo
@ -796,11 +795,8 @@ devbox_init_command() {
echo "==> Checking parameters..."
for component in "${components[@]}"; do
echo "==> Checking ${component} image repo...value: $(get_arg "--${component}-image-repo")"
if [[ -n "$(get_arg "--${component}-image-repo")" ]]; then
is_pull_all_components=false
else
start_components+=("${component}")
fi
@ -836,10 +832,12 @@ devbox_init_command() {
fi
done
echo "==> is_pull_all_components: $is_pull_all_components"
# If is_pull_all_components is true, then pull all components
if [[ "$is_pull_all_components" == true ]]; then
start_components=("${components[@]}")
fi
echo " ===================================================== "
echo "Parameters:"
echo " OS = $OS"
echo " ARCH = $ARCH"
@ -899,7 +897,6 @@ devbox_init_command() {
fi
fi
echo "==> Detected OS: $OS, ARCH: $ARCH"
# Default arch tag value if Arch is amd64 then latest-linux-amd64 else latest-linux-arm64
local arch_tag="latest-linux-${ARCH}"
@ -934,15 +931,21 @@ devbox_init_command() {
exit 1
fi
# -------------------------------------------------------------------
# 4. If .devbox-instance exists, --forceuse it
# -------------------------------------------------------------------
if [[ -f "$WORKING_HOME/.devbox-instance" && -z "$FORCE_INIT" ]]; then
echo "ERROR: DevBox already initialized. Use --force to overwrite."
exit 1
# Echo all start_components
if [[ "${#start_components[@]}" -gt 0 ]]; then
for component in "${start_components[@]}"; do
if [[ -f "$WORKING_HOME/.${component}-instance" ]]; then
echo "ERROR: Container named $component already exists. Use --force to remove it."
exit 1
fi
done
else
echo "ERROR: Container named $DEVBOX_NAME already exists. Use --force to remove it."
exit 1
fi
fi
# -------------------------------------------------------------------
# 5.install docker and check docker running
# -------------------------------------------------------------------
@ -972,13 +975,20 @@ devbox_init_command() {
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"
else
echo "ERROR: Container named $DEVBOX_NAME already exists. Use --force to remove it."
exit 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...'
@ -1068,7 +1078,6 @@ else
fi
# Save MongoDB and RabbitMQ container ids to .mongodb-instance and .rabbitmq-instance
mongo_container_id=$(docker ps -a --format '{{.Names}}' | grep "^freeleaps2-mongodb\$")
echo "$mongo_container_id" > "$WORKING_HOME/.mongodb-instance"
@ -1081,6 +1090,7 @@ echo "$USE_LOCAL_COMPONENT" > "$WORKING_HOME/.use-local-component"
pushd $WORKING_HOME
# Make a user input (Y/N) to continue pull freeleaps.com code and start if N then exit
read -p "Do you want to continue to pull freeleaps.com code and start the services? (Y/N): " user_input
if [[ "$user_input" == "N" || "$user_input" == "n" ]]; then
@ -1089,6 +1099,13 @@ if [[ "$user_input" == "N" || "$user_input" == "n" ]]; then
exit 0
fi
# Check if username and password are set
if [[ -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
# Check if freeleaps2-frontend exists, if not git clone it
if [ ! -d $WORKING_HOME/freeleaps ]; then
echo "Git cloning freeleaps.com:3443/products/freeleaps.git"
@ -1462,21 +1479,6 @@ devbox_start_command() {
local devbox_container_id=$(cat "$devbox_container_id_file_path")
echo "==> Starting DevBox services..."
# Check if DevBox container is running
if ! docker ps --no-trunc --format '{{.ID}}' | grep -q "^${devbox_container_id}\$"; then
echo "==> DevBox container is not running, starting container..."
# Start the container
if ! docker start "${devbox_container_id}"; then
echo "ERROR: Failed to start DevBox container."
exit 1
fi
echo "==> DevBox container started successfully."
else
echo "==> DevBox container is already running."
fi
# Check if use local component
if [[ -f "$WORKING_HOME/.use-local-component" ]]; then
USE_LOCAL_COMPONENT=$(cat "$WORKING_HOME/.use-local-component")
@ -1487,7 +1489,7 @@ devbox_start_command() {
if [[ "$USE_LOCAL_COMPONENT" == "true" ]]; then
# If no component is specified, start all components
if [[ -z "$COMPONENT" ]]; then
COMPONENTS=("mongodb" "rabbitmq" "devbox" "devsvc" "notification" "content" "central_storage" "authentication")
COMPONENTS=( "gitea" "mongodb" "rabbitmq" "devbox" "devsvc" "notification" "content" "central_storage" "authentication")
else
COMPONENTS=("$COMPONENT")
fi
@ -1509,91 +1511,7 @@ devbox_start_command() {
# Start the specified components
for comp in "${COMPONENTS[@]}"; do
case "$comp" in
"gitea")
echo "==> Starting Gitea..."
# Check if Gitea container file path
local gitea_container_id_file_path="${WORKING_HOME}/.gitea-instance"
if [[ ! -f "$gitea_container_id_file_path" ]]; then
echo "ERROR: Gitea container is not running. Please run 'devbox init' first."
else
local gitea_container_id=$(cat "$gitea_container_id_file_path")
if ! docker ps --no-trunc --format '{{.ID}}' | grep -q "^${gitea_container_id}\$"; then
echo "==> Gitea container is not running, starting container..."
# Start the container
if ! docker start "${gitea_container_id}"; then
echo "ERROR: Failed to start Gitea container."
exit 1
fi
echo "==> Gitea container started successfully."
else
echo "==> Gitea container is already running."
fi
fi
;;
"mongodb")
echo "==> Starting MongoDB..."
# Check if MongoDB container file path
local mongodb_container_id_file_path="${WORKING_HOME}/.mongodb-instance"
if [[ ! -f "$mongodb_container_id_file_path" ]]; then
echo "ERROR: MongoDB container is not running. Please run 'devbox init' first."
else
local mongodb_container_id=$(cat "$mongodb_container_id_file_path")
if ! docker ps --no-trunc --format '{{.ID}}' | grep -q "^${mongodb_container_id}\$"; then
echo "==> MongoDB container is not running, starting container..."
# Start the container
if ! docker start "${mongodb_container_id}"; then
echo "ERROR: Failed to start MongoDB container."
exit 1
fi
echo "==> MongoDB container started successfully."
else
echo "==> MongoDB container is already running."
fi
fi
;;
"rabbitmq")
echo "==> Starting RabbitMQ..."
# Check if RabbitMQ container file path
local rabbitmq_container_id_file_path="${WORKING_HOME}/.rabbitmq-instance"
if [[ ! -f "$rabbitmq_container_id_file_path" ]]; then
echo "ERROR: RabbitMQ container is not running. Please run 'devbox init' first."
else
local rabbitmq_container_id=$(cat "$rabbitmq_container_id_file_path")
if ! docker ps --no-trunc --format '{{.ID}}' | grep -q "^${rabbitmq_container_id}\$"; then
echo "==> RabbitMQ container is not running, starting container..."
# Start the container
if ! docker start "${rabbitmq_container_id}"; then
echo "ERROR: Failed to start RabbitMQ container."
exit 1
fi
echo "==> RabbitMQ container started successfully."
else
echo "==> RabbitMQ container is already running."
fi
fi
;;
"devbox")
echo "==> Starting DevBox..."
# Check if DevBox container file path
local devbox_container_id_file_path="${WORKING_HOME}/.devbox-instance"
if [[ ! -f "$devbox_container_id_file_path" ]]; then
echo "ERROR: DevBox container is not running. Please run 'devbox init' first."
else
local devbox_container_id=$(cat "$devbox_container_id_file_path")
if ! docker ps --no-trunc --format '{{.ID}}' | grep -q "^${devbox_container_id}\$"; then
echo "==> DevBox container is not running, starting container..."
# Start the container
if ! docker start "${devbox_container_id}"; then
echo "ERROR: Failed to start DevBox container."
exit 1
fi
echo "==> DevBox container started successfully."
else
echo "==> DevBox container is already running."
fi
fi
;;
"devsvc" | "notification" | "content" | "central_storage" | "authentication")
"gitea" | "mongodb" | "rabbitmq" | "devbox" | "devsvc" | "notification" | "content" | "central_storage" | "authentication")
echo "==> Starting $comp service..."
# Check if the component container file exists
local component_container_id_file_path="${WORKING_HOME}/.${comp}-instance"
@ -1621,7 +1539,20 @@ devbox_start_command() {
esac
done
if [[ "$FREELEAPS_ENDPOINT" == "true" ]]; then
# Sleep for 10 seconds to allow the services to start and echo 10 seconds increase from 1 to 10 in each second
for i in {1..20}; do
if docker ps --no-trunc --format '{{.ID}}' | grep -q "^${devbox_container_id}\$"; then
break
fi
echo -n "-"
sleep 1
done
if docker ps --no-trunc --format '{{.ID}}' | grep -q "^${devbox_container_id}\$"; then
echo "==> Starting Freeleaps frontend and backend services..."
# Start the backend and frontend services
docker exec -i "$devbox_container_id" bash <<EOF
# Start the backend and frontend services
@ -1703,7 +1634,12 @@ fi
echo "Backend and frontend services started successfully."
EOF
else
echo "ERROR: DevBox container is not running."
exit 1
fi
fi
@ -1718,7 +1654,7 @@ devbox_stop_command() {
# If the DevBox container is not running, exit
if [[ -z "$COMPONENT" ]]; then
COMPONENTS=("mongodb" "rabbitmq" "devbox" "devsvc" "notification" "content" "central_storage" "authentication")
COMPONENTS=("gitea" "mongodb" "rabbitmq" "devbox" "devsvc" "notification" "content" "central_storage" "authentication")
else
COMPONENTS=("$COMPONENT")
fi
@ -1726,46 +1662,34 @@ devbox_stop_command() {
# Stop the specified components
for comp in "${COMPONENTS[@]}"; do
case "$comp" in
"mongodb")
echo "==> Stopping MongoDB..."
if [[ -f "${WORKING_HOME}/.mongodb-instance" ]]; then
local container_id
container_id=$(cat "${WORKING_HOME}/.mongodb-instance")
docker stop "$container_id" &>/dev/null || true
else
echo "==> MongoDB container is not running."
fi
;;
"rabbitmq")
echo "==> Stopping RabbitMQ..."
if [[ -f "${WORKING_HOME}/.rabbitmq-instance" ]]; then
local container_id
container_id=$(cat "${WORKING_HOME}/.rabbitmq-instance")
docker stop "$container_id" &>/dev/null || true
else
echo "==> RabbitMQ container is not running."
fi
;;
"devbox")
echo "==> Stopping devbox..."
if [[ -f "${WORKING_HOME}/.devbox-instance" ]]; then
local container_id
container_id=$(cat "${WORKING_HOME}/.devbox-instance")
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"
if docker ps --no-trunc --format '{{.ID}}' | grep -q "^${container_id}\$"; then
echo "==> 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."
fi
else
echo "==> Backend service is not running."
fi
;;
"devsvc" | "notification" | "content" | "central_storage" | "authentication")
echo "==> Stopping $comp service..."
"gitea"| "mongodb"| "rabbitmq" | "devsvc" | "notification" | "content" | "central_storage" | "authentication")
if [[ -f "${WORKING_HOME}/.${comp}-instance" ]]; then
local container_id
container_id=$(cat "${WORKING_HOME}/.${comp}-instance")
docker stop "$container_id" &>/dev/null || true
if docker ps --no-trunc --format '{{.ID}}' | grep -q "^${container_id}\$"; then
echo "==> Stopping $comp service..."
docker stop "$container_id" &>/dev/null || true
else
echo "==> $comp service is not running."
fi
else
echo "==> $comp service is not running."
fi
@ -1775,9 +1699,8 @@ devbox_stop_command() {
exit 1
;;
esac
echo "==> $comp service stopped successfully."
done
echo "==> DevBox services stopped successfully."
echo "==> All conponent services stopped successfully."
}
# :command.function
@ -1895,19 +1818,6 @@ devbox_restart_command() {
fi
local devbox_container_id=$(cat "$devbox_container_id_file_path")
if ! docker ps -a --no-trunc --format '{{.ID}}' | grep -q "^${devbox_container_id}\$"; then
echo "ERROR: DevBox container is not running. Please run 'devbox init' first."
rm -f "$devbox_container_id_file_path"
exit 1
fi
if docker ps --no-trunc --format '{{.ID}}' | grep -q "^${devbox_container_id}\$"; then
echo "==> DevBox container is running."
else
echo "==> DevBox container is not running."
docker start "$devbox_container_id" &>/dev/null || true
sleep 20
fi
# Check if current environment is using local components
USE_LOCAL_COMPONENT=$(cat "${WORKING_HOME}/.use-local-component" 2>/dev/null || true)
@ -1935,41 +1845,11 @@ devbox_restart_command() {
# Stop the specified components
for comp in "${COMPONENTS[@]}"; do
case "$comp" in
"gitea")
echo "==> Stopping Gitea..."
if [[ -f "${WORKING_HOME}/.gitea-instance" ]]; then
local container_id
container_id=$(cat "${WORKING_HOME}/.gitea-instance")
docker stop "$container_id" &>/dev/null || true
else
echo "==> Gitea container is not running."
fi
;;
"mongodb")
echo "==> Stopping MongoDB..."
if [[ -f "${WORKING_HOME}/.mongodb-instance" ]]; then
local container_id
container_id=$(cat "${WORKING_HOME}/.mongodb-instance")
docker stop "$container_id" &>/dev/null || true
else
echo "==> MongoDB container is not running."
fi
;;
"rabbitmq")
echo "==> Stopping RabbitMQ..."
if [[ -f "${WORKING_HOME}/.rabbitmq-instance" ]]; then
local container_id
container_id=$(cat "${WORKING_HOME}/.rabbitmq-instance")
docker stop "$container_id" &>/dev/null || true
else
echo "==> RabbitMQ container is not running."
fi
;;
"devbox")
echo "==> Stopping devbox service..."
if [[ -f "${WORKING_HOME}/.devbox-instance" ]]; then
local container_id
container_id=$(cat "${WORKING_HOME}/.devbox-instance")
echo "==> Stopping devbox service..."
docker stop "$container_id" &>/dev/null || true
# Remove the frontend and backend pid files
@ -1980,11 +1860,12 @@ devbox_restart_command() {
fi
;;
"devsvc" | "notification" | "content" | "central_storage" | "authentication")
echo "==> Stopping $comp service..."
"gitea" | "mongodb" | "rabbitmq" | "devsvc" | "notification" | "content" | "central_storage" | "authentication")
if [[ -f "${WORKING_HOME}/.${comp}-instance" ]]; then
local container_id
container_id=$(cat "${WORKING_HOME}/.${comp}-instance")
echo "==> Stopping $comp service..."
docker stop "$container_id" &>/dev/null || true
else
echo "==> $comp service is not running."
@ -2000,47 +1881,7 @@ devbox_restart_command() {
# Start the specified components
for comp in "${COMPONENTS[@]}"; do
case "$comp" in
"gitea")
echo "==> Restarting Gitea..."
if [[ -f "${WORKING_HOME}/.gitea-instance" ]]; then
local container_id
container_id=$(cat "${WORKING_HOME}/.gitea-instance")
docker start "$container_id" &>/dev/null || true
else
echo "==> Gitea container is not running."
fi
;;
"mongodb")
echo "==> Restarting MongoDB..."
if [[ -f "${WORKING_HOME}/.mongodb-instance" ]]; then
local container_id
container_id=$(cat "${WORKING_HOME}/.mongodb-instance")
docker start "$container_id" &>/dev/null || true
else
echo "==> MongoDB container is not running."
fi
;;
"rabbitmq")
echo "==> Restarting RabbitMQ..."
if [[ -f "${WORKING_HOME}/.rabbitmq-instance" ]]; then
local container_id
container_id=$(cat "${WORKING_HOME}/.rabbitmq-instance")
docker start "$container_id" &>/dev/null || true
else
echo "==> RabbitMQ container is not running."
fi
;;
"devbox")
echo "==> Restarting devbox service..."
if [[ -f "${WORKING_HOME}/.devbox-instance" ]]; then
local container_id
container_id=$(cat "${WORKING_HOME}/.devbox-instance")
docker start "$container_id" &>/dev/null || true
else
echo "==> Devbox is not running."
fi
;;
"devsvc" | "notification" | "content" | "central_storage" | "authentication")
"gitea" | "mongodb" | "rabbitmq" | "devbox" | "devsvc" | "notification" | "content" | "central_storage" | "authentication")
echo "==> Restarting $comp service..."
if [[ -f "${WORKING_HOME}/.${comp}-instance" ]]; then
local container_id
@ -2058,6 +1899,14 @@ devbox_restart_command() {
done
if [[ "$FREELEAPS_ENDPOINT" == "true" ]]; then
# Sleep for 10 seconds to allow the services to start and echo 10 seconds increase from 1 to 10 in each second
for i in {1..20}; do
if docker ps --no-trunc --format '{{.ID}}' | grep -q "^${devbox_container_id}\$"; then
break
fi
echo -n "-"
sleep 1
done
# Start the backend and frontend services
docker exec -i "$devbox_container_id" bash <<EOF
# Start the backend and frontend services
@ -2661,8 +2510,6 @@ devbox_init_parse_requirements() {
if [ -z "$(get_arg '--use-local-component')" ]; then
add_arg '--use-local-component' "false"
fi
}