106 lines
2.9 KiB
Go
106 lines
2.9 KiB
Go
package z_dep
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"github.com/go-playground/validator/v10"
|
|
"runtime"
|
|
"text/template"
|
|
"wdd.io/agent-common/assert"
|
|
"wdd.io/agent-common/logger"
|
|
"wdd.io/agent-common/utils"
|
|
)
|
|
|
|
var ApplyFilePrefix = ""
|
|
|
|
var Asserter = assert.Asserter
|
|
|
|
var log = logger.Log
|
|
|
|
func init() {
|
|
switch runtime.GOOS {
|
|
case "linux":
|
|
ApplyFilePrefix = "/home/wdd/IdeaProjects/ProjectOctopus/agent-operator/deploy/z_5.5.0/"
|
|
case "darwin": // macOS
|
|
ApplyFilePrefix = "MacOS value"
|
|
case "windows":
|
|
ApplyFilePrefix = "C:\\Users\\wddsh\\Documents\\IdeaProjects\\ProjectOctopus\\agent-operator\\deploy\\z_xjyd\\"
|
|
default:
|
|
ApplyFilePrefix = "Unknown OS"
|
|
}
|
|
|
|
fmt.Printf("ApplyFilePrefix: %s\n", ApplyFilePrefix)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
//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 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
|
|
}
|