[CMII] [Operator] - offline image download

This commit is contained in:
zeaslity
2024-01-25 11:08:08 +08:00
parent 8d4e304237
commit 5069372348
9 changed files with 349 additions and 80 deletions

View File

@@ -3,6 +3,8 @@ package cmii_operator
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/docker/docker/api/types/filters"
"github.com/klauspost/pgzip"
@@ -188,6 +190,82 @@ func ImagePullFromCmiiHarborByMap(imageVersionMap map[string]string, silentMode
}
func ImagePullFromFileJson(filePathName string) {
readFile, err := os.ReadFile(filePathName)
if err != nil {
log.ErrorF("[ImagePullFromFileJson] - 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())
return
}
for image, tag := range resultMap {
pullResult := ImagePullFromCmiiHarbor(image + ":" + tag)
if pullResult == nil {
continue
}
scanner := bufio.NewScanner(pullResult)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "\"status\":\"Pulling from") {
fmt.Println(line)
}
if strings.Contains(line, "Status: Image is up to date for") {
fmt.Println(line)
}
}
fmt.Println()
}
}
func ImagePullFromList(depContainerList []string) {
for _, dep := range depContainerList {
loginToDockerHub()
pullResult := ImagePullFromCmiiHarbor(dep)
if pullResult == nil {
continue
}
scanner := bufio.NewScanner(pullResult)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "\"status\":\"Pulling from") {
fmt.Println(line)
}
if strings.Contains(line, "Status: Image is up to date for") {
fmt.Println(line)
}
}
fmt.Println()
}
}
func ImagePullFromListAndCompressSplit(imageNameList []string, gzipFolder string) {
ImagePullFromList(imageNameList)
// generate a project folder
err := os.MkdirAll(gzipFolder, os.ModeDir)
if err != nil {
if !errors.Is(err, os.ErrExist) {
log.ErrorF("[ImagePullFromListAndCompressSplit] - create folder of %s error %s", gzipFolder, err.Error())
}
}
for _, image := range imageNameList {
ImageSaveToTarGZ(image, gzipFolder)
}
}
func ImageSaveToTarGZ(targetImageName, folderPathPrefix string) bool {
imageGetByName := ImageGetByName(targetImageName)
@@ -236,8 +314,13 @@ func ImageSaveToTarGZ(targetImageName, folderPathPrefix string) bool {
func convertImageGzipFileName(imageRepoTag string) (gzipFileName string) {
split := strings.Split(imageRepoTag, ":")
first := strings.Split(split[0], "/")
//log.DebugF(" %s to %s", imageRepoTag, split)
if len(split) == 1 {
return "docker=" + imageRepoTag + "=latest.tar.gz"
}
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="
@@ -250,11 +333,15 @@ func convertImageGzipFileName(imageRepoTag string) (gzipFileName string) {
gzipFileName += first[2]
gzipFileName += "="
} else if len(first) == 2 {
//
gzipFileName += "docker="
gzipFileName += first[0]
gzipFileName += "="
gzipFileName += first[1]
gzipFileName += "="
} else if len(first) == 1 {
//
return "docker=" + split[0] + "=" + split[1] + ".tar.gz"
}
gzipFileName += split[1]
@@ -262,3 +349,18 @@ func convertImageGzipFileName(imageRepoTag string) (gzipFileName string) {
return gzipFileName
}
func loginToDockerHub() {
login, err := apiClient.RegistryLogin(context.TODO(), types.AuthConfig{
Username: "icederce",
Password: "loveff.cxc.23",
Auth: "aWNlZGVyY2U6bG92ZWZmLmN4Yy4yMw==",
ServerAddress: "https://registry-1.docker.io",
})
if err != nil {
log.ErrorF("[loginToDockerHub] - login failed !")
}
log.DebugF("[loginToDockerHub] - login is %s", login.Status)
}