[ Cmii ] [ Octopus ] - image sync refresh

This commit is contained in:
zeaslity
2024-03-18 16:38:54 +08:00
parent 5e80d7baad
commit df44d6eef9
31 changed files with 360 additions and 208 deletions

View File

@@ -17,6 +17,7 @@ import (
"strings"
"wdd.io/agent-go/executor"
"wdd.io/agent-go/logger"
"wdd.io/agent-go/utils"
)
var apiClient = newClient()
@@ -109,7 +110,7 @@ func Delete(imageName string) []types.ImageDeleteResponseItem {
func PruneAllCmiiImages() (errorRemoveImageNameList []string) {
apiClient.ImagesPrune(context.TODO(), filters.Args{})
_, _ = apiClient.ImagesPrune(context.TODO(), filters.Args{})
imageGetAll := GetAll()
@@ -134,6 +135,8 @@ func PruneAllCmiiImages() (errorRemoveImageNameList []string) {
}
}
_, _ = apiClient.ImagesPrune(context.TODO(), filters.Args{})
return errorRemoveImageNameList
}
@@ -175,11 +178,15 @@ func PushToOctopusKindHarbor(targetImageName string) (pushResult io.ReadCloser)
return pushResult
}
// TagFromListAndPushToCHarbor 需要支持 harbor.cdcyy.cn ip:8033 rancher/rancher:v2.5.7 nginx:latest
func TagFromListAndPushToCHarbor(referenceImageList []string, targetHarborHost string) (errorPushImageNameList []string) {
for _, imageName := range referenceImageList {
// check image
// harbor.cdcyy.cn
cmiiImageFullName := imageName
if strings.HasPrefix(imageName, "cmii") {
cmiiImageFullName = CmiiHarborPrefix + imageName
}
@@ -190,7 +197,10 @@ func TagFromListAndPushToCHarbor(referenceImageList []string, targetHarborHost s
}
if strings.Contains(imageName, "/") {
//
imageName = strings.Split(imageName, "/")[1]
} else {
}
targetImageName := targetHarborHost + ":8033/" + targetProject + "/" + imageName
@@ -232,14 +242,14 @@ func PullFromCmiiHarbor(imageName string) (pullResult io.ReadCloser) {
return pullResult
}
func ImagePullFromCmiiHarborByMap(imageVersionMap map[string]string, silentMode bool) (errorPullImageList []string) {
func PullFromCmiiHarborByMap(imageVersionMap map[string]string, silentMode bool) (fullImageNameList, errorPullImageList []string) {
fullImageNameList := ConvertCMiiImageMapToList(imageVersionMap)
return ImagePullFromFullNameList(fullImageNameList)
fullImageNameList = ConvertCMiiImageMapToList(imageVersionMap)
return fullImageNameList, PullFromFullNameList(fullImageNameList)
}
func ImagePullCMiiFromFileJson(filePathName string) {
func PullCmiiFromFileJson(filePathName string) {
readFile, err := os.ReadFile(filePathName)
if err != nil {
@@ -274,7 +284,8 @@ func ImagePullCMiiFromFileJson(filePathName string) {
}
func ImagePullFromFullNameList(fullImageNameList []string) (errorPullImageList []string) {
// PullFromFullNameList 根据镜像名列表拉取全部的镜像
func PullFromFullNameList(fullImageNameList []string) (errorPullImageList []string) {
for _, dep := range fullImageNameList {
@@ -301,7 +312,7 @@ func ImagePullFromFullNameList(fullImageNameList []string) (errorPullImageList [
func PullFromListAndCompressSplit(fullImageNameList []string, gzipFolder string) (errorPullImageList, errorGzipImageList []string) {
errorPullImageList = ImagePullFromFullNameList(fullImageNameList)
errorPullImageList = PullFromFullNameList(fullImageNameList)
// generate a project folder
err := os.MkdirAll(gzipFolder, os.ModeDir)
@@ -311,16 +322,22 @@ func PullFromListAndCompressSplit(fullImageNameList []string, gzipFolder string)
}
}
var tarGzipFileNameList []string
for _, image := range fullImageNameList {
if !SaveToTarGZ(image, gzipFolder) {
errorGzipImageList = append(errorGzipImageList, image)
continue
}
tarGzipFileNameList = append(tarGzipFileNameList, convertImageGzipFileName(image))
}
utils.BeautifulPrintListWithTitle(tarGzipFileNameList, "image gzip name list")
return errorPullImageList, errorGzipImageList
}
func ImageLoadFromFile(gzipFullPath string) bool {
func LoadFromGzipFilePath(gzipFullPath string) bool {
openFile, err := os.OpenFile(gzipFullPath, 0, fs.ModePerm)
if err != nil {
log.ErrorF("[ImageLoadFromFile] - failed to open file %s, error is %s", gzipFullPath, err.Error())
@@ -344,7 +361,7 @@ func ImageLoadFromFile(gzipFullPath string) bool {
return true
}
func ImageLoadFromFolderPath(folderPath string) (errorLoadImageNameList []string) {
func LoadFromFolderPath(folderPath string) (errorLoadImageNameList []string) {
if !strings.HasSuffix(folderPath, "/") {
folderPath += "/"
}
@@ -358,7 +375,7 @@ func ImageLoadFromFolderPath(folderPath string) (errorLoadImageNameList []string
// load gzip file
for _, dirEntry := range dirEntries {
if strings.HasSuffix(dirEntry.Name(), ".tar.gz") {
if !ImageLoadFromFile(folderPath + dirEntry.Name()) {
if !LoadFromGzipFilePath(folderPath + dirEntry.Name()) {
errorLoadImageNameList = append(errorLoadImageNameList, folderPath+dirEntry.Name())
}
}
@@ -385,6 +402,7 @@ func SaveToTarGZ(targetImageName, folderPathPrefix string) bool {
for _, repoTag := range imageGetByName.RepoTags {
if !strings.Contains(repoTag, "8033") {
realImageTag = repoTag
break
}
}
@@ -393,9 +411,9 @@ func SaveToTarGZ(targetImageName, folderPathPrefix string) bool {
folderPathPrefix += "/"
}
_ = os.MkdirAll(folderPathPrefix, os.ModeDir)
gzipImageFile = folderPathPrefix + gzipImageFile
log.InfoF("[ImageSaveToTarGZ] - start to save [%s] to [%s]", realImageTag, gzipImageFile)
_ = os.Remove(gzipImageFile)
tarFile, err := os.Create(gzipImageFile)
if err != nil {
@@ -420,9 +438,19 @@ func SaveToTarGZ(targetImageName, folderPathPrefix string) bool {
return true
}
// convertImageGzipFileName 必须输出长度完全一致的内容
// convertImageGzipFileName 必须输出长度为4的内容 =出现得次数为3
func convertImageGzipFileName(imageRepoTag string) (gzipFileName string) {
// harbor.cdcyy.cn/cmii/cmii-uav-platform:5.4.0 ==> cmlc=cmii=cmii-uav-platform=5.4.0.tar.gz
// rancher/fleet:v0.3.4
// ossr/srs:v5.0.1 ==> docker=cmii=srs=v5.0.1.tar.gz
// nginx:latest
// bitnami/minio:2022.5.4
// simonrupf/chronyd:0.4.3
// 10.1.1.1:8033/cmii/ok:1.2 不支持 不允许存在
split := strings.Split(imageRepoTag, ":")
//log.DebugF(" %s to %s", imageRepoTag, split)
if len(split) == 1 {
@@ -433,28 +461,23 @@ func convertImageGzipFileName(imageRepoTag string) (gzipFileName string) {
first := strings.Split(split[0], "/")
//log.DebugF(" split[0] %s to %s", split[0], first)
if len(first) == 3 {
if strings.Contains(first[0], "cdcyy") {
gzipFileName += "cmlc="
// harbor.cdcyy.cn/cmii/cmii-uav-platform:5.4.0
// docker.io/ossr/srs:v5.0.1
if strings.HasPrefix(split[0], CmiiHarborPrefix) {
gzipFileName += "cmlc=cmii="
} else {
gzipFileName += "docker="
gzipFileName += "docker=cmii="
}
gzipFileName += first[1]
gzipFileName += "="
gzipFileName += first[2]
gzipFileName += "="
} else if len(first) == 4 {
if strings.Contains(first[0], "cdcyy") {
gzipFileName += "cmlc="
} else {
gzipFileName += "docker="
// harbor.cdcyy.cn/cmii/ossr/srs:v5.0.1
if !strings.HasPrefix(split[0], CmiiHarborPrefix) {
return imageRepoTag
}
gzipFileName += first[1]
gzipFileName += "="
gzipFileName += first[2]
gzipFileName += "cmlc=cmii="
gzipFileName += first[3]
gzipFileName += "="
} else if len(first) == 2 {
// bitnami/redis
@@ -465,7 +488,7 @@ func convertImageGzipFileName(imageRepoTag string) (gzipFileName string) {
gzipFileName += first[1]
gzipFileName += "="
} else if len(first) == 1 {
// nginx
// nginx:latest
return "docker=library=" + split[0] + "=" + split[1] + ".tar.gz"
}