feat: add server status install script

This commit is contained in:
moonrailgun 2023-11-07 16:56:47 +08:00
parent 28b326bdb2
commit 2967fcd05d
5 changed files with 211 additions and 12 deletions

View File

@ -25,3 +25,4 @@ jobs:
goversion: 1.21.1
binary_name: "tianji-reporter"
compress_assets: "OFF"
asset_name: "${BINARY_NAME}-${GOOS}-${GOARCH}"

View File

@ -48,6 +48,7 @@
"express-async-errors": "^3.1.1",
"express-validator": "^7.0.1",
"filesize": "^10.0.12",
"fs-extra": "^11.1.1",
"is-localhost-ip": "^2.0.0",
"jsonwebtoken": "^9.0.2",
"lodash": "^4.17.21",
@ -104,7 +105,6 @@
"@vitejs/plugin-react": "^4.0.4",
"autoprefixer": "^10.4.15",
"cross-env": "^7.0.3",
"fs-extra": "^11.1.1",
"nodemon": "^2.0.22",
"postcss": "^8.4.29",
"prisma": "^5.4.2",

View File

@ -85,6 +85,9 @@ dependencies:
filesize:
specifier: ^10.0.12
version: 10.0.12
fs-extra:
specifier: ^11.1.1
version: 11.1.1
is-localhost-ip:
specifier: ^2.0.0
version: 2.0.0
@ -249,9 +252,6 @@ devDependencies:
cross-env:
specifier: ^7.0.3
version: 7.0.3
fs-extra:
specifier: ^11.1.1
version: 11.1.1
nodemon:
specifier: ^2.0.22
version: 2.0.22
@ -3780,8 +3780,8 @@ packages:
dependencies:
graceful-fs: 4.2.11
jsonfile: 6.1.0
universalify: 2.0.0
dev: true
universalify: 2.0.1
dev: false
/fs-extra@8.1.0:
resolution: {integrity: sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==}
@ -3946,6 +3946,7 @@ packages:
/graceful-fs@4.2.11:
resolution: {integrity: sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==}
dev: false
/graphlib@2.1.8:
resolution: {integrity: sha512-jcLLfkpoVGmH7/InMC/1hIvOPSUh38oJtGhvrOFGzioE1DZ+0YW16RgmOJhHiuWTvGiJQ9Z1Ik43JvkRPRvE+A==}
@ -4372,10 +4373,10 @@ packages:
/jsonfile@6.1.0:
resolution: {integrity: sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==}
dependencies:
universalify: 2.0.0
universalify: 2.0.1
optionalDependencies:
graceful-fs: 4.2.11
dev: true
dev: false
/jsonwebtoken@9.0.2:
resolution: {integrity: sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==}
@ -7046,10 +7047,10 @@ packages:
engines: {node: '>= 4.0.0'}
dev: false
/universalify@2.0.0:
resolution: {integrity: sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==}
/universalify@2.0.1:
resolution: {integrity: sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==}
engines: {node: '>= 10.0.0'}
dev: true
dev: false
/unpipe@1.0.0:
resolution: {integrity: sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==}

174
scripts/install.sh Normal file
View File

@ -0,0 +1,174 @@
#!/usr/bin/env bash
#=================================================
# System Required: CentOS/Debian/ArchLinux with Systemd Support
# Description: Tianji Report
# Author: moonrailgun
#=================================================
Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
Info="${Green_font_prefix}[Info]${Font_color_suffix}"
Error="${Red_font_prefix}[Error]${Font_color_suffix}"
Tip="${Green_font_prefix}[Tip]${Font_color_suffix}"
function check_sys() {
if [[ -f /etc/redhat-release ]]; then
release="centos"
elif grep -q -E -i "debian|ubuntu" /etc/issue; then
release="debian"
elif grep -q -E -i "centos|red hat|redhat" /etc/issue; then
release="centos"
elif grep -q -E -i "Arch|Manjaro" /etc/issue; then
release="arch"
elif grep -q -E -i "debian|ubuntu" /proc/version; then
release="debian"
elif grep -q -E -i "centos|red hat|redhat" /proc/version; then
release="centos"
else
echo -e "Tianji Report not support this linux version"
exit 1
fi
bit=$(uname -m)
}
function check_pid() {
PID=$(pgrep -f "tianji-reporter")
}
function install_dependencies() {
case ${release} in
centos)
yum install -y wget curl
;;
debian)
apt-get update -y
apt-get install -y wget curl
;;
arch)
pacman -Syu --noconfirm wget curl
;;
*)
exit 1
;;
esac
}
function input_dsn() {
defaultServer="{{DEFAULT_SERVER}}"
read -p "${Info} Please input server url(default: $defaultWorkspace)" serverUrl
serverUrl=${serverUrl:$defaultWorkspace}
defaultWorkspace="{{DEFAULT_WORKSPACE}}"
read -p "${Info} Please input workspaceId(default: $defaultWorkspace)" workspace
workspace=${workspace:$defaultWorkspace}
}
service_conf=/usr/lib/systemd/system/tianji-reporter.service
function write_service() {
echo -e "${Info} Write to systemd configuration"
cat >${service_conf} <<-EOF
[Unit]
Description=Tianji-Report
Documentation=https://github.com/msgbyte/tianji
After=network.target
[Service]
ExecStart=/usr/local/tianji/reporter/tianji-reporter --url "${serverUrl}" --workspace "${workspace}"
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
}
function enable_service() {
write_service
systemctl enable tianji-reporter
systemctl start tianji-reporter
check_pid
if [[ -n ${PID} ]]; then
echo -e "${Info} Tianji Reporter Started!"
else
echo -e "${Error} Tianji Reporter Start Failed!"
fi
}
function restart_service() {
write_service
systemctl daemon-reload
systemctl restart tianji-reporter
check_pid
if [[ -n ${PID} ]]; then
echo -e "${Info} Tianji Reporter Started!"
else
echo -e "${Error} Tianji Reporter Start Failed!"
fi
}
function reset_config() {
restart_service
}
function install_client() {
case ${bit} in
x86_64)
arch=amd64
;;
# i386)
# arch=386
# ;;
aarch64 | aarch64_be | arm64 | armv8b | armv8l)
arch=arm64
;;
# arm | armv6l | armv7l | armv5tel | armv5tejl)
# arch=arm
# ;;
# mips | mips64)
# arch=mips
# ;;
*)
exit 1
;;
esac
echo -e "${Info} Downloading ${arch} binary file..."
mkdir -p /usr/local/tianji/reporter/
cd /tmp && wget "https://github.com/msgbyte/tianji/releases/latest/download/tianji-reporter-linux-${arch}"
mv tianji-reporter-linux-${arch} /usr/local/tianji/reporter/tianji-reporter
chmod +x /usr/local/tianji/reporter/tianji-reporter
enable_service
}
function auto_install() {
dsn=$(echo ${*})
install_client
}
function uninstall_client() {
systemctl stop tianji-reporter
systemctl disable tianji-reporter
rm -rf /usr/local/tianji/reporter/
rm -rf /usr/lib/systemd/system/tianji-reporter.service
}
check_sys
case "$1" in
uninstall|uni)
uninstall_client
;;
reset_conf|re)
input_dsn
reset_config
;;
-dsn)
shift 1
install_dependencies
auto_install ${*}
;;
*)
install_dependencies
input_dsn
install_client
;;
esac

View File

@ -1,9 +1,15 @@
import { Router } from 'express';
import { body, header, validate } from '../middleware/validate';
import { body, header, param, validate } from '../middleware/validate';
import { recordServerStatus } from '../model/serverStatus';
import fs from 'fs-extra';
import path from 'path';
export const serverStatusRouter = Router();
const installScript = fs.readFileSync(
path.resolve(process.cwd(), './scripts/install.sh')
);
serverStatusRouter.post(
'/report',
validate(
@ -22,3 +28,20 @@ serverStatusRouter.post(
res.send('success');
}
);
serverStatusRouter.get(
'/:workspaceId/install.sh',
validate(param('workspaceId').isString()),
async (req, res) => {
const { workspaceId } = req.params;
const server = `${req.protocol}://${req.get('Host')}`;
res
.setHeader('Content-Type', 'text/plain')
.send(
String(installScript)
.replace('{{DEFAULT_SERVER}}', server)
.replace('{{DEFAULT_WORKSPACE}}', workspaceId)
);
}
);