[Agent] [Octopus] - caqa fix bugs

This commit is contained in:
zeaslity
2024-01-30 15:54:33 +08:00
parent a471dabe46
commit 36c650301e
15 changed files with 327 additions and 47 deletions

View File

@@ -11,7 +11,9 @@ import (
"github.com/docker/docker/client"
"github.com/klauspost/pgzip"
"io"
"io/fs"
"os"
"strconv"
"strings"
)
@@ -137,6 +139,7 @@ func ImageTagFromSourceToTarget(sourceImageName, targetImageName string) bool {
log.ErrorF("[ImageTagFromSourceToTarget] - from %s to %s error %s", sourceImageName, targetImageName, err.Error())
}
log.InfoF("[ImageTagFromSourceToTarget] - from %s to %s success!", sourceImageName, targetImageName)
return true
}
@@ -161,6 +164,45 @@ func ImagePushToOctopusKindHarbor(targetImageName string) (pushResult io.ReadClo
return pushResult
}
func ImageTagFromListAndPushToCHarbor(referenceImageList []string, targetHarborHost string) (errorPushImageNameList []string) {
for _, imageName := range referenceImageList {
// check image
cmiiImageFullName := imageName
if strings.HasPrefix(imageName, "cmii") {
cmiiImageFullName = CmiiHarborPrefix + imageName
}
targetProject := "cmii"
if strings.HasPrefix(imageName, "rancher") {
targetProject = "rancher"
}
if strings.Contains(imageName, "/") {
imageName = strings.Split(imageName, "/")[1]
}
targetImageName := targetHarborHost + ":8033/" + targetProject + "/" + imageName
if ImageTagFromSourceToTarget(cmiiImageFullName, targetImageName) {
pushResult := ImagePushToOctopusKindHarbor(targetImageName)
if pushResult == nil {
errorPushImageNameList = append(errorPushImageNameList, cmiiImageFullName)
}
scanner := bufio.NewScanner(pushResult)
for scanner.Scan() {
}
log.InfoF("[ImageTagFromListAndPushToCHarbor] - push of %s success!", targetImageName)
} else {
errorPushImageNameList = append(errorPushImageNameList, cmiiImageFullName)
}
}
return errorPushImageNameList
}
func ImagePullFromCmiiHarbor(imageName string) (pullResult io.ReadCloser) {
pullResult, err := apiClient.ImagePull(context.TODO(), imageName, types.ImagePullOptions{
All: false,
@@ -185,18 +227,18 @@ func ImagePullFromCmiiHarborByMap(imageVersionMap map[string]string, silentMode
}
func ImagePullFromFileJson(filePathName string) {
func ImagePullCMiiFromFileJson(filePathName string) {
readFile, err := os.ReadFile(filePathName)
if err != nil {
log.ErrorF("[ImagePullFromFileJson] - file %s read error ! %s", filePathName, err.Error())
log.ErrorF("[ImagePullCMiiFromFileJson] - file %s read error ! %s", filePathName, err.Error())
return
}
var resultMap map[string]string
err = json.Unmarshal(readFile, &readFile)
if err != nil {
log.ErrorF("[ImagePullFromFileJson] - file %s un marshal error ! %s", filePathName, err.Error())
log.ErrorF("[ImagePullCMiiFromFileJson] - file %s un marshal error ! %s", filePathName, err.Error())
return
}
@@ -267,6 +309,53 @@ func ImagePullFromListAndCompressSplit(fullImageNameList []string, gzipFolder st
return errorPullImageList, errorGzipImageList
}
func ImageLoadFromFile(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())
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())
return false
}
log.InfoF("[ImageLoadFromFile] - load of %s, result is %s", gzipFullPath, strconv.FormatBool(loadResponse.JSON))
scanner := bufio.NewScanner(loadResponse.Body)
for scanner.Scan() {
line := scanner.Text()
fmt.Println(line)
}
return true
}
func ImageLoadFromFolderPath(folderPath string) (errorLoadImageNameList []string) {
if !strings.HasSuffix(folderPath, "/") {
folderPath += "/"
}
dirEntries, err := os.ReadDir(folderPath)
if err != nil {
log.ErrorF("[ImageLoadFromFolderPath] - error read folder %s error is %s", folderPath, err.Error())
return
}
// load gzip file
for _, dirEntry := range dirEntries {
if strings.HasSuffix(dirEntry.Name(), ".tar.gz") {
if !ImageLoadFromFile(folderPath + dirEntry.Name()) {
errorLoadImageNameList = append(errorLoadImageNameList, folderPath+dirEntry.Name())
}
}
}
return errorLoadImageNameList
}
func ImageSaveToTarGZ(targetImageName, folderPathPrefix string) bool {
imageGetByName := ImageGetByName(targetImageName)