Files
ProjectOctopus/agent-deploy/z_dep/G.go
2024-06-14 10:37:40 +08:00

129 lines
4.0 KiB
Go
Executable File

package z_dep
import (
"bytes"
"fmt"
"github.com/go-playground/validator/v10"
"text/template"
"wdd.io/agent-common/assert"
"wdd.io/agent-common/logger"
"wdd.io/agent-common/utils"
)
var (
K8sDashboardApplyFilePath = ""
EmqxApplyFilePath = ""
MongoApplyFilePath = ""
RabbitMQApplyFilePath = ""
RedisApplyFilePath = ""
MySQLApplyFilePath = ""
NacosApplyFilePath = ""
PVCApplyFilePath = ""
NfsApplyFilePath = ""
NfsTestApplyFilePath = ""
BackendApplyFilePath = ""
FrontendApplyFilePath = ""
SRSApplyFilePath = ""
IngresApplyFilePath = ""
ConfigMapApplyFilePath = ""
log = logger.Log
Asserter = assert.Asserter
)
type CommonEnvironmentConfig struct {
WebIP string `json:"web_ip,omitempty" valid:"required"` //A1C1IP
WebPort string `json:"web_port,omitempty" valid:"required"` //A1C1JS
HarborIP string `json:"harbor_ip,omitempty" valid:"required"` //A1C2IP
HarborPort string `json:"harbor_port,omitempty" valid:"required"` // default 8033
Namespace string `json:"namespace,omitempty" valid:"required"` // SUPREME
TagVersion string `json:"tag_version,omitempty" valid:"required"` // KIMMY
TenantEnv string `json:"tenant_env,omitempty"` // TENANT_ENV 只在内部使用
MinioPublicIP string `json:"minio_public_ip,omitempty"` // M2C1IP
MinioInnerIP string `json:"minio_inner_ip,omitempty"` // M2D2IP
NFSServerIP string `json:"nfs_server_ip,omitempty"` // N1C2IP
ApplyFilePrefix string
}
//func (env *CommonEnvironmentConfig) CompactEnv() {
//
// copySameFields(env, c_app.DefaultCmiiBackendConfig)
// copySameFields(env, c_app.DefaultCmiiFrontendConfig)
//}
//
//
func (env *CommonEnvironmentConfig) ValidateAndUniform() bool {
validate := validator.New()
err := validate.Struct(env)
if err != nil {
fmt.Printf("backend config validate error: %v\n", err)
return false
}
// uniform all
if env.MinioInnerIP == "" {
env.MinioInnerIP = env.HarborIP
}
if env.MinioPublicIP == "" {
env.MinioPublicIP = env.WebIP
}
return true
}
func (env *CommonEnvironmentConfig) ParseCommonEnvToApplyFile(applyTemplate string, applyFilePath string) bool {
return ParseEnvToApplyFile(env, applyTemplate, applyFilePath)
}
func (env *CommonEnvironmentConfig) GenerateApplyFilePath() {
Asserter.NotEmpty(env.ApplyFilePrefix, "apply file prefix is empty!")
K8sDashboardApplyFilePath = env.ApplyFilePrefix + "k8s-dashboard.yaml"
EmqxApplyFilePath = env.ApplyFilePrefix + "k8s-emqx.yaml"
MongoApplyFilePath = env.ApplyFilePrefix + "k8s-mongo.yaml"
RabbitMQApplyFilePath = env.ApplyFilePrefix + "k8s-rabbitmq.yaml"
RedisApplyFilePath = env.ApplyFilePrefix + "k8s-redis.yaml"
MySQLApplyFilePath = env.ApplyFilePrefix + "k8s-mysql.yaml"
NacosApplyFilePath = env.ApplyFilePrefix + "k8s-nacos.yaml"
PVCApplyFilePath = env.ApplyFilePrefix + "k8s-pvc.yaml"
NfsApplyFilePath = env.ApplyFilePrefix + "k8s-nfs.yaml"
NfsTestApplyFilePath = env.ApplyFilePrefix + "k8s-nfs-test.yaml"
BackendApplyFilePath = env.ApplyFilePrefix + "k8s-backend.yaml"
FrontendApplyFilePath = env.ApplyFilePrefix + "k8s-frontend.yaml"
SRSApplyFilePath = env.ApplyFilePrefix + "k8s-srs.yaml"
IngresApplyFilePath = env.ApplyFilePrefix + "k8s-ingress.yaml"
ConfigMapApplyFilePath = env.ApplyFilePrefix + "k8s-configmap.yaml"
}
func ParseEnvToApplyFile(environment any, applyTemplate string, applyFilePath string) bool {
randomString := utils.GenerateRandomString(8)
// Deployment
tmpl, err := template.New(randomString).Parse(applyTemplate)
if err != nil {
log.ErrorF("parse template error: %v", err)
return false
}
// 应用数据并打印结果
var result bytes.Buffer
err = tmpl.Execute(&result, environment)
if err != nil {
log.ErrorF("template execute error: %v", err)
return false
}
// append to file
if !utils.AppendContentWithSplitLineToFile(result.String(), applyFilePath) {
return false
}
return true
}