|
| 1 | +#!/usr/bin/env bash |
| 2 | +#================================================= |
| 3 | +# System Required: CentOS/Debian/ArchLinux with Systemd Support |
| 4 | +# Description: ServerStatus-goclient |
| 5 | +# Version: v1.0.1 |
| 6 | +# Author: Kagurazaka Mizuki && RvvcIm |
| 7 | +#================================================= |
| 8 | + |
| 9 | +Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m" |
| 10 | +Info="${Green_font_prefix}[信息]${Font_color_suffix}" |
| 11 | +Error="${Red_font_prefix}[错误]${Font_color_suffix}" |
| 12 | +Tip="${Green_font_prefix}[注意]${Font_color_suffix}" |
| 13 | + |
| 14 | +function check_sys() { |
| 15 | + if [[ -f /etc/redhat-release ]]; then |
| 16 | + release="centos" |
| 17 | + elif grep -q -E -i "debian|ubuntu" /etc/issue; then |
| 18 | + release="debian" |
| 19 | + elif grep -q -E -i "centos|red hat|redhat" /etc/issue; then |
| 20 | + release="centos" |
| 21 | + elif grep -q -E -i "Arch|Manjaro" /etc/issue; then |
| 22 | + release="arch" |
| 23 | + elif grep -q -E -i "debian|ubuntu" /proc/version; then |
| 24 | + release="debian" |
| 25 | + elif grep -q -E -i "centos|red hat|redhat" /proc/version; then |
| 26 | + release="centos" |
| 27 | + else |
| 28 | + echo -e "Status Client 暂不支持该 Linux 发行版" |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | + bit=$(uname -m) |
| 32 | +} |
| 33 | + |
| 34 | +function check_pid() { |
| 35 | + PID=$(pgrep -f "status-client") |
| 36 | +} |
| 37 | + |
| 38 | +function install_dependencies() { |
| 39 | + case ${release} in |
| 40 | + centos) |
| 41 | + yum install -y wget curl |
| 42 | + ;; |
| 43 | + debian) |
| 44 | + apt-get update -y |
| 45 | + apt-get install -y wget curl |
| 46 | + ;; |
| 47 | + arch) |
| 48 | + pacman -Syu --noconfirm wget curl |
| 49 | + ;; |
| 50 | + *) |
| 51 | + exit 1 |
| 52 | + ;; |
| 53 | + esac |
| 54 | +} |
| 55 | + |
| 56 | +function input_dsn() { |
| 57 | + echo -e "${Info} 请输入服务端的 DSN, 格式为 “username:password@masterip(:port)”" |
| 58 | + read -re dsn |
| 59 | +} |
| 60 | + |
| 61 | +service_conf=/usr/lib/systemd/system/status-client.service |
| 62 | +#service_conf=test.conf |
| 63 | + |
| 64 | +function write_service() { |
| 65 | + echo -e "${Info} 写入systemd配置中" |
| 66 | + cat >${service_conf} <<-EOF |
| 67 | +[Unit] |
| 68 | +Description=ServerStatus-Client |
| 69 | +Documentation=https://github.com/cokemine/ServerStatus-goclient |
| 70 | +After=network.target |
| 71 | +
|
| 72 | +[Service] |
| 73 | +ExecStart=/usr/local/ServerStatus/client/status-client -dsn="${dsn}" |
| 74 | +ExecReload=/bin/kill -HUP $MAINPID |
| 75 | +Restart=on-failure |
| 76 | +
|
| 77 | +[Install] |
| 78 | +WantedBy=multi-user.target |
| 79 | +EOF |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +function enable_service() { |
| 84 | + write_service |
| 85 | + systemctl enable status-client |
| 86 | + systemctl start status-client |
| 87 | + check_pid |
| 88 | + if [[ -n ${PID} ]]; then |
| 89 | + echo -e "${Info} Status Client 启动成功!" |
| 90 | + else |
| 91 | + echo -e "${Error} Status Client 启动失败!" |
| 92 | + fi |
| 93 | +} |
| 94 | + |
| 95 | +function restart_service() { |
| 96 | + write_service |
| 97 | + systemctl daemon-reload |
| 98 | + systemctl restart status-client |
| 99 | + check_pid |
| 100 | + if [[ -n ${PID} ]]; then |
| 101 | + echo -e "${Info} Status Client 启动成功!" |
| 102 | + else |
| 103 | + echo -e "${Error} Status Client 启动失败!" |
| 104 | + fi |
| 105 | +} |
| 106 | + |
| 107 | +function reset_config() { |
| 108 | + restart_service |
| 109 | +} |
| 110 | + |
| 111 | +function install_client() { |
| 112 | + case ${bit} in |
| 113 | + x86_64) |
| 114 | + arch=amd64 |
| 115 | + ;; |
| 116 | + i386) |
| 117 | + arch=386 |
| 118 | + ;; |
| 119 | + aarch64 | aarch64_be | arm64 | armv8b | armv8l) |
| 120 | + arch=arm64 |
| 121 | + ;; |
| 122 | + arm | armv6l | armv7l | armv5tel | armv5tejl) |
| 123 | + arch=arm |
| 124 | + ;; |
| 125 | + mips | mips64) |
| 126 | + arch=mips |
| 127 | + ;; |
| 128 | + *) |
| 129 | + exit 1 |
| 130 | + ;; |
| 131 | + esac |
| 132 | + echo -e "${Info} 下载 ${arch} 二进制文件" |
| 133 | + mkdir -p /usr/local/ServerStatus/client/ |
| 134 | + cd /tmp && wget "https://github.com/cokemine/ServerStatus-goclient/releases/latest/download/status-client_linux_${arch}.tar.gz" |
| 135 | + tar -zxvf "status-client_linux_${arch}.tar.gz" status-client |
| 136 | + mv status-client /usr/local/ServerStatus/client/ |
| 137 | + chmod +x /usr/local/ServerStatus/client/status-client |
| 138 | + enable_service |
| 139 | +} |
| 140 | + |
| 141 | +function auto_install() { |
| 142 | + dsn=$(echo ${*}) |
| 143 | + install_client |
| 144 | +} |
| 145 | + |
| 146 | +function uninstall_client() { |
| 147 | + systemctl stop status-client |
| 148 | + systemctl disable status-client |
| 149 | + rm -rf /usr/local/ServerStatus/client/ |
| 150 | + rm -rf /usr/lib/systemd/system/status-client.service |
| 151 | +} |
| 152 | + |
| 153 | +check_sys |
| 154 | +case "$1" in |
| 155 | +uninstall|uni) |
| 156 | + uninstall_client |
| 157 | + ;; |
| 158 | +reset_conf|re) |
| 159 | + input_dsn |
| 160 | + reset_config |
| 161 | + ;; |
| 162 | +-dsn) |
| 163 | + shift 1 |
| 164 | + install_dependencies |
| 165 | + auto_install ${*} |
| 166 | + ;; |
| 167 | +*) |
| 168 | + install_dependencies |
| 169 | + input_dsn |
| 170 | + install_client |
| 171 | + ;; |
| 172 | +esac |
0 commit comments