2026-03-08 18:05:17 +08:00
|
|
|
|
# DevOps Agent
|
|
|
|
|
|
|
|
|
|
|
|
1. WebSocket Agent (Jenkins Node), 把自己注册到 Api Server 作为一个任务运行阶段
|
|
|
|
|
|
2. 需要执行来自于 Api Server 下发的任务, 执行中需要把日志和执行结果返回给 Api Server
|
|
|
|
|
|
3. 怎么运行任务喃,我们是封装一个 script的模块, 然后调用这个模块来运行任务
|
|
|
|
|
|
4. 任务需要有任务名称, 与 任务参数 这些基础信息
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-03-15 16:24:01 +08:00
|
|
|
|
## 开发脚本执行引擎
|
|
|
|
|
|
|
|
|
|
|
|
1. 构造单元测试的环境(ioc), 被测试的对象在ioc里面, 名字叫: script_excutor, 运行单元测试的时候,需要ioc容器启动,并且完成初始化
|
|
|
|
|
|
```go
|
|
|
|
|
|
package test
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/infraboard/mcube/v2/ioc"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 用于设置测试环境的函数,比如初始化日志系统,配置环境变量等
|
|
|
|
|
|
func Setup() {
|
|
|
|
|
|
// 配置文件的逻辑
|
|
|
|
|
|
// 和我们本地运行时的保存一致, etc/application.toml, 这是一个相对路径(工程名称)
|
|
|
|
|
|
// 点击单元测试的时候, 单元测试的运行目录是变化的,因此这里需要给绝对逻辑
|
|
|
|
|
|
ioc.DevelopmentSetupWithPaths(os.Getenv("workspaceFolder") + "/agent/etc/application.toml")
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 注册脚本执行引擎
|
|
|
|
|
|
|
|
|
|
|
|
1. 注册
|
|
|
|
|
|
```go
|
|
|
|
|
|
// 把ScriptExcutor 托管给IOC管理
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
ioc.Controller().Registry(&ScriptExcutor{
|
|
|
|
|
|
WorkDirPrefix: "workspace",
|
|
|
|
|
|
ScriptDirPrefix: "scripts",
|
|
|
|
|
|
IsVerifyScriptIntegrity: false,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
2. 初始化
|
|
|
|
|
|
```go
|
|
|
|
|
|
func (e *ScriptExcutor) Init() error {
|
|
|
|
|
|
// 初始化日志记录器
|
|
|
|
|
|
e.log = log.Sub(e.Name())
|
|
|
|
|
|
// 初始化脚本完整性管理器
|
|
|
|
|
|
e.integrityManager = NewScriptIntegrityManager(e.ScriptDirPrefix, e.IsVerifyScriptIntegrity)
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (e *ScriptExcutor) Name() string {
|
|
|
|
|
|
return APP_NAME
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 服务启动
|
|
|
|
|
|
|
|
|
|
|
|
1. 服务配置
|
|
|
|
|
|
```toml
|
|
|
|
|
|
[app]
|
|
|
|
|
|
name = "devops_agent"
|
|
|
|
|
|
|
|
|
|
|
|
[http]
|
|
|
|
|
|
port = 8848
|
|
|
|
|
|
|
|
|
|
|
|
[script_excutor]
|
|
|
|
|
|
work_dir_prefix = "workspace"
|
|
|
|
|
|
script_dir_prefix = "scripts"
|
|
|
|
|
|
is_verify_script_integrity = false
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
2. 服务启动
|
|
|
|
|
|
```go
|
|
|
|
|
|
import (
|
|
|
|
|
|
"github.com/infraboard/mcube/v2/ioc/server/cmd"
|
|
|
|
|
|
|
|
|
|
|
|
// 工程引用到的一些通用工具(API)
|
|
|
|
|
|
// 这些接口 不是用来做功能实现, 提供健康检查,性能,等额外信息
|
|
|
|
|
|
// 健康检查
|
|
|
|
|
|
_ "github.com/infraboard/mcube/v2/ioc/apps/health/restful"
|
|
|
|
|
|
|
|
|
|
|
|
// 非业务模块
|
|
|
|
|
|
_ "github.com/infraboard/mcube/v2/ioc/apps/metric/restful"
|
|
|
|
|
|
|
|
|
|
|
|
// 开启API Doc
|
|
|
|
|
|
_ "github.com/infraboard/mcube/v2/ioc/apps/apidoc/restful"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
// 启动服务(ioc)
|
|
|
|
|
|
cmd.Start()
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|