182 lines
4.7 KiB
Go
182 lines
4.7 KiB
Go
package executor
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
|
|
"github.com/mittwald/goharbor-client/v5/apiv2"
|
|
"github.com/mittwald/goharbor-client/v5/apiv2/model"
|
|
)
|
|
|
|
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"`
|
|
|
|
TargetHarborClient *apiv2.RESTClient
|
|
|
|
SourceHarborClient *apiv2.RESTClient
|
|
}
|
|
|
|
// 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) {
|
|
|
|
if hOp.TargetHarborClient == nil {
|
|
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs)
|
|
if !ok {
|
|
return false, []string{
|
|
"[Harbor Create Project] - Error !",
|
|
}
|
|
}
|
|
hOp.TargetHarborClient = createClient
|
|
}
|
|
client := hOp.TargetHarborClient
|
|
|
|
// create project
|
|
// 定义你想要创建的仓库(项目)的详细信息
|
|
|
|
log.Debug("[Harbor Create Project] - create project !")
|
|
needToCreateProjectNameList := []string{"cmii", "rancher"}
|
|
// 使用客户端创建项目
|
|
ctx := context.Background()
|
|
for _, projectName := range needToCreateProjectNameList {
|
|
|
|
log.DebugF("start to create proect => %s", projectName)
|
|
projectReq := &model.ProjectReq{
|
|
ProjectName: projectName, // 仓库名称
|
|
Metadata: &model.ProjectMetadata{
|
|
Public: "true", // 是否是公开的
|
|
},
|
|
}
|
|
|
|
exists, _ := client.ProjectExists(ctx, projectName)
|
|
if !exists {
|
|
|
|
err := client.NewProject(ctx, projectReq)
|
|
if err != nil {
|
|
errorLog := fmt.Sprintf("Error creating project %s: %s\n", projectName, err.Error())
|
|
return false, []string{errorLog}
|
|
}
|
|
}
|
|
|
|
log.DebugF("[Harbor Create Project] - Project %s already exists ! continue ", projectName)
|
|
|
|
}
|
|
|
|
successLog := "[Harbor CreateProjectExec] - Project Create Success !"
|
|
log.Info(successLog)
|
|
|
|
return true, []string{successLog}
|
|
}
|
|
|
|
func (hOp *HarborOperator) CheckAndBuildHarborClient(funcArgs []string) (bool, *apiv2.RESTClient) {
|
|
|
|
targetHarborHost := funcArgs[0]
|
|
log.InfoF("[Harbor Client Create] - start to create harbor client %s", targetHarborHost)
|
|
|
|
parseIP := net.ParseIP(targetHarborHost)
|
|
if parseIP == nil {
|
|
log.Error(
|
|
fmt.Sprintf("[Harbor Client Create] - ip format is wrong! parseIP is => %s ", parseIP),
|
|
)
|
|
return false, nil
|
|
}
|
|
|
|
hOp.TargetHarborHost = "http://" + targetHarborHost + ":" + hOp.HarborPort + "/api/"
|
|
log.DebugF("[Harbor Client Create] - harbor host is => %s", hOp.TargetHarborHost)
|
|
|
|
// check connection
|
|
client, err := apiv2.NewRESTClientForHost(hOp.TargetHarborHost, hOp.HarborAdminUser, hOp.HarborAdminPass, nil)
|
|
if err != nil {
|
|
errorLog := fmt.Sprintf("Error creating REST client: %s\n", err.Error())
|
|
log.Error(errorLog)
|
|
return false, nil
|
|
}
|
|
|
|
return true, client
|
|
}
|
|
|
|
func (hOp *HarborOperator) ListProjectExec(funcArgs []string) (bool, []string) {
|
|
|
|
if hOp.TargetHarborClient == nil {
|
|
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs)
|
|
if !ok {
|
|
return false, []string{
|
|
"[Harbor Create Project ] - Error !",
|
|
}
|
|
}
|
|
hOp.TargetHarborClient = createClient
|
|
}
|
|
client := hOp.TargetHarborClient
|
|
|
|
// 使用客户端列出所有项目
|
|
ctx := context.Background()
|
|
projects, err := client.ListProjects(ctx, "")
|
|
if err != nil {
|
|
fmt.Printf("Error listing projects: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// 打印所有项目的信息
|
|
for _, project := range projects {
|
|
fmt.Printf("Project ID: %d, Name: %s, Public: %v\n", project.ProjectID, project.Name, project.Metadata.Public)
|
|
}
|
|
|
|
marshal, _ := json.Marshal(projects)
|
|
|
|
return true, []string{
|
|
fmt.Sprintf("List Projects of %s ", hOp.TargetHarborHost),
|
|
string(marshal),
|
|
}
|
|
}
|
|
|
|
func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
|
return true, nil
|
|
}
|
|
|
|
func (hOp *HarborOperator) Command(baseFuncName string, funcArgs ...string) []string {
|
|
return nil
|
|
}
|