记录一次写工具的(流水账)全过程,从一个脚本长到项目分离前后端再到部署到主机,处处掉坑处处倒霉。记录下来每次看一看这样就不会轻易犯相同毛病了。
探针Agent端开发
编写探针用户端
搭建python及其虚拟环境
debian13源python3增量为开发环境。参考这一片贴文。
编写探针Agent端测试代码。使用Uvicorn转换http请求为字典,配合FastAPI抓取本机数据转换为json返回,并且生成swagger文档。
分离core功能和数据请求模块。考虑pull模式被动Agent,因此代码逻辑添加token鉴权。
编写token配置
生成token并且写入
.env## 在项目根目录 edge_monitor/ 下执行 echo "AGENT_TOKEN=$(openssl rand -hex 16)" > .env ## 确认文件内容 cat .env ## 输出示例: AGENT_TOKEN=8f4b2e1c9d3a5f6e...检查
.dockerignore, 屏蔽面板服务端内容例如dash*, 确认不包含.env在
config.yaml中包含生成的token.
docker注册
- 网页注册dockerHUB
docker login开发机器登陆
docker部署
构建docker容器上传Hub
## 1. 构建 (Build)
## -t: 给镜像打标签
## --platform linux/amd64: 确保兼容云服务器
sudo docker build --platform linux/amd64 -t pilliaredrw/edge-monitor:v1 .
## 2. 上传 (Push)
## 把镜像推送到 Docker Hub 仓库
sudo docker push pilliaredrw/edge-monitor:v1在服务器安装docker并且拉项目下来
- 参考Docker文档, 使用官方换源的方式安装docker.
- 复制
docker-compose.yml,.env进入服务器。 - 运行
docker compose up -d拉取。 - 运行
docker ps检查状态。如果是restarting..就检查docker log <容器ID>- 当需要删除正在运行的容器,停止容器:
docker stop <容器ID>,删除容器docker rm <容器ID>。 - 当更改了云端容器之后,重新拉取容器到本地:
docker compose pull,然后docker compose up -d。
- 当需要删除正在运行的容器,停止容器:
- 测试运行状态
curl -H "Authorization: Bearer <手动复制TOKEN>" http://127.0.0.1:8000/system/status
探针汇聚端开发
编写测试服务端cil
编写服务端逻辑
拆分目录结构
这个项目写的很仓促,也没有git管理。现在它是这样的:
marisad@marisadw:~/Desktop/edge_monitor$ ls -a
. dashcil.py dockerfile .env __pycache__
.. dash.py .dockerignore monitor_core.py README
config.yaml docker-compose.yml edge_monitor monitor.py requirements.txt所以现在需要重构目录。纠结的点只有requirements.txt。最后选择了直接无脑拷贝(其实可以重新生成)。重构后如下:
marisad@marisadw:~/Desktop/edge_monitor$ ls
agent dash edge_monitor README
marisad@marisadw:~/Desktop/edge_monitor$ ls agent/
docker-compose.yml dockerfile monitor_core.py monitor.py __pycache__ requirements.txt
marisad@marisadw:~/Desktop/edge_monitor$ ls dash/
config.yaml dashcil.py dash.py requirements.txt
编写前端
前端的想法是使用vue编写,顺便加个TwindCSS. 所以结构应该是nodejs-vue/TwindCSS.
跨域问题
跨域访问,也就是CORS. 在开发环境中,前端例如http://localhost:5173需要去访问后端接口例如http://localhost:5173。此时开发可以直接设置allow_origins=[”*“]在后端方便浏览器无脑访问后端(此时FASTapi通过在Header里面添加Access-Control-Allow-Origin: *的方式来允许。此时浏览器会读出Body内容显示)。而部署之后,需要通过nginx来将后端封装为接口,通过访问自己的域名来解决跨域问题。
Nodejs问题
apt源的nodejs往往会拆分模块包例如node-twindcss. 所以要卸载原本的包,装上nvm来管理nodejs.
- 卸载原本nodejs.
sudo apt remove nodejs npm - 安装nvm, 选择使用需要的版本。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.6/install.sh | bash
source ~/.bashrc
nvm install 20
nvm use 20- 项目安装依赖。
rm -rf node_modules package-lock.json
npm install
npm install -D tailwindcss@3 postcss autoprefixer
npx tailwindcss init -p使用Github Action部署到服务器
再次重构项目结构,作自动推送的准备
- 修改项目格式,将dash面板前后端分离。 修改后的项目结构差不多是这样
- 更新config.yaml为占位符token,方便后续直接通过git push的方式更新结点列表。
服务器设置SSH-KEY登陆
- ssh-keygen -t rsa -b 4096
- cat /root/.ssh/id_rsa获取私钥
Github Action准备
- 创建Github项目,push上去。
- 编写
.github/workflows/deploy.yml.当github检测到这个项目,就会自动去尝试部署。
name: Edge Monitor Deploy
on:
push:
branches: [ "main" ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# 1. 编译前端 (在 GitHub 虚拟机里跑)
- name: Build Frontend
run: |
cd dash/frontend
npm install
npm run build
# 2. 将编译好的静态文件 (dist) 传送到阿里云
- name: Deploy Frontend to Nginx
uses: appleboy/scp-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
source: "dash/frontend/dist/*"
target: "/var/www/edge_monitor"
strip_components: 3 # 关键:去掉 dash/frontend/dist/ 这三层路径
# 3. 将后端源码传送到阿里云
- name: Deploy Backend to Server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
source: "dash/backend/*"
target: "/opt/edge_manager"
strip_components: 2 # 关键:去掉 dash/backend/ 这两层路径
# 4. 远程执行 Docker 构建与重启
- name: SSH Remote Command
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /opt/edge_manager
# 构建后端镜像
docker build -t edge-aggregator .
# 杀掉并移除旧容器
docker stop edge-aggregator-run || true
docker rm edge-aggregator-run || true
# 启动新容器,注入 Secret 中的 Token,并挂载宿主机的配置文件
docker run -d --name edge-aggregator-run \
-p 9000:9000 \
-e AGENT_TOKEN="${{ secrets.AGENT_TOKEN }}" \
-v /opt/edge_manager/config.yaml:/app/config.yaml \
edge-aggregator
- 设置github项目级别secret。充当2步骤中deploy.yml的环境变量。在这里我设置了四个,分别是登录用户,登陆主机IP, SSH私钥,agent_token.
设置杂项
- CF设置A记录为目标主机IP.
- 服务器设置nginx转发请求到前端。
server {
listen 80;
server_name <DOMAIN>;
## 前端静态文件
location / {
root /var/www/<前端文件夹>;
index index.html;
try_files $uri $uri/ /index.html;
}
## 后端 API
location /api/ {
proxy_pass http://127.0.0.1:<后端端口>;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
(附)内网穿透
官网注册Tailscale。
在服务器安装Tailscale服务
curl -fsSL https://tailscale.com/install.sh | sh安装之后,直接按照终端内提示登陆。确保开发机在浏览器已经登陆Tailscale。会在终端出现登陆链接。
设置开机启动并且检查
systemctl enable --now tailscaled systemctl status tailscaled