From 70c0417b778013b902b0810aef4f58f856d160ae Mon Sep 17 00:00:00 2001 From: "Xinwei Xiong(cubxxw-openim)" <3293172751nss@gmail.com> Date: Sat, 15 Jul 2023 15:08:42 +0800 Subject: [PATCH] feat: fix scripts show Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> --- .env | 4 +- Makefile | 5 ++ bash.sh | 52 +++++++++++++++ config/config.yaml | 20 +++--- init_docker.sh | 9 +++ install_im_compose.sh | 21 ------- install_im_server.sh | 43 ------------- scripts/docker_check_service.sh | 105 ++++++++++++++++++++++++------- scripts/env_check.sh | 14 +++-- scripts/init_pwd.sh | 23 +++++-- scripts/install_im_compose.sh | 108 ++++++++++++++++++++++++++++++++ scripts/install_im_server.sh | 63 +++++++++++++++++++ scripts/make-rules/golang.mk | 6 ++ 13 files changed, 365 insertions(+), 108 deletions(-) create mode 100755 bash.sh delete mode 100755 install_im_compose.sh delete mode 100755 install_im_server.sh create mode 100755 scripts/install_im_compose.sh create mode 100755 scripts/install_im_server.sh diff --git a/.env b/.env index 048662d97..e7971a07a 100644 --- a/.env +++ b/.env @@ -1,5 +1,5 @@ USER=root PASSWORD=openIM123 -MINIO_ENDPOINT=http://127.0.0.1:10005 -API_URL=http://127.0.0.1:10002/object/ +MINIO_ENDPOINT=http://116.30.3.80:10005 +API_URL=http://116.30.3.80:10002/object/ DATA_DIR=./ diff --git a/Makefile b/Makefile index d1c733717..74e183543 100644 --- a/Makefile +++ b/Makefile @@ -63,6 +63,11 @@ build: multiarch: @$(MAKE) go.build.multiarch +## install: Install deployment openim ✨ +.PHONY: install +install: + @$(MAKE) go.install + ## tidy: tidy go.mod ✨ .PHONY: tidy tidy: diff --git a/bash.sh b/bash.sh new file mode 100755 index 000000000..7d60f14e2 --- /dev/null +++ b/bash.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +trap 'onCtrlC' INT +function onCtrlC () { + #捕获CTRL+C,当脚本被ctrl+c的形式终止时同时终止程序的后台进程 + kill -9 ${do_sth_pid} ${progress_pid} + echo + echo 'Ctrl+C is captured' + exit 1 +} + +do_sth() { + #运行的主程序 + echo "++++++++++++++++++++++++" + sleep 5 + echo "++++++++++++++++++++++++" + + sleep 10 +} + +progress() { + #进度条程序 + local main_pid=$1 + local length=20 + local ratio=1 + while [ "$(ps -p ${main_pid} | wc -l)" -ne "1" ] ; do + mark='>' + progress_bar= + for i in $(seq 1 "${length}"); do + if [ "$i" -gt "${ratio}" ] ; then + mark='-' + fi + progress_bar="${progress_bar}${mark}" + done + printf "Progress: ${progress_bar}\r" + ratio=$((ratio+1)) + #ratio=`expr ${ratio} + 1` + if [ "${ratio}" -gt "${length}" ] ; then + ratio=1 + fi + sleep 0.1 + done +} + +do_sth & +do_sth_pid=$(jobs -p | tail -1) + +progress "${do_sth_pid}" & +progress_pid=$(jobs -p | tail -1) + +wait "${do_sth_pid}" +printf "Progress: done \n" \ No newline at end of file diff --git a/config/config.yaml b/config/config.yaml index 1c01031dc..2d38a5e36 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -24,8 +24,8 @@ zookeeper: mysql: address: [ 127.0.0.1:13306 ] #目前仅支持单机 - username: root #用户名 - password: openIM123 #密码 + username: root + password: openIM123 database: openIM_v3 #不建议修改 maxOpenConn: 1000 #最大连接数 maxIdleConn: 100 #最大空闲连接数 @@ -37,14 +37,14 @@ mongo: uri: #不为空则直接使用该值 address: [ 127.0.0.1:37017 ] #单机时为mongo地址,使用分片集群时,为mongos地址 database: openIM_v3 #mongo db 默认即可 - username: root #用户名 - password: openIM123 #密码 + username: root + password: openIM123 maxPoolSize: 100 redis: address: [ 127.0.0.1:16379 ] # username: #only redis version 6.0+ need username - password: openIM123 #密码 + password: openIM123 kafka: username: #用户名 @@ -77,9 +77,9 @@ object: apiURL: "http://127.0.0.1:10002/object/" #地址需要app能访问到 minio: bucket: "openim" #不建议修改 - endpoint: "http://127.0.0.1:10005" #minio对外服务的ip和端口,app要能访问此ip和端口 - accessKeyID: "root" #ID - secretAccessKey: "openIM123" #秘钥 + endpoint: http://116.30.3.80:10005 + accessKeyID: root + secretAccessKey: openIM123 sessionToken: "" #token cos: #tencent cos bucketURL: "https://temp-1252357374.cos.ap-chengdu.myqcloud.com" @@ -90,7 +90,7 @@ object: endpoint: "https://oss-cn-chengdu.aliyuncs.com" bucket: "demo-9999999" bucketURL: "https://demo-9999999.oss-cn-chengdu.aliyuncs.com" - accessKeyID: "" + accessKeyID: root accessKeySecret: "" sessionToken: "" @@ -167,7 +167,7 @@ retainChatRecords: 365 #mongo保存离线消息 chatRecordsClearTime: "0 2 * * 3" #每周三凌晨2点清理mongo中的过期(超过retainChatRecords时间)消息,这个删除是为了清理满足上个配置retainChatRecords的过期消息,不会发送通知,仅仅作为清理磁盘使用 msgDestructTime: "0 2 * * *" #消息自动删除时间,每天凌晨2点删除过期消息,这个删除是为了删除保留时间超过超过会话字段msg_destruct_time(秒)的消息。 -secret: tuoyun #秘钥,获取token时校验 +secret: openIM123 tokenPolicy: expire: 90 #过期时间(天) diff --git a/init_docker.sh b/init_docker.sh index 9815fe871..b06eda469 100755 --- a/init_docker.sh +++ b/init_docker.sh @@ -1,5 +1,14 @@ #!/usr/bin/env bash +mark='' +for ((ratio=0;${ratio}<=100;ratio+=5)) +do + sleep 0.2 + printf "progress:[%-40s]%d%%\r" "${mark}" "${ratio}" + mark="##${mark}" +done +echo + set -e # Change directory to the 'scripts' folder diff --git a/install_im_compose.sh b/install_im_compose.sh deleted file mode 100755 index 687a827d1..000000000 --- a/install_im_compose.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env bash -internet_ip=`curl ifconfig.me -s` -echo $internet_ip - -source .env -echo $MINIO_ENDPOINT -if [ $MINIO_ENDPOINT == "http://127.0.0.1:10005" ]; then - sed -i "s/127.0.0.1/${internet_ip}/" .env - -fi - -cd scripts ; -chmod +x *.sh ; -./init_pwd.sh -./env_check.sh; -cd .. ; -docker-compose -f im-compose.yaml up -d -docker ps - - - diff --git a/install_im_server.sh b/install_im_server.sh deleted file mode 100755 index c526d43db..000000000 --- a/install_im_server.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env bash - -# Get the public internet IP address -internet_ip=$(curl ifconfig.me -s) -echo $internet_ip - -# Load environment variables from .env file -source .env -echo $MINIO_ENDPOINT - -# Replace local IP address with the public IP address in .env file -if [ $API_URL == "http://127.0.0.1:10002/object/" ]; then - sed -i "s/127.0.0.1/${internet_ip}/" .env -fi - -if [ $MINIO_ENDPOINT == "http://127.0.0.1:10005" ]; then - sed -i "s/127.0.0.1/${internet_ip}/" .env -fi - -# Change directory to scripts folder -cd scripts -chmod +x *.sh - -# Execute necessary scripts -./init_pwd.sh -./env_check.sh - -# Go back to the previous directory -cd .. - -# Check if docker-compose command is available -if command -v docker-compose &> /dev/null -then - docker-compose up -d -else - docker compose up -d -fi - -# Change directory to scripts folder again -cd scripts - -# Check docker services -./docker_check_service.sh \ No newline at end of file diff --git a/scripts/docker_check_service.sh b/scripts/docker_check_service.sh index 4acbde407..658199b95 100755 --- a/scripts/docker_check_service.sh +++ b/scripts/docker_check_service.sh @@ -1,33 +1,92 @@ #!/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. - -echo "docker-compose ps..........................." -cd .. - -if command -v docker-compose &> /dev/null -then +# Include shell font styles and some basic information +SCRIPTS_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. + +source "$SCRIPTS_ROOT/style_info.sh" + +echo -e "${GREEN_PREFIX}=========> Check docker-compose status ${COLOR_SUFFIX} \n" + +trap 'onCtrlC' INT + +function onCtrlC() { + kill -9 "${do_sth_pid}" "${progress_pid}" "${countdown_pid}" + echo + echo 'Ctrl+C is captured' + exit 1 +} + +cd "$OPENIM_ROOT" + +if command -v docker-compose &> /dev/null; then docker-compose ps else docker compose ps fi +progress() { + local _main_pid="$1" + local _length=20 + local _ratio=1 + local _colors=("31" "32" "33" "34" "35" "36" "37") + local _wave=("▁" "▂" "▃" "▄" "▅" "▆" "▇" "█" "▇" "▆" "▅" "▄" "▃" "▂") + + while pgrep -P "$_main_pid" &> /dev/null; do + local _mark='>' + local _progress_bar= + for ((i = 1; i <= _length; i++)); do + if ((i > _ratio)); then + _mark='-' + fi + _progress_bar="${_progress_bar}${_mark}" + done + + local _color_idx=$((_ratio % ${#_colors[@]})) + local _color_prefix="\033[${_colors[_color_idx]}m" + local _reset_suffix="\033[0m" + + local _wave_idx=$((_ratio % ${#_wave[@]})) + local _wave_progress=${_wave[_wave_idx]} + + printf "Progress: ${_color_prefix}${_progress_bar}${_reset_suffix} ${_wave_progress} Countdown: %2ds \r" "$_countdown" + ((_ratio++)) + ((_ratio > _length)) && _ratio=1 + sleep 0.1 + done +} + +countdown() { + local _duration="$1" + + for ((i = _duration; i >= 1; i--)); do + printf "\rCountdown: %2ds \r" "$i" + sleep 1 + done + printf "\rCountdown: %2ds \r" "$_duration" +} + +do_sth() { + echo "++++++++++++++++++++++++" + progress $$ & + local _progress_pid=$! + local _countdown=30 + + countdown "$_countdown" & + local _countdown_pid=$! + + sleep 30 + + kill "$_progress_pid" "$_countdown_pid" + "${SCRIPTS_ROOT}/check_all.sh" + echo -e "${PURPLE_PREFIX}=========> Check docker-compose status ${COLOR_SUFFIX} \n" +} -cd scripts -echo "check OpenIM................................" +do_sth & +do_sth_pid=$(jobs -p | tail -1) -sleep 30 -./check_all.sh +progress "${do_sth_pid}" & +progress_pid=$(jobs -p | tail -1) +wait "${do_sth_pid}" +printf "Progress: done \n" diff --git a/scripts/env_check.sh b/scripts/env_check.sh index 8c84567b7..35f918d80 100755 --- a/scripts/env_check.sh +++ b/scripts/env_check.sh @@ -13,7 +13,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -source ./style_info.sh +#Include shell font styles and some basic information +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 + +cd $SCRIPTS_ROOT echo -e "check time synchronize.................................." t=`curl http://time.akamai.com/?iso -s` @@ -27,7 +36,6 @@ else fi - echo -e "check login user........................................" user=`whoami` if [ $user == "root" ] ; then @@ -36,8 +44,6 @@ else echo -e ${RED_PREFIX}"Warning: The current user is not root "${COLOR_SUFFIX} fi - - echo -e "check docker............................................" docker_running=`systemctl status docker | grep running | grep active | wc -l` diff --git a/scripts/init_pwd.sh b/scripts/init_pwd.sh index a3ce6048a..3d964c6da 100755 --- a/scripts/init_pwd.sh +++ b/scripts/init_pwd.sh @@ -14,7 +14,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -source ../.env +#Include shell font styles and some basic information +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 + +cd $SCRIPTS_ROOT + +source $OPENIM_ROOT/.env # Check if PASSWORD only contains letters and numbers if [[ "$PASSWORD" =~ ^[a-zA-Z0-9]+$ ]] @@ -25,10 +36,12 @@ else exit fi -echo "your user is:$USER" -echo "your password is:$PASSWORD" -echo "your minio endPoint is:$MINIO_ENDPOINT" -echo "your data dir is $DATA_DIR" +echo "" +echo -e "===> ${PURPLE_PREFIX} you user is:$USER ${COLOR_SUFFIX}" +echo -e "===> ${PURPLE_PREFIX} you password is:$PASSWORD ${COLOR_SUFFIX}" +echo -e "===> ${PURPLE_PREFIX} you minio endpoint is:$MINIO_ENDPOINT ${COLOR_SUFFIX}" +echo -e "===> ${PURPLE_PREFIX} you api url is:$API_URL ${COLOR_SUFFIX}" +echo "" # Specify the config file config_file='../config/config.yaml' diff --git a/scripts/install_im_compose.sh b/scripts/install_im_compose.sh new file mode 100755 index 000000000..399e1e49d --- /dev/null +++ b/scripts/install_im_compose.sh @@ -0,0 +1,108 @@ + +#Include shell font styles and some basic information +SCRIPTS_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. + +# docker-compose.yaml file name +docker_compose_file_name="docker-compose.yaml" + +trap 'onCtrlC' INT +function onCtrlC () { + #Capture CTRL+C, terminate the background process of the program when the script is terminated in the form of ctrl+c + kill -9 ${do_sth_pid} ${progress_pid} + echo + echo 'Ctrl+C is captured' + exit 1 +} + +do_sth() { + #Main program to run + echo "++++++++++++++++++++++++" + sleep 5 + echo "++++++++++++++++++++++++" + + sleep 10 +} + +#Import environment variables +source .env + +#Get the public IP address of the local machine +internet_ip=$(curl ifconfig.me -s) +echo -e "\033[1;34mCurrent public IP address: ${internet_ip}\033[0m\n" + +#If MINIO_ENDPOINT is "http://127.0.0.1:10005", replace it with the current public IP address +if [[ $MINIO_ENDPOINT == "http://127.0.0.1:10005" ]]; then + sed -i "s/127.0.0.1/${internet_ip}/" .env +fi + +do_progress_bar() { + local duration=${1} + local max_progress=20 + local current_progress=0 + + while true; do + ((current_progress++)) + if [[ $current_progress -gt $max_progress ]]; then + break + fi + sleep "$duration" + echo "=====> Progress: [${current_progress}/${max_progress}]" + done +} + +#Start Docker containers +start_docker_containers() { + if command -v docker-compose >/dev/null 2>&1; then + echo -e "\033[1;34mFound docker-compose command, starting docker containers...\033[0m\n" + docker-compose -f ${OPENIM_ROOT}/${docker_compose_file_name} up -d + else + if command -v docker >/dev/null 2>&1; then + echo -e "\033[1;34mFound docker command, starting docker containers...\033[0m\n" + docker compose -f ${OPENIM_ROOT}/${docker_compose_file_name} up -d + else + echo -e "\033[1;31mFailed to find docker-compose or docker command, please make sure they are installed and configured correctly.\033[0m" + return 1 + fi + fi +} + +#Execute scripts +setup_script() { + chmod +x ${SCRIPTS_ROOT}/*.sh + echo -e "\033[1;34m============>Executing init_pwd.sh script...\033[0m\n" + ${SCRIPTS_ROOT}/init_pwd.sh + echo -e "\033[1;34m============>Executing env_check.sh script...\033[0m\n" + ${SCRIPTS_ROOT}/env_check.sh +} + +setup_script & + +#Start Docker containers (timeout 10 seconds) +start_docker_containers + +docker_pid=$! +timeout 10s tail --pid=${docker_pid} -f /dev/null +docker_exit_code=$? + +if [ $docker_exit_code -eq 0 ]; then + echo -e "\033[1;32m============>Docker containers started successfully!\033[0m\n" +else + echo -e "\033[1;31m============>Failed to start Docker containers, please check the environment configuration and dependencies.\033[0m\n" + exit 1 +fi + +echo -e "\033[1;34m============>Executing docker_check_service.sh script...\033[0m\n" + +#View running Docker containers +echo -e "\033[1;34m============>Viewing running Docker containers...\033[0m\n" +echo "" +docker ps + +#Replace the progress bar section with the pv command +echo -e "\033[1;34m============>Starting progress bar...\033[0m\n" +do_progress_bar 0.5 | pv -l -s 20 > /dev/null +echo -e "\033[1;34m============>Progress bar completed.\033[0m\n" + +#Execute the main program +do_sth diff --git a/scripts/install_im_server.sh b/scripts/install_im_server.sh new file mode 100755 index 000000000..1990f748f --- /dev/null +++ b/scripts/install_im_server.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash + +#Include shell font styles and some basic information +SCRIPTS_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. + +source $SCRIPTS_ROOT/style_info.sh + +# docker-compose.yaml file name +docker_compose_file_name="docker-compose.yaml" + +trap 'onCtrlC' INT +function onCtrlC () { + #Capture CTRL+C, terminate the background process of the program when the script is terminated in the form of ctrl+c + kill -9 ${do_sth_pid} ${progress_pid} + echo + echo 'Ctrl+C is captured' + exit 1 +} + +# Get the public internet IP address +internet_ip=$(curl ifconfig.me -s) +echo -e "${PURPLE_PREFIX}=========> Your public internet IP address is ${internet_ip} ${COLOR_SUFFIX} \n" + +# Load environment variables from .env file +source ${OPENIM_ROOT}/.env + +echo -e "${PURPLE_PREFIX}=========> Your minio endpoint is ${MINIO_ENDPOINT} ${COLOR_SUFFIX} \n" + +# Change directory to scripts folder + +chmod +x ${SCRIPTS_ROOT}/*.sh + +# Execute necessary scripts +echo -e "${PURPLE_PREFIX}=========> init_pwd.sh ${COLOR_SUFFIX} \n" + +${SCRIPTS_ROOT}/init_pwd.sh + +echo -e "${PURPLE_PREFIX}=========> env_check.sh ${COLOR_SUFFIX} \n" + +${SCRIPTS_ROOT}/env_check.sh + +# Replace local IP address with the public IP address in .env file +if [ $API_URL == "http://127.0.0.1:10002/object/" ]; then + sed -i "s/127.0.0.1/${internet_ip}/" ${OPENIM_ROOT}/.env +fi + +if [ $MINIO_ENDPOINT == "http://127.0.0.1:10005" ]; then + sed -i "s/127.0.0.1/${internet_ip}/" ${OPENIM_ROOT}/.env +fi + +# Go back to the previous directory +cd ${OPENIM_ROOT} + +# Check if docker-compose command is available +if command -v docker-compose &> /dev/null +then + docker-compose up -d +else + docker compose up -d +fi + +${SCRIPTS_ROOT}/docker_check_service.sh \ No newline at end of file diff --git a/scripts/make-rules/golang.mk b/scripts/make-rules/golang.mk index adefa5085..cf07f0cda 100644 --- a/scripts/make-rules/golang.mk +++ b/scripts/make-rules/golang.mk @@ -131,6 +131,12 @@ go.build.%: $(BIN_DIR)/platforms/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT) $(ROOT_DIR)/cmd/$(COMMAND)/main.go; \ fi +## go.install: Install deployment openim +.PHONY: go.install +go.install: + @echo "===========> Installing deployment openim" + @$(ROOT_DIR)/scripts/install_im_server.sh + ## go.multiarch: Build multi-arch binaries .PHONY: go.build.multiarch go.build.multiarch: go.build.verify $(foreach p,$(PLATFORMS),$(addprefix go.build., $(addprefix $(p)., $(BINS))))