From b7851eca620212adacb2f13f1721ceb9a780fc96 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Tue, 2 Apr 2024 19:59:55 +0800 Subject: [PATCH] Script Refactoring --- scripts-new/bricks.sh | 5 +- scripts-new/lib/init.sh | 3 +- scripts-new/lib/util.sh | 223 +--------------------------------------- 3 files changed, 5 insertions(+), 226 deletions(-) diff --git a/scripts-new/bricks.sh b/scripts-new/bricks.sh index c8d4f515b..fb8a98cb4 100644 --- a/scripts-new/bricks.sh +++ b/scripts-new/bricks.sh @@ -13,8 +13,7 @@ stop_binaries() { done } -LOG_FILE=log.file -ERR_LOG_FILE=err.log.file + #启动所有的二进制 start_binaries() { @@ -26,7 +25,7 @@ start_binaries() { for ((i=0; i> "${LOG_FILE}" 2> >(tee -a "$ERR_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) & + nohup "${cmd[@]}" >> "${OPENIM_INIT_LOG_FILE}" 2> >(tee -a "$OPENIM_INIT_ERR_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) & done done } diff --git a/scripts-new/lib/init.sh b/scripts-new/lib/init.sh index 139c0cd05..d7ac32135 100644 --- a/scripts-new/lib/init.sh +++ b/scripts-new/lib/init.sh @@ -38,7 +38,8 @@ OPENIM_OUTPUT_TOOLS="${OPENIM_OUTPUT}/tools/" OPENIM_OUTPUT_TMP="${OPENIM_OUTPUT}/tmp/" OPENIM_OUTPUT_LOGS="${OPENIM_OUTPUT}/logs/" OPENIM_OUTPUT_CONFIG="${OPENIM_ROOT}/config/" - +OPENIM_INIT_ERR_LOG_FILE="$OPENIM_OUTPUT_LOGS/openim-init-err.log" +OPENIM_INIT_LOG_FILE="$OPENIM_OUTPUT_LOGS/openim-init.log" # This controls rsync compression. Set to a value > 0 to enable rsync # compression for build container diff --git a/scripts-new/lib/util.sh b/scripts-new/lib/util.sh index d6fb88358..ec5bd64e0 100644 --- a/scripts-new/lib/util.sh +++ b/scripts-new/lib/util.sh @@ -22,8 +22,7 @@ # OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) # source "${OPENIM_ROOT}/scripts/lib/logging.sh" -#1、将IP写在一个文件里,比如文件名为hosts_file,一行一个IP地址。 -#2、修改ssh-mutual-trust.sh里面的用户名及密码,默认为root用户及密码123。 + # hosts_file_path="path/to/your/hosts/file" # openim:util::setup_ssh_key_copy "$hosts_file_path" "root" "123" function openim:util::setup_ssh_key_copy() { @@ -283,105 +282,10 @@ openim::util::check_docker_and_compose_versions() { } - -# The `openim::util::check_ports` function analyzes the state of processes based on given ports. -# It accepts multiple ports as arguments and prints: -# 1. The state of the process (whether it's running or not). -# 2. The start time of the process if it's running. -# User: -# openim::util::check_ports 8080 8081 8082 -# The function returns a status of 1 if any of the processes is not running. -openim::util::check_ports() { - # An array to collect ports of processes that are not running. - local not_started=() - - # An array to collect information about processes that are running. - local started=() - - echo "Checking ports: $*" - # Iterate over each given port. - for port in "$@"; do - # Initialize variables - # Check the OS and use the appropriate command - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - if command -v ss > /dev/null 2>&1; then - info=$(ss -ltnp | grep ":$port" || true) - else - info=$(netstat -ltnp | grep ":$port" || true) - fi - elif [[ "$OSTYPE" == "darwin"* ]]; then - # For macOS, use lsof - info=$(lsof -P -i:"$port" | grep "LISTEN" || true) - fi - - # Check if any process is using the port - if [[ -z $info ]]; then - not_started+=($port) - else - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - # Extract relevant details for Linux: Process Name, PID, and FD. - details=$(echo $info | sed -n 's/.*users:(("\([^"]*\)",pid=\([^,]*\),fd=\([^)]*\))).*/\1 \2 \3/p') - command=$(echo $details | awk '{print $1}') - pid=$(echo $details | awk '{print $2}') - fd=$(echo $details | awk '{print $3}') - elif [[ "$OSTYPE" == "darwin"* ]]; then - # Handle extraction for macOS - pid=$(echo $info | awk '{print $2}' | cut -d'/' -f1) - command=$(ps -p $pid -o comm= | xargs basename) - fd=$(echo $info | awk '{print $4}' | cut -d'/' -f1) - fi - - # Get the start time of the process using the PID - if [[ -z $pid ]]; then - start_time="N/A" - else - start_time=$(ps -p $pid -o lstart=) - fi - - started+=("Port $port - Command: $command, PID: $pid, FD: $fd, Started: $start_time") - fi - done - - # Print information about ports whose processes are not running. - if [[ ${#not_started[@]} -ne 0 ]]; then - echo "### Not started ports:" - for port in "${not_started[@]}"; do - openim::log::error "Port $port is not started." - done - fi - - # Print information about ports whose processes are running. - if [[ ${#started[@]} -ne 0 ]]; then - echo "### Started ports:" - for info in "${started[@]}"; do - echo "$info" - done - fi - - # If any of the processes is not running, return a status of 1. - 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 - #openim::log::success "All specified ports are running." - return 0 - fi -} - -# set +o errexit -# Sample call for testing: -# openim::util::check_ports 10002 1004 12345 13306 -# - - - openim::util::check_process_names() { 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 @@ -392,7 +296,6 @@ openim::util::check_process_names() { fi } - openim::util::check_process_names_exist() { local process_path="$1" local running_count=$(ps -ef | grep "$process_path" | grep -v grep | wc -l) @@ -404,13 +307,6 @@ openim::util::check_process_names_exist() { fi } - - - - - - - # This figures out the host platform without relying on golang. We need this as # we don't want a golang install to be a prerequisite to building yet we need # this info to figure out where the final binaries are placed. @@ -1329,111 +1225,7 @@ openim::util::check_docker_and_compose_versions() { } -# The `openim::util::check_ports` function analyzes the state of processes based on given ports. -# It accepts multiple ports as arguments and prints: -# 1. The state of the process (whether it's running or not). -# 2. The start time of the process if it's running. -# User: -# openim::util::check_ports 8080 8081 8082 -# The function returns a status of 1 if any of the processes is not running. -openim::util::check_ports() { - # An array to collect ports of processes that are not running. - local not_started=() - - # An array to collect information about processes that are running. - local started=() - - echo "Checking ports: $*" - # Iterate over each given port. - for port in "$@"; do - # Initialize variables - # Check the OS and use the appropriate command - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - if command -v ss > /dev/null 2>&1; then - info=$(ss -ltnp | grep ":$port" || true) - else - info=$(netstat -ltnp | grep ":$port" || true) - fi - elif [[ "$OSTYPE" == "darwin"* ]]; then - # For macOS, use lsof - info=$(lsof -i:"$port" | grep "\*:$port" || true) - fi - - # Check if any process is using the port - if [[ -z $info ]]; then - not_started+=($port) - else - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - # Extract relevant details for Linux: Process Name, PID, and FD. - details=$(echo $info | sed -n 's/.*users:(("\([^"]*\)",pid=\([^,]*\),fd=\([^)]*\))).*/\1 \2 \3/p') - command=$(echo $details | awk '{print $1}') - pid=$(echo $details | awk '{print $2}') - fd=$(echo $details | awk '{print $3}') - elif [[ "$OSTYPE" == "darwin"* ]]; then - # Handle extraction for macOS - pid=$(echo $info | awk '{print $2}' | cut -d'/' -f1) - command=$(ps -p $pid -o comm= | xargs basename) - fd=$(echo $info | awk '{print $4}' | cut -d'/' -f1) - fi - - # Get the start time of the process using the PID - if [[ -z $pid ]]; then - start_time="N/A" - else - start_time=$(ps -p $pid -o lstart=) - fi - - started+=("Port $port - Command: $command, PID: $pid, FD: $fd, Started: $start_time") - fi - done - - # Print information about ports whose processes are not running. - if [[ ${#not_started[@]} -ne 0 ]]; then - printf "\n### Not started ports:" - for port in "${not_started[@]}"; do - openim::log::error "Port $port is not started." - done - fi - - # Print information about ports whose processes are running. - if [[ ${#started[@]} -ne 0 ]]; then - printf "\n### Started ports:" - for info in "${started[@]}"; do - echo "$info" - done - fi - - # If any of the processes is not running, return a status of 1. - 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}" - echo "" - cat "$TMP_LOG_FILE" | awk '{print "\033[31m" $0 "\033[0m"}' - return 1 - else - openim::log::success "All specified processes are running." - return 0 - fi -} - -# set +o errexit -# Sample call for testing: -# openim::util::check_ports 10002 1004 12345 13306 -# - - -# The `openim::util::stop_services_with_name` function stops services with specified names. -# It accepts multiple service names as arguments and performs the following: -# 1. Attempts to stop any services with the specified names. -# 2. Prints details of services successfully stopped and those that failed to stop. -# Usage: -# openim::util::stop_services_with_name nginx apache -# The function returns a status of 1 if any service couldn't be stopped. -# sleep 333333& -# sleep 444444& -# ps -ef | grep "sleep" -# openim::util::stop_services_with_name "sleep 333333" "sleep 444444" # This figures out the host platform without relying on golang. We need this as # we don't want a golang install to be a prerequisite to building yet we need @@ -2435,12 +2227,6 @@ function openim::util::is_running_in_container() { } - - - - - - function openim::util::find_ports_for_all_services() { local services=("$@") for service in "${services[@]}"; do @@ -2449,7 +2235,6 @@ function openim::util::find_ports_for_all_services() { } - function openim::util::print_binary_ports() { binary_path="$1" @@ -2486,9 +2271,6 @@ function openim::util::print_binary_ports() { fi } - - - function openim::util::kill_exist_binary() { local binary_path="$1" local pids=$(pgrep -f "$binary_path") @@ -2506,11 +2288,8 @@ function openim::util::kill_exist_binary() { } - - if [[ "$*" =~ openim::util:: ]];then eval $* fi -