[ Cmii ] [ Octopus ] - add Image Function - 1
This commit is contained in:
@@ -522,6 +522,46 @@ func BasicDownloadFile(downloadUrl, desFile string) (downloadOk bool, resultLog
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func BasicDownloadFileWithProxy(downloadUrl, proxyUrl, desFile string) (downloadOk bool, resultLog []string) {
|
||||
|
||||
var ok bool
|
||||
if !BasicCommandExistByPath("curl") {
|
||||
return false, []string{
|
||||
"curl not exits!",
|
||||
}
|
||||
}
|
||||
|
||||
ok, resultLog = AllCommandExecutor([]string{
|
||||
"curl",
|
||||
"-x",
|
||||
proxyUrl,
|
||||
"-o",
|
||||
desFile,
|
||||
"--insecure",
|
||||
"--max-time",
|
||||
"5",
|
||||
downloadUrl,
|
||||
})
|
||||
|
||||
errLog := fmt.Sprintf("[BasicDownloadFileWithProxy] - download file [ %s ] with proxy [ %s ] from [ %s ] failed !", desFile, proxyUrl, downloadUrl)
|
||||
if !ok {
|
||||
log.Error(errLog)
|
||||
return false, []string{
|
||||
errLog,
|
||||
}
|
||||
}
|
||||
// check file exists
|
||||
existAndNotNull := BasicFileExistAndNotNull(desFile)
|
||||
if !existAndNotNull {
|
||||
return false, []string{
|
||||
errLog,
|
||||
"[BasicDownloadFile] - file not exist !",
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// BasicAppendSourceToFile 将源文件的内容添加到目标文件,使用golang标准库完成,跨平台、安全性更强
|
||||
func BasicAppendSourceToFile(sourceFile, targetFile string) bool {
|
||||
|
||||
|
||||
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 {
|
||||
//
|
||||
//}
|
||||
@@ -1,51 +1 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// quic sort s a slice of ints using quicksort algorithm.
|
||||
// The function returns nothing. It modifies the slice in place.
|
||||
func QuickSort(a []int) { // <-- pass by value, so we can't modify it
|
||||
quick_sort(0, len(a)-1, a)
|
||||
}
|
||||
|
||||
/*
|
||||
* The following function implements the quicksort algorithm. It requires three parameters:
|
||||
* left: an index indicating the left bound of the subarray to be sorted.
|
||||
* right: an index indicating the right bound of the subarray to be sorted.
|
||||
* a: the slice of integers to sort. The function modifies the slice in place.
|
||||
*/
|
||||
func quick_sort(left int, right int, a []int) {
|
||||
if left < right { // only continue if there is more than one element in the subarray
|
||||
pivot := partition(left, right, a)
|
||||
quick_sort(left, pivot-1, a)
|
||||
quick_sort(pivot+1, right, a) // <-- call this function again on the right half of the subarray
|
||||
} else { // if there is only one element in the subarray, we can return immediately
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func partition(left int, right int, a []int) int {
|
||||
// find a pivot element. This is just one of the examples in the book
|
||||
pivot := a[right]
|
||||
|
||||
// index i will keep track of where to put the pivot element
|
||||
i := left - 1
|
||||
|
||||
// swap the pivot element with a[right]
|
||||
a[right], a[left] = a[left], a[right]
|
||||
|
||||
for j := left; j < right; j++ {
|
||||
if a[j] <= pivot {
|
||||
i += 1 // increment i
|
||||
a[i], a[j] = a[j], a[i]
|
||||
}
|
||||
}
|
||||
|
||||
return i + 1 // put the pivot element at its final position
|
||||
}
|
||||
|
||||
func main() {
|
||||
arr := []int{2, 45, 36, -78, 90, 1000, -1, 0, -76}
|
||||
QuickSort(arr)
|
||||
fmt.Println("Sorted array:", arr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user