[Agent][Deploy] - 修改ImageSync模块 DCU的核心内容

This commit is contained in:
zeaslity
2024-08-20 15:32:38 +08:00
parent 40b540f082
commit e26b7a7a00
5 changed files with 922 additions and 132 deletions

View File

@@ -13,6 +13,7 @@ import (
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
@@ -445,10 +446,11 @@ func SaveToGzipFile(imageFullName, folderPathPrefix string) (gzipOK bool, gzipIm
}
gzipImageFileFullPath = image2.ImageFullNameToGzipFileName(realImageTag)
if !strings.HasSuffix(folderPathPrefix, "/") {
folderPathPrefix += "/"
if err := os.MkdirAll(filepath.Dir(gzipImageFileFullPath), os.ModePerm); err != nil {
log.ErrorF("[ImageSaveToTarGZ] - failed to create directory: %s", err)
return false, ""
}
_ = os.MkdirAll(folderPathPrefix, os.ModeDir)
// 生成gzip压缩文件的全路径名称
gzipImageFileFullPath = folderPathPrefix + gzipImageFileFullPath
@@ -456,7 +458,10 @@ func SaveToGzipFile(imageFullName, folderPathPrefix string) (gzipOK bool, gzipIm
log.InfoF("[ImageSaveToTarGZ] - start to save [%s] to [%s]", realImageTag, gzipImageFileFullPath)
// 删除掉旧的Gzip文件
_ = os.Remove(gzipImageFileFullPath)
if err := os.Remove(gzipImageFileFullPath); err != nil && !os.IsNotExist(err) {
log.ErrorF("[ImageSaveToTarGZ] - failed to remove old gzip file: %s", err)
return false, ""
}
// 创建
tarFile, err := os.Create(gzipImageFileFullPath)
@@ -483,6 +488,76 @@ func SaveToGzipFile(imageFullName, folderPathPrefix string) (gzipOK bool, gzipIm
return true, gzipImageFileFullPath
}
// SaveImageListToGzipFile 将一个列表内的镜像全部压缩为一个tar.gz文件
func SaveImageListToGzipFile(imageFullNames []string, folderPathPrefix string, outputFileName string) (gzipOK bool, gzipFileFullPath string, errorGzipImageList []string) {
if len(imageFullNames) == 0 {
log.Error("[SaveImagesToGzipFile] - no images provided")
return false, "", errorGzipImageList
}
// 确保输出文件路径
if err := os.MkdirAll(filepath.Dir(folderPathPrefix), os.ModePerm); err != nil {
log.ErrorF("[SaveImagesToGzipFile] - failed to create directory: %s", err)
return false, "", errorGzipImageList
}
gzipFileFullPath = filepath.Join(folderPathPrefix, outputFileName)
log.InfoF("[SaveImagesToGzipFile] - start saving images to [%s]", gzipFileFullPath)
// 删除旧的Gzip文件
if err := os.Remove(gzipFileFullPath); err != nil && !os.IsNotExist(err) {
log.ErrorF("[SaveImagesToGzipFile] - failed to remove old gzip file: %s", err)
return false, "", errorGzipImageList
}
tarFile, err := os.Create(gzipFileFullPath)
if err != nil {
log.ErrorF("[SaveImagesToGzipFile] - error creating gzip file: %s", err)
return false, "", errorGzipImageList
}
defer tarFile.Close()
gw, err := pgzip.NewWriterLevel(tarFile, pgzip.DefaultCompression)
if err != nil {
log.ErrorF("[SaveImagesToGzipFile] - pgzip writer creation error: %s", err)
return false, "", errorGzipImageList
}
defer gw.Close()
errorGzipImageList = []string{}
for _, imageFullName := range imageFullNames {
imageGetByName := GetByName(imageFullName)
if imageGetByName == nil {
log.WarnF("[SaveImagesToGzipFile] - %s not exists, skipping", imageFullName)
errorGzipImageList = append(errorGzipImageList, imageFullName)
continue
}
imageSaveTarStream, err := apiClient.ImageSave(context.TODO(), imageGetByName.RepoTags)
if err != nil {
log.ErrorF("[SaveImagesToGzipFile] - image save error for %s: %s", imageFullName, err)
errorGzipImageList = append(errorGzipImageList, imageFullName)
continue
}
if _, err := io.Copy(gw, imageSaveTarStream); err != nil {
log.ErrorF("[SaveImagesToGzipFile] - failed to copy tar archive for %s to gzip writer: %s", imageFullName, err)
errorGzipImageList = append(errorGzipImageList, imageFullName)
continue
}
}
if err := gw.Close(); err != nil {
log.ErrorF("[SaveImagesToGzipFile] - error closing gzip writer: %s", err)
return false, "", errorGzipImageList
}
log.InfoF("[SaveImagesToGzipFile] - successfully saved images to [%s]", gzipFileFullPath)
return true, gzipFileFullPath, errorGzipImageList
}
func CmiiImageMapToFullNameList(cmiiImageVersionMap map[string]string) (fullImageNameList []string) {
for image, tag := range cmiiImageVersionMap {
@@ -523,10 +598,10 @@ func GenerateCmiiTagVersionImageMap(specificTag string) (backendMap, frontendMap
frontendMap = make(map[string]string, len(d_app.CmiiFrontendAppMap))
srsMap = make(map[string]string, len(d_app.CmiiSrsAppMap))
for imageName, _ := range d_app.CmiiBackendAppMap {
for imageName := range d_app.CmiiBackendAppMap {
backendMap[imageName] = specificTag
}
for imageName, _ := range d_app.CmiiFrontendAppMap {
for imageName := range d_app.CmiiFrontendAppMap {
frontendMap[imageName] = specificTag
}
for imageName, imageTag := range d_app.CmiiSrsAppMap {