Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 公共构建机支持持久化构建容器调度 #9269 #9388

Merged
merged 12 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/agent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ on:
push:
branches: [master]
paths:
- 'src/agent/**'
- 'src/agent/agent/**'
tangruotian marked this conversation as resolved.
Show resolved Hide resolved
pull_request:
branches: [master, release-*]
paths:
- 'src/agent/**'
- 'src/agent/agent/**'
tangruotian marked this conversation as resolved.
Show resolved Hide resolved
workflow_dispatch:
workflow_call:
inputs:
Expand All @@ -30,11 +30,11 @@ jobs:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('src/agent/go.sum') }}
key: ${{ runner.os }}-go-${{ hashFiles('src/agent/agent/go.sum') }}
- run: make BUILD_OUT_TAG=out clean all
working-directory: src/agent/
working-directory: src/agent/agent/
- uses: actions/upload-artifact@v3
if: ${{ inputs.upload == 'true' }}
with:
name: agent
path: src/agent/bin/
path: src/agent/agent/bin/
41 changes: 41 additions & 0 deletions src/agent/agent-slim/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
BINDIR := $(CURDIR)/bin
CMDDIR := $(CURDIR)/cmd
BuildTime := $(shell date '+%Y-%m-%d.%H:%M:%S%Z')
GitCommit := $(shell git rev-parse HEAD)
AGENT_VERSION=v1.12.0-beta.36
BUILD_FLAGS := -ldflags="-w -s -X github.com/TencentBlueKing/bk-ci/agent/src/pkg/config.BuildTime=$(BuildTime) \
-X github.com/TencentBlueKing/bk-ci/agent/src/pkg/config.GitCommit=$(GitCommit) -X github.com/TencentBlueKing/bk-ci/agent/src/pkg/config.AgentVersion=$(AGENT_VERSION)"

format:
find ./ -name "*.go" | xargs gofmt -w

test: test-unit

.PHONY: test-unit
test-unit:
@echo
@echo "==> Running unit tests <=="
GO111MODULE=on go test -tags=${BUILD_OUT_TAG} -run . ./...

# 相对于代码生成器的位置
I18N_DIR := "../../../../../support-files/i18n/agent-slim/"

i18nfilegen:
I18N_DIR=${I18N_DIR} go generate -x pkg/i18n/i18n.go

all: linux

linux: build_linux build_linux_arm64

build_linux: test clean i18nfilegen
mkdir -p $(BINDIR)
GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ${BUILD_FLAGS} -o $(BINDIR)/agentslim_linux $(CMDDIR)/slim.go
ls -la $(BINDIR)
build_linux_arm64: test clean i18nfilegen
mkdir -p $(BINDIR)
GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build ${BUILD_FLAGS} -o $(BINDIR)/agentslim_linux_arm64 $(CMDDIR)/slim.go
ls -la $(BINDIR)

clean:
mkdir -p $(BINDIR)
rm -f $(BINDIR)/*
57 changes: 57 additions & 0 deletions src/agent/agent-slim/cmd/slim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
"github.com/TencentBlueKing/bk-ci/agentcommon/logs"
"github.com/TencentBlueKing/bk-ci/agentslim/pkg"
"github.com/TencentBlueKing/bk-ci/agentslim/pkg/config"
"os"
"path/filepath"
)

func main() {
if len(os.Args) == 2 {
switch os.Args[1] {
case "version":
fmt.Println(config.AgentVersion)
os.Exit(0)
case "fullVersion":
fmt.Println(config.AgentVersion)
fmt.Println(config.GitCommit)
fmt.Println(config.BuildTime)
os.Exit(0)
}
}

// 初始化配置
if err := config.InitConfig(); err != nil {
fmt.Printf("Init config error %s\n", err.Error())
os.Exit(1)
}

// 初始化日志
var logStd bool
if config.Config.IsDebug {
logStd = true
}
if err := logs.Init(filepath.Join(config.Config.LogDir, "devopsAgent.log"), config.Config.IsDebug, logStd); err != nil {
fmt.Printf("init agent log error %s\n", err.Error())
os.Exit(1)
}

// 以agent安装目录为工作目录
if err := os.Chdir(config.Config.WorkDir); err != nil {
logs.WithError(err).Error("change work dir failed")
os.Exit(1)
}

logs.Debug("agent start")
logs.Debug("pid: ", os.Getpid())
logs.Debug("agent version: ", config.AgentVersion)
logs.Debug("git commit: ", config.GitCommit)
logs.Debug("build time: ", config.BuildTime)
logs.Debug("current user userName: ", config.Config.StartUser)
logs.Debug("work dir: ", config.Config.WorkDir)

pkg.Run()
}
19 changes: 19 additions & 0 deletions src/agent/agent-slim/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module github.com/TencentBlueKing/bk-ci/agentslim

go 1.19

require (
github.com/Netflix/go-env v0.0.0-20220526054621-78278af1949d
github.com/TencentBlueKing/bk-ci/agentcommon v0.0.0-00010101000000-000000000000
github.com/nicksnyder/go-i18n/v2 v2.2.1
github.com/pkg/errors v0.9.1
golang.org/x/text v0.12.0
)

require (
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/sys v0.5.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
)

replace github.com/TencentBlueKing/bk-ci/agentcommon => ../common
54 changes: 54 additions & 0 deletions src/agent/agent-slim/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/Netflix/go-env v0.0.0-20220526054621-78278af1949d h1:wvStE9wLpws31NiWUx+38wny1msZ/tm+eL5xmm4Y7So=
github.com/Netflix/go-env v0.0.0-20220526054621-78278af1949d/go.mod h1:9XMFaCeRyW7fC9XJOWQ+NdAv8VLG7ys7l3x4ozEGLUQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/nicksnyder/go-i18n/v2 v2.2.1 h1:aOzRCdwsJuoExfZhoiXHy4bjruwCMdt5otbYojM/PaA=
github.com/nicksnyder/go-i18n/v2 v2.2.1/go.mod h1:fF2++lPHlo+/kPaj3nB0uxtPwzlPm+BlgwGX7MkeGj0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
68 changes: 68 additions & 0 deletions src/agent/agent-slim/pkg/api/ErrorEnum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package api

import "github.com/TencentBlueKing/bk-ci/agentslim/pkg/i18n"

type ErrorCode int

const (
BuildProcessRunError ErrorCode = 2128040 + iota
RecoverRunFileError
LoseRunFileError
MakeTmpDirError
BuildProcessStartError
PrepareScriptCreateError
)

type ErrorTypes string

const (
System ErrorTypes = "SYSTEM"
User ErrorTypes = "USER"
ThirdParty ErrorTypes = "THIRD_PARTY"
Plugin ErrorTypes = "PLUGIN"
)

type ErrorEnum struct {
Type ErrorTypes
Code ErrorCode
// 方便国际化
MessageId string
}

var (
NoErrorEnum = &ErrorEnum{Type: "", Code: 0, MessageId: ""}
BuildProcessRunErrorEnum = &ErrorEnum{Type: User, Code: BuildProcessRunError, MessageId: "EC_BuildProcessRunError"}
LoseRunFileErrorEnum = &ErrorEnum{Type: User, Code: LoseRunFileError, MessageId: "EC_LoseRunFileError"}
MakeTmpDirErrorEnum = &ErrorEnum{Type: User, Code: MakeTmpDirError, MessageId: "EC_MakeTmpDirError"}
BuildProcessStartErrorEnum = &ErrorEnum{Type: User, Code: BuildProcessStartError, MessageId: "EC_BuildProcessStartError"}
PrepareScriptCreateErrorEnum = &ErrorEnum{Type: User, Code: PrepareScriptCreateError, MessageId: "EC_PrepareScriptCreateError"}
)

func (t *PersistenceBuildInfo) ToFinish(
success bool,
message string,
errorEnum *ErrorEnum,
) *PersistenceBuildWithStatus {
if success || errorEnum == NoErrorEnum {
return &PersistenceBuildWithStatus{
PersistenceBuildInfo: *t,
Success: success,
Message: message,
Error: nil,
}
}
errMsg := ""
if errorEnum.MessageId != "" {
errMsg = i18n.Localize(errorEnum.MessageId, nil)
}
return &PersistenceBuildWithStatus{
PersistenceBuildInfo: *t,
Success: success,
Message: message,
Error: &Error{
ErrorType: errorEnum.Type,
ErrorMessage: errMsg,
ErrorCode: errorEnum.Code,
},
}
}
Loading