From 4d9fa9125091881c5c90ac979a01f31363f1273a Mon Sep 17 00:00:00 2001 From: "Xinwei Xiong(cubxxw-openim)" <3293172751nss@gmail.com> Date: Thu, 27 Jul 2023 11:45:33 +0800 Subject: [PATCH] feat: add openim install scripts Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> --- install.sh | 476 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 353 insertions(+), 123 deletions(-) diff --git a/install.sh b/install.sh index 23ad7363c..b9962c4c6 100755 --- a/install.sh +++ b/install.sh @@ -1,105 +1,102 @@ #!/bin/bash - -# Check if the script is run as root -if [ "$EUID" -ne 0 ]; then - echo "Please run the script as root or use sudo." - exit -fi +# 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. +# +# https://gist.github.com/cubxxw/28f997f2c9aff408630b072f010c1d64 +# set -e set -o pipefail -set -o noglob - -# Color definitions -openim_color() { - BLACK_PREFIX="\033[30m" # Black prefix - RED_PREFIX="\033[31m" # Red prefix - GREEN_PREFIX="\033[32m" # Green prefix - YELLOW_PREFIX="\033[33m" # Yellow prefix - BLUE_PREFIX="\033[34m" # Blue prefix - SKY_BLUE_PREFIX="\033[36m" # Sky blue prefix - WHITE_PREFIX="\033[37m" # White prefix - BOLD_PREFIX="\033[1m" # Bold prefix - UNDERLINE_PREFIX="\033[4m" # Underline prefix - ITALIC_PREFIX="\033[3m" # Italic prefix -} - - -print_with_delay() { - text="$1" - delay="$2" - for i in $(seq 0 $((${#text}-1))); do - printf "${text:$i:1}" - sleep $delay - done - printf "\n" -} - -print_progress() { - total="$1" - delay="$2" - - printf "[" - for i in $(seq 1 $total); do - printf "#" - sleep $delay - done - printf "]\n" -} ############### OpenIM Github ############### # ... rest of the script ... -# OpenKF Repo +# OpenIM Repo OWNER="OpenIMSDK" REPO="Open-IM-Server" # Update your Go version here GO_VERSION="1.18" -# --- helper functions for logs --- -info() -{ - echo -e "[${GREEN_PREFIX}INFO${COLOR_SUFFIX}] " "$@" -} -warn() -{ - echo -e "[${YELLOW_PREFIX}WARN${COLOR_SUFFIX}] " "$@" >&2 -} -fatal() -{ - echo -e "[${RED_PREFIX}ERROR${COLOR_SUFFIX}] " "$@" >&2 - exit 1 -} +# HTTP_PORT=80 -# Function to download and install Go if it's not already installed -install_go() { - command -v go >/dev/null 2>&1 - if [[ $? -ne 0 ]]; then - warn "Go is not installed. Installing now..." - curl -LO "https://golang.org/dl/go${GO_VERSION}.linux-amd64.tar.gz" - if [ $? -ne 0 ]; then - fatal "Download failed! Please check your network connectivity." - fi - sudo tar -C /usr/local -xzf "go${GO_VERSION}.linux-amd64.tar.gz" - echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc - source ~/.bashrc - info "Go has been installed successfully." - else - info "Go is already installed." +# CPU core number, concurrent execution +# CPU=$(grep -c ^processor /proc/cpuinfo) + +# default is latest tag: https://github.com/OpenIMSDK/Open-IM-Server/releases +# LATEST_TAG=v3.0.0 + +# default OpenIM install directory is /tmp +DOWNLOAD_OPENIM_DIR="/test" + +# github proxy +# PROXY="https://ghproxy.com/" +PROXY= +GITHUB_TOKEN= + +USER=root #no need to modify +PASSWORD=openIM123 #A combination of 8 or more numbers and letters, this password applies to redis, mysql, mongo, as well as accessSecret in config/config.yaml +ENDPOINT=http://127.0.0.1:10005 #minio's external service IP and port, or use the domain name storage.xx.xx, the app must be able to access this IP and port or domain, +API_URL=http://127.0.0.1:10002/object/ #the app must be able to access this IP and port or domain, +DATA_DIR=./ #designate large disk directory, default is current directory + +############### OpenIM Functions ############### +# Install horizon of the script +# +# Pre-requisites: +# - git +# - make +# - jq +# - docker +# - docker-compose +# - go +# + +# Check if the script is run as root +function check_isroot() { + if [ "$EUID" -ne 0 ]; then + fatal "Please run the script as root or use sudo." fi } -# Function for colored echo -color_echo() { - COLOR=$1 - shift - echo -e "${COLOR}===========> $* ${COLOR_SUFFIX}" +# check if the current directory is a OpenIM git repository +function check_git_repo() { + if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + # Inside a git repository + for remote in $(git remote); do + repo_url=$(git remote get-url $remote) + if [[ $repo_url == "https://github.com/OpenIMSDK/Open-IM-Server.git" || \ + $repo_url == "https://github.com/OpenIMSDK/Open-IM-Server" || \ + $repo_url == "git@github.com:OpenIMSDK/Open-IM-Server.git" ]]; then + # If it's OpenIMSDK repository + info "Current directory is OpenIMSDK git repository." + info "Executing installation directly." + install_openim + exit 0 + fi + debug "Remote: $remote, URL: $repo_url" + done + # If it's not OpenIMSDK repository + debug "Current directory is not OpenIMSDK git repository." + fi + info "Current directory is not a git repository." } # Function to update and install necessary tools -install_tools() { +function install_tools() { + info "Checking and installing necessary tools, about git, make, jq, docker, docker-compose." local tools=("git" "make" "jq" "docker" "docker-compose") local install_cmd update_cmd os @@ -115,7 +112,7 @@ install_tools() { fatal "Unsupported OS, please use Ubuntu or CentOS." fi - info "Detected OS: $os" + debug "Detected OS: $os" info "Updating system package repositories..." $update_cmd @@ -123,32 +120,285 @@ install_tools() { if ! command -v $tool &> /dev/null; then warn "$tool is not installed. Installing now..." $install_cmd $tool - info "$tool has been installed successfully." + success "$tool has been installed successfully." else info "$tool is already installed." fi done } +# Function to check if Docker and Docker Compose are installed +function check_docker() { + if ! command -v docker &> /dev/null; then + fatal "Docker is not installed. Please install Docker first." + fi + if ! command -v docker-compose &> /dev/null; then + fatal "Docker Compose is not installed. Please install Docker Compose first." + fi +} + +# Function to download and install Go if it's not already installed +function install_go() { + command -v go >/dev/null 2>&1 + # Determines if GO_VERSION is defined + if [ -z "$GO_VERSION" ]; then + GO_VERSION="1.18" + fi + + if [[ $? -ne 0 ]]; then + warn "Go is not installed. Installing now..." + curl -LO "https://golang.org/dl/go${GO_VERSION}.linux-amd64.tar.gz" + if [ $? -ne 0 ]; then + fatal "Download failed! Please check your network connectivity." + fi + sudo tar -C /usr/local -xzf "go${GO_VERSION}.linux-amd64.tar.gz" + echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc + source ~/.bashrc + success "Go has been installed successfully." + else + info "Go is already installed." + fi +} + +function download_source_code() { + + # If LATEST_TAG was not defined outside the function, get it here example: v3.0.1-beta.1 + if [ -z "$LATEST_TAG" ]; then + LATEST_TAG=$(curl -s "https://api.github.com/repos/$OWNER/$REPO/tags" | jq -r '.[0].name') + fi + + # If LATEST_TAG is still empty, set a default value + local DEFAULT_TAG="v3.0.0" + + LATEST_TAG="${LATEST_TAG:-$DEFAULT_TAG}" + + debug "DEFAULT_TAG: $DEFAULT_TAG" + info "Use OpenIM Version LATEST_TAG: $LATEST_TAG" + + # If MODIFIED_TAG was not defined outside the function, modify it here,example: 3.0.1-beta.1 + if [ -z "$MODIFIED_TAG" ]; then + MODIFIED_TAG=$(echo $LATEST_TAG | sed 's/v//') + fi + + # If MODIFIED_TAG is still empty, set a default value + local DEFAULT_MODIFIED_TAG="${DEFAULT_TAG#v}" + MODIFIED_TAG="${MODIFIED_TAG:-$DEFAULT_MODIFIED_TAG}" + + debug "MODIFIED_TAG: $MODIFIED_TAG" + + # Construct the tarball URL + TARBALL_URL="${PROXY}https://github.com/$OWNER/$REPO/archive/refs/tags/$LATEST_TAG.tar.gz" + + info "Downloaded OpenIM TARBALL_URL: $TARBALL_URL" + + info "Starting the OpenIM automated one-click deployment script." + + # Set the download and extract directory to /tmp + if [ -z "$DOWNLOAD_OPENIM_DIR" ]; then + DOWNLOAD_OPENIM_DIR="/tmp" + fi + + # Check if /tmp directory exists + if [ ! -d "$DOWNLOAD_OPENIM_DIR" ]; then + warn "$DOWNLOAD_OPENIM_DIR does not exist. Creating it..." + mkdir -p "$DOWNLOAD_OPENIM_DIR" + fi + + info "Downloading OpenIM source code from $TARBALL_URL to $DOWNLOAD_OPENIM_DIR" + + curl -L -o "${DOWNLOAD_OPENIM_DIR}/${MODIFIED_TAG}.tar.gz" $TARBALL_URL + + tar -xzvf "${DOWNLOAD_OPENIM_DIR}/${MODIFIED_TAG}.tar.gz" -C "$DOWNLOAD_OPENIM_DIR" + cd "$DOWNLOAD_OPENIM_DIR/$REPO-$MODIFIED_TAG" + + success "Source code downloaded and extracted to $REPO-$MODIFIED_TAG" +} + +function set_openim_env() { + warn "This command can only be executed once. It will modify the component passwords in docker-compose based on the PASSWORD variable in .env, and modify the component passwords in config/config.yaml. If the password in .env changes, you need to first execute docker-compose down; rm components -rf and then execute this command." + # Set default values for user input + user="root" + password="openIM123" + endpoint="http://" +} + +function install_openim() { + info "Installing OpenIM" + make -j${CPU} install V=1 + + info "Checking installation" + make check + + success "OpenIM installation completed successfully. Happy chatting!" +} + +############### OpenIM Help ############### + +# Function to display help message +display_help() { + openim_color + color_echo ${BRIGHT_GREEN_PREFIX} "Usage: $0 [options]" + color_echo ${BRIGHT_GREEN_PREFIX} "Options:" + echo + color_echo ${BLUE_PREFIX} "-u, --user ${CYAN_PREFIX}set user (default: root)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-p, --password ${CYAN_PREFIX}set password (default: openIM123)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-e, --endpoint ${CYAN_PREFIX}set endpoint (default: http://127.0.0.1:10005)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-a, --api ${CYAN_PREFIX}set API URL (default: http://127.0.0.1:10002/object/)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-d, --directory ${CYAN_PREFIX}set directory for large disk space (default: ./)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-h, --help ${CYAN_PREFIX}display this help message and exit${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-cn, --china ${CYAN_PREFIX}set to use the Chinese domestic proxy${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-t, --tag ${CYAN_PREFIX}specify the tag (default option, set to latest if not specified)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-r, --release ${CYAN_PREFIX}specify the release branch (cannot be used with the tag option)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-g, --go-version ${CYAN_PREFIX}set the Go language version (default: GO_VERSION=\"1.18\")${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-i, --install-dir ${CYAN_PREFIX}set the OpenIM installation directory (default: /tmp)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-gt, --github-token ${CYAN_PREFIX}set the GITHUB_TOKEN (default: not set)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "--cpu ${CYAN_PREFIX}set the number of concurrent processes${COLOR_SUFFIX}" + echo + color_echo ${RED_PREFIX} "Note: Only one of the -t/--tag or -r/--release options can be used at a time.${COLOR_SUFFIX}" + color_echo ${RED_PREFIX} "If both are used, the -t/--tag option will be prioritized.${COLOR_SUFFIX}" + echo + exit 1 +} + +# Use getopts to parse command line flags +while getopts ":h:u:p:e:a:d:" opt; do + case ${opt} in + h ) + display_help + ;; + u ) + user=$OPTARG + ;; + p ) + password=$OPTARG + ;; + e ) + endpoint=$OPTARG + ;; + a ) + api_url=$OPTARG + ;; + d ) + data_dir=$OPTARG + ;; + \? ) + echo "Invalid Option: -$OPTARG" 1>&2 + exit 1 + ;; + : ) + echo "Invalid Option: -$OPTARG requires an argument" 1>&2 + exit 1 + ;; + esac +done + ############### OpenIM LOGO ############### # Set text color to cyan for header and URL +print_with_delay() { + text="$1" + delay="$2" + + for i in $(seq 0 $((${#text}-1))); do + printf "${text:$i:1}" + sleep $delay + done + printf "\n" +} + +print_progress() { + total="$1" + delay="$2" -openim_logo() { + printf "[" + for i in $(seq 1 $total); do + printf "#" + sleep $delay + done + printf "]\n" +} + +# Function for colored echo +color_echo() { + COLOR=$1 + shift + echo -e "${COLOR}===> $* ${COLOR_SUFFIX}" +} + +# Color definitions +function openim_color() { + COLOR_SUFFIX="\033[0m" # End all colors and special effects + + BLACK_PREFIX="\033[30m" # Black prefix + RED_PREFIX="\033[31m" # Red prefix + GREEN_PREFIX="\033[32m" # Green prefix + YELLOW_PREFIX="\033[33m" # Yellow prefix + BLUE_PREFIX="\033[34m" # Blue prefix + SKY_BLUE_PREFIX="\033[36m" # Sky blue prefix + WHITE_PREFIX="\033[37m" # White prefix + BOLD_PREFIX="\033[1m" # Bold prefix + UNDERLINE_PREFIX="\033[4m" # Underline prefix + ITALIC_PREFIX="\033[3m" # Italic prefix + BRIGHT_GREEN_PREFIX='\033[1;32m' # Bright green prefix + + CYAN_PREFIX="\033[0;36m" # Cyan prefix +} + +# --- helper functions for logs --- +info() +{ + echo -e "[${GREEN_PREFIX}INFO${COLOR_SUFFIX}] " "$@" +} +warn() +{ + echo -e "[${YELLOW_PREFIX}WARN${COLOR_SUFFIX}] " "$@" >&2 +} +fatal() +{ + echo -e "[${RED_PREFIX}ERROR${COLOR_SUFFIX}] " "$@" >&2 + exit 1 +} +debug() +{ + echo -e "[${BLUE_PREFIX}DEBUG${COLOR_SUFFIX}]===> " "$@" +} +success() +{ + echo -e "${BRIGHT_GREEN_PREFIX}=== [SUCCESS] ===${COLOR_SUFFIX}\n=> " "$@" +} + +function openim_logo() { + # Set text color to cyan for header and URL echo -e "\033[0;36m" # Display fancy ASCII Art logo + # look http://patorjk.com/software/taag/#p=display&h=1&v=1&f=Doh&t=OpenIM print_with_delay ' -########################################################################## - ____ _ _ - / __ \ (_) | - | | | |_ __ ___ _ __ _| |_ _ _ _ __ ___ _ __ - | | | | '"'"'_ \ / _ \| '"'"'__| | __| | | | '"'"'_ ` _ \| '"'"'_ \ - | |__| | |_) | (_) | | | | |_| |_| | | | | | | |_) | - \____/| .__/ \___/|_| |_|\__|\__,_|_| |_| |_| .__/ - | | | | - |_| |_| -########################################################################## - ' 0.01 + + + OOOOOOOOO IIIIIIIIIIMMMMMMMM MMMMMMMM + OO:::::::::OO I::::::::IM:::::::M M:::::::M + OO:::::::::::::OO I::::::::IM::::::::M M::::::::M +O:::::::OOO:::::::O II::::::IIM:::::::::M M:::::::::M +O::::::O O::::::Oppppp ppppppppp eeeeeeeeeeee nnnn nnnnnnnn I::::I M::::::::::M M::::::::::M +O:::::O O:::::Op::::ppp:::::::::p ee::::::::::::ee n:::nn::::::::nn I::::I M:::::::::::M M:::::::::::M +O:::::O O:::::Op:::::::::::::::::p e::::::eeeee:::::een::::::::::::::nn I::::I M:::::::M::::M M::::M:::::::M +O:::::O O:::::Opp::::::ppppp::::::pe::::::e e:::::enn:::::::::::::::n I::::I M::::::M M::::M M::::M M::::::M +O:::::O O:::::O p:::::p p:::::pe:::::::eeeee::::::e n:::::nnnn:::::n I::::I M::::::M M::::M::::M M::::::M +O:::::O O:::::O p:::::p p:::::pe:::::::::::::::::e n::::n n::::n I::::I M::::::M M:::::::M M::::::M +O:::::O O:::::O p:::::p p:::::pe::::::eeeeeeeeeee n::::n n::::n I::::I M::::::M M:::::M M::::::M +O::::::O O::::::O p:::::p p::::::pe:::::::e n::::n n::::n I::::I M::::::M MMMMM M::::::M +O:::::::OOO:::::::O p:::::ppppp:::::::pe::::::::e n::::n n::::nII::::::IIM::::::M M::::::M + OO:::::::::::::OO p::::::::::::::::p e::::::::eeeeeeee n::::n n::::nI::::::::IM::::::M M::::::M + OO:::::::::OO p::::::::::::::pp ee:::::::::::::e n::::n n::::nI::::::::IM::::::M M::::::M + OOOOOOOOO p::::::pppppppp eeeeeeeeeeeeee nnnnnn nnnnnnIIIIIIIIIIMMMMMMMM MMMMMMMM + p:::::p + p:::::p + p:::::::p + p:::::::p + p:::::::p + ppppppppp + + ' 0.0001 # Display product URL print_with_delay "Discover more and contribute at: https://github.com/OpenIMSDK/Open-IM-Server" 0.01 @@ -175,42 +425,22 @@ openim_logo() { print_with_delay "Join our developer community on Slack: https://join.slack.com/t/openimsdk/shared_invite/zt-1tmoj26uf-_FDy3dowVHBiGvLk9e5Xkg" 0.01 # Reset text color back to normal - echo -e "\033[0m" + echo -e "\033[0m" } # Main function to run the script -main() { +function openim_main() { + check_git_repo + check_isroot openim_color - install_tools + check_docker install_go - - LATEST_TAG=$(curl -s "https://api.github.com/repos/$OWNER/$REPO/tags" | jq -r '.[0].name') - MODIFIED_TAG=$(echo $LATEST_TAG | sed -r 's/(v3\.0\.)[1-9][0-9]*$/\10/g') - TARBALL_URL="https://github.com/$OWNER/$REPO/archive/refs/tags/$MODIFIED_TAG.tar.gz" - - color_echo ${GREEN_PREFIX} "Starting the OpenIM automated one-click deployment script." - - color_echo ${GREEN_PREFIX} "Downloading OpenIM source code from $TARBALL_URL" - curl -L -o "${MODIFIED_TAG}.tar.gz" $TARBALL_URL - tar -xzvf "${MODIFIED_TAG}.tar.gz" - cd "$REPO-$MODIFIED_TAG" - + download_source_code + set_openim_env + install_openim openim_logo - info "Source code downloaded and extracted to $REPO-$MODIFIED_TAG" - - # Add the logic to modify .env based on user input here - - warn "This command can only be executed once. It will modify the component passwords in docker-compose based on the PASSWORD variable in .env, and modify the component passwords in config/config.yaml. If the password in .env changes, you need to first execute docker-compose down; rm components -rf and then execute this command." - - color_echo ${GREEN_PREFIX} "Installing OpenIM" - make --debug -j install V=1 - - color_echo ${GREEN_PREFIX} "Checking installation" - make --debug check - - color_echo ${GREEN_PREFIX} "OpenIM installation completed successfully. Happy chatting!" } -main "$@" \ No newline at end of file +openim_main "$@" \ No newline at end of file