From be6f189365655c18d2fdb067554b4e974b5b2c00 Mon Sep 17 00:00:00 2001 From: "Xinwei Xiong(cubxxw-openim)" <3293172751nss@gmail.com> Date: Tue, 8 Aug 2023 22:44:15 +0800 Subject: [PATCH] fix: fix scripts and optimize Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> --- Makefile | 5 +++ scripts/README.md | 32 +++++++++++++++- scripts/batch_start_all.sh | 3 +- scripts/build_all_service.sh | 2 +- scripts/check_all.sh | 2 +- scripts/enterprise/check_all.sh | 2 +- scripts/enterprise/function.sh | 4 +- scripts/env_check.sh | 3 +- scripts/function.sh | 34 ----------------- scripts/genconfig.sh | 6 +-- scripts/lib/color.sh | 7 ++++ scripts/lib/util.sh | 67 +++++++++++++++++++++++++++++++-- scripts/make-rules/golang.mk | 6 +++ scripts/msg_gateway_start.sh | 6 +-- scripts/msg_transfer_start.sh | 2 +- scripts/push_start.sh | 4 +- scripts/start_cron.sh | 3 +- scripts/start_rpc_service.sh | 4 +- 18 files changed, 131 insertions(+), 61 deletions(-) delete mode 100755 scripts/function.sh diff --git a/Makefile b/Makefile index f6cbe10c1..7ba608be5 100644 --- a/Makefile +++ b/Makefile @@ -191,6 +191,11 @@ advertise: release: release.verify release.ensure-tag @scripts/release.sh +## demo: Run demo ✨ +.PHONY: demo +demo: + @$(MAKE) go.demo + ## help: Show this help info. ✨ .PHONY: help help: Makefile diff --git a/scripts/README.md b/scripts/README.md index da21596d6..751ea7de8 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -84,7 +84,37 @@ Each directory and script in the structure should be understood as a part of a l - Linux MIPS64LE (linux_mips64le) : Suitable for 64-bit Linux systems with little endian MIPS architecture. - +## Get started quickly - demo.sh + +Is the `demo.sh` script teaching you how to quickly get started with OpenIM development and use + + +Steps to run demo: + +```sh +make demo +``` + +Instructions for producing the demo movie: + +```bash +# Create temporary directory +mkdir /tmp/kb-demo +cd /tmp/kb-demo + +asciinema rec +/scripts/demo/run.sh + + to terminate the script + to terminate the asciinema recording + to save the recording locally + +# Edit the recorded file by editing the controller-gen path +# Once you are happy with the recording, use svg-term program to generate the svg + +svg-term --cast= --out _output/demo.svg --window +``` + ## examples Scripts to perform various build, install, analysis, etc operations. diff --git a/scripts/batch_start_all.sh b/scripts/batch_start_all.sh index 45ec813c5..0d1704521 100755 --- a/scripts/batch_start_all.sh +++ b/scripts/batch_start_all.sh @@ -21,9 +21,8 @@ SCRIPTS_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. #Include shell font styles and some basic information -source $SCRIPTS_ROOT/style_info.sh +source $SCRIPTS_ROOT/lib/init.sh source $SCRIPTS_ROOT/path_info.sh -source $SCRIPTS_ROOT/function.sh cd $SCRIPTS_ROOT diff --git a/scripts/build_all_service.sh b/scripts/build_all_service.sh index eb40e5f78..89cbfbad4 100755 --- a/scripts/build_all_service.sh +++ b/scripts/build_all_service.sh @@ -55,7 +55,7 @@ if [ $? -ne 0 ]; then exit 1 fi -gen_os_arch +openim::util::gen_os_arch # Determine if all scripts were successfully built BUILD_SUCCESS=true diff --git a/scripts/check_all.sh b/scripts/check_all.sh index ea9c2b5b7..511c8a875 100755 --- a/scripts/check_all.sh +++ b/scripts/check_all.sh @@ -43,7 +43,7 @@ service_port_name=( ) for i in ${service_port_name[*]}; do list=$(cat $config_path | grep -w ${i} | awk -F '[:]' '{print $NF}') - list_to_string $list + openim::util:list-to-string $list for j in ${ports_array}; do port=$(ss -tunlp| grep openim | awk '{print $5}' | grep -w ${j} | awk -F '[:]' '{print $NF}') if [[ ${port} -ne ${j} ]]; then diff --git a/scripts/enterprise/check_all.sh b/scripts/enterprise/check_all.sh index 271dfb452..5a0bb6c98 100755 --- a/scripts/enterprise/check_all.sh +++ b/scripts/enterprise/check_all.sh @@ -38,7 +38,7 @@ service_port_name=( for i in ${service_port_name[*]}; do list=$(cat $config_path | grep -w ${i} | awk -F '[:]' '{print $NF}') - list_to_string $list + openim::util:list-to-string $list for j in ${ports_array}; do port=$(ss -tunlp| grep openim | awk '{print $5}' | grep -w ${j} | awk -F '[:]' '{print $NF}') if [[ ${port} -ne ${j} ]]; then diff --git a/scripts/enterprise/function.sh b/scripts/enterprise/function.sh index 8b5cb0b2b..a33ff65d6 100755 --- a/scripts/enterprise/function.sh +++ b/scripts/enterprise/function.sh @@ -15,7 +15,7 @@ #input:[10023,2323,3434] #output:10023 2323 3434 -list_to_string(){ +openim::util:list-to-string(){ ports_list=$* sub_s1=`echo $ports_list | sed 's/ //g'` sub_s2=${sub_s1//,/ } @@ -23,7 +23,7 @@ list_to_string(){ sub_s4=${sub_s3%]*} ports_array=$sub_s4 } -remove_space(){ +openim::util::remove_space(){ value=$* result=`echo $value | sed 's/ //g'` } \ No newline at end of file diff --git a/scripts/env_check.sh b/scripts/env_check.sh index 7cd9f83a3..08d6a84b1 100755 --- a/scripts/env_check.sh +++ b/scripts/env_check.sh @@ -19,9 +19,8 @@ SCRIPTS_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) OPENIM_ROOT=$(dirname "${SCRIPTS_ROOT}")/.. #Include shell font styles and some basic information -source $SCRIPTS_ROOT/style_info.sh source $SCRIPTS_ROOT/path_info.sh -source $SCRIPTS_ROOT/function.sh +source $SCRIPTS_ROOT/lib/init.sh cd $SCRIPTS_ROOT diff --git a/scripts/function.sh b/scripts/function.sh deleted file mode 100755 index ea7b50c5a..000000000 --- a/scripts/function.sh +++ /dev/null @@ -1,34 +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. - -# input: [10023, 2323, 3434] -# output: 10023 2323 3434 - -# 函数功能:将列表转换为字符串,去除空格和括号 -list_to_string() { - ports_list=$* # 获取传入的参数列表 - sub_s1=$(echo $ports_list | sed 's/ //g') # 去除空格 - sub_s2=${sub_s1//,/ } # 将逗号替换为空格 - sub_s3=${sub_s2#*[} # 去除左括号及其之前的内容 - sub_s4=${sub_s3%]*} # 去除右括号及其之后的内容 - ports_array=$sub_s4 # 将处理后的字符串赋值给变量 ports_array -} - -# 函数功能:去除字符串中的空格 -remove_space() { - value=$* # 获取传入的参数 - result=$(echo $value | sed 's/ //g') # 去除空格 -} diff --git a/scripts/genconfig.sh b/scripts/genconfig.sh index cee105a8a..457d48a73 100755 --- a/scripts/genconfig.sh +++ b/scripts/genconfig.sh @@ -7,12 +7,12 @@ # 本脚本功能:根据 scripts/environment.sh 配置,生成 OPENIM 组件 YAML 配置文件。 # 示例:genconfig.sh scripts/environment.sh configs/openim-apiserver.yaml +# Path to the original script file env_file="$1" +# Path to the generated config file template_file="$2" -OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. - -source "${OPENIM_ROOT}/scripts/lib/init.sh" +. $(dirname ${BASH_SOURCE})/lib/init.sh if [ $# -ne 2 ];then openim::log::error "Usage: genconfig.sh scripts/environment.sh configs/openim-apiserver.yaml" diff --git a/scripts/lib/color.sh b/scripts/lib/color.sh index e6b9c3924..92686affa 100755 --- a/scripts/lib/color.sh +++ b/scripts/lib/color.sh @@ -47,6 +47,13 @@ ITALIC_PREFIX="\033[3m" # Italic prefix BRIGHT_GREEN_PREFIX='\033[1;32m' # Bright green prefix CYAN_PREFIX="\033[0;36m" # Cyan prefix +# --- make demo (run demo) --- +readonly reset=$(tput sgr0) +readonly green=$(tput bold; tput setaf 2) +readonly yellow=$(tput bold; tput setaf 3) +readonly blue=$(tput bold; tput setaf 6) +readonly timeout=$(if [ "$(uname)" == "Darwin" ]; then echo "1"; else echo "0.1"; fi) + # --- helper functions for logs --- info() { diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 7485e9e55..7fec6cb8d 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -710,12 +710,71 @@ fi # ex: ts=2 sw=2 et filetype=sh +function openim::util::desc() { + openim::util:run::maybe_first_prompt + rate=25 + if [ -n "$DEMO_RUN_FAST" ]; then + rate=1000 + fi + echo "$blue# $@$reset" | pv -qL $rate + openim::util:run::prompt +} + +function openim::util:run::prompt() { + echo -n "$yellow\$ $reset" +} + +started="" +function openim::util:run::maybe_first_prompt() { + if [ -z "$started" ]; then + openim::util:run::prompt + started=true + fi +} + +# After a `run` this variable will hold the stdout of the command that was run. +# If the command was interactive, this will likely be garbage. +DEMO_RUN_STDOUT="" + +function openim::util::run() { + openim::util:run::maybe_first_prompt + rate=25 + if [ -n "$DEMO_RUN_FAST" ]; then + rate=1000 + fi + echo "$green$1$reset" | pv -qL $rate + if [ -n "$DEMO_RUN_FAST" ]; then + sleep 0.5 + fi + OFILE="$(mktemp -t $(basename $0).XXXXXX)" + if [ "$(uname)" == "Darwin" ]; then + script -q "$OFILE" $1 + else + script -eq -c "$1" -f "$OFILE" + fi + r=$? + read -d '' -t "${timeout}" -n 10000 # clear stdin + openim::util:run::prompt + if [ -z "$DEMO_AUTO_RUN" ]; then + read -s + fi + DEMO_RUN_STDOUT="$(tail -n +2 $OFILE | sed 's/\r//g')" + return $r +} + +function openim::util::run::relative() { + for arg; do + echo "$(realpath $(dirname $(which $0)))/$arg" | sed "s|$(realpath $(pwd))|.|" + done +} + +trap "echo" EXIT + # input: [10023, 2323, 3434] # output: 10023 2323 3434 - # Function function: Converts a list to a string, removing Spaces and parentheses -function list_to_string() { +function openim::util:list-to-string() { ports_list=$* # 获取传入的参数列表 sub_s1=$(echo $ports_list | sed 's/ //g') # 去除空格 sub_s2=${sub_s1//,/ } # 将逗号替换为空格 @@ -725,12 +784,12 @@ function list_to_string() { } # Function Function: Remove Spaces in the string -function remove_space() { +function openim::util::remove_space() { value=$* # 获取传入的参数 result=$(echo $value | sed 's/ //g') # 去除空格 } -function gen_os_arch() { +function openim::util::gen_os_arch() { # Get the current operating system and architecture OS=$(uname -s | tr '[:upper:]' '[:lower:]') ARCH=$(uname -m) diff --git a/scripts/make-rules/golang.mk b/scripts/make-rules/golang.mk index cc54d017a..3d538384a 100644 --- a/scripts/make-rules/golang.mk +++ b/scripts/make-rules/golang.mk @@ -163,6 +163,12 @@ go.lint: tools.verify.golangci-lint go.test: @$(GO) test ./... +## go.demo: Run demo +.PHONY: go.demo +go.demo: + @echo "===========> Run demo" + @$(ROOT_DIR)/scripts/demo.sh + ## go.test.junit-report: Run unit test .PHONY: go.test.junit-report go.test.junit-report: tools.verify.go-junit-report diff --git a/scripts/msg_gateway_start.sh b/scripts/msg_gateway_start.sh index 49d84fac5..6a663a627 100755 --- a/scripts/msg_gateway_start.sh +++ b/scripts/msg_gateway_start.sh @@ -36,11 +36,11 @@ ulimit -n 200000 list1=$(cat $config_path | grep openImMessageGatewayPort | awk -F '[:]' '{print $NF}') list2=$(cat $config_path | grep openImWsPort | awk -F '[:]' '{print $NF}') list3=$(cat $config_path | grep messageGatewayPrometheusPort | awk -F '[:]' '{print $NF}') -list_to_string $list1 +openim::util:list-to-string $list1 rpc_ports=($ports_array) -list_to_string $list2 +openim::util:list-to-string $list2 ws_ports=($ports_array) -list_to_string $list3 +openim::util:list-to-string $list3 prome_ports=($ports_array) if [ ${#rpc_ports[@]} -ne ${#ws_ports[@]} ]; then diff --git a/scripts/msg_transfer_start.sh b/scripts/msg_transfer_start.sh index 46c077c5d..42d57d89e 100755 --- a/scripts/msg_transfer_start.sh +++ b/scripts/msg_transfer_start.sh @@ -32,7 +32,7 @@ logs_dir="$OPENIM_ROOT/logs" cd $OPENIM_ROOT list1=$(cat $config_path | grep messageTransferPrometheusPort | awk -F '[:]' '{print $NF}') -list_to_string $list1 +openim::util:list-to-string $list1 prome_ports=($ports_array) #Check if the service exists diff --git a/scripts/push_start.sh b/scripts/push_start.sh index b474e1b99..5b549c828 100755 --- a/scripts/push_start.sh +++ b/scripts/push_start.sh @@ -37,9 +37,9 @@ cd "$OPENIM_ROOT/scripts/" list1=$(cat $config_path | grep openImPushPort | awk -F '[:]' '{print $NF}') list2=$(cat $config_path | grep pushPrometheusPort | awk -F '[:]' '{print $NF}') -list_to_string $list1 +openim::util:list-to-string $list1 rpc_ports=($ports_array) -list_to_string $list2 +openim::util:list-to-string $list2 prome_ports=($ports_array) #Check if the service exists diff --git a/scripts/start_cron.sh b/scripts/start_cron.sh index ddd3661ae..45a07fa28 100755 --- a/scripts/start_cron.sh +++ b/scripts/start_cron.sh @@ -19,9 +19,8 @@ SCRIPTS_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. #Include shell font styles and some basic information -source $SCRIPTS_ROOT/style_info.sh +source $SCRIPTS_ROOT/lib/init.sh source $SCRIPTS_ROOT/path_info.sh -source $SCRIPTS_ROOT/function.sh cd $SCRIPTS_ROOT diff --git a/scripts/start_rpc_service.sh b/scripts/start_rpc_service.sh index 51e13298f..9dac6a635 100755 --- a/scripts/start_rpc_service.sh +++ b/scripts/start_rpc_service.sh @@ -90,11 +90,11 @@ for ((i = 0; i < ${#service_filename[*]}; i++)); do cd $BIN_DIR # Get the rpc port in the configuration file portList=$(cat $config_path | grep ${service_port_name[$i]} | awk -F '[:]' '{print $NF}') - list_to_string ${portList} + openim::util:list-to-string ${portList} service_ports=($ports_array) portList2=$(cat $config_path | grep ${service_prometheus_port_name[$i]} | awk -F '[:]' '{print $NF}') - list_to_string $portList2 + openim::util:list-to-string $portList2 prome_ports=($ports_array) #Start related rpc services based on the number of ports for ((j = 0; j < ${#service_ports[*]}; j++)); do