[ Agent ] [ CMII ] - 新增大量功能
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
)
|
||||
|
||||
var CmiiOperator = CmiiK8sOperator{}
|
||||
var updateLogPath = "C:\\Users\\wddsh\\Documents\\IdeaProjects\\ProjectOctopus\\agent-go\\k8s_exec\\log\\cmii-update-log.txt"
|
||||
|
||||
// FindDeploymentRestartCountGreaterThanN 重启次数大于N的所有Deployment
|
||||
func FindDeploymentRestartCountGreaterThanN(cmiiEnv string, restartCount int32) []CmiiDeploymentInterface {
|
||||
@@ -38,7 +39,7 @@ func FindDeploymentRestartCountGreaterThanN(cmiiEnv string, restartCount int32)
|
||||
cmiiDeploymentInterface := CmiiDeploymentInterface{}
|
||||
index := 0
|
||||
|
||||
//log.DebugF("[FindDeploymentRestartCountGreaterThanN] - restart map is => %v", restartMap)
|
||||
log.DebugF("[FindDeploymentRestartCountGreaterThanN] - restart map is => %v", restartMap)
|
||||
// find deployment convert to interface
|
||||
for key, value := range restartMap {
|
||||
deployment := CmiiOperator.DeploymentExist(cmiiEnv, key)
|
||||
@@ -55,6 +56,22 @@ func FindDeploymentRestartCountGreaterThanN(cmiiEnv string, restartCount int32)
|
||||
return result[:index]
|
||||
}
|
||||
|
||||
func FindDeploymentReplicasSmallerThanN(cmiiEnv string, replicasMin int32) (deploymentList []CmiiDeploymentInterface) {
|
||||
|
||||
// get all deployments
|
||||
cmiiDeploymentInterfaces := CmiiOperator.DeploymentAllInterface(cmiiEnv)
|
||||
|
||||
// filter
|
||||
for _, deploymentInterface := range cmiiDeploymentInterfaces {
|
||||
if deploymentInterface.Replicas <= replicasMin {
|
||||
deploymentList = append(deploymentList, deploymentInterface)
|
||||
}
|
||||
}
|
||||
|
||||
// convert
|
||||
return deploymentList
|
||||
}
|
||||
|
||||
func RestartDeploymentFromList(deploymentList []CmiiDeploymentInterface) bool {
|
||||
|
||||
result := true
|
||||
@@ -86,50 +103,147 @@ func RestartCmiiBackendDeployment(cmiiEnv string) {
|
||||
|
||||
}
|
||||
|
||||
// GetCmiiAllDeploymentFromDemo 从DEMO提取全部的CMII的应用
|
||||
func GetCmiiAllDeploymentFromDemo() {
|
||||
allInterface := CmiiOperator.DeploymentAllInterface("demo")
|
||||
func UpdateCmiiDeploymentImageTag(cmiiEnv, appName, newTag string) bool {
|
||||
|
||||
filePath := "C:\\Users\\wddsh\\Documents\\IdeaProjects\\ProjectOctopus\\agent-go\\k8s_exec\\all-cmii-image.txt"
|
||||
deployment := CmiiOperator.DeploymentExist(cmiiEnv, appName)
|
||||
if deployment == nil {
|
||||
log.ErrorF("[UpdateCmiiDeploymentImageTag] - [%s] [%s] not exists !", cmiiEnv, appName)
|
||||
return false
|
||||
}
|
||||
|
||||
deploymentInterface := CmiiDeploymentInterface{}
|
||||
cmiiDeploymentInterface := deploymentInterface.Convert(*deployment)
|
||||
// check if need to update
|
||||
if cmiiDeploymentInterface.ImageTag == newTag {
|
||||
log.DebugF("[UpdateCmiiDeploymentImageTag] - [%s] [%s] image tag are the same ! no need to update !", cmiiEnv, appName)
|
||||
return true
|
||||
}
|
||||
|
||||
content := executor.BasicWordSpaceCompletion(utils.TimeSplitFormatString()+" "+cmiiDeploymentInterface.Namespace, 45)
|
||||
content = executor.BasicWordSpaceCompletion(content+cmiiDeploymentInterface.Name, 85)
|
||||
content = executor.BasicWordSpaceCompletion(content+cmiiDeploymentInterface.ImageTag, 105)
|
||||
content = content + newTag + "\n"
|
||||
|
||||
log.DebugF("[UpdateCmiiDeploymentImageTag] - prepare to update [%s]!", content)
|
||||
|
||||
// update
|
||||
tag := CmiiOperator.DeploymentUpdateTag(cmiiEnv, appName, newTag)
|
||||
if !tag {
|
||||
log.ErrorF("[UpdateCmiiDeploymentImageTag] - [%s] update failed !", content)
|
||||
return false
|
||||
}
|
||||
|
||||
// append log
|
||||
executor.BasicAppendContentToFile(content, updateLogPath)
|
||||
|
||||
// re-get from env
|
||||
time.Sleep(time.Second)
|
||||
deploy := CmiiOperator.DeploymentOneInterface(cmiiEnv, appName)
|
||||
if deploy == nil {
|
||||
log.ErrorF("[UpdateCmiiDeploymentImageTag] - unknown error happened ! [%s] [%s] not exists !", cmiiEnv, appName)
|
||||
return false
|
||||
}
|
||||
|
||||
// log
|
||||
log.InfoF("[UpdateCmiiDeploymentImageTag] - [%s] success ! real image tag are [%s] ", content, deploy.Image)
|
||||
return true
|
||||
}
|
||||
|
||||
func RollBackCmiiDeploymentFromUpdateLog(updateLog string) bool {
|
||||
|
||||
if !executor.BasicFindContentInFile(updateLog, updateLogPath) {
|
||||
log.ErrorF("[RollBackCmiiDeploymentFromUpdateLog] - [%s] no this update log ! use update instead ! => ", updateLog)
|
||||
return false
|
||||
}
|
||||
|
||||
split := strings.Split(updateLog, " ")
|
||||
index := 0
|
||||
cmiiEnv := ""
|
||||
appName := ""
|
||||
fromTag := ""
|
||||
newTag := ""
|
||||
for _, s := range split {
|
||||
if s != "" {
|
||||
if index == 1 {
|
||||
cmiiEnv = s
|
||||
} else if index == 2 {
|
||||
appName = s
|
||||
} else if index == 3 {
|
||||
fromTag = s
|
||||
} else if index == 4 {
|
||||
newTag = s
|
||||
}
|
||||
index++
|
||||
}
|
||||
}
|
||||
|
||||
log.InfoF("[RollBackCmiiDeploymentFromUpdateLog] - rollback [%s] [%s] from [%s] to [%s]", cmiiEnv, appName, newTag, fromTag)
|
||||
rollback := UpdateCmiiDeploymentImageTag(cmiiEnv, appName, fromTag)
|
||||
|
||||
return rollback
|
||||
}
|
||||
|
||||
// BackupAllDeploymentFromEnv 从DEMO提取全部的CMII的应用
|
||||
func BackupAllDeploymentFromEnv(cmiiEnv string) bool {
|
||||
|
||||
allInterface := CmiiOperator.DeploymentAllInterface(cmiiEnv)
|
||||
|
||||
filePath := "C:\\Users\\wddsh\\Documents\\IdeaProjects\\ProjectOctopus\\agent-go\\k8s_exec\\log\\all-" + CmiiOperator.CurrentNamespace + "-" + utils.TimeSplitFormatString() + ".txt"
|
||||
|
||||
log.InfoF("[BackupAllDeploymentFromEnv] - backup all image from %s => %s", CmiiOperator.CurrentNamespace, filePath)
|
||||
|
||||
firstCol := 0
|
||||
secondCol := 0
|
||||
thirdCol := 0
|
||||
fourthCol := 0
|
||||
|
||||
for _, deploymentInterface := range allInterface {
|
||||
if strings.Contains(deploymentInterface.Name, "proxy") {
|
||||
continue
|
||||
}
|
||||
firstCol = utils.MaxInt(len(deploymentInterface.Name), firstCol)
|
||||
|
||||
for name, image := range deploymentInterface.ContainerImageMap {
|
||||
secondCol = utils.MaxInt(len(name), secondCol)
|
||||
thirdCol = utils.MaxInt(len(image), thirdCol)
|
||||
}
|
||||
secondCol = utils.MaxInt(len(deploymentInterface.ImageTag), secondCol)
|
||||
thirdCol = utils.MaxInt(len(deploymentInterface.GitBranch), thirdCol)
|
||||
fourthCol = utils.MaxInt(len(deploymentInterface.GitCommit), fourthCol)
|
||||
}
|
||||
|
||||
firstCol += 4
|
||||
secondCol += 4
|
||||
firstCol += 2
|
||||
secondCol += 2
|
||||
secondCol += firstCol
|
||||
thirdCol += 4
|
||||
thirdCol += 2
|
||||
thirdCol += secondCol
|
||||
fourthCol += 2
|
||||
fourthCol += thirdCol
|
||||
|
||||
for _, deploymentInterface := range allInterface {
|
||||
if strings.Contains(deploymentInterface.Name, "proxy") {
|
||||
continue
|
||||
}
|
||||
|
||||
content := executor.BasicWordSpaceCompletion(deploymentInterface.Name, firstCol)
|
||||
content = executor.BasicWordSpaceCompletion(content+deploymentInterface.ImageTag, secondCol)
|
||||
content = executor.BasicWordSpaceCompletion(content+deploymentInterface.GitBranch, thirdCol)
|
||||
content = executor.BasicWordSpaceCompletion(content+deploymentInterface.GitCommit, fourthCol)
|
||||
content += "\n"
|
||||
|
||||
for name, image := range deploymentInterface.ContainerImageMap {
|
||||
content = executor.BasicWordSpaceCompletion(content+name, secondCol)
|
||||
content = executor.BasicWordSpaceCompletion(content+image, thirdCol)
|
||||
content += "\n"
|
||||
if !executor.BasicAppendContentToFile(content, filePath) {
|
||||
log.ErrorF("[BackupAllDeploymentFromEnv] - write to file %s error with contend %s", filePath, content)
|
||||
return false
|
||||
}
|
||||
|
||||
if !executor.BasicAppendContentToFile(content, filePath) {
|
||||
log.ErrorF("[GetCmiiAllDeploymentFromDemo] - write to file %s error with contend %s", filePath, content)
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func BackupAllCmiiDeploymentToMap(cmiiEnv string) (backendMap, frontendMap map[string]int32) {
|
||||
|
||||
allInterface := CmiiOperator.DeploymentAllInterface(cmiiEnv)
|
||||
|
||||
backendMap = make(map[string]int32, len(allInterface))
|
||||
frontendMap = make(map[string]int32, len(allInterface))
|
||||
|
||||
for _, deploymentInterface := range allInterface {
|
||||
if strings.Contains(deploymentInterface.Name, "platform") {
|
||||
frontendMap[deploymentInterface.Name] = deploymentInterface.Replicas
|
||||
} else {
|
||||
backendMap[deploymentInterface.Name] = deploymentInterface.Replicas
|
||||
}
|
||||
}
|
||||
|
||||
return backendMap, frontendMap
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user