modify scripts
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,7 +4,7 @@ logs/
|
|||||||
*.log
|
*.log
|
||||||
|
|
||||||
# 忽略编译后的二进制文件
|
# 忽略编译后的二进制文件
|
||||||
bin/
|
bin/*
|
||||||
obj/
|
obj/
|
||||||
*.exe
|
*.exe
|
||||||
*.out
|
*.out
|
||||||
|
|||||||
41
bin/deploy.sh
Executable file
41
bin/deploy.sh
Executable file
@ -0,0 +1,41 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 远程服务器信息
|
||||||
|
REMOTE_USER="ubuntu"
|
||||||
|
REMOTE_HOST="170.106.191.35"
|
||||||
|
REMOTE_DIR="/usr/local/aigrammar"
|
||||||
|
BACKUP_DIR="$REMOTE_DIR/backup"
|
||||||
|
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
|
||||||
|
|
||||||
|
# 配置文件环境(默认生产环境)
|
||||||
|
ENV=${1:-prod} # 运行时可以传入 `dev` 或 `prod`
|
||||||
|
CONFIG_FILE="conf/config.${ENV}.toml"
|
||||||
|
|
||||||
|
# 检查本地文件是否存在
|
||||||
|
if [[ ! -f "bin/aigrammar" || ! -f "bin/service.sh" || ! -f "$CONFIG_FILE" ]]; then
|
||||||
|
echo "❌ 关键文件不存在,请检查 bin/aigrammar, bin/service.sh, $CONFIG_FILE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 远程创建必要目录(如果不存在)
|
||||||
|
echo "🔹 确保远程目录结构完整..."
|
||||||
|
ssh $REMOTE_USER@$REMOTE_HOST "mkdir -p $REMOTE_DIR/{bin,conf,log,backup}"
|
||||||
|
|
||||||
|
# 备份远程服务器的旧文件
|
||||||
|
echo "📂 备份远程服务器文件..."
|
||||||
|
ssh $REMOTE_USER@$REMOTE_HOST "mkdir -p $BACKUP_DIR && \
|
||||||
|
[ -f $REMOTE_DIR/bin/aigrammar ] && mv $REMOTE_DIR/bin/aigrammar $BACKUP_DIR/aigrammar_$TIMESTAMP || true && \
|
||||||
|
[ -f $REMOTE_DIR/bin/service.sh ] && mv $REMOTE_DIR/bin/service.sh $BACKUP_DIR/service_$TIMESTAMP.sh || true && \
|
||||||
|
[ -f $REMOTE_DIR/conf/config.toml ] && mv $REMOTE_DIR/conf/config.toml $BACKUP_DIR/config_$TIMESTAMP.toml || true"
|
||||||
|
|
||||||
|
# 复制文件到远程服务器(保持目录结构)
|
||||||
|
echo "📤 复制文件到远程服务器..."
|
||||||
|
scp bin/aigrammar $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/bin/
|
||||||
|
scp bin/service.sh $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/bin/
|
||||||
|
scp $CONFIG_FILE $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/conf/config.toml
|
||||||
|
|
||||||
|
# 远程执行 restart
|
||||||
|
echo "🔄 远程重启服务..."
|
||||||
|
ssh $REMOTE_USER@$REMOTE_HOST "cd $REMOTE_DIR/bin && chmod +x service.sh && ./service.sh restart"
|
||||||
|
|
||||||
|
echo "✅ 发布完成!"
|
||||||
58
bin/service.sh
Executable file
58
bin/service.sh
Executable file
@ -0,0 +1,58 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 确保脚本使用一个参数
|
||||||
|
if [ $# -ne 1 ]; then
|
||||||
|
echo "Usage: $0 {start|stop|restart}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 获取当前脚本所在目录
|
||||||
|
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
program_name="aigrammar"
|
||||||
|
program_path="$script_dir/$program_name"
|
||||||
|
|
||||||
|
# 配置和日志目录
|
||||||
|
config_file="$script_dir/../conf/config.toml"
|
||||||
|
log_dir="$script_dir/../log"
|
||||||
|
log_file="$log_dir/output.log"
|
||||||
|
pid_file="$script_dir/${program_name}.pid"
|
||||||
|
|
||||||
|
# 确保日志目录存在
|
||||||
|
mkdir -p "$log_dir"
|
||||||
|
|
||||||
|
case "$1" in
|
||||||
|
start)
|
||||||
|
echo "Starting $program_name..."
|
||||||
|
if [ -f "$pid_file" ] && kill -0 $(cat "$pid_file") 2>/dev/null; then
|
||||||
|
echo "$program_name is already running."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
nohup "$program_path" --config="$config_file" > "$log_file" 2>&1 &
|
||||||
|
echo $! > "$pid_file"
|
||||||
|
echo "$program_name started with PID $(cat "$pid_file")."
|
||||||
|
;;
|
||||||
|
|
||||||
|
stop)
|
||||||
|
echo "Stopping $program_name..."
|
||||||
|
if [ -f "$pid_file" ]; then
|
||||||
|
kill -TERM $(cat "$pid_file") 2>/dev/null && rm -f "$pid_file"
|
||||||
|
echo "$program_name stopped."
|
||||||
|
else
|
||||||
|
echo "No PID file found, attempting pkill..."
|
||||||
|
pkill -f "$program_name" && echo "$program_name stopped."
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
|
||||||
|
restart)
|
||||||
|
echo "Restarting $program_name..."
|
||||||
|
$0 stop
|
||||||
|
sleep 2
|
||||||
|
$0 start
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
echo "Unknown command: $1"
|
||||||
|
echo "Usage: $0 {start|stop|restart}"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
75
bin/stat.sh
Executable file
75
bin/stat.sh
Executable file
@ -0,0 +1,75 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
LOG_FILE="/usr/local/aigrammar/log/app.log"
|
||||||
|
PY_SCRIPT="/home/ubuntu/projects/devops/tools/send_to_wecom.py"
|
||||||
|
TODAY=$(date +%Y-%m-%d)
|
||||||
|
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
|
||||||
|
FILTERED_LOG="${LOG_FILE%.log}_${YESTERDAY}.log"
|
||||||
|
|
||||||
|
# Check if log file exists
|
||||||
|
if [ ! -f "$LOG_FILE" ]; then
|
||||||
|
echo "Error: Log file not found - $LOG_FILE" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if Python script exists and is executable
|
||||||
|
if [ ! -f "$PY_SCRIPT" ]; then
|
||||||
|
echo "Error: Python script not found or not executable - $PY_SCRIPT" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create filtered log file
|
||||||
|
echo "Filtering yesterday's logs..."
|
||||||
|
#grep "$TODAY" "$LOG_FILE" > "$FILTERED_LOG"
|
||||||
|
grep "$YESTERDAY" "$LOG_FILE" > "$FILTERED_LOG"
|
||||||
|
|
||||||
|
# Check if filtered log has content
|
||||||
|
if [ ! -s "$FILTERED_LOG" ]; then
|
||||||
|
echo "Warning: No logs found for $YESTERDAY"
|
||||||
|
content="Date: $YESTERDAY\nNo log records found"
|
||||||
|
python3 "$PY_SCRIPT" "$content"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Filtered log created: $FILTERED_LOG"
|
||||||
|
|
||||||
|
# Statistics
|
||||||
|
echo "Calculating statistics..."
|
||||||
|
request_total=$(grep -E "\"level\":\"info\"" "$FILTERED_LOG" | grep "\"func\":\"main.parseToken\"" | wc -l)
|
||||||
|
grammar_total=$(grep -v "\"level\":\"debug\"" "$FILTERED_LOG" | grep "\"func\":\"main.GrammarHandler\"" | wc -l)
|
||||||
|
translate_total=$(grep -v "\"level\":\"debug\"" "$FILTERED_LOG" | grep "\"func\":\"main.TranslateHandler\"" | wc -l)
|
||||||
|
words_total=$(grep -v "\"level\":\"debug\"" "$FILTERED_LOG" | grep "\"func\":\"main.WordsHandler\"" | wc -l)
|
||||||
|
free_limit_total=$(grep -E "\"level\":\"warn\"" "$FILTERED_LOG" | grep "\"func\":\"main.queryUserBenefits\"" | wc -l)
|
||||||
|
error_total=$(grep "\"level\":\"error\"" "$FILTERED_LOG" | wc -l)
|
||||||
|
|
||||||
|
# Generate content to send
|
||||||
|
content="Date: $YESTERDAY
|
||||||
|
Total Requests: $request_total
|
||||||
|
Grammar Check Requests: $grammar_total
|
||||||
|
Translation Requests: $translate_total
|
||||||
|
Words Requests: $words_total
|
||||||
|
Free Quota Exceeded: $free_limit_total
|
||||||
|
Total Errors: $error_total"
|
||||||
|
|
||||||
|
# Print statistics
|
||||||
|
echo "===== Statistics ====="
|
||||||
|
echo -e "$content"
|
||||||
|
|
||||||
|
# Send to WeCom
|
||||||
|
echo "Sending to WeChat Work..."
|
||||||
|
python3 "$PY_SCRIPT" "$content"
|
||||||
|
|
||||||
|
# Check result
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo "Successfully sent to WeChat Work"
|
||||||
|
else
|
||||||
|
echo "Error: Failed to send to WeChat Work" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# remove tmp logfile
|
||||||
|
rm -rf "$FILTERED_LOG"
|
||||||
|
|
||||||
|
echo "Script execution completed"
|
||||||
|
exit 0
|
||||||
Reference in New Issue
Block a user