From edb2aa46e1ced51ae6fbaa47f4104b73a819c494 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Mon, 1 Apr 2024 20:16:25 +0800 Subject: [PATCH] Script Refactoring --- scripts-new/check-all.sh | 100 ++-------- scripts-new/install/openim-crontask.sh | 126 ------------ scripts-new/install/openim-push.sh | 150 --------------- scripts-new/install/openim-rpc.sh | 254 ------------------------- scripts-new/lib/init.sh | 14 +- scripts-new/lib/path.sh | 23 +-- scripts-new/lib/util.sh | 81 ++------ scripts-new/start-all.sh | 111 ----------- scripts-new/stop-all.sh | 60 ------ 9 files changed, 34 insertions(+), 885 deletions(-) delete mode 100644 scripts-new/install/openim-crontask.sh delete mode 100644 scripts-new/install/openim-push.sh delete mode 100644 scripts-new/install/openim-rpc.sh delete mode 100644 scripts-new/start-all.sh delete mode 100644 scripts-new/stop-all.sh diff --git a/scripts-new/check-all.sh b/scripts-new/check-all.sh index c4363a93c..250ccd2bf 100644 --- a/scripts-new/check-all.sh +++ b/scripts-new/check-all.sh @@ -24,98 +24,22 @@ -OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. -source "${OPENIM_ROOT}/scripts/install/common.sh" +OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/ +source "${OPENIM_ROOT}/lib/util.sh" +source "${OPENIM_ROOT}/define/binaries.sh" -if openim::util::is_running_in_container; then - exec >> ${DOCKER_LOG_FILE} 2>&1 -fi +for binary in "${!binaries[@]}"; do + expected_count=${binaries[$binary]} + full_path=$(get_bin_full_path "$binary") -OPENIM_VERBOSE=4 + result=$(openim::util::check_process_names "$full_path" "$expected_count") -openim::log::info "\n# Begin to check all OpenIM service" - -openim::log::status "Check all dependent service ports" -# Elegant printing function -# Elegant printing function -print_services_and_ports() { - local service_names=("$@") - local half_length=$((${#service_names[@]} / 2)) - local service_ports=("${service_names[@]:half_length}") - - echo "+-------------------------+----------+" - echo "| Service Name | Port |" - echo "+-------------------------+----------+" - - for ((index=0; index < half_length; index++)); do - printf "| %-23s | %-8s |\n" "${service_names[$index]}" "${service_ports[$index]}" - done - - echo "+-------------------------+----------+" -} - - -# Assuming OPENIM_SERVER_NAME_TARGETS and OPENIM_SERVER_PORT_TARGETS are defined -# Similarly for OPENIM_DEPENDENCY_TARGETS and OPENIM_DEPENDENCY_PORT_TARGETS - -# Print out services and their ports -print_services_and_ports "${OPENIM_SERVER_NAME_TARGETS[@]}" "${OPENIM_SERVER_PORT_TARGETS[@]}" - -# Print out dependencies and their ports -print_services_and_ports "${OPENIM_DEPENDENCY_TARGETS[@]}" "${OPENIM_DEPENDENCY_PORT_TARGETS[@]}" - -# OpenIM check -#echo "++ The port being checked: ${OPENIM_SERVER_PORT_LISTARIES[@]}" -openim::log::info "\n## Check all dependent components service ports" -#echo "++ The port being checked: ${OPENIM_DEPENDENCY_PORT_LISTARIES[@]}" - - -# Later, after discarding Docker, the Docker keyword is unreliable, and Kubepods is used -if grep -qE 'docker|kubepods' /proc/1/cgroup || [ -f /.dockerenv ]; then - openim::color::echo ${COLOR_CYAN} "Environment in the interior of the container" -else - openim::color::echo ${COLOR_CYAN}"The environment is outside the container" - openim::util::check_ports ${OPENIM_DEPENDENCY_PORT_LISTARIES[@]} -fi - -if [[ $? -ne 0 ]]; then - openim::log::error_exit "The service does not start properly, please check the port, query variable definition!" - echo "+++ https://github.com/openimsdk/open-im-server/tree/main/scripts/install/environment.sh +++" -else - openim::log::success "All components depended on by OpenIM are running normally! " -fi - - -openim::log::status "Check OpenIM service:" -openim::log::colorless "${OPENIM_OUTPUT_HOSTBIN}/openim-msgtransfer" -result=$(. $(dirname ${BASH_SOURCE})/install/openim-msgtransfer.sh openim::msgtransfer::check) -if [[ $? -ne 0 ]]; then - #echo "+++ cat openim log file >>> ${LOG_FILE}" - - openim::log::error "The service is not running properly, please check the logs $result" -fi - - -openim::log::status "Check OpenIM service:" -for item in "${OPENIM_ALL_SERVICE_LIBRARIES_NO_TRANSFER[@]}"; do - openim::log::colorless "$item" + if [ "$result" -eq 0 ]; then + echo "ok $binary" + else + echo "failed $binary, miss $result " + fi done -result=$(openim::util::check_process_names ${OPENIM_ALL_SERVICE_LIBRARIES_NO_TRANSFER[@]}) -if [[ $? -ne 0 ]]; then - #echo "+++ cat OpenIM log file >>> ${LOG_FILE}" - openim::log::error "The service is not running properly, please check the logs " - echo "$result" - exit 1 -else - openim::log::status "List the ports listened to by the OpenIM service:" - openim::util::find_ports_for_all_services ${OPENIM_ALL_SERVICE_LIBRARIES_NO_TRANSFER[@]} - openim::util::find_ports_for_all_services ${OPENIM_MSGTRANSFER_BINARY[@]} - openim::log::success "All OpenIM services are running normally! " -fi - - - - diff --git a/scripts-new/install/openim-crontask.sh b/scripts-new/install/openim-crontask.sh deleted file mode 100644 index d785b37a4..000000000 --- a/scripts-new/install/openim-crontask.sh +++ /dev/null @@ -1,126 +0,0 @@ -#!/usr/bin/env bash - -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# OpenIM CronTask Control Script -# -# Description: -# This script provides a control interface for the OpenIM CronTask service within a Linux environment. It supports two installation methods: installation via function calls to systemctl, and direct installation through background processes. -# -# Features: -# 1. Robust error handling leveraging Bash built-ins such as 'errexit', 'nounset', and 'pipefail'. -# 2. Capability to source common utility functions and configurations, ensuring environmental consistency. -# 3. Comprehensive logging tools, offering clear operational insights. -# 4. Support for creating, managing, and interacting with Linux systemd services. -# 5. Mechanisms to verify the successful running of the service. -# -# Usage: -# 1. Direct Script Execution: -# This will start the OpenIM CronTask directly through a background process. -# Example: ./openim-crontask.sh openim::crontask::start -# -# 2. Controlling through Functions for systemctl operations: -# Specific operations like installation, uninstallation, and status check can be executed by passing the respective function name as an argument to the script. -# Example: ./openim-crontask.sh openim::crontask::install -# -# Note: Ensure that the appropriate permissions and environmental variables are set prior to script execution. -# - -OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) -[[ -z ${COMMON_SOURCED} ]] && source "${OPENIM_ROOT}"/scripts/install/common.sh - -SERVER_NAME="openim-crontask" - -function openim::crontask::start() { - - rm -rf "$TMP_LOG_FILE" - - openim::log::info "Start OpenIM Cron, binary root: ${SERVER_NAME}" - openim::log::status "Start OpenIM Cron, path: ${OPENIM_CRONTASK_BINARY}" - - openim::log::status "start cron_task process, path: ${OPENIM_CRONTASK_BINARY}" - #nohup ${OPENIM_CRONTASK_BINARY} -c ${OPENIM_PUSH_CONFIG} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & - cmd="${OPENIM_CRONTASK_BINARY} -c ${OPENIM_PUSH_CONFIG}" - nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) >> "${LOG_FILE}" 2>&1 & - return 0 - -} - -###################################### Linux Systemd ###################################### -SYSTEM_FILE_PATH="/etc/systemd/system/${SERVER_NAME}.service" - -# Print the necessary information after installation -function openim::crontask::info() { -cat << EOF -openim-crontask listen on: ${OPENIM_CRONTASK_HOST} -EOF -} - -# install openim-crontask -function openim::crontask::install() { - pushd "${OPENIM_ROOT}" - - # 1. Build openim-crontask - make build BINS=${SERVER_NAME} - - openim::common::sudo "cp -r ${OPENIM_OUTPUT_HOSTBIN}/${SERVER_NAME} ${OPENIM_INSTALL_DIR}/${SERVER_NAME}" - openim::log::status "${SERVER_NAME} binary: ${OPENIM_INSTALL_DIR}/${SERVER_NAME}/${SERVER_NAME}" - - # 2. Generate and install the openim-crontask configuration file (openim-crontask.yaml) - openim::log::status "${SERVER_NAME} config file: ${OPENIM_CONFIG_DIR}/config.yaml" - - # 3. Create and install the ${SERVER_NAME} systemd unit file - echo ${LINUX_PASSWORD} | sudo -S bash -c \ - "SERVER_NAME=${SERVER_NAME} ./scripts/genconfig.sh ${ENV_FILE} deployments/templates/openim.service > ${SYSTEM_FILE_PATH}" - openim::log::status "${SERVER_NAME} systemd file: ${SYSTEM_FILE_PATH}" - - # 4. Start the openim-crontask service - openim::common::sudo "systemctl daemon-reload" - openim::common::sudo "systemctl restart ${SERVER_NAME}" - openim::common::sudo "systemctl enable ${SERVER_NAME}" - openim::crontask::status || return 1 - openim::crontask::info - - openim::log::info "install ${SERVER_NAME} successfully" - popd -} - - -# Unload -function openim::crontask::uninstall() { - set +o errexit - openim::common::sudo "systemctl stop ${SERVER_NAME}" - openim::common::sudo "systemctl disable ${SERVER_NAME}" - openim::common::sudo "rm -f ${OPENIM_INSTALL_DIR}/${SERVER_NAME}" - openim::common::sudo "rm -f ${OPENIM_CONFIG_DIR}/${SERVER_NAME}.yaml" - openim::common::sudo "rm -f /etc/systemd/system/${SERVER_NAME}.service" - - openim::log::info "uninstall ${SERVER_NAME} successfully" -} - -# Status Check -function openim::crontask::status() { - # Check the running status of the ${SERVER_NAME}. If active (running) is displayed, the ${SERVER_NAME} is started successfully. - if systemctl is-active --quiet "${SERVER_NAME}"; then - openim::log::info "${SERVER_NAME} is running successfully." - else - openim::log::error "${SERVER_NAME} failed to start, maybe not installed properly" - return 1 - fi -} - -if [[ "$*" =~ openim::crontask:: ]];then - eval $* -fi diff --git a/scripts-new/install/openim-push.sh b/scripts-new/install/openim-push.sh deleted file mode 100644 index aafb24c7b..000000000 --- a/scripts-new/install/openim-push.sh +++ /dev/null @@ -1,150 +0,0 @@ -#!/usr/bin/env bash -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# OpenIM Push Control Script -# -# Description: -# This script provides a control interface for the OpenIM Push service within a Linux environment. It supports two installation methods: installation via function calls to systemctl, and direct installation through background processes. -# -# Features: -# 1. Robust error handling leveraging Bash built-ins such as 'errexit', 'nounset', and 'pipefail'. -# 2. Capability to source common utility functions and configurations, ensuring environmental consistency. -# 3. Comprehensive logging tools, offering clear operational insights. -# 4. Support for creating, managing, and interacting with Linux systemd services. -# 5. Mechanisms to verify the successful running of the service. -# -# Usage: -# 1. Direct Script Execution: -# This will start the OpenIM push directly through a background process. -# Example: ./openim-push.sh -# -# 2. Controlling through Functions for systemctl operations: -# Specific operations like installation, uninstallation, and status check can be executed by passing the respective function name as an argument to the script. -# Example: ./openim-push.sh openim::push::install -# -# ENVIRONMENT VARIABLES: -# export OPENIM_PUSH_BINARY="8080 8081 8082" -# export OPENIM_PUSH_PORT="9090 9091 9092" -# -# Note: Ensure that the appropriate permissions and environmental variables are set prior to script execution. -# - - - - -OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) -[[ -z ${COMMON_SOURCED} ]] && source "${OPENIM_ROOT}"/scripts/install/common.sh - -SERVER_NAME="openim-push" - -function openim::push::start() { - - rm -rf "$TMP_LOG_FILE" - - openim::log::status "Start OpenIM Push, binary root: ${SERVER_NAME}" - openim::log::info "Start OpenIM Push, path: ${OPENIM_PUSH_BINARY}" - - openim::log::status "prepare start push process, path: ${OPENIM_PUSH_BINARY}" - openim::log::status "prepare start push process, port: ${OPENIM_PUSH_PORT}, prometheus port: ${PUSH_PROM_PORT}" - - OPENIM_PUSH_PORTS_ARRAY=$(openim::util::list-to-string ${OPENIM_PUSH_PORT} ) - PUSH_PROM_PORTS_ARRAY=$(openim::util::list-to-string ${PUSH_PROM_PORT} ) - - openim::log::status "push port list: ${OPENIM_PUSH_PORTS_ARRAY[@]}" - openim::log::status "prometheus port list: ${PUSH_PROM_PORTS_ARRAY[@]}" - - if [ ${#OPENIM_PUSH_PORTS_ARRAY[@]} -ne ${#PUSH_PROM_PORTS_ARRAY[@]} ]; then - openim::log::error "The length of the two port lists is different!" - fi - - for (( i=0; i<${#OPENIM_PUSH_PORTS_ARRAY[@]}; i++ )); do - openim::log::info "start push process, port: ${OPENIM_PUSH_PORTS_ARRAY[$i]}, prometheus port: ${PUSH_PROM_PORTS_ARRAY[$i]}" - cmd="${OPENIM_PUSH_BINARY} --port ${OPENIM_PUSH_PORTS_ARRAY[$i]} -c ${OPENIM_PUSH_CONFIG} --prometheus_port ${PUSH_PROM_PORTS_ARRAY[$i]}" - #nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) >/dev/null & - nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) >> "${LOG_FILE}" 2>&1 & - #nohup ${OPENIM_PUSH_BINARY} --port ${OPENIM_PUSH_PORTS_ARRAY[$i]} -c ${OPENIM_PUSH_CONFIG} --prometheus_port ${PUSH_PROM_PORTS_ARRAY[$i]} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & - done - return 0 - -} - -###################################### Linux Systemd ###################################### -SYSTEM_FILE_PATH="/etc/systemd/system/${SERVER_NAME}.service" - -# Print the necessary information after installation -function openim::push::info() { -cat << EOF -openim-push listen on: ${OPENIM_PUSH_HOST}:${OPENIM_PUSH_PORT} -EOF -} - -# install openim-push -function openim::push::install() { - pushd "${OPENIM_ROOT}" - - # 1. Build openim-push - make build BINS=${SERVER_NAME} - openim::common::sudo "cp -r ${OPENIM_OUTPUT_HOSTBIN}/${SERVER_NAME} ${OPENIM_INSTALL_DIR}/${SERVER_NAME}" - openim::log::status "${SERVER_NAME} binary: ${OPENIM_INSTALL_DIR}/${SERVER_NAME}/${SERVER_NAME}" - - # 2. Generate and install the openim-push configuration file (config) - openim::log::status "${SERVER_NAME} config file: ${OPENIM_CONFIG_DIR}/config.yaml" - - # 3. Create and install the ${SERVER_NAME} systemd unit file - echo ${LINUX_PASSWORD} | sudo -S bash -c \ - "SERVER_NAME=${SERVER_NAME} ./scripts/genconfig.sh ${ENV_FILE} deployments/templates/openim.service > ${SYSTEM_FILE_PATH}" - openim::log::status "${SERVER_NAME} systemd file: ${SYSTEM_FILE_PATH}" - - # 4. Start the openim-push service - openim::common::sudo "systemctl daemon-reload" - openim::common::sudo "systemctl restart ${SERVER_NAME}" - openim::common::sudo "systemctl enable ${SERVER_NAME}" - openim::push::status || return 1 - openim::push::info - - openim::log::info "install ${SERVER_NAME} successfully" - popd -} - -# Unload -function openim::push::uninstall() { - set +o errexit - openim::common::sudo "systemctl stop ${SERVER_NAME}" - openim::common::sudo "systemctl disable ${SERVER_NAME}" - openim::common::sudo "rm -f ${OPENIM_INSTALL_DIR}/${SERVER_NAME}" - openim::common::sudo "rm -f ${OPENIM_CONFIG_DIR}/${SERVER_NAME}.yaml" - openim::common::sudo "rm -f /etc/systemd/system/${SERVER_NAME}.service" - - openim::log::info "uninstall ${SERVER_NAME} successfully" -} - -# Status Check -function openim::push::status() { - # Check the running status of the ${SERVER_NAME}. If active (running) is displayed, the ${SERVER_NAME} is started successfully. - systemctl status ${SERVER_NAME}|grep -q 'active' || { - openim::log::error "${SERVER_NAME} failed to start, maybe not installed properly" - return 1 - } - - # The listening port is hardcode in the configuration file - if echo | telnet ${OPENIM_MSGGATEWAY_HOST} ${OPENIM_PUSH_PORT} 2>&1|grep refused &>/dev/null;then # Assuming a different port for push - openim::log::error "cannot access health check port, ${SERVER_NAME} maybe not startup" - return 1 - fi -} - -if [[ "$*" =~ openim::push:: ]];then - eval $* -fi diff --git a/scripts-new/install/openim-rpc.sh b/scripts-new/install/openim-rpc.sh deleted file mode 100644 index 1265a11c6..000000000 --- a/scripts-new/install/openim-rpc.sh +++ /dev/null @@ -1,254 +0,0 @@ -#!/usr/bin/env bash - -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# OpenIM RPC Service Control Script -# -# Description: -# This script provides a control interface for the OpenIM RPC service within a Linux environment. It offers functionalities to start multiple RPC services, each denoted by their respective names under openim::rpc::service_name. -# -# Features: -# 1. Robust error handling using Bash built-ins like 'errexit', 'nounset', and 'pipefail'. -# 2. The capability to source common utility functions and configurations to ensure uniform environmental settings. -# 3. Comprehensive logging functionalities, providing a detailed understanding of operational processes. -# 4. Provision for declaring and managing a set of RPC services, each associated with its unique name and corresponding ports. -# 5. The ability to define and associate Prometheus ports for service monitoring purposes. -# 6. Functionalities to start each RPC service, along with its designated ports, in a sequence. -# -# Usage: -# 1. Direct Script Execution: -# This initiates all the RPC services declared under the function openim::rpc::service_name. -# Example: ./openim-rpc-{rpc-name}.sh openim::rpc::start -# 2. Controlling through Functions for systemctl operations: -# Specific operations like installation, uninstallation, and status check can be executed by passing the respective function name as an argument to the script. -# Example: ./openim-rpc-{rpc-name}.sh openim::rpc::install -# -# Note: Before executing this script, ensure that the necessary permissions are granted and relevant environmental variables are set. -# - -OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) -[[ -z ${COMMON_SOURCED} ]] && source "${OPENIM_ROOT}"/scripts/install/common.sh - -SERVER_NAME="openim-rpc" -readonly OPENIM_RPC_CONFIG="${OPENIM_ROOT}"/config - -openim::rpc::service_name() { - local targets=( - openim-rpc-user - openim-rpc-friend - openim-rpc-msg - openim-rpc-group - openim-rpc-auth - openim-rpc-conversation - openim-rpc-third - ) - echo "${targets[@]}" -} -IFS=" " read -ra OPENIM_RPC_SERVICE_TARGETS <<< "$(openim::rpc::service_name)" -readonly OPENIM_RPC_SERVICE_TARGETS -readonly OPENIM_RPC_SERVICE_LISTARIES=("${OPENIM_RPC_SERVICE_TARGETS[@]##*/}") - - -OPENIM_ALL_RPC_FULL_PATH=() -for target in openim::rpc::service_name; do - OPENIM_ALL_RPC_FULL_PATH+=("${OPENIM_OUTPUT_HOSTBIN}/${target}") -done -readonly OPENIM_ALL_RPC_FULL_PATH - - - - -# Make sure the environment is only called via common to avoid too much nesting -openim::rpc::service_port() { - local targets=( - ${OPENIM_USER_PORT} # User service 10110 - ${OPENIM_FRIEND_PORT} # Friend service 10120 - ${OPENIM_MESSAGE_PORT} # Message service 10130 - # ${OPENIM_MESSAGE_GATEWAY_PORT} # Message gateway 10140 - ${OPENIM_GROUP_PORT} # Group service 10150 - ${OPENIM_AUTH_PORT} # Authorization service 10160 - # ${OPENIM_PUSH_PORT} # Push service 10170 - ${OPENIM_CONVERSATION_PORT} # Conversation service 10180 - ${OPENIM_THIRD_PORT} # Third-party service 10190 - ) - echo "${targets[@]}" -} -IFS=" " read -ra OPENIM_RPC_PORT_TARGETS <<< "$(openim::rpc::service_port)" -readonly OPENIM_RPC_PORT_TARGETS -readonly OPENIM_RPC_PORT_LISTARIES=("${OPENIM_RPC_PORT_TARGETS[@]##*/}") - -openim::rpc::prometheus_port() { - # Declare an array to hold all the Prometheus ports for different services - local targets=( - ${USER_PROM_PORT} # Prometheus port for user service - ${FRIEND_PROM_PORT} # Prometheus port for friend service - ${MESSAGE_PROM_PORT} # Prometheus port for message service - ${GROUP_PROM_PORT} # Prometheus port for group service - ${AUTH_PROM_PORT} # Prometheus port for authentication service - ${CONVERSATION_PROM_PORT} # Prometheus port for conversation service - ${THIRD_PROM_PORT} # Prometheus port for third-party integrations service - ) - # Print the list of ports - echo "${targets[@]}" -} -IFS=" " read -ra OPENIM_RPC_PROM_PORT_TARGETS <<< "$(openim::rpc::prometheus_port)" -readonly OPENIM_RPC_PROM_PORT_TARGETS -readonly OPENIM_RPC_PROM_PORT_LISTARIES=("${OPENIM_RPC_PROM_PORT_TARGETS[@]##*/}") - -function openim::rpc::start() { - rm -rf "$TMP_LOG_FILE" - - echo "OPENIM_RPC_SERVICE_LISTARIES: ${OPENIM_RPC_SERVICE_LISTARIES[@]}" - echo "OPENIM_RPC_PROM_PORT_LISTARIES: ${OPENIM_RPC_PROM_PORT_LISTARIES[@]}" - echo "OPENIM_RPC_PORT_LISTARIES: ${OPENIM_RPC_PORT_LISTARIES[@]}" - - openim::log::info "Starting ${SERVER_NAME} ..." - - printf "+------------------------+-------+-----------------+\n" - printf "| Service Name | Port | Prometheus Port |\n" - printf "+------------------------+-------+-----------------+\n" - - length=${#OPENIM_RPC_SERVICE_LISTARIES[@]} - - for ((i=0; i<$length; i++)); do - printf "| %-22s | %-5s | %-15s |\n" "${OPENIM_RPC_SERVICE_LISTARIES[$i]}" "${OPENIM_RPC_PORT_LISTARIES[$i]}" "${OPENIM_RPC_PROM_PORT_LISTARIES[$i]}" - printf "+------------------------+-------+-----------------+\n" - done - - - # start all rpc services - for ((i = 0; i < ${#OPENIM_RPC_SERVICE_LISTARIES[*]}; i++)); do - - openim::log::info "OpenIM ${OPENIM_RPC_SERVICE_LISTARIES[$i]} config path: ${OPENIM_RPC_CONFIG}" - # Get the service and Prometheus ports. - OPENIM_RPC_SERVICE_PORTS=( $(openim::util::list-to-string ${OPENIM_RPC_PORT_LISTARIES[$i]}) ) - read -a OPENIM_RPC_SERVICE_PORTS_ARRAY <<< ${OPENIM_RPC_SERVICE_PORTS} - - OPENIM_RPC_PROM_PORTS=( $(openim::util::list-to-string ${OPENIM_RPC_PROM_PORT_LISTARIES[$i]}) ) - read -a OPENIM_RPC_PROM_PORTS_ARRAY <<< ${OPENIM_RPC_PROM_PORTS} - - for ((j = 0; j < ${#OPENIM_RPC_SERVICE_PORTS_ARRAY[@]}; j++)); do - openim::log::info "Starting ${OPENIM_RPC_SERVICE_LISTARIES[$i]} service, port: ${OPENIM_RPC_SERVICE_PORTS[j]}, prometheus port: ${OPENIM_RPC_PROM_PORTS[j]}, binary root: ${OPENIM_OUTPUT_HOSTBIN}/${OPENIM_RPC_SERVICE_LISTARIES[$i]}" - result=$(openim::rpc::start_service "${OPENIM_RPC_SERVICE_LISTARIES[$i]}" "${OPENIM_RPC_SERVICE_PORTS[j]}" "${OPENIM_RPC_PROM_PORTS[j]}") - if [[ $? -ne 0 ]]; then - openim::log::error "start ${SERVER_NAME} failed" - else - openim::log::info "$result" - fi - done - done - - - return 0 -} - -function openim::rpc::start_service() { - local binary_name="$1" - local service_port="$2" - local prometheus_port="$3" - - local cmd="${OPENIM_OUTPUT_HOSTBIN}/${binary_name} --port ${service_port} -c ${OPENIM_RPC_CONFIG}" - - if [ -n "${prometheus_port}" ]; then - printf "Specifying prometheus port: %s\n" "${prometheus_port}" - cmd="${cmd} --prometheus_port ${prometheus_port}" - fi - #nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & - #nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) >/dev/null & - nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) >> "${LOG_FILE}" 2>&1 & - return 0 -} - -###################################### Linux Systemd ###################################### -declare -A SYSTEM_FILE_PATHS -for service in "${OPENIM_RPC_SERVICE_LISTARIES[@]}"; do - SYSTEM_FILE_PATHS["$service"]="/etc/systemd/system/${service}.service" -done - -# Print the necessary information after installation -function openim::rpc::info() { - for service in "${OPENIM_RPC_SERVICE_LISTARIES[@]}"; do - echo "${service} listen on: ${OPENIM_RPC_PORT_LISTARIES[@]}" - done -} - -# install openim-rpc -function openim::rpc::install() { - pushd "${OPENIM_ROOT}" - - # 1. Build openim-rpc - for service in "${OPENIM_RPC_SERVICE_LISTARIES[@]}"; do - make build BINS=${service} - openim::common::sudo "cp -r ${OPENIM_OUTPUT_HOSTBIN}/${service} ${OPENIM_INSTALL_DIR}/${service}" - openim::log::status "${service} binary: ${OPENIM_INSTALL_DIR}/${service}/${service}" - done - - # 2. Generate and install the openim-rpc configuration file (config) - openim::log::status "openim-rpc config file: ${OPENIM_CONFIG_DIR}/config.yaml" - - # 3. Create and install the systemd unit files - for service in "${OPENIM_RPC_SERVICE_LISTARIES[@]}"; do - echo ${LINUX_PASSWORD} | sudo -S bash -c \ - "SERVER_NAME=${service} ./scripts/genconfig.sh ${ENV_FILE} deployments/templates/openim.service > ${SYSTEM_FILE_PATHS[$service]}" - openim::log::status "${service} systemd file: ${SYSTEM_FILE_PATHS[$service]}" - done - - # 4. Start the openim-rpc services - openim::common::sudo "systemctl daemon-reload" - for service in "${OPENIM_RPC_SERVICE_LISTARIES[@]}"; do - openim::common::sudo "systemctl restart ${service}" - openim::common::sudo "systemctl enable ${service}" - done - openim::rpc::status || return 1 - openim::rpc::info - - openim::log::info "install openim-rpc successfully" - popd -} - -# Unload -function openim::rpc::uninstall() { - set +o errexit - for service in "${OPENIM_RPC_SERVICE_LISTARIES[@]}"; do - openim::common::sudo "systemctl stop ${service}" - openim::common::sudo "systemctl disable ${service}" - openim::common::sudo "rm -f ${OPENIM_INSTALL_DIR}/${service}" - openim::common::sudo "rm -f ${OPENIM_CONFIG_DIR}/${service}.yaml" - openim::common::sudo "rm -f ${SYSTEM_FILE_PATHS[$service]}" - done - - openim::log::info "uninstall openim-rpc successfully" -} - -# Status Check -function openim::rpc::status() { - for service in "${OPENIM_RPC_SERVICE_LISTARIES[@]}"; do - # Check the running status of the ${service}. If active (running) is displayed, the ${service} is started successfully. - systemctl status ${service}|grep -q 'active' || { - openim::log::error "${service} failed to start, maybe not installed properly" - return 1 - } - - # The listening port is hardcoded in the configuration file - if echo | telnet ${OPENIM_MSGGATEWAY_HOST} ${OPENIM_RPC_PORT_LISTARIES[@]} 2>&1|grep refused &>/dev/null;then - openim::log::error "cannot access health check port, ${service} maybe not startup" - return 1 - fi - done -} - -if [[ "$*" =~ openim::rpc:: ]];then - eval $* -fi diff --git a/scripts-new/lib/init.sh b/scripts-new/lib/init.sh index 6f12db36e..139c0cd05 100644 --- a/scripts-new/lib/init.sh +++ b/scripts-new/lib/init.sh @@ -32,11 +32,13 @@ OPENIM_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)" OPENIM_OUTPUT_SUBPATH="${OPENIM_OUTPUT_SUBPATH:-_output}" OPENIM_OUTPUT="${OPENIM_ROOT}/${OPENIM_OUTPUT_SUBPATH}" -OPENIM_OUTPUT_BINPATH="${OPENIM_OUTPUT}/bin/platforms" -OPENIM_OUTPUT_BINTOOLPATH="${OPENIM_OUTPUT}/bin/tools" -OPENIM_OUTPUT_TOOLS="${OPENIM_OUTPUT}/tools" -OPENIM_OUTPUT_TMP="${OPENIM_OUTPUT}/tmp" -OPENIM_OUTPUT_LOGS="${OPENIM_OUTPUT}/logs" +OPENIM_OUTPUT_BINPATH="${OPENIM_OUTPUT}/bin/platforms/" +OPENIM_OUTPUT_BINTOOLPATH="${OPENIM_OUTPUT}/bin/tools/" +OPENIM_OUTPUT_TOOLS="${OPENIM_OUTPUT}/tools/" +OPENIM_OUTPUT_TMP="${OPENIM_OUTPUT}/tmp/" +OPENIM_OUTPUT_LOGS="${OPENIM_OUTPUT}/logs/" +OPENIM_OUTPUT_CONFIG="${OPENIM_ROOT}/config/" + # This controls rsync compression. Set to a value > 0 to enable rsync # compression for build container @@ -61,8 +63,10 @@ openim::util::ensure-bash-version . $(dirname ${BASH_SOURCE})/golang.sh . $(dirname ${BASH_SOURCE})/chat.sh + OPENIM_OUTPUT_HOSTBIN="${OPENIM_OUTPUT_BINPATH}/$(openim::util::host_platform)" export OPENIM_OUTPUT_HOSTBIN + OPENIM_OUTPUT_HOSTBIN_TOOLS="${OPENIM_OUTPUT_BINTOOLPATH}/$(openim::util::host_platform)" export OPENIM_OUTPUT_HOSTBIN_TOOLS diff --git a/scripts-new/lib/path.sh b/scripts-new/lib/path.sh index bfe4483f3..80cfb7e6d 100644 --- a/scripts-new/lib/path.sh +++ b/scripts-new/lib/path.sh @@ -1,34 +1,13 @@ #!/usr/bin/env bash -source "$(dirname "${BASH_SOURCE[0]}")/../lib/util.sh" source "$(dirname "${BASH_SOURCE[0]}")/../lib/init.sh" -get_conf_dir() { - local project_path="$1" - echo "${project_path}/conf/" -} - -get_log_dir() { - local project_path="$1" - echo "${project_path}/_output/logs/" -} - -get_bin_dir() { - local project_path="$1" - echo "${project_path}/_output/bin/" -} get_bin_full_path() { - local project_path="$1" - local bin_name="$2" - - local host_platform=$(openim::util::host_platform) - - local bin_dir=$(get_bin_dir "$project_path") - + local bin_name="$1" local bin_full_path="${OPENIM_OUTPUT_HOSTBIN}/${bin_name}" echo ${bin_full_path} } diff --git a/scripts-new/lib/util.sh b/scripts-new/lib/util.sh index 13bd263ca..eb9c0e458 100644 --- a/scripts-new/lib/util.sh +++ b/scripts-new/lib/util.sh @@ -382,80 +382,23 @@ openim::util::check_ports() { # User: # openim::util::check_process_names nginx mysql redis # The function returns a status of 1 if any of the processes is not running. + openim::util::check_process_names() { - # Function to get the port of a process - get_port() { - local pid=$1 - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - # Linux - ss -ltnp 2>/dev/null | grep $pid | awk '{print $4}' | cut -d ':' -f2 - elif [[ "$OSTYPE" == "darwin"* ]]; then - # macOS - lsof -nP -iTCP -sTCP:LISTEN -a -p $pid | awk 'NR>1 {print $9}' | sed 's/.*://' + local process_path="$1" + local expected_count="$2" + + + local running_count=$(ps -ef | grep "$process_path" | grep -v grep | wc -l) + + if [ "$running_count" -eq "$expected_count" ]; then + return 0 else - echo "Unsupported OS" - return 1 + return $(($expected_count - $running_count)) fi - } - - # Arrays to collect details of processes - local not_started=() - local started=() +} + - # Iterate over each given process name - for process_name in "$@"; do - # Use `pgrep` to find process IDs related to the given process name - local pids=($(pgrep -f $process_name)) - - # Check if any process IDs were found - if [[ ${#pids[@]} -eq 0 ]]; then - not_started+=($process_name) - else - # If there are PIDs, loop through each one - for pid in "${pids[@]}"; do - local command=$(ps -p $pid -o comm=) - local start_time=$(ps -p $pid -o lstart=) - local port=$(get_port $pid) - - # Check if port information was found for the PID - if [[ -z $port ]]; then - port="N/A" - fi - - started+=("Process $process_name - Command: $command, PID: $pid, Port: $port, Start time: $start_time") - done - fi - done - - # Print information - if [[ ${#not_started[@]} -ne 0 ]]; then - echo "Not started processes:" - for process_name in "${not_started[@]}"; do - echo "Process $process_name is not started." - done - fi - - if [[ ${#started[@]} -ne 0 ]]; then - echo - echo "Started processes:" - for info in "${started[@]}"; do - echo "$info" - done - fi - - # Return status - if [[ ${#not_started[@]} -ne 0 ]]; then - #openim::color::echo $COLOR_RED "OpenIM Stdout Log >> cat ${LOG_FILE}" - #openim::color::echo $COLOR_RED "OpenIM Stderr Log >> cat ${STDERR_LOG_FILE}" - cat "$TMP_LOG_FILE" | awk '{print "\033[31m" $0 "\033[0m"}' - return 1 - else - echo "" - openim::log::success "All processes are running." - return 0 - fi -} openim::util::check_process_names_for_stop() { # Function to get the port of a process diff --git a/scripts-new/start-all.sh b/scripts-new/start-all.sh deleted file mode 100644 index 3b95dace1..000000000 --- a/scripts-new/start-all.sh +++ /dev/null @@ -1,111 +0,0 @@ -#!/usr/bin/env bash -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -#FIXME This script is the startup script for multiple servers. -#FIXME The full names of the shell scripts that need to be started are placed in the `need_to_start_server_shell` array. - -OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. -source "${OPENIM_ROOT}/scripts/install/common.sh" - -# Function to execute the scripts. -function execute_start_scripts() { - for script_path in "${OPENIM_SERVER_SCRIPT_START_LIST[@]}"; do - # Extract the script name without extension for argument generation. - script_name_with_prefix=$(basename "$script_path" .sh) - - # Remove the "openim-" prefix. - script_name=${script_name_with_prefix#openim-} - - # Construct the argument based on the script name. - arg="openim::${script_name}::start" - - # Check if the script file exists and is executable. - if [[ -x "$script_path" ]]; then - openim::log::colorless "Starting script: ${script_path##*/}" # Log the script name. - # Execute the script with the constructed argument. - result=$("$script_path" "$arg") - if [[ $? -ne 0 ]]; then - openim::log::error "Start script: ${script_path##*/} failed" - openim::log::error "$result" - return 1 - fi - - else - openim::log::errexit "Script ${script_path##*/} is missing or not executable." - return 1 - fi - done -} - -if openim::util::is_running_in_container; then - exec >> ${DOCKER_LOG_FILE} 2>&1 -fi - -openim::golang::check_openim_binaries -if [[ $? -ne 0 ]]; then - openim::log::error "OpenIM binaries are not found. Please run 'make build' to build binaries." - "${OPENIM_ROOT}"/scripts/build-all-service.sh -fi - -"${OPENIM_ROOT}"/scripts/init-config.sh --skip - -#openim::log::print_blue "Execute the following script in sequence: ${OPENIM_SERVER_SCRIPTARIES[@]}" - -# TODO Prelaunch tools, simple for now, can abstract functions later -TOOLS_START_SCRIPTS_PATH=${START_SCRIPTS_PATH}/openim-tools.sh - -openim::log::status "Start the pre-start tools:" - -# if ! ${TOOLS_START_SCRIPTS_PATH} openim::tools::pre-start; then -# openim::log::error "Start the pre-start tools, aborting!" -# exit 1 -# fi - - -openim::log::colorless "pre-start has been successfully completed!" - -result=$("${OPENIM_ROOT}"/scripts/stop-all.sh) -if [[ $? -ne 0 ]]; then - openim::log::error "View the error logs from this startup. ${LOG_FILE} \n" - openim::log::error "Some programs have not exited; the start process is aborted .\n $result" - exit 1 -fi - -openim::log::status "Start the OpenIM startup scripts: " -execute_start_scripts -openim::log::status "OpenIM startup scripts have been successfully completed!" - -sleep 2 - -result=$(. $(dirname ${BASH_SOURCE})/install/openim-msgtransfer.sh openim::msgtransfer::check) -if [[ $? -ne 0 ]]; then - openim::log::error "The OpenIM services may fail to start.\n $result" - exit 1 -fi - - -result=$(openim::util::check_process_names ${OPENIM_ALL_SERVICE_LIBRARIES_NO_TRANSFER[@]}) -if [[ $? -ne 0 ]]; then - openim::log::error "The OpenIM services may fail to start.\n $result" - exit 1 -fi - -openim::log::status "Start the post-start tools:" -# ${TOOLS_START_SCRIPTS_PATH} openim::tools::post-start -openim::log::status "post-start has been successfully completed!" -openim::util::find_ports_for_all_services ${OPENIM_ALL_SERVICE_LIBRARIES_NO_TRANSFER[@]} -openim::util::find_ports_for_all_services ${OPENIM_MSGTRANSFER_BINARY[@]} - -openim::log::success "All OpenIM services have been successfully started!" \ No newline at end of file diff --git a/scripts-new/stop-all.sh b/scripts-new/stop-all.sh deleted file mode 100644 index b2572f7d5..000000000 --- a/scripts-new/stop-all.sh +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/env bash -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# This script is stop all openim service -# -# Usage: `scripts/stop.sh`. -# Encapsulated as: `make stop`. - - - - - -OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. - -source "${OPENIM_ROOT}/scripts/install/common.sh" - -openim::log::status "Begin to stop all openim service" - -openim::log::status "Stop all processes in the path ${OPENIM_OUTPUT_HOSTBIN}" - -openim::util::stop_services_with_name "${OPENIM_OUTPUT_HOSTBIN}" -# todo OPENIM_ALL_SERVICE_LIBRARIES - - - - -max_retries=15 -attempt=0 - -while [[ $attempt -lt $max_retries ]] -do - result=$(openim::util::check_process_names_for_stop) - - if [[ $? -ne 0 ]]; then - if [[ $attempt -ne 0 ]] ; then - echo "+++ cat openim log file >>> ${LOG_FILE} " $attempt - openim::log::error "stop process failed. continue waiting\n" "${result}" - fi - sleep 1 - ((attempt++)) - else - openim::log::success " All openim processes to be stopped" - exit 0 - fi -done - -openim::log::error "openim processes stopped failed" -exit 1