[ Agent ] [ Initialization ] - add harbor func
This commit is contained in:
@@ -969,7 +969,7 @@ func (op *AgentOsOperator) installHarborExec() (bool, []string) {
|
||||
// check docker-compose
|
||||
if !BasicFileExistAndNotNull("/usr/local/bin/docker-compose") {
|
||||
return false, []string{
|
||||
"docker-compose uninstalled ! can't install harbor!",
|
||||
"[install harbor] - docker-compose uninstalled ! can't install harbor!",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -987,7 +987,7 @@ func (op *AgentOsOperator) installHarborExec() (bool, []string) {
|
||||
})
|
||||
if ok {
|
||||
alreadyInstalledLog := []string{
|
||||
"install harbor, container harbor-core already running! harbor installed !",
|
||||
"[install harbor] - container harbor-core already running! harbor installed !",
|
||||
}
|
||||
|
||||
log.Info(alreadyInstalledLog[0])
|
||||
@@ -1091,20 +1091,29 @@ func (op *AgentOsOperator) installHarborExec() (bool, []string) {
|
||||
"-i",
|
||||
"s/$HarborAdminPas/V2ryStr@ngPss/g",
|
||||
"/root/wdd/harbor/harbor.yml",
|
||||
}})
|
||||
},
|
||||
{
|
||||
"chmod",
|
||||
"+x",
|
||||
"/root/wdd/harbor/install.sh",
|
||||
},
|
||||
})
|
||||
|
||||
log.InfoF("harbor config changed success to => %s!", op.AgentServerInfo.ServerIPInV4)
|
||||
log.InfoF("[install harbor] - harbor config changed success to => %s!", op.AgentServerInfo.ServerIPInV4)
|
||||
|
||||
// install
|
||||
executor, l := AllCommandExecutor([]string{
|
||||
log.Info("[install harbor] - going to start harbor !")
|
||||
ReadTimeCommandExecutor([]string{
|
||||
"bash",
|
||||
"/root/wdd/harbor/install.sh",
|
||||
"--with-chartmuseum",
|
||||
})
|
||||
if !executor {
|
||||
return false, l
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
return true, l
|
||||
msg := "[install harbor] - harbor start complete !"
|
||||
log.Info(msg)
|
||||
|
||||
return true, []string{msg}
|
||||
}
|
||||
|
||||
func (op *AgentOsOperator) chronyToPublicNTP() [][]string {
|
||||
|
||||
@@ -50,6 +50,16 @@ func Execute(em *ExecutionMessage) (bool, []string) {
|
||||
ok, resultLog = AgentOsOperatorCache.Deploy(em.FuncContent[0])
|
||||
}
|
||||
|
||||
} else if strings.HasPrefix(em.ExecutionType, "HARBOR") {
|
||||
if em.FuncContent == nil || len(em.FuncContent) <= 1 {
|
||||
ok = false
|
||||
resultLog = []string{
|
||||
"[Harbor Execute] - functions args is wrong!",
|
||||
}
|
||||
}
|
||||
// Harbor Execute
|
||||
ok, resultLog = HarborOperatorCache.Exec(em.FuncContent[0], em.FuncContent[1:]...)
|
||||
|
||||
} else {
|
||||
// shell command
|
||||
if em.PipeLineCommand != nil && len(em.PipeLineCommand) != 0 {
|
||||
|
||||
@@ -41,7 +41,7 @@ func AllCommandExecutor(singleLineCommand []string) (resultOk bool, resultLog []
|
||||
resultOk = false
|
||||
}
|
||||
|
||||
log.DebugF("all out command executor result are => %v", resultSlice)
|
||||
log.DebugF("all command of %v result are => %v", singleLineCommand, resultSlice)
|
||||
|
||||
return resultOk, resultSlice
|
||||
}
|
||||
|
||||
125
agent-go/executor/HarborExecutor.go
Normal file
125
agent-go/executor/HarborExecutor.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/mittwald/goharbor-client/v5/apiv2"
|
||||
"github.com/mittwald/goharbor-client/v5/apiv2/model"
|
||||
"net"
|
||||
)
|
||||
|
||||
type HarborOperator struct {
|
||||
SourceHarborHost string `json:"sourceHarborHost,omitempty"`
|
||||
|
||||
TargetHarborHost string `json:"targetHarborHost,omitempty"`
|
||||
|
||||
HarborPort string `json:"harborPort,omitempty"`
|
||||
|
||||
HarborAdminUser string `json:"harborAdminUser,omitempty"`
|
||||
|
||||
HarborAdminPass string `json:"harborAdminPass,omitempty"`
|
||||
}
|
||||
|
||||
// NewHarborOperator 返回一个带有默认HarborAdminPass的HarborOperator实例
|
||||
func NewHarborOperator() *HarborOperator {
|
||||
return &HarborOperator{
|
||||
HarborPort: "8033",
|
||||
HarborAdminUser: "admin", // 设置默认值
|
||||
HarborAdminPass: "V2ryStr@ngPss", // 设置默认值
|
||||
}
|
||||
}
|
||||
|
||||
// HarborOperatorCache 饿汉式单例
|
||||
var HarborOperatorCache = NewHarborOperator()
|
||||
|
||||
func (hOp *HarborOperator) Exec(baseFuncName string, funcArgs ...string) (bool, []string) {
|
||||
// 参见 HarborFunctionEnum
|
||||
|
||||
resultOk := true
|
||||
var resultLog []string
|
||||
|
||||
switch baseFuncName {
|
||||
case "CREATE_PROJECT":
|
||||
resultOk, resultLog = hOp.CreateProjectExec(funcArgs)
|
||||
break
|
||||
case "LIST_PROJECT":
|
||||
resultOk, resultLog = hOp.ListProjectExec(funcArgs)
|
||||
break
|
||||
case "SYNC_PROJECT_BETWEEN_HARBOR":
|
||||
resultOk, resultLog = hOp.SyncProjectExec(funcArgs)
|
||||
break
|
||||
|
||||
}
|
||||
|
||||
return resultOk, resultLog
|
||||
}
|
||||
|
||||
func (hOp *HarborOperator) CreateProjectExec(funcArgs []string) (bool, []string) {
|
||||
|
||||
targetHarborHost := funcArgs[0]
|
||||
parseIP := net.ParseIP(targetHarborHost)
|
||||
if parseIP != nil {
|
||||
return false, []string{
|
||||
"[Harbor CreateProjectExec] - ip format is wrong!",
|
||||
}
|
||||
}
|
||||
|
||||
hOp.TargetHarborHost = string(parseIP) + ":" + hOp.HarborPort
|
||||
|
||||
// check connection
|
||||
client, err := apiv2.NewRESTClientForHost(hOp.TargetHarborHost, hOp.HarborAdminUser, hOp.HarborAdminPass, nil)
|
||||
if err != nil {
|
||||
errorLog := fmt.Sprintf("Error creating REST client: %v\n", err)
|
||||
log.Error(errorLog)
|
||||
return false, []string{errorLog}
|
||||
}
|
||||
// check project exists
|
||||
|
||||
// create project
|
||||
// 定义你想要创建的仓库(项目)的详细信息
|
||||
cmiiProject := &model.ProjectReq{
|
||||
ProjectName: "cmii", // 仓库名称
|
||||
Metadata: &model.ProjectMetadata{
|
||||
Public: "true", // 是否是公开的
|
||||
},
|
||||
}
|
||||
rancherProject := &model.ProjectReq{
|
||||
ProjectName: "rancher", // 仓库名称
|
||||
Metadata: &model.ProjectMetadata{
|
||||
Public: "true", // 是否是公开的
|
||||
},
|
||||
}
|
||||
|
||||
// 使用客户端创建项目
|
||||
ctx := context.Background()
|
||||
err = client.NewProject(ctx, cmiiProject)
|
||||
if err != nil {
|
||||
errorLog := fmt.Sprintf("Error creating project: %v\n", err)
|
||||
return false, []string{errorLog}
|
||||
}
|
||||
|
||||
err = client.NewProject(ctx, rancherProject)
|
||||
if err != nil {
|
||||
errorLog := fmt.Sprintf("Error creating project: %v\n", err)
|
||||
return false, []string{errorLog}
|
||||
}
|
||||
|
||||
successLog := "[Harbor CreateProjectExec] - Project Create Success !"
|
||||
log.Info(successLog)
|
||||
|
||||
return true, []string{successLog}
|
||||
}
|
||||
|
||||
func (hOp *HarborOperator) ListProjectExec(funcArgs []string) (bool, []string) {
|
||||
|
||||
return true, nil
|
||||
|
||||
}
|
||||
|
||||
func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (hOp *HarborOperator) Command(baseFuncName string, funcArgs ...string) []string {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user