diff --git a/devbox/cli/devbox b/devbox/cli/devbox index bc6572a..15c1315 100755 --- a/devbox/cli/devbox +++ b/devbox/cli/devbox @@ -1,6 +1,9 @@ #!/usr/bin/env bash # Modifying it manually is not recommended +# All components that can be started in the DevBox container. [:COMPONENT_SETTINGS] +DEVBOX_COMPONENTS=("devsvc" "notification" "content" "central_storage" "chat" "authentication") + log_info() { echo "[INFO] $(date '+%Y-%m-%d %H:%M:%S') $*" } @@ -612,28 +615,12 @@ build_local_image() { fi } -# Define the local components and their corresponding ports -local_components_ports_keys=("devsvc" "notification" "content" "central_storage" "chat" "authentication") - -local_components_ports_values=("8007" "8003" "8013" "8005" "8012" "8004") - # used for repository username and password encoding url_encode() { echo "$1" | sed 's/@/%40/g' } -# Get the port number for a local component -get_port() { - local comp="$1" - local port="" - for i in "${!local_components_ports_keys[@]}"; do - if [ "${local_components_ports_keys[i]}" = "$comp" ]; then - port="${local_components_ports_values[i]}" - break - fi - done -} ############################################### @@ -641,6 +628,7 @@ get_port() { ############################################### init_compile_env() { +# Update for export environments [] docker exec -i "$DEVBOX_NAME" bash </dev/null || true - # Remove the frontend and backend pid files - rm -f "${WORKING_HOME}/.backend.pid" - rm -f "${WORKING_HOME}/.frontend.pid" - else - log_info "DevBox container is not running." - fi + if [ "$comp" = "devbox" ]; then + 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 + log_info "Stopping devbox..." + docker stop "$container_id" &>/dev/null || true + rm -f "${WORKING_HOME}/.backend.pid" + rm -f "${WORKING_HOME}/.frontend.pid" else - log_info "Backend service is not running." + log_info "DevBox container is not running." fi - ;; - - "gitea"| "mongodb"| "rabbitmq" | "devsvc" | "notification" | "content" | "central_storage" | "chat" | "authentication") - 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 - log_info "Stopping $comp service..." - docker stop "$container_id" &>/dev/null || true - else - log_info "$comp service is not running." - fi + else + log_info "Backend service is not running." + fi + continue + fi + + should_stop=false + + for basic in "${BASIC_COMPONENTS[@]}"; do + if [ "$comp" = "$basic" ]; then + should_stop=true + break + fi + done + + if [ "$should_stop" = false ]; then + for dev in "${DEVBOX_COMPONENTS[@]}"; do + if [ "$comp" = "$dev" ]; then + should_stop=true + break + fi + done + fi + + if [ "$should_stop" = true ]; then + 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 + log_info "Stopping $comp service..." + docker stop "$container_id" &>/dev/null || true else log_info "$comp service is not running." fi - ;; - *) - exit_with_message " Unknown component: $comp, please check the component name." 1 - ;; - esac + else + log_info "$comp service is not running." + fi + else + exit_with_message " Unknown component: $comp, please check the component name." 1 + fi done + exit_with_message "Stopped Freeleaps services successfully. " 0 @@ -2724,79 +2765,46 @@ devbox_status_command() { # If no component is specified, check all components if [[ -z "$COMPONENT" ]]; then - COMPONENTS=("mongodb" "rabbitmq" "devbox" "devsvc" "notification" "content" "central_storage" "chat" "authentication") + COMPONENTS=("mongodb" "rabbitmq" "gitea" "redis" "devbox") + COMPONENTS+=("${DEVBOX_COMPONENTS[@]}") else COMPONENTS=("$COMPONENT") fi # Check the status of the specified components + BASIC_COMPONENTS=("mongodb" "rabbitmq" "gitea" "redis") + ALL_COMPONENTS=("${BASIC_COMPONENTS[@]}" "${DEVBOX_COMPONENTS[@]}" "devbox") + for comp in "${COMPONENTS[@]}"; do - case "$comp" in - "mongodb") - 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 - log_info "[RESULT]: MongoDB container is running." - else - log_info "[RESULT]: MongoDB container is not running." - fi - else - log_info "[RESULT]: MongoDB container is not running." - fi - ;; - - "rabbitmq") - 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 - log_info "[RESULT]: RabbitMQ container is running." - else - log_info "[RESULT]: RabbitMQ container is not running." - fi - else - log_info "[RESULT]: RabbitMQ container is not running." - fi - ;; - - "devbox") - 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 - log_info "[RESULT]: devbox container is running." - else - log_info "[RESULT]: devbox container is not running." - fi - else - log_info "[RESULT]: devbox container is not running." - fi - ;; - - "devsvc" | "notification" | "content" | "central_storage" | "chat" | "authentication") - 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 - log_info "[RESULT]: $comp service is running." + case_checked=false + # devsvc, notification, content... + if [ "$case_checked" = false ]; then + for svc in "${ALL_COMPONENTS[@]}"; do + if [ "$comp" = "$svc" ]; then + 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 + log_info "[RESULT]: $comp service is running." + else + log_info "[RESULT]: $comp service is not running." + fi else log_info "[RESULT]: $comp service is not running." fi - else - log_info "[RESULT]: $comp service is not running." + case_checked=true + break fi - ;; - - *) - exit_with_message " Unknown component: $comp, please check the component name." 1 - ;; - esac + done + fi + + # unknown + if [ "$case_checked" = false ]; then + exit_with_message " Unknown component: $comp, please check the component name." 1 + fi done + exit_with_message " DevBox services status checked successfully." 0 } @@ -2870,77 +2878,100 @@ devbox_restart_command() { if [[ "$USE_LOCAL_COMPONENT" == "true" ]]; then log_info "Using local components..." if [[ -z "$COMPONENT" ]]; then - COMPONENTS=("gitea" "mongodb" "rabbitmq" "devbox" "devsvc" "notification" "content" "central_storage" "chat" "authentication") + COMPONENTS=("mongodb" "rabbitmq" "gitea" "redis" "devbox") + COMPONENTS+=("${DEVBOX_COMPONENTS[@]}") else COMPONENTS=("$COMPONENT") fi else log_info "Using remote components..." if [[ -z "$COMPONENT" ]]; then - COMPONENTS=("mongodb" "rabbitmq" "devbox") + COMPONENTS=("mongodb" "rabbitmq" "gitea" "redis" "devbox") else - if [[ "$COMPONENT" == "devsvc" || "$COMPONENT" == "notification" || "$COMPONENT" == "content" || "$COMPONENT" == "central_storage" || "$COMPONENT" == "chat" || "$COMPONENT" == "authentication" ]]; then - exit_with_message " Remote component $COMPONENT cannot be restarted, please use local components." 1 + found=false + for item in "${DEVBOX_COMPONENTS[@]}"; do + if [ "$item" = "$COMPONENT" ]; then + found=true + break + fi + done + + if [ "$found" = true ]; then + exit_with_message "Remote component $COMPONENT cannot be restarted, please use local components." 1 fi COMPONENTS=("$COMPONENT") fi fi + # Stop the specified components + BASIC_COMPONENTS=("mongodb" "rabbitmq" "gitea" "redis") + ALL_COMPONENTS=("${BASIC_COMPONENTS[@]}" "${DEVBOX_COMPONENTS[@]}" "devbox") + # Stop the specified components for comp in "${COMPONENTS[@]}"; do - case "$comp" in - "devbox") - if [[ -f "${WORKING_HOME}/.devbox-instance" ]]; then - local container_id - container_id=$(cat "${WORKING_HOME}/.devbox-instance") - 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 - log_info "Devbox is not running." - fi - ;; - - "gitea" | "mongodb" | "rabbitmq" | "devsvc" | "notification" | "content" | "central_storage" | "chat" | "authentication") - - if [[ -f "${WORKING_HOME}/.${comp}-instance" ]]; then - local container_id - container_id=$(cat "${WORKING_HOME}/.${comp}-instance") - log_info "Stopping $comp service..." - docker stop "$container_id" &>/dev/null || true - else - log_info "$comp service is not running." - fi - ;; - *) - exit_with_message " Unknown component: $comp, please check the component name." 1 - ;; - esac + if [ "$comp" = "devbox" ]; then + if [ -f "${WORKING_HOME}/.devbox-instance" ]; then + local container_id + container_id=$(cat "${WORKING_HOME}/.devbox-instance") + log_info "Stopping devbox service..." + docker stop "$container_id" &>/dev/null || true + + rm -f "${WORKING_HOME}/.backend.pid" + rm -f "${WORKING_HOME}/.frontend.pid" + else + log_info "Devbox is not running." + fi + continue + fi + + should_stop=false + for known in "${BASIC_COMPONENTS[@]}" "${DEVBOX_COMPONENTS[@]}"; do + if [ "$comp" = "$known" ]; then + should_stop=true + break + fi + done + + if [ "$should_stop" = true ]; then + if [ -f "${WORKING_HOME}/.${comp}-instance" ]; then + local container_id + container_id=$(cat "${WORKING_HOME}/.${comp}-instance") + log_info "Stopping $comp service..." + docker stop "$container_id" &>/dev/null || true + else + log_info "$comp service is not running." + fi + else + exit_with_message " Unknown component: $comp, please check the component name." 1 + fi done - # Start the specified components + # Sleep 5 seconds to allow the services to stop, for each second echo 5 seconds increase from 1 to 5 in each second by - for comp in "${COMPONENTS[@]}"; do - case "$comp" in - "gitea" | "mongodb" | "rabbitmq" | "devbox" | "devsvc" | "notification" | "content" | "central_storage" | "chat" | "authentication") - 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 - log_info "$comp service is not running." - fi - ;; - *) - exit_with_message " Unknown component: $comp, please check the component name." 1 - ;; - esac + is_known=false + for known in "${ALL_COMPONENTS[@]}"; do + if [ "$comp" = "$known" ]; then + is_known=true + break + fi + done + + if [ "$is_known" = true ]; then + 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 + log_info "$comp service is not running." + fi + else + exit_with_message " Unknown component: $comp, please check the component name." 1 + fi done + exit_with_message " DevBox services restarted successfully." 0 } @@ -3319,54 +3350,6 @@ devbox_init_parse_requirements() { add_arg '--devbox-image-tag' "devbox_local" fi - if [ -z "$(get_arg '--devsvc-image-tag')" ]; then - if [ "$current_arch" == "arm64" ]; then - add_arg '--devsvc-image-tag' "latest-linux-arm64" - else - add_arg '--devsvc-image-tag' "latest-linux-amd64" - fi - fi - - if [ -z "$(get_arg '--content-image-tag')" ]; then - if [ "$current_arch" == "arm64" ]; then - add_arg '--content-image-tag' "latest-linux-arm64" - else - add_arg '--content-image-tag' "latest-linux-amd64" - fi - fi - - if [ -z "$(get_arg '--central_storage-image-tag')" ]; then - if [ "$current_arch" == "arm64" ]; then - add_arg '--central_storage-image-tag' "latest-linux-arm64" - else - add_arg '--central_storage-image-tag' "latest-linux-amd64" - fi - fi - - if [ -z "$(get_arg '--chat-image-tag')" ]; then - if [ "$current_arch" == "arm64" ]; then - add_arg '--chat-image-tag' "latest-linux-arm64" - else - add_arg '--chat-image-tag' "latest-linux-amd64" - fi - fi - - if [ -z "$(get_arg '--notification-image-tag')" ]; then - if [ "$current_arch" == "arm64" ]; then - add_arg '--notification-image-tag' "latest-linux-arm64" - else - add_arg '--notification-image-tag' "latest-linux-amd64" - fi - fi - - if [ -z "$(get_arg '--authentication-image-tag')" ]; then - if [ "$current_arch" == "arm64" ]; then - add_arg '--authentication-image-tag' "latest-linux-arm64" - else - add_arg '--authentication-image-tag' "latest-linux-amd64" - fi - fi - if [ -z "$(get_arg '--use-custom-repository')" ]; then add_arg '--use-custom-repository' "" fi