safesight-control/scripts/deploy/build-windows.ps1
tian 46f264d4d4 refactor: rebrand to SafeSight Control
- Rename binary: managerd → safesightd
- Rename config: managerd.json → safesightd.json
- Update Go module: 3588AdminBackend → safesight-control
- Update all import paths and references
- Rename scripts and deploy files
2026-07-17 11:00:12 +08:00

218 lines
7.1 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# SafeSight Control Windows 构建脚本
# 在 Windows 上交叉编译 Linux 版本并打包
# 配置
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$ProjectDir = Split-Path -Parent (Split-Path -Parent $ScriptDir)
$BuildDir = "$ProjectDir\build"
$PackageName = "SafeSight Control-离线部署包-$(Get-Date -Format 'yyyyMMdd')"
$PackageDir = "$BuildDir\$PackageName"
Write-Host "========== SafeSight Control Windows 构建 ==========" -ForegroundColor Green
Write-Host "项目目录: $ProjectDir"
Write-Host "构建目录: $BuildDir"
Write-Host ""
# 检查 Go
$GoVersion = go version 2>$null
if ($LASTEXITCODE -ne 0) {
Write-Host "错误: 未找到 Go 命令,请先安装 Go" -ForegroundColor Red
exit 1
}
Write-Host "Go 版本: $GoVersion"
Write-Host ""
# 清理旧构建
Write-Host "[1/6] 清理旧构建..."
if (Test-Path $BuildDir) {
Remove-Item -Recurse -Force $BuildDir
}
New-Item -ItemType Directory -Path $PackageDir -Force | Out-Null
# 创建目录结构
Write-Host "[2/6] 创建目录结构..."
New-Item -ItemType Directory -Path "$PackageDir\bin" -Force | Out-Null
New-Item -ItemType Directory -Path "$PackageDir\config" -Force | Out-Null
New-Item -ItemType Directory -Path "$PackageDir\scripts" -Force | Out-Null
New-Item -ItemType Directory -Path "$PackageDir\docs" -Force | Out-Null
New-Item -ItemType Directory -Path "$PackageDir\systemd" -Force | Out-Null
# 编译项目
Write-Host "[3/6] 交叉编译 Linux AMD64 版本..."
cd $ProjectDir
$env:GOOS = "linux"
$env:GOARCH = "amd64"
$env:GOTOOLCHAIN = "local"
go build -ldflags="-s -w" -o "$PackageDir\bin\safesightd" ./cmd/safesightd
if ($LASTEXITCODE -ne 0 -or -not (Test-Path "$PackageDir\bin\safesightd")) {
Write-Host "错误: 编译失败" -ForegroundColor Red
exit 1
}
Write-Host " 编译成功" -ForegroundColor Green
# 复制配置文件
Write-Host "[4/6] 复制配置文件..."
if (Test-Path "$ProjectDir\safesightd.json") {
Copy-Item "$ProjectDir\safesightd.json" "$PackageDir\config\safesightd.json.example"
} else {
@"
{
"listen": "0.0.0.0:18080",
"discovery_port": 35688,
"discovery_timeout_ms": 1200,
"offline_after_ms": 10000,
"agent_token": "CHANGE_ME",
"concurrency": 5
}
"@ | Out-File -FilePath "$PackageDir\config\safesightd.json.example" -Encoding UTF8
}
# 复制模板文件(如果有)
if (Test-Path "$ProjectDir\templates") {
Write-Host " 复制模板文件..."
Copy-Item -Recurse "$ProjectDir\templates" "$PackageDir\"
}
# 复制脚本
Write-Host "[5/6] 复制部署脚本..."
Copy-Item "$ScriptDir\safesightd" "$PackageDir\scripts\"
Copy-Item "$ScriptDir\safesightd.service" "$PackageDir\systemd\"
# 修复换行符为 LFUnix 格式)
$scriptContent = Get-Content "$PackageDir\scripts\safesightd" -Raw
$scriptContent = $scriptContent -replace "`r`n", "`n"
Set-Content -Path "$PackageDir\scripts\safesightd" -Value $scriptContent -NoNewLine
$serviceContent = Get-Content "$PackageDir\systemd\safesightd.service" -Raw
$serviceContent = $serviceContent -replace "`r`n", "`n"
Set-Content -Path "$PackageDir\systemd\safesightd.service" -Value $serviceContent -NoNewLine
# 复制文档
Write-Host "[6/6] 复制文档..."
if (Test-Path "$ProjectDir\docs") {
Get-ChildItem "$ProjectDir\docs\*.md" -ErrorAction SilentlyContinue | ForEach-Object {
Copy-Item $_.FullName "$PackageDir\docs\"
}
}
if (Test-Path "$ProjectDir\Readme.md") {
Copy-Item "$ProjectDir\Readme.md" "$PackageDir\docs\"
}
if (Test-Path "$ProjectDir\API_Device_RemoteMgmt_InterfaceTable.md") {
Copy-Item "$ProjectDir\API_Device_RemoteMgmt_InterfaceTable.md" "$PackageDir\docs\"
}
# 创建版本信息
@"
SafeSight Control 线
: 1.0.0
: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')
Go: $GoVersion
: Ubuntu 22.04.5 LTS x86_64
: $env:COMPUTERNAME (Windows)
"@ | Out-File -FilePath "$PackageDir\VERSION" -Encoding UTF8
# 创建快速安装说明
@"
SafeSight Control 线 -
- Ubuntu 22.04.5 LTS (x86_64)
- root
Go
线
Windows
1. Ubuntu
scp -r $PackageName.tar.gz user@ubuntu-server:/tmp/
2.
tar -xzf $PackageName.tar.gz
cd $PackageName/
3.
sudo ./scripts/safesightd install
4.
sudo nano /opt/safesightd/config/safesightd.json
:
- agent_token:
- listen: 访 "0.0.0.0:18080"
5.
sudo safesightd start
6.
sudo safesightd status
safesightd start -
safesightd stop -
safesightd status -
safesightd restart -
safesightd upgrade -
safesightd uninstall -
safesightd check -
safesightd-start -
safesightd-stop -
safesightd-status -
: /opt/safesightd
: /opt/safesightd/config/safesightd.json
: /opt/safesightd/logs/safesightd.log
1. : tail -f /opt/safesightd/logs/safesightd.log
2. : python3 -m json.tool /opt/safesightd/config/safesightd.json
3. : sudo netstat -tlnp | grep safesightd
sudo ufw allow 18080/tcp # HTTP API
sudo ufw allow 35688/udp # UDP
"@ | Out-File -FilePath "$PackageDir\README-离线安装.txt" -Encoding UTF8
# 打包(使用 tarWindows 10/11 内置)
Write-Host ""
Write-Host "打包..."
cd $BuildDir
# 创建 tar.gz 包(使用正斜杠避免 Windows 路径问题)
$PackageNameUnix = $PackageName -replace '\\', '/'
tar -czf "$PackageName.tar.gz" "$PackageNameUnix"
if ($LASTEXITCODE -eq 0 -and (Test-Path "$PackageName.tar.gz")) {
Write-Host ""
Write-Host "========== 构建完成 ==========" -ForegroundColor Green
Write-Host ""
Write-Host "输出文件:"
Get-ChildItem "$PackageName.tar.gz" | ForEach-Object {
$size = [math]::Round($_.Length / 1MB, 2)
Write-Host " $($_.Name) ($size MB)"
}
Write-Host ""
Write-Host "部署包位置: $BuildDir\$PackageName.tar.gz" -ForegroundColor Yellow
Write-Host ""
Write-Host "使用说明:"
Write-Host "1. 将 $PackageName.tar.gz 上传到 Ubuntu 服务器"
Write-Host "2. 解压并运行: sudo ./scripts/safesightd install"
Write-Host ""
} else {
Write-Host "错误: 打包失败" -ForegroundColor Red
exit 1
}