Skip to content

Commit

Permalink
Merge pull request #10547 from tangruotian/issue_10234
Browse files Browse the repository at this point in the history
Agent清理进程为worker兜底 #10234
  • Loading branch information
bkci-bot authored Jun 27, 2024
2 parents 3ef974b + 3a745cd commit b315b7b
Show file tree
Hide file tree
Showing 82 changed files with 1,978 additions and 761 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func main() {
}
}

// 生成器保存分析的状态。 主要用来缓冲 format.Source 的输出。
// Generator 生成器保存分析的状态。 主要用来缓冲 format.Source 的输出。
type Generator struct {
buf bytes.Buffer // 累计输出
}
Expand Down
8 changes: 4 additions & 4 deletions src/agent/agent-slim/pkg/constant/constant.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package constant

const (
// 构建机默认国际化语言
DEFAULT_LANGUAGE_TYPE = "zh_CN"
// 构建机接取任务间隔时间
// DefaultLanguageType 构建机默认国际化语言
DefaultLanguageType = "zh_CN"
// BuildIntervalInSeconds 构建机接取任务间隔时间
BuildIntervalInSeconds = 5
// api 鉴权的头信息
// AuthHeaderBuildType api 鉴权的头信息
AuthHeaderBuildType = "X-DEVOPS-BUILD-TYPE" // 构建类型
AuthHeaderProjectId = "X-DEVOPS-PROJECT-ID" // 项目ID
AuthHeaderAgentId = "X-DEVOPS-AGENT-ID" // Agent ID
Expand Down
2 changes: 1 addition & 1 deletion src/agent/agent-slim/pkg/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (

var localizer *localizerType

var defaultLocalTag = language.Make(constant.DEFAULT_LANGUAGE_TYPE)
var defaultLocalTag = language.Make(constant.DefaultLanguageType)

type localizerType struct {
nowLocalizer language.Tag
Expand Down
2 changes: 1 addition & 1 deletion src/agent/agent-slim/pkg/job/build_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var GBuildManager *buildManager
func init() {
GBuildManager = &buildManager{
lock: &sync.RWMutex{},
instances: make(map[int]*buildData, 0),
instances: make(map[int]*buildData),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/agent/agent/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/
63 changes: 51 additions & 12 deletions src/agent/agent/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Agent(Golang)

构建Agent是指包含了agent进程监控和调度部分逻辑的代码,不包含与流水线交互的构建类业务逻辑代码,需要与另外一个worker(kotlin) 一起整合才能是完整的Agent包。
构建Agent是指包含了agent进程监控和调度部分逻辑的代码,不包含与流水线交互的构建类业务逻辑代码,需要与另外一个worker(kotlin)
一起整合才能是完整的Agent包。

## Agent二进制程序编译

Expand All @@ -14,7 +15,8 @@ windows编译,执行脚本 `build_windows.bat`

执行以上编译脚本或命令后,会在 `bin` 目录下生成对应的可执行文件。

比如执行 `make clean build_linux` 命令会在 `bin` 目录下生成 `devopsDaemon_linux``devopsAgent_linux``upgrader_linux` 文件,其他系统依此类推。
比如执行 `make clean build_linux` 命令会在 `bin` 目录下生成 `devopsDaemon_linux``devopsAgent_linux``upgrader_linux`
文件,其他系统依此类推。

- devopsDaemon: 用于守护agent进程,监控和拉起agent进程
- devopsAgent: 用于和调度服务通信,以及拉起构建进程worker
Expand All @@ -29,32 +31,49 @@ windows编译,执行脚本 `build_windows.bat`
- scripts/linux/stop.sh: agent停止脚本
- scripts/linux/uninstall.sh: agent卸载脚本


## 架构设计说明

### Agent 模块说明

agent模块为GoAgent的核心模块。主要负责**执行worker以及做相关辅助工作**

#### 模块主流程说明

![image](https://github.com/Tencent/bk-ci/blob/master/docs/resource/img/Agent%E6%A8%A1%E5%9D%97%E6%B5%81%E7%A8%8B%E5%9B%BE.png)

#### 步骤说明

针对流程图步骤补充说明,序号为流程图步骤序号。

##### 1 检查进程。
主要通过 **CheckProcess** 函数,函数主要通过输入的模块名称 例如:agent,获取runtime目录下的 name.pid 和 name.lock 文件。通过是否可以拿到 name.lock 文件锁的锁,来判断当前进程是否正在运行,如若没有在运行,则获取全局锁 total-lock.lock 来修改 name.pid 文件写入当前agent进程ID,同时一直持有 name.lock 锁直到进程退出时自动释放。

主要通过 **CheckProcess** 函数,函数主要通过输入的模块名称 例如:agent,获取runtime目录下的 name.pid 和 name.lock
文件。通过是否可以拿到 name.lock 文件锁的锁,来判断当前进程是否正在运行,如若没有在运行,则获取全局锁 total-lock.lock 来修改
name.pid 文件写入当前agent进程ID,同时一直持有 name.lock 锁直到进程退出时自动释放。

##### 2 初始化配置。
1.**.agent.properties** 文件中读取agent配置信息到 **GAgentConfig** 对象中。在读取的同时会写入一次,将 GAgentConfig 对象初始化的一些值持久化到 .agent.properties中。

1.**.agent.properties** 文件中读取agent配置信息到 **GAgentConfig** 对象中。在读取的同时会写入一次,将 GAgentConfig
对象初始化的一些值持久化到 .agent.properties中。
2. 初始化证书。 如果根目录中存在 **.cert** 文件,则将其添加至系统证书池,并添加至agent发送https请求的 **TLS** 配置中
3. 初始化Agent环境变量。读取系统一些agent可能用到的环境变量到agent **GAgentEnv** 中,方便后续使用,例如 HostName,osName等。注:windows系统在这一步中因为可能持有虚拟网卡的IP,所以在上报agentIP地址时会 **忽略被配置了忽略虚拟网卡的windows机器IP**
3. 初始化Agent环境变量。读取系统一些agent可能用到的环境变量到agent **GAgentEnv** 中,方便后续使用,例如
HostName,osName等。注:windows系统在这一步中因为可能持有虚拟网卡的IP,所以在上报agentIP地址时会 *
*忽略被配置了忽略虚拟网卡的windows机器IP**

##### 3 上报后台Agent启动。

**POST|env服务|agent/thirdPartyAgent/startup** 并解析返回结果,如果成功返回则执行后续步骤否则退出。

##### A 数据采集。

通过配置替换代码中写好的采集配置,启动 **telegraf** agent,并将数据上报到后台的influxDB。

##### B 心跳上报。

无限循环,10s一次。通过 **POST|env服务|agent/thirdPartyAgent/agents/newHeartbeat** 发送agent环境的一些配置到后台,并解析后台结果,修改部分agent配置。

##### C 检查升级。无限循环,20s一次。

1. 获取当前jdk版本信息
2. 通过 **POST|dispatch环境|agent/thirdPartyAgent/upgradeNew** 上报agent,worker,jdk版本信息判断是否需要升级。不需要升级则直接返回。
3. 下载最新版本应用对比md5判断是否需要升级。不需要升级则直接返回。
Expand All @@ -64,42 +83,62 @@ agent模块为GoAgent的核心模块。主要负责**执行worker以及做相关
7. 升级agent。执行 **upgrader** 模块。

##### D pipeline。无限循环,30s一次。

1. 通过 **GET|env服务|agent/thirdPartyAgent/agents/pipelines** 获取需要执行的pipeline任务。
2. 通过 **PUT|env服务|agent/thirdPartyAgent/agents/pipelines** 更新任务状态。
3. 将任务脚本写入 工作空间下 **devops_pipeline_SeqId_type.sh** 文件。执行后获取输出内容,并通过更新任务状态上报后台,同时删除脚本文件。

##### E 定期清理。

无限循环,2小时一次。删除超过配置中 **LogsKeepHours**的垃圾文件(hs_err_pid前缀文件)和日志文件。

##### 4 从后台拉取构建任务。无限循环,5s一次。

1. 调用 **GET|env服务|agent/thirdPartyAgent/status** 获取agent状态,如果状态返回不正常则跳过这次任务。
2. 判断当前运行的instance是否超过配置 **ParallelTaskCount** 最大任务数,如果超过则跳过循环。
3. 获取**构建锁**,防止与其他任务差生冲突。
4. 通过 **GET|dispatch服务|agent/thirdPartyAgent/startup** 获取构建任务,如果任务为空则跳过这次。否则则添加**预处理任务****GBuildManager** 对象。注:unix构建机受**非登录用户启动进程无法拿到 ~/.bashrc 中的环境变量**的影响,需要创建 prestart和start脚本,通过设置 **exec -l** 来通过当前用户启动。
4. 通过 **GET|dispatch服务|agent/thirdPartyAgent/startup** 获取构建任务,如果任务为空则跳过这次。否则则添加**预处理任务**
**GBuildManager** 对象。注:unix构建机受**非登录用户启动进程无法拿到 ~/.bashrc 中的环境变量**的影响,需要创建
prestart和start脚本,通过设置 **exec -l** 来通过当前用户启动。

##### 5 启动构建。
1. 获取工作空间下的 **worker.jar** 文件。如果没有则尝试通过获取工作空间下的 **tmp** 目录下中同名文件进行复制使用来尝试自愈,如果 tmp 目录中的worker版本不对或者不存在文件,则进行 **结束构建**逻辑。
2. 在工作空间下创建 **build_tmp** 目录,并在目录下启动 worker进程执行构建任务。任务启动后删除**预处理任务**同时添加**任务实例****GBuildManager** 对象。

1. 获取工作空间下的 **worker.jar** 文件。如果没有则尝试通过获取工作空间下的 **tmp** 目录下中同名文件进行复制使用来尝试自愈,如果
tmp 目录中的worker版本不对或者不存在文件,则进行 **结束构建**逻辑。
2. 在工作空间下创建 **build_tmp** 目录,并在目录下启动 worker进程执行构建任务。任务启动后删除**预处理任务**同时添加**任务实例
****GBuildManager** 对象。

##### F 等待进程结束

通过 **process.Wait()** 来等待进程结束,删除**GBuildManager** 对象中的任务实例,并执行 **结束构建**逻辑。

##### G 清理构建空间,即结束构建逻辑。

- 清理构建过程中产生的文件脚本,与 build_tmp目录下超过7天的文件。
- 通过**POST|dispathc服务|agent/thirdPartyAgent/workerBuildFinish** 上报后台任务结束。



### Daemon 模块说明

daemon模块是agent模块的守护进程,主要功能是维护agent进程一直运行。
daemon模块在windows系统和unix系统中有不同的实现

#### windows实现

win实现主要是靠 **github.com/kardianos/service** 库调用的windows service功能实现,通过实现库中service接口维护agent一直运行。

#### unix实现
unix中的实现主要通过使用**定时器,5s一次**检查Agent模块进程是否存在,检测方式通过获取agent的文件锁,进程推出后文件锁会自动释放,所以如果无法获取文件锁,说明进程正常运行,如果可以获取文件锁,则说明agent进程退出了,这时Daemon会将其拉起。

unix中的实现主要通过使用**定时器,5s一次**
检查Agent模块进程是否存在,检测方式通过获取agent的文件锁,进程推出后文件锁会自动释放,所以如果无法获取文件锁,说明进程正常运行,如果可以获取文件锁,则说明agent进程退出了,这时Daemon会将其拉起。

### Install & Upgrade 模块说明

安装和升级模块,主要负责daemon模块和agent模块的升级。

#### install模块

安装模块功能较为简单,主要通过调用安装脚本,安装agent。

#### upgrade模块

升级模块和agent模块中的升级Agent的逻辑一致,通过杀掉Agent进程后,替换Agent文件并启动来完成升级。
56 changes: 32 additions & 24 deletions src/agent/agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/TencentBlueKing/bk-ci/agentcommon v0.0.0-00010101000000-000000000000
github.com/docker/docker v23.0.3+incompatible
github.com/docker/docker v24.0.9+incompatible
github.com/gofrs/flock v0.8.1
// 1.24 以上的版本引入了memcall和memguard会导致
// 1、ulimit corefile被设置为0 https://github.com/ci-plugins/memguard/blob/master/core/init.go
Expand All @@ -14,10 +14,10 @@ require (
github.com/nicksnyder/go-i18n/v2 v2.2.1
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.3
golang.org/x/text v0.7.0
golang.org/x/text v0.14.0
gopkg.in/ini.v1 v1.67.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gotest.tools/v3 v3.0.3
gotest.tools/v3 v3.5.0
)

require (
Expand All @@ -32,9 +32,9 @@ require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/gosnmp/gosnmp v1.35.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/influxdata/toml v0.0.0-20190415235208-270119a8ce65 // indirect
Expand All @@ -46,7 +46,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/naoina/go-stringutil v0.1.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect
github.com/philhofer/fwd v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20220216144756-c35f1ee13d7c // indirect
Expand All @@ -57,29 +57,35 @@ require (
// 1.24 telegraf 必须跟随 3.22.9 版本,之上的版本windows编译不通过
github.com/shirou/gopsutil/v3 v3.22.9 // indirect
github.com/sleepinggenius2/gosmi v0.4.4 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/tinylib/msgp v1.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.6.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/fsouza/go-dockerclient v1.9.7
github.com/gorilla/websocket v1.5.0
golang.org/x/sync v0.1.0
golang.org/x/sync v0.3.0
)

require (
cloud.google.com/go/compute v1.10.0 // indirect
cloud.google.com/go v0.110.4 // indirect
cloud.google.com/go/bigquery v1.52.0 // indirect
cloud.google.com/go/compute v1.21.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.1 // indirect
cloud.google.com/go/monitoring v1.15.1 // indirect
cloud.google.com/go/pubsub v1.32.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/aws/aws-sdk-go-v2 v1.17.1 // indirect
github.com/aws/aws-sdk-go-v2/config v1.17.8 // indirect
Expand All @@ -94,10 +100,13 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.17.4 // indirect
github.com/aws/smithy-go v1.13.4 // indirect
github.com/blues/jsonata-go v1.5.4 // indirect
github.com/containerd/containerd v1.6.18 // indirect
github.com/containerd/containerd v1.6.26 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/moby/patternmatcher v0.5.0 // indirect
Expand All @@ -106,20 +115,19 @@ require (
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/runc v1.1.5 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/oauth2 v0.3.0 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/api v0.100.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/tools v0.9.1 // indirect
google.golang.org/api v0.126.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221014213838-99cd37c6964a // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.3 // indirect
)

require (
github.com/docker/cli v23.0.1+incompatible
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/go-connections v0.4.0
github.com/spf13/pflag v1.0.5
)
Expand Down
Loading

0 comments on commit b315b7b

Please sign in to comment.