120 lines
3.2 KiB
Go
120 lines
3.2 KiB
Go
package z_dep
|
||
|
||
import (
|
||
"bytes"
|
||
"fmt"
|
||
"github.com/go-playground/validator/v10"
|
||
"reflect"
|
||
"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 = "Linux value"
|
||
case "darwin": // macOS
|
||
ApplyFilePrefix = "MacOS value"
|
||
case "windows":
|
||
ApplyFilePrefix = "C:\\Users\\wddsh\\Documents\\IdeaProjects\\ProjectOctopus\\agent-operator\\deploy\\z_file\\"
|
||
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
|
||
}
|
||
|
||
// CopySameFields 利用反射,将a中的所有同名字段的值 复制到b中的对应字段
|
||
func CopySameFields(a, b interface{}) {
|
||
va := reflect.ValueOf(a).Elem()
|
||
vb := reflect.ValueOf(b).Elem()
|
||
|
||
for i := 0; i < va.NumField(); i++ {
|
||
fieldName := va.Type().Field(i).Name
|
||
if vb.FieldByName(fieldName).IsValid() {
|
||
vb.FieldByName(fieldName).Set(va.Field(i))
|
||
}
|
||
}
|
||
}
|