1 Commits

Author SHA1 Message Date
68f6f02ec5 更新各种脚本 2026-02-22 20:55:05 +08:00
9 changed files with 330 additions and 8 deletions

12
.cargo/config.toml Normal file
View File

@ -0,0 +1,12 @@
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
[target.x86_64-unknown-linux-gnu]
linker = "gcc"
[target.x86_64-unknown-linux-musl]
rustflags = ["-C", "target-feature=+crt-static", "-C", "link-self-contained=yes"]
[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-gnu-gcc"
rustflags = ["-C", "target-feature=+crt-static", "-C", "link-self-contained=yes"]

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
/target
/release
/.cargo

2
Cargo.lock generated
View File

@ -1180,7 +1180,7 @@ dependencies = [
[[package]]
name = "pt_system_checking"
version = "0.1.0"
version = "0.1.4"
dependencies = [
"anyhow",
"chrono",

View File

@ -1,6 +1,6 @@
[package]
name = "pt_system_checking"
version = "0.1.0"
version = "0.1.4"
edition = "2024"
[dependencies]

View File

@ -10,14 +10,14 @@ BUILD_DIR = target
RELEASE_DIR = release
# 目标架构
ARCH_AMD64 = x86_64-unknown-linux-gnu
ARCH_AMD64 = x86_64-unknown-linux-musl
ARCH_ARM64 = aarch64-unknown-linux-gnu
# 输出二进制文件名
BINARY_AMD64 = $(PROJECT_NAME)-amd64
BINARY_ARM64 = $(PROJECT_NAME)-arm64
.PHONY: all help install-targets build-amd64 build-arm64 build-all clean release test prepare-release
.PHONY: all help install-targets build-amd64 build-arm64 build-all clean release test prepare-release create-release
# 默认目标
all: build-all
@ -31,6 +31,7 @@ help:
@echo " make build-all - 构建所有架构版本"
@echo " make release - 创建发布版本(优化编译)"
@echo " make prepare-release - 准备发布文件(包含安装脚本)"
@echo " make create-release - 创建 Gitea Release需要设置 VERSION 变量)"
@echo " make test - 运行测试"
@echo " make clean - 清理构建文件"
@echo " make install - 安装到系统(需要 root 权限)"
@ -104,6 +105,15 @@ prepare-release: release
@echo "请将以下文件上传到 Gitea Releases:"
@ls -lh $(RELEASE_DIR)/
# 创建 Gitea Release
create-release: prepare-release
@echo "创建 Gitea Release v$(VERSION)..."
tea releases create --tag v$(VERSION) --title "V$(VERSION) 版本发布" \
--asset "$(RELEASE_DIR)/install.sh" \
--asset "$(RELEASE_DIR)/$(BINARY_AMD64)" \
--asset "$(RELEASE_DIR)/$(BINARY_ARM64)"
@echo "Release 创建完成"
# 安装到系统(默认安装当前架构)
install:
cargo install --path .

View File

@ -33,6 +33,26 @@ sudo bash install.sh
3. 交互式配置服务参数(服务器 URL、检查间隔、日志级别
4. 安装并启动 systemd 服务
### 更新服务
```bash
curl -fsSL https://gitea.service.jazzwhom.top/Passthem/pt-disk-report/raw/branch/main/update.sh | sudo bash
```
或者下载后运行:
```bash
wget https://gitea.service.jazzwhom.top/Passthem/pt-disk-report/raw/branch/main/update.sh
sudo bash update.sh
```
更新脚本会:
1. 检查当前安装的版本
2. 获取最新版本信息
3. 自动备份当前版本
4. 下载并替换新版本
5. 重启服务(如果更新失败会自动恢复备份)
### 卸载服务
```bash
@ -63,6 +83,7 @@ make prepare-release
- `pt_system_checking-amd64` - x86_64 架构二进制文件
- `pt_system_checking-arm64` - ARM64 架构二进制文件(适用于香橙派 Zero3
- `install.sh` - 一键部署脚本
- `update.sh` - 一键更新脚本
## 配置说明

View File

@ -91,6 +91,21 @@ interactive_config() {
print_info "=== 配置服务参数 ==="
echo ""
# 检查是否为非交互模式(通过管道执行)
if [ ! -t 0 ]; then
print_info "检测到非交互模式,使用默认配置"
SERVER_URL=""
INTERVAL=300
LOG_LEVEL="info"
print_info "配置摘要:"
echo " 服务器 URL: ${SERVER_URL:-未配置}"
echo " 检查间隔: ${INTERVAL}"
echo " 日志级别: ${LOG_LEVEL}"
echo ""
return
fi
# 服务器 URL
read -p "请输入服务器 URL用于上报磁盘状态留空则不上报: " SERVER_URL
SERVER_URL=${SERVER_URL:-""}

View File

@ -1,4 +1,4 @@
use std::time::Duration;
use std::{collections::HashSet, time::Duration};
use clap::{Parser};
use config::{Config, ConfigError, Environment};
@ -40,12 +40,29 @@ async fn task(settings: &Settings) -> anyhow::Result<()> {
let disks = sysinfo::Disks::new_with_refreshed_list();
let mut available = 0;
let mut seen_device = HashSet::new();
for disk in disks.list().iter().filter(|d| !d.is_removable()) {
available += disk.available_space();
let mount_path = disk.mount_point().to_string_lossy();
if mount_path.contains("/snap") || mount_path.contains("/docker") {
continue;
}
match disk.kind() {
sysinfo::DiskKind::SSD | sysinfo::DiskKind::HDD => {
let dname = disk.name().to_string_lossy().to_string();
if !seen_device.insert(dname.clone()) {
continue;
}
let davailable = disk.available_space();
tracing::info!(disk=dname, available=davailable, "检查一块硬盘");
available += davailable;
}
_ => continue, // 跳过 Unknown, Overlaid 等类型
}
}
let report = (available >> 20) / 100;
let report = ((available >> 20) * 10) >> 10;
tracing::info!(report = report, "获取剩余硬盘空间(单位 .1GB");
if settings.server_url.is_empty() {

248
update.sh Executable file
View File

@ -0,0 +1,248 @@
#!/bin/bash
# PT System Checking 一键更新脚本
# 从 Gitea 下载最新版本并更新服务
set -e
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 配置变量
GITEA_REPO="https://gitea.service.jazzwhom.top/Passthem/pt-disk-report"
SERVICE_NAME="pt_system_checking"
INSTALL_DIR="/opt/${SERVICE_NAME}"
SYSTEMD_SERVICE="/etc/systemd/system/${SERVICE_NAME}.service"
# 打印信息函数
print_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检查是否以 root 运行
check_root() {
if [ "$EUID" -ne 0 ]; then
print_error "请使用 root 权限运行此脚本"
echo "使用方法: sudo bash update.sh"
exit 1
fi
}
# 检查服务是否已安装
check_installed() {
if [ ! -f "${INSTALL_DIR}/${SERVICE_NAME}" ]; then
print_error "服务未安装,请先运行 install.sh 进行安装"
exit 1
fi
if [ ! -f "${SYSTEMD_SERVICE}" ]; then
print_error "systemd 服务文件不存在,请先运行 install.sh 进行安装"
exit 1
fi
}
# 检测系统架构
detect_arch() {
local arch=$(uname -m)
case $arch in
x86_64)
echo "amd64"
;;
aarch64|arm64)
echo "arm64"
;;
*)
print_error "不支持的架构: $arch"
exit 1
;;
esac
}
# 获取当前版本
get_current_version() {
if [ -f "${INSTALL_DIR}/${SERVICE_NAME}" ]; then
local version=$("${INSTALL_DIR}/${SERVICE_NAME}" --version 2>/dev/null | grep -oP 'v\K[0-9.]+' || echo "未知")
echo "$version"
else
echo "未安装"
fi
}
# 获取最新版本
get_latest_version() {
local version=$(curl -s "${GITEA_REPO}/releases" | grep -oP 'releases/tag/v\K[0-9.]+' | head -1)
if [ -z "$version" ]; then
version="0.1.0"
print_warn "无法自动获取版本,使用默认版本 ${version}" >&2
fi
echo "$version"
}
# 下载二进制文件
download_binary() {
local arch=$1
local version=$2
local binary_name="${SERVICE_NAME}-${arch}"
local download_url="${GITEA_REPO}/releases/download/v${version}/${binary_name}"
print_info "下载 ${arch} 架构的二进制文件..."
print_info "下载地址: ${download_url}"
if ! curl -L -f -o "/tmp/${binary_name}" "${download_url}"; then
print_error "下载失败,请检查版本号和网络连接"
print_info "尝试的 URL: ${download_url}"
exit 1
fi
chmod +x "/tmp/${binary_name}"
print_info "下载完成"
}
# 备份当前版本
backup_current() {
local backup_file="${INSTALL_DIR}/${SERVICE_NAME}.backup"
if [ -f "${INSTALL_DIR}/${SERVICE_NAME}" ]; then
print_info "备份当前版本..."
cp "${INSTALL_DIR}/${SERVICE_NAME}" "${backup_file}"
print_info "备份保存至: ${backup_file}"
fi
}
# 恢复备份
restore_backup() {
local backup_file="${INSTALL_DIR}/${SERVICE_NAME}.backup"
if [ -f "${backup_file}" ]; then
print_warn "恢复备份版本..."
cp "${backup_file}" "${INSTALL_DIR}/${SERVICE_NAME}"
chmod +x "${INSTALL_DIR}/${SERVICE_NAME}"
print_info "已恢复到备份版本"
fi
}
# 更新服务
update_service() {
local arch=$1
local binary_name="${SERVICE_NAME}-${arch}"
print_info "更新服务..."
# 停止服务
if systemctl is-active --quiet "${SERVICE_NAME}"; then
print_info "停止服务..."
systemctl stop "${SERVICE_NAME}"
fi
# 替换二进制文件
cp "/tmp/${binary_name}" "${INSTALL_DIR}/${SERVICE_NAME}"
chmod +x "${INSTALL_DIR}/${SERVICE_NAME}"
print_info "服务更新完成"
}
# 启动服务
start_service() {
print_info "启动服务..."
systemctl start "${SERVICE_NAME}"
sleep 2
if systemctl is-active --quiet "${SERVICE_NAME}"; then
print_info "服务启动成功!"
echo ""
print_info "常用命令:"
echo " 查看状态: systemctl status ${SERVICE_NAME}"
echo " 查看日志: journalctl -u ${SERVICE_NAME} -f"
echo " 停止服务: systemctl stop ${SERVICE_NAME}"
echo " 重启服务: systemctl restart ${SERVICE_NAME}"
else
print_error "服务启动失败,正在恢复备份..."
restore_backup
systemctl start "${SERVICE_NAME}"
if systemctl is-active --quiet "${SERVICE_NAME}"; then
print_info "已恢复到备份版本"
else
print_error "恢复失败,请查看日志:"
echo " journalctl -u ${SERVICE_NAME} -n 50"
fi
exit 1
fi
}
# 主函数
main() {
echo ""
print_info "=== PT System Checking 一键更新脚本 ==="
echo ""
check_root
check_installed
# 检测架构
ARCH=$(detect_arch)
print_info "检测到系统架构: ${ARCH}"
# 获取版本信息
CURRENT_VERSION=$(get_current_version)
print_info "当前版本: ${CURRENT_VERSION}"
print_info "获取最新版本信息..."
LATEST_VERSION=$(get_latest_version)
print_info "最新版本: v${LATEST_VERSION}"
# 版本比较
if [ "$CURRENT_VERSION" == "$LATEST_VERSION" ]; then
print_info "已是最新版本,无需更新"
exit 0
fi
echo ""
print_warn "即将从 v${CURRENT_VERSION} 更新到 v${LATEST_VERSION}"
# 检查是否为非交互模式
if [ -t 0 ]; then
read -p "是否继续?(y/n): " confirm
if [[ ! $confirm =~ ^[Yy]$ ]]; then
print_warn "取消更新"
exit 0
fi
else
print_info "检测到非交互模式,自动继续更新"
fi
echo ""
# 备份当前版本
backup_current
# 下载新版本
download_binary "${ARCH}" "${LATEST_VERSION}"
# 更新服务
update_service "${ARCH}"
# 启动服务
start_service
# 清理临时文件
rm -f "/tmp/${SERVICE_NAME}-${ARCH}"
echo ""
print_info "更新完成!版本: v${LATEST_VERSION}"
}
# 运行主函数
main "$@"