[ Server ] [ Harbor ] - list project
This commit is contained in:
@@ -2,8 +2,10 @@ package executor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/mittwald/goharbor-client/v5/apiv2"
|
||||
"github.com/mittwald/goharbor-client/v5/apiv2/model"
|
||||
@@ -19,6 +21,10 @@ type HarborOperator struct {
|
||||
HarborAdminUser string `json:"harborAdminUser,omitempty"`
|
||||
|
||||
HarborAdminPass string `json:"harborAdminPass,omitempty"`
|
||||
|
||||
TargetHarborClient *apiv2.RESTClient
|
||||
|
||||
SourceHarborClient *apiv2.RESTClient
|
||||
}
|
||||
|
||||
// NewHarborOperator 返回一个带有默认HarborAdminPass的HarborOperator实例
|
||||
@@ -57,54 +63,46 @@ func (hOp *HarborOperator) Exec(baseFuncName string, funcArgs ...string) (bool,
|
||||
|
||||
func (hOp *HarborOperator) CreateProjectExec(funcArgs []string) (bool, []string) {
|
||||
|
||||
targetHarborHost := funcArgs[0]
|
||||
parseIP := net.ParseIP(targetHarborHost)
|
||||
if parseIP == nil {
|
||||
return false, []string{
|
||||
fmt.Sprintf("[Harbor CreateProjectExec] - ip format is wrong! parseIP is => %s ", parseIP),
|
||||
client := hOp.TargetHarborClient
|
||||
if hOp.TargetHarborClient == nil {
|
||||
ok, createClient := hOp.checkAndBuildHarborClient(funcArgs)
|
||||
if !ok {
|
||||
return false, []string{
|
||||
"[Harbor Create Project] - Error !",
|
||||
}
|
||||
}
|
||||
hOp.TargetHarborClient = createClient
|
||||
}
|
||||
|
||||
hOp.TargetHarborHost = "http://" + targetHarborHost + ":" + hOp.HarborPort + "/api"
|
||||
|
||||
log.DebugF("[Harbor CreateProjectExec] - 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, []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", // 是否是公开的
|
||||
},
|
||||
}
|
||||
|
||||
needToCreateProjectNameList := []string{"cmii", "rancher"}
|
||||
|
||||
var err error
|
||||
// 使用客户端创建项目
|
||||
ctx := context.Background()
|
||||
err = client.NewProject(ctx, cmiiProject)
|
||||
if err != nil {
|
||||
errorLog := fmt.Sprintf("Error creating project: %s\n", err.Error())
|
||||
return false, []string{errorLog}
|
||||
}
|
||||
for _, projectName := range needToCreateProjectNameList {
|
||||
|
||||
projectReq := &model.ProjectReq{
|
||||
ProjectName: projectName, // 仓库名称
|
||||
Metadata: &model.ProjectMetadata{
|
||||
Public: "true", // 是否是公开的
|
||||
},
|
||||
}
|
||||
|
||||
exists, _ := client.ProjectExists(ctx, projectName)
|
||||
if exists {
|
||||
log.DebugF("[Harbor Create Project] - Project %s already exists ! continue ", projectName)
|
||||
continue
|
||||
}
|
||||
|
||||
err = client.NewProject(ctx, projectReq)
|
||||
if err != nil {
|
||||
errorLog := fmt.Sprintf("Error creating project %s: %s\n", projectName, err.Error())
|
||||
return false, []string{errorLog}
|
||||
}
|
||||
|
||||
err = client.NewProject(ctx, rancherProject)
|
||||
if err != nil {
|
||||
errorLog := fmt.Sprintf("Error creating project: %s\n", err.Error())
|
||||
return false, []string{errorLog}
|
||||
}
|
||||
|
||||
successLog := "[Harbor CreateProjectExec] - Project Create Success !"
|
||||
@@ -113,10 +111,63 @@ func (hOp *HarborOperator) CreateProjectExec(funcArgs []string) (bool, []string)
|
||||
return true, []string{successLog}
|
||||
}
|
||||
|
||||
func (hOp *HarborOperator) checkAndBuildHarborClient(funcArgs []string) (bool, *apiv2.RESTClient) {
|
||||
|
||||
targetHarborHost := funcArgs[0]
|
||||
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) {
|
||||
|
||||
return true, nil
|
||||
client := hOp.TargetHarborClient
|
||||
if hOp.TargetHarborClient == nil {
|
||||
ok, createClient := hOp.checkAndBuildHarborClient(funcArgs)
|
||||
if !ok {
|
||||
return false, []string{
|
||||
"[Harbor Create Project ] - Error !",
|
||||
}
|
||||
}
|
||||
hOp.TargetHarborClient = createClient
|
||||
}
|
||||
|
||||
// 使用客户端列出所有项目
|
||||
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) {
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
SourcePath=/vmfs/volumes/HardDrive
|
||||
DestinationPath=/vmfs/volumes/datastore2
|
||||
|
||||
|
||||
main() {
|
||||
check_input "$@"
|
||||
# 继续执行其他操作
|
||||
@@ -29,14 +28,14 @@ main() {
|
||||
|
||||
echo "检查复制结果 ↓↓↓↓↓↓"
|
||||
ls "$SourcePath/$vmHostname"
|
||||
|
||||
|
||||
}
|
||||
|
||||
check_input() {
|
||||
if [ "$#" -ne 1 ]; then
|
||||
echo "输入参数数量必须为1个"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
main "$@"
|
||||
main "$@"
|
||||
|
||||
@@ -53,42 +53,81 @@ public class HarborFuncScheduler {
|
||||
log.error("create harbor project failed !");
|
||||
}
|
||||
|
||||
// check harbor project
|
||||
// list
|
||||
if (!ListHarborProject(projectDeployContext)) {
|
||||
log.error("list harbor project failed !");
|
||||
}
|
||||
|
||||
// 1 - sync harbor
|
||||
|
||||
|
||||
// 1 - load image from tar.gz
|
||||
|
||||
|
||||
// check harbor project
|
||||
|
||||
|
||||
log.info("Harbor Image Synchronized Succeed !");
|
||||
}
|
||||
|
||||
|
||||
private boolean CreateHarborProject(ProjectDeployContext projectDeployContext) {
|
||||
|
||||
// use master node as harbor server
|
||||
ServerInfoPO masterNode = projectDeployContext.getMasterNode();
|
||||
|
||||
ArrayList<String> createProjectArgZList = new ArrayList<>();
|
||||
createProjectArgZList.add(HarborFunctionEnum.CREATE_PROJECT.getOpName());
|
||||
createProjectArgZList.add(masterNode.getServerIpInV4());
|
||||
ArrayList<String> createProjectArgList = new ArrayList<>();
|
||||
createProjectArgList.add(HarborFunctionEnum.CREATE_PROJECT.getOpName());
|
||||
createProjectArgList.add(masterNode.getServerIpInV4());
|
||||
|
||||
// send harbor create message
|
||||
boolean createProjectOK = funcService.callHarborFuncAndJudge(
|
||||
masterNode.getTopicName(),
|
||||
HarborFunctionEnum.CREATE_PROJECT,
|
||||
createProjectArgZList
|
||||
createProjectArgList
|
||||
);
|
||||
if (!createProjectOK) {
|
||||
log.error(
|
||||
"[CreateHarborProject] - project create failed ! => {}",
|
||||
createProjectArgZList
|
||||
createProjectArgList
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
// check harbor project exists
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* check harbor project exists
|
||||
*
|
||||
* @param projectDeployContext
|
||||
* @return
|
||||
*/
|
||||
private boolean ListHarborProject(ProjectDeployContext projectDeployContext) {
|
||||
|
||||
// use master node as harbor server
|
||||
ServerInfoPO masterNode = projectDeployContext.getMasterNode();
|
||||
|
||||
ArrayList<String> listProjectArgList = new ArrayList<>();
|
||||
listProjectArgList.add(HarborFunctionEnum.LIST_PROJECT.getOpName());
|
||||
listProjectArgList.add(masterNode.getServerIpInV4());
|
||||
|
||||
// send harbor create message
|
||||
boolean createProjectOK = funcService.callHarborFuncAndJudge(
|
||||
masterNode.getTopicName(),
|
||||
HarborFunctionEnum.LIST_PROJECT,
|
||||
listProjectArgList
|
||||
);
|
||||
|
||||
if (!createProjectOK) {
|
||||
log.error(
|
||||
"[ListHarborProject] - List Harbor Project Failed !=> {}",
|
||||
listProjectArgList
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user