[agent-operator] - update dltu part

This commit is contained in:
zeaslity
2024-04-26 17:51:11 +08:00
parent 318a5a8026
commit 021690f5c4
12 changed files with 338 additions and 197 deletions

View File

@@ -181,7 +181,7 @@ func TagFromSourceToTarget(sourceImageName, targetImageName string) bool {
return true
}
func PushToOctopusKindHarbor(targetImageName string) (pushResult io.ReadCloser) {
func UploadToOctopusKindHarbor(targetImageName string) (pushResult io.ReadCloser) {
if GetByName(targetImageName) == nil {
log.ErrorF("[ImagePushToOctopusKindHarbor] - %s not exits !", targetImageName)
@@ -202,6 +202,17 @@ func PushToOctopusKindHarbor(targetImageName string) (pushResult io.ReadCloser)
return pushResult
}
func UploadToHarbor(targetImageName string) (uploadOK bool) {
pushResult := UploadToOctopusKindHarbor(targetImageName)
defer pushResult.Close()
scanner := bufio.NewScanner(pushResult)
for scanner.Scan() {
}
fmt.Println()
log.InfoF("[UploadToHarbor] - upload %s success!", targetImageName)
fmt.Println()
}
// TagFromListAndPushToCHarbor 需要支持 harbor.cdcyy.cn ip:8033 rancher/rancher:v2.5.7 nginx:latest
func TagFromListAndPushToCHarbor(referenceImageList []string, targetHarborHost string) (errorPushImageNameList []string) {
@@ -239,7 +250,7 @@ func TagFromListAndPushToCHarbor(referenceImageList []string, targetHarborHost s
fmt.Println(targetImageName)
//if TagFromSourceToTarget(cmiiImageFullName, targetImageName) {
// pushResult := PushToOctopusKindHarbor(targetImageName)
// pushResult := UploadToOctopusKindHarbor(targetImageName)
// if pushResult == nil {
// errorPushImageNameList = append(errorPushImageNameList, cmiiImageFullName)
// log.InfoF("[ImageTagFromListAndPushToCHarbor] - push of %s error error !", targetImageName)
@@ -360,11 +371,13 @@ func PullFromListAndCompressSplit(fullImageNameList []string, gzipFolder string)
for _, image := range fullImageNameList {
if !SaveToTarGZ(image, gzipFolder) {
ok, path := SaveToGzipFile(image, gzipFolder)
if !ok {
errorGzipImageList = append(errorGzipImageList, image)
continue
}
tarGzipFileNameList = append(tarGzipFileNameList, image2.ImageFullNameToGzipFileName(image))
tarGzipFileNameList = append(tarGzipFileNameList, path)
}
utils.BeautifulPrintListWithTitle(tarGzipFileNameList, "image gzip name list")
@@ -372,20 +385,20 @@ func PullFromListAndCompressSplit(fullImageNameList []string, gzipFolder string)
}
// LoadFromGzipFilePath 根据Gzip文件的全名称进行Load操作
func LoadFromGzipFilePath(gzipFullPath string) bool {
openFile, err := os.OpenFile(gzipFullPath, 0, fs.ModePerm)
func LoadFromGzipFilePath(gzipFileNameFullPath string) bool {
openFile, err := os.OpenFile(gzipFileNameFullPath, 0, fs.ModePerm)
if err != nil {
log.ErrorF("[ImageLoadFromFile] - failed to open file %s, error is %s", gzipFullPath, err.Error())
log.ErrorF("[ImageLoadFromFile] - failed to open file %s, error is %s", gzipFileNameFullPath, err.Error())
return false
}
loadResponse, err := apiClient.ImageLoad(context.TODO(), openFile, true)
if err != nil {
log.ErrorF("[ImageLoadFromFile] - load error %s, error is %s", gzipFullPath, err.Error())
log.ErrorF("[ImageLoadFromFile] - load error %s, error is %s", gzipFileNameFullPath, err.Error())
return false
}
log.InfoF("[ImageLoadFromFile] - load of %s, result is %s", gzipFullPath, strconv.FormatBool(loadResponse.JSON))
log.InfoF("[ImageLoadFromFile] - load of %s, result is %s", gzipFileNameFullPath, strconv.FormatBool(loadResponse.JSON))
scanner := bufio.NewScanner(loadResponse.Body)
for scanner.Scan() {
@@ -420,18 +433,19 @@ func LoadFromFolderPath(folderPath string) (errorLoadImageNameList []string) {
return errorLoadImageNameList
}
func SaveToTarGZ(targetImageName, folderPathPrefix string) bool {
// SaveToGzipFile 根据目标镜像的全名称 将镜像压缩为Gzip文件
func SaveToGzipFile(imageFullName, folderPathPrefix string) (gzipOK bool, gzipImageFileFullPath string) {
imageGetByName := GetByName(targetImageName)
imageGetByName := GetByName(imageFullName)
if imageGetByName == nil {
log.ErrorF("[ImageSaveToTarGZ] - %s not exits", targetImageName)
return false
log.ErrorF("[ImageSaveToTarGZ] - %s not exits", imageFullName)
return false, ""
}
imageSaveTarStream, err := apiClient.ImageSave(context.TODO(), imageGetByName.RepoTags)
if err != nil {
log.ErrorF("[ImageSaveToTarGZ] - image save error %s", err.Error())
return false
return false, ""
}
var realImageTag string
@@ -442,19 +456,25 @@ func SaveToTarGZ(targetImageName, folderPathPrefix string) bool {
}
}
gzipImageFile := image2.ImageFullNameToGzipFileName(realImageTag)
gzipImageFileFullPath = image2.ImageFullNameToGzipFileName(realImageTag)
if !strings.HasSuffix(folderPathPrefix, "/") {
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)
// 生成gzip压缩文件的全路径名称
gzipImageFileFullPath = folderPathPrefix + gzipImageFileFullPath
log.InfoF("[ImageSaveToTarGZ] - start to save [%s] to [%s]", realImageTag, gzipImageFileFullPath)
// 删除掉旧的Gzip文件
_ = os.Remove(gzipImageFileFullPath)
// 创建
tarFile, err := os.Create(gzipImageFileFullPath)
if err != nil {
log.ErrorF("[ImageSaveToTarGZ] - error create gzip %s file ! => %s ", gzipImageFile, err.Error())
return false
log.ErrorF("[ImageSaveToTarGZ] - error create gzip %s file ! => %s ", gzipImageFileFullPath, err.Error())
return false, ""
}
defer tarFile.Close()
@@ -468,10 +488,11 @@ func SaveToTarGZ(targetImageName, folderPathPrefix string) bool {
// Copy the tar archive to the gzip writer.
if _, err := io.Copy(gw, imageSaveTarStream); err != nil {
log.ErrorF("[ImageSaveToTarGZ] - failed to copy tar archive to gzip writer: %s", err.Error())
return false
return false, ""
}
return true
// 成功
return true, gzipImageFileFullPath
}
func CmiiImageMapToFullNameList(cmiiImageVersionMap map[string]string) (fullImageNameList []string) {