195 lines
5.1 KiB
Go
Executable File
195 lines
5.1 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
image2 "wdd.io/agent-common/image"
|
|
"wdd.io/agent-common/utils"
|
|
agent_deploy "wdd.io/agent-deploy"
|
|
"wdd.io/agent-deploy/z_dep"
|
|
"wdd.io/agent-operator/image"
|
|
)
|
|
|
|
func CmiiEnvDeploy(cmiiNamespace string, shouldDoCompleteDeploy bool, deploySpecificTag string, backupFromEnv string) {
|
|
|
|
folderPrefix := "/home/wdd/IdeaProjects/ProjectOctopus/agent-deploy/" + cmiiNamespace + "/"
|
|
|
|
tenantEnv := cmiiNamespace
|
|
if strings.Contains(cmiiNamespace, "-") {
|
|
split := strings.Split(cmiiNamespace, "-")
|
|
tenantEnv = split[len(split)-1]
|
|
}
|
|
|
|
// common environment
|
|
common := &z_dep.CommonEnvironmentConfig{
|
|
WebIP: "lab.uavcmlc.com",
|
|
WebPort: "",
|
|
HarborIP: image2.CmiiHarborPrefix,
|
|
HarborPort: "",
|
|
Namespace: cmiiNamespace,
|
|
TagVersion: deploySpecificTag,
|
|
TenantEnv: tenantEnv,
|
|
ApplyFilePrefix: folderPrefix,
|
|
}
|
|
|
|
var backendMap map[string]string
|
|
var frontendMap map[string]string
|
|
var srsMap map[string]string
|
|
|
|
// 输出特定版本的Tag
|
|
backendMap, frontendMap, srsMap = image.GenerateCmiiTagVersionImageMap(deploySpecificTag)
|
|
|
|
if backupFromEnv != "" {
|
|
// 从 backupFromEnv 环境拉取
|
|
backendMapFromEnv, frontendMapFromEnv, srsMapFromEnv := BackupAllCmiiDeploymentToMap(backupFromEnv)
|
|
|
|
backendMap = utils.MergeMap(backendMap, backendMapFromEnv)
|
|
frontendMap = utils.MergeMap(frontendMap, frontendMapFromEnv)
|
|
srsMap = utils.MergeMap(srsMap, srsMapFromEnv)
|
|
}
|
|
|
|
utils.BeautifulPrintWithTitle(backendMap, "backendMap")
|
|
utils.BeautifulPrintWithTitle(frontendMap, "frontendMap")
|
|
utils.BeautifulPrintWithTitle(srsMap, "srsMap")
|
|
|
|
common.GenerateApplyFilePath()
|
|
|
|
// generate and get all old stuff
|
|
agent_deploy.CmiiEnvironmentDeploy(shouldDoCompleteDeploy, common, backendMap, frontendMap)
|
|
|
|
// test
|
|
//GetNodeWideByKubectl(cmiiNamespace)
|
|
|
|
// clear old apply file
|
|
//clearOldApplyStuff(common, shouldDoCompleteDeploy)
|
|
|
|
// apply new app
|
|
//applyNewAppStuff(common, shouldDoCompleteDeploy)
|
|
|
|
fmt.Println()
|
|
fmt.Println("-------------------- all done ---------------------")
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
func applyNewAppStuff(common *z_dep.CommonEnvironmentConfig, shouldDoCompleteDeploy bool) bool {
|
|
files, err := os.ReadDir(common.ApplyFilePrefix)
|
|
if err != nil {
|
|
log.ErrorF("failed to read directory: %v", err)
|
|
return false
|
|
}
|
|
if len(files) == 0 {
|
|
log.WarnF("no apply file found in %s", common.ApplyFilePrefix)
|
|
return false
|
|
}
|
|
|
|
namespace := common.Namespace
|
|
|
|
if shouldDoCompleteDeploy {
|
|
|
|
// pvc
|
|
ApplyByKubectl(z_dep.PVCApplyFilePath, namespace)
|
|
|
|
// mysql
|
|
ApplyByKubectl(z_dep.MySQLApplyFilePath, namespace)
|
|
if !DefaultCmiiOperator.PodStatusCheckTimeout("helm-mysql-0", namespace, 180) {
|
|
return false
|
|
}
|
|
|
|
// mongo
|
|
ApplyByKubectl(z_dep.MongoApplyFilePath, namespace)
|
|
if !DefaultCmiiOperator.PodStatusCheckTimeout("helm-mongo-0", namespace, 180) {
|
|
return false
|
|
}
|
|
|
|
// redis
|
|
ApplyByKubectl(z_dep.RedisApplyFilePath, namespace)
|
|
if !DefaultCmiiOperator.PodStatusCheckTimeout("helm-redis-master-0", namespace, 180) {
|
|
return false
|
|
}
|
|
if !DefaultCmiiOperator.PodStatusCheckTimeout("helm-redis-replicas-0", namespace, 180) {
|
|
return false
|
|
}
|
|
|
|
// rabbitmq
|
|
ApplyByKubectl(z_dep.RabbitMQApplyFilePath, namespace)
|
|
if !DefaultCmiiOperator.PodStatusCheckTimeout("helm-rabbitmq-0", namespace, 180) {
|
|
return false
|
|
}
|
|
|
|
// nacos
|
|
ApplyByKubectl(z_dep.NacosApplyFilePath, namespace)
|
|
if !DefaultCmiiOperator.PodStatusCheckTimeout("helm-nacos-0", namespace, 180) {
|
|
return false
|
|
}
|
|
|
|
}
|
|
|
|
// frontend
|
|
ApplyByKubectl(z_dep.IngresApplyFilePath, namespace)
|
|
ApplyByKubectl(z_dep.ConfigMapApplyFilePath, namespace)
|
|
ApplyByKubectl(z_dep.FrontendApplyFilePath, namespace)
|
|
|
|
// backend
|
|
ApplyByKubectl(z_dep.BackendApplyFilePath, namespace)
|
|
|
|
return true
|
|
}
|
|
|
|
func clearOldApplyStuff(common *z_dep.CommonEnvironmentConfig, shouldDoCompleteDeploy bool) bool {
|
|
|
|
oldApplyFilePath := filepath.Join(common.ApplyFilePrefix, "old")
|
|
files, err := os.ReadDir(oldApplyFilePath)
|
|
if err != nil {
|
|
log.ErrorF("failed to read directory: %v", err)
|
|
return false
|
|
}
|
|
if len(files) == 0 {
|
|
log.WarnF("no apply file found in %s", common.ApplyFilePrefix)
|
|
return false
|
|
}
|
|
|
|
namespace := common.Namespace
|
|
|
|
for _, fileEntry := range files {
|
|
switch fileEntry.Name() {
|
|
case z_dep.PVCApplyFileName:
|
|
continue
|
|
case z_dep.MySQLApplyFileName:
|
|
continue
|
|
case z_dep.MongoApplyFileName:
|
|
continue
|
|
case z_dep.RedisApplyFileName:
|
|
continue
|
|
case z_dep.RabbitMQApplyFileName:
|
|
continue
|
|
case z_dep.EmqxApplyFileName:
|
|
continue
|
|
case z_dep.NacosApplyFileName:
|
|
continue
|
|
case z_dep.ConfigMapApplyFileName:
|
|
continue
|
|
case z_dep.IngresApplyFileName:
|
|
continue
|
|
default:
|
|
log.InfoF("[clearOldApplyStuff] - delete %s", fileEntry.Name())
|
|
DeleteByKubectl(filepath.Join(oldApplyFilePath, fileEntry.Name()), namespace)
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// rearrangeCmiiDeploySequence 重新排序CmiiDeploy顺序
|
|
func rearrangeCmiiDeploySequence(files []os.DirEntry) {
|
|
|
|
for _, file := range files {
|
|
if filepath.Ext(file.Name()) == ".yaml" || filepath.Ext(file.Name()) == ".yml" {
|
|
//filePath := filepath.Join(oldApplyYamlFolder, file.Name())
|
|
//DeleteByKubectl(filePath, cmiiEnv)
|
|
}
|
|
}
|
|
}
|