[ Cmii ] [ Octopus ] - add Image Function - 1
This commit is contained in:
249
agent-go/executor/ImageFunction.go
Normal file
249
agent-go/executor/ImageFunction.go
Normal file
@@ -0,0 +1,249 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"wdd.io/agent-common/image"
|
||||
)
|
||||
|
||||
var LocalGzipImageFolderPrefix = "/var/lib/docker/image_sync/"
|
||||
|
||||
func (op *AgentOsOperator) Sync(baseFuncName string, funcArgs ...string) (bool, []string) {
|
||||
resultOk := false
|
||||
var errorLog []string
|
||||
|
||||
switch baseFuncName {
|
||||
|
||||
case "DOWNLOAD_DOCKER_IMAGE":
|
||||
resultOk, errorLog = op.downloadDockerImage(funcArgs)
|
||||
break
|
||||
case "COMPRESS_IMAGE_TO_GZIP":
|
||||
resultOk, errorLog = op.compressImageToGzip(funcArgs)
|
||||
break
|
||||
case "UPLOAD_GZIP_TO_OSS":
|
||||
resultOk, errorLog = op.uploadGzipFileToOss(funcArgs)
|
||||
break
|
||||
case "DOWNLOAD_GZIP_IMAGE_FILE":
|
||||
resultOk, errorLog = op.downloadGzipImageFile(funcArgs)
|
||||
break
|
||||
case "LOAD_DOCKER_IMAGE_FROM_GZIP":
|
||||
resultOk, errorLog = op.loadDockerImageFromGzip(funcArgs)
|
||||
break
|
||||
case "PUSH_IMAGE_TO_TARGET_HARBOR":
|
||||
resultOk, errorLog = op.pushImageToTargetHarbor(funcArgs)
|
||||
break
|
||||
case "UPDATE_IMAGE_TAG":
|
||||
resultOk, errorLog = op.updateImageTag(funcArgs)
|
||||
break
|
||||
default:
|
||||
resultOk, errorLog = op.okExec(funcArgs)
|
||||
}
|
||||
|
||||
return resultOk, errorLog
|
||||
}
|
||||
|
||||
func (op *AgentOsOperator) downloadDockerImage(funcArgs []string) (bool, []string) {
|
||||
// funcArgs are imageFullName gzipFolderPrefix gzipFileName ossUrlPrefix namespace newImageTag
|
||||
|
||||
if !BasicCommandExistByPath("docker") {
|
||||
return false, []string{
|
||||
"docker not exits !",
|
||||
}
|
||||
}
|
||||
imageFullName := funcArgs[0]
|
||||
log.InfoF("[downloadDockerImage]- start to pull docker image %s", imageFullName)
|
||||
if !PureResultSingleExecute([]string{
|
||||
"docker",
|
||||
"pull",
|
||||
imageFullName,
|
||||
}) {
|
||||
return false, []string{
|
||||
"docker pull failed of " + imageFullName,
|
||||
}
|
||||
}
|
||||
|
||||
if !JudgeDockerImageExists(funcArgs[0]) {
|
||||
return false, []string{
|
||||
"image not exits ! unknown error happened!",
|
||||
}
|
||||
}
|
||||
return true, []string{
|
||||
imageFullName,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *AgentOsOperator) compressImageToGzip(funcArgs []string) (bool, []string) {
|
||||
if !BasicCommandExistByPath("docker") {
|
||||
return false, []string{
|
||||
"docker not exits !",
|
||||
}
|
||||
}
|
||||
if !BasicCommandExistByPath("gzip") {
|
||||
return false, []string{
|
||||
"gzip not exits !",
|
||||
}
|
||||
}
|
||||
|
||||
gzipFolderPrefix := funcArgs[1]
|
||||
if !BasicFolderExists(gzipFolderPrefix) {
|
||||
BasicCreateFolder(gzipFolderPrefix)
|
||||
}
|
||||
|
||||
imageFullName := funcArgs[0]
|
||||
if JudgeDockerImageExists(imageFullName) {
|
||||
return false, []string{
|
||||
"image not exits !",
|
||||
}
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(gzipFolderPrefix, "/") {
|
||||
gzipFolderPrefix += "/"
|
||||
}
|
||||
|
||||
gzipImageFromFullName := image.ImageFullNameToGzipFileName(imageFullName)
|
||||
dockerSaveCommand := "docker save " + imageFullName + " | gzip > " + gzipFolderPrefix + gzipImageFromFullName
|
||||
|
||||
executor, i := HardCodeCommandExecutor(dockerSaveCommand)
|
||||
if !executor {
|
||||
return false, i
|
||||
}
|
||||
|
||||
if !BasicFileExistAndNotNull(gzipFolderPrefix + gzipImageFromFullName) {
|
||||
return false, []string{
|
||||
"gzip of ile error ",
|
||||
}
|
||||
}
|
||||
|
||||
return true, []string{
|
||||
gzipImageFromFullName,
|
||||
}
|
||||
}
|
||||
|
||||
func (op *AgentOsOperator) uploadGzipFileToOss(funcArgs []string) (bool, []string) {
|
||||
|
||||
if !BasicCommandExistByPath("mc") {
|
||||
return false, []string{
|
||||
"mc not exits!",
|
||||
}
|
||||
}
|
||||
|
||||
gzipFolderPrefix := funcArgs[1]
|
||||
gzipImageFromFullName := funcArgs[2]
|
||||
|
||||
ok, resultLog := AllCommandExecutor([]string{
|
||||
"mc",
|
||||
"cp",
|
||||
gzipFolderPrefix + gzipImageFromFullName,
|
||||
"demo/cmlc-installation/tmp/",
|
||||
})
|
||||
|
||||
if !ok {
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
return true, nil
|
||||
|
||||
}
|
||||
|
||||
func (op *AgentOsOperator) downloadGzipImageFile(funcArgs []string) (bool, []string) {
|
||||
|
||||
ossUrlPrefix := funcArgs[3]
|
||||
gzipImageFromFullName := funcArgs[2]
|
||||
proxyUrl := funcArgs[4]
|
||||
|
||||
if proxyUrl == "" {
|
||||
BasicDownloadFile(ossUrlPrefix+gzipImageFromFullName, LocalGzipImageFolderPrefix+gzipImageFromFullName)
|
||||
} else {
|
||||
BasicDownloadFileWithProxy(ossUrlPrefix+gzipImageFromFullName, proxyUrl, LocalGzipImageFolderPrefix+gzipImageFromFullName)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (op *AgentOsOperator) loadDockerImageFromGzip(funcArgs []string) (bool, []string) {
|
||||
gzipImageFromFullName := funcArgs[2]
|
||||
|
||||
if !BasicFileExistAndNotNull(LocalGzipImageFolderPrefix + gzipImageFromFullName) {
|
||||
return false, []string{
|
||||
LocalGzipImageFolderPrefix + gzipImageFromFullName,
|
||||
"local gzip file not exits!",
|
||||
}
|
||||
}
|
||||
|
||||
hardCodeCommand := "docker load < " + LocalGzipImageFolderPrefix + gzipImageFromFullName
|
||||
executor, i := HardCodeCommandExecutor(hardCodeCommand)
|
||||
if !executor {
|
||||
return false, i
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
func (op *AgentOsOperator) pushImageToTargetHarbor(funcArgs []string) (bool, []string) {
|
||||
|
||||
targetHarborHost := funcArgs[5]
|
||||
imageFullName := funcArgs[0]
|
||||
|
||||
if !strings.Contains(targetHarborHost, "8033") {
|
||||
targetHarborHost += ":8033"
|
||||
}
|
||||
|
||||
targetImageFullName := image.ImageFullNameToTargetImageFullName(imageFullName, targetHarborHost)
|
||||
|
||||
if !PureResultSingleExecute([]string{
|
||||
"docker",
|
||||
"tag",
|
||||
imageFullName,
|
||||
targetImageFullName,
|
||||
}) {
|
||||
return false, []string{
|
||||
"docker tag error!",
|
||||
}
|
||||
}
|
||||
|
||||
HardCodeCommandExecutor("docker login -u admin -p V2ryStr@ngPss " + targetHarborHost)
|
||||
|
||||
ok, resultLog := AllCommandExecutor([]string{
|
||||
"docker",
|
||||
"push",
|
||||
targetImageFullName,
|
||||
})
|
||||
if !ok {
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
return true, []string{
|
||||
targetImageFullName,
|
||||
}
|
||||
}
|
||||
func (op *AgentOsOperator) updateImageTag(funcArgs []string) (bool, []string) {
|
||||
namespace := funcArgs[6]
|
||||
targetImageFullName := funcArgs[7]
|
||||
if !BasicCommandExistByPath("kubectl") {
|
||||
return false, []string{
|
||||
"kubectl not exits !",
|
||||
}
|
||||
}
|
||||
imageFullName := funcArgs[0]
|
||||
if !strings.HasPrefix(imageFullName, image.CmiiHarborPrefix) {
|
||||
return false, []string{
|
||||
"cant update this image !",
|
||||
}
|
||||
}
|
||||
|
||||
appName := image.ImageFullNameToAppName(imageFullName)
|
||||
|
||||
updateCommand := "kubectl -n " + namespace + " patch deployment " + appName + "-p \"{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"name\": " + appName + ",\"image\": " + targetImageFullName + "}]}}}}"
|
||||
executor, i := HardCodeCommandExecutor(updateCommand)
|
||||
if !executor {
|
||||
return false, i
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func JudgeDockerImageExists(imageFullName string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
//func BuildGzipImageFromFullName(imageFullName string) string {
|
||||
//
|
||||
//}
|
||||
Reference in New Issue
Block a user