From 12e0afb4e35216fdb45c1bbe0fd8de50944e1a38 Mon Sep 17 00:00:00 2001 From: "Xinwei Xiong(cubxxw-openim)" <3293172751nss@gmail.com> Date: Tue, 15 Aug 2023 09:31:47 +0800 Subject: [PATCH] feat: save scripts file Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> --- scripts/README.md | 2 +- scripts/docker_start_all.sh | 2 +- scripts/install/openim-crontask | 119 ++++++++++++++++++++++++++++++++ scripts/install/start_cron.sh | 51 -------------- scripts/lib/golang.sh | 2 +- scripts/lib/util.sh | 17 ++--- 6 files changed, 127 insertions(+), 66 deletions(-) create mode 100755 scripts/install/openim-crontask delete mode 100755 scripts/install/start_cron.sh diff --git a/scripts/README.md b/scripts/README.md index 604860aa5..d0ce8eeb5 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -58,7 +58,7 @@ scripts/ ├── push_start.sh # Script to start push service ├── release.sh # Script to perform release process ├── start_all.sh # Script to start all services -├── start_cron.sh # Script to start cron jobs +├── openim-crontask.sh # Script to start cron jobs ├── start_rpc_service.sh # Script to start RPC service ├── stop_all.sh # Script to stop all services └── style_info.sh # Script containing style related information diff --git a/scripts/docker_start_all.sh b/scripts/docker_start_all.sh index 4bf6dab34..057f0504d 100755 --- a/scripts/docker_start_all.sh +++ b/scripts/docker_start_all.sh @@ -29,7 +29,7 @@ need_to_start_server_shell=( ${SCRIPTS_ROOT}/msg_gateway_start.sh ${SCRIPTS_ROOT}/push_start.sh ${SCRIPTS_ROOT}/msg_transfer_start.sh - ${SCRIPTS_ROOT}/start_cron.sh + ${SCRIPTS_ROOT}/openim-crontask.sh ) component_check=start_component_check.sh diff --git a/scripts/install/openim-crontask b/scripts/install/openim-crontask new file mode 100755 index 000000000..13aea37ce --- /dev/null +++ b/scripts/install/openim-crontask @@ -0,0 +1,119 @@ +#!/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. + +set -o errexit +set +o nounset +set -o pipefail + +OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) +[[ -z ${COMMON_SOURCED} ]] && source ${OPENIM_ROOT}/scripts/install/common.sh + +readonly SERVER_NAME="openim-crontask" +readonly SERVER_PATH="${OPENIM_OUTPUT_HOSTBIN}/openim-crontask" + +openim::log::status "Start OpenIM Cron, binary root: ${SERVER_NAME}" +openim::log::info "Start OpenIM Cron, path: ${SERVER_PATH}" + +openim::util::stop_services_with_name ${SERVER_NAME} + +sleep 1 + +openim::log::status "start cron_task process" + +nohup ${SERVER_PATH} >>${log_file} 2>&1 & + +# Check launched service process +check=`ps | grep -w ./${cron_task_name} | grep -v grep| wc -l` +if [ $check -ge 1 ] +then +newPid=`ps | grep -w ./${cron_task_name} | grep -v grep|awk '{print $2}'` +allPorts="" + echo -e ${SKY_BLUE_PREFIX}"SERVICE START SUCCESS "${COLOR_SUFFIX} + echo -e ${SKY_BLUE_PREFIX}"SERVICE_NAME: "${COLOR_SUFFIX}${BACKGROUND_GREEN}${cron_task_name}${COLOR_SUFFIX} + echo -e ${SKY_BLUE_PREFIX}"PID: "${COLOR_SUFFIX}${BACKGROUND_GREEN}${newPid}${COLOR_SUFFIX} + echo -e ${SKY_BLUE_PREFIX}"LISTENING_PORT: "${COLOR_SUFFIX}${BACKGROUND_GREEN}${allPorts}${COLOR_SUFFIX} +else + echo -e ${BACKGROUND_GREEN}${cron_task_name}${COLOR_SUFFIX}${RED_PREFIX}"\n SERVICE START ERROR, PLEASE CHECK openIM.log"${COLOR_SUFFIX} +fi + + +# Print the necessary information after installation +function iam::pump::info() { +cat << EOF +iam-pumpn listen on: ${IAM_PUMP_HOST} +EOF +} + +# 安装 +function iam::pump::install() +{ + pushd ${IAM_ROOT} + + # 1. 构建 iam-pump + make build BINS=iam-pump + iam::common::sudo "cp ${LOCAL_OUTPUT_ROOT}/platforms/linux/amd64/iam-pump ${IAM_INSTALL_DIR}/bin" + + # 2. 生成并安装 iam-pump 的配置文件(iam-pump.yaml) + echo ${LINUX_PASSWORD} | sudo -S bash -c \ + "./scripts/genconfig.sh ${ENV_FILE} configs/iam-pump.yaml > ${IAM_CONFIG_DIR}/iam-pump.yaml" + + # 3. 创建并安装 iam-pump systemd unit 文件 + echo ${LINUX_PASSWORD} | sudo -S bash -c \ + "./scripts/genconfig.sh ${ENV_FILE} init/iam-pump.service > /etc/systemd/system/iam-pump.service" + + # 4. 启动 iam-pump 服务 + iam::common::sudo "systemctl daemon-reload" + iam::common::sudo "systemctl restart iam-pump" + iam::common::sudo "systemctl enable iam-pump" + iam::pump::status || return 1 + iam::pump::info + + iam::log::info "install iam-pump successfully" + popd +} + +# 卸载 +function iam::pump::uninstall() +{ + set +o errexit + iam::common::sudo "systemctl stop iam-pump" + iam::common::sudo "systemctl disable iam-pump" + iam::common::sudo "rm -f ${IAM_INSTALL_DIR}/bin/iam-pump" + iam::common::sudo "rm -f ${IAM_CONFIG_DIR}/iam-pump.yaml" + iam::common::sudo "rm -f /etc/systemd/system/iam-pump.service" + set -o errexit + iam::log::info "uninstall iam-pump successfully" +} + +# 状态检查 +function iam::pump::status() +{ + # 查看 iam-pump 运行状态,如果输出中包含 active (running) 字样说明 iam-pump 成功启动。 + systemctl status iam-pump|grep -q 'active' || { + iam::log::error "iam-pump failed to start, maybe not installed properly" + return 1 + } + + # 监听端口在配置文件中是 hardcode + if echo | telnet 127.0.0.1 7070 2>&1|grep refused &>/dev/null;then + iam::log::error "cannot access health check port, iam-pump maybe not startup" + return 1 + fi +} + +if [[ "$*" =~ iam::pump:: ]];then + eval $* +fi diff --git a/scripts/install/start_cron.sh b/scripts/install/start_cron.sh deleted file mode 100755 index 5da464cdd..000000000 --- a/scripts/install/start_cron.sh +++ /dev/null @@ -1,51 +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. - -set -o errexit -set +o nounset -set -o pipefail - -OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) -[[ -z ${COMMON_SOURCED} ]] && source ${OPENIM_ROOT}/scripts/install/common.sh - -readonly SERVER_NAME="openim-crontask" -readonly SERVER_PATH="${OPENIM_OUTPUT_HOSTBIN}/openim-crontask" - -openim::log::status "Start OpenIM Cron, binary root: ${SERVER_NAME}" -openim::log::info "Start OpenIM Cron, path: ${SERVER_PATH}" - -openim::util::stop_services_with_name ${SERVER_NAME} - -sleep 1 - -openim::log::status "start cron_task process" - -nohup ./${cron_task_name} >>$OPENIM_ROOT/logs/openIM.log 2>&1 & -#done - -#Check launched service process -check=`ps | grep -w ./${cron_task_name} | grep -v grep| wc -l` -if [ $check -ge 1 ] -then -newPid=`ps | grep -w ./${cron_task_name} | grep -v grep|awk '{print $2}'` -allPorts="" - echo -e ${SKY_BLUE_PREFIX}"SERVICE START SUCCESS "${COLOR_SUFFIX} - echo -e ${SKY_BLUE_PREFIX}"SERVICE_NAME: "${COLOR_SUFFIX}${BACKGROUND_GREEN}${cron_task_name}${COLOR_SUFFIX} - echo -e ${SKY_BLUE_PREFIX}"PID: "${COLOR_SUFFIX}${BACKGROUND_GREEN}${newPid}${COLOR_SUFFIX} - echo -e ${SKY_BLUE_PREFIX}"LISTENING_PORT: "${COLOR_SUFFIX}${BACKGROUND_GREEN}${allPorts}${COLOR_SUFFIX} -else - echo -e ${BACKGROUND_GREEN}${cron_task_name}${COLOR_SUFFIX}${RED_PREFIX}"\n SERVICE START ERROR, PLEASE CHECK openIM.log"${COLOR_SUFFIX} -fi diff --git a/scripts/lib/golang.sh b/scripts/lib/golang.sh index 20abd5875..ba9f51a1c 100755 --- a/scripts/lib/golang.sh +++ b/scripts/lib/golang.sh @@ -98,7 +98,7 @@ openim::golang::start_script_list() { push_start.sh msg_transfer_start.sh msg_gateway_start.sh - start_cron.sh + openim-crontask.sh ) local result=() for target in "${targets[@]}"; do diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 9a11feb04..690c8d043 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -378,18 +378,18 @@ openim::util::stop_services_with_name() { stopped=() # Iterate over each given service name. - for name in "$@"; do + for server_name in "$@"; do # Use the `pgrep` command to find process IDs related to the given service name. - pids=$(pgrep -f $name) + pids=$(ps aux | awk -v pattern="$server_name" '$0 ~ pattern {print $2}') for pid in $pids; do # If there's a Process ID, it means the service with the name is running. if [[ -n $pid ]]; then # Try to stop the service by killing its process. if kill -TERM $pid; then - stopped+=($name) + stopped+=($server_name) else - not_stopped+=($name) + not_stopped+=($server_name) fi fi done @@ -411,14 +411,7 @@ openim::util::stop_services_with_name() { openim::log::info "Successfully stopped the $name service." done fi - - # If any of the services couldn't be stopped, return a status of 1. - if [[ ${#not_stopped[@]} -ne 0 ]]; then - return 1 - else - openim::log::success "All specified services were stopped." - return 0 - fi + openim::log::success "All specified services were stopped." }