Skip to content

Commit

Permalink
Merge branch '2418-iss-update-dk-env' into 'dev'
Browse files Browse the repository at this point in the history
Resolve "DK 升级命令支持多种 ENV 注入"

See merge request cloudcare-tools/datakit!3260
  • Loading branch information
谭彪 committed Nov 1, 2024
2 parents 2637249 + 98bfc69 commit 685b089
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 201 deletions.
195 changes: 192 additions & 3 deletions cmd/installer/installer/dkconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package installer
import (
"bytes"
"fmt"
"net"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -219,10 +220,16 @@ func mergeDefaultInputs(defaultList, enabledList []string, appendDefault bool) [

func setupDefaultInputs(mc *config.Config, arg string, list []string, upgrade bool) {
if upgrade {
if len(mc.DefaultEnabledInputs) == 0 { // all default inputs disabled
mc.DefaultEnabledInputs = mergeDefaultInputs(list, []string{"-"}, true)
if arg == "" {
if len(mc.DefaultEnabledInputs) == 0 { // all default inputs disabled
mc.DefaultEnabledInputs = mergeDefaultInputs(list, []string{"-"}, true)
} else {
mc.DefaultEnabledInputs = mergeDefaultInputs(list, mc.DefaultEnabledInputs, true)
}
} else {
mc.DefaultEnabledInputs = mergeDefaultInputs(list, mc.DefaultEnabledInputs, true)
enabledList := strings.Split(arg, ",")
enabledList = append(enabledList, mc.DefaultEnabledInputs...)
mc.DefaultEnabledInputs = mergeDefaultInputs(list, enabledList, true)
}
} else {
if arg == "" {
Expand Down Expand Up @@ -338,3 +345,185 @@ func getDataway() (*dataway.Dataway, error) {
return nil, fmt.Errorf("dataway is not set")
}
}

func loadDKEnvCfg(mc *config.Config) *config.Config {
var err error
// prepare dataway info and check token format
if len(DatawayURLs) != 0 {
mc.Dataway, err = getDataway()
if err != nil {
l.Errorf("getDataway failed: %s", err.Error())
l.Fatal(err)
}

l.Infof("Set dataway to %s", DatawayURLs)

mc.Dataway.GlobalCustomerKeys = dataway.ParseGlobalCustomerKeys(SinkerGlobalCustomerKeys)
mc.Dataway.EnableSinker = (EnableSinker != "")

l.Infof("Set dataway global sinker customer keys: %+#v", mc.Dataway.GlobalCustomerKeys)
}

if OTA {
mc.AutoUpdate = OTA
l.Info("set auto update(OTA enabled)")
}

if HTTPPublicAPIs != "" {
apis := strings.Split(HTTPPublicAPIs, ",")
idx := 0
for _, api := range apis {
api = strings.TrimSpace(api)
if api != "" {
if !strings.HasPrefix(api, "/") {
api = "/" + api
}
apis[idx] = api
idx++
}
}
mc.HTTPAPI.PublicAPIs = apis[:idx]
l.Infof("set PublicAPIs to %s", strings.Join(apis[:idx], ","))
}

if DCAEnable != "" {
config.Cfg.DCAConfig.Enable = true
if DCAWhiteList != "" {
config.Cfg.DCAConfig.WhiteList = strings.Split(DCAWhiteList, ",")
}

// check white list whether is empty or invalid
if len(config.Cfg.DCAConfig.WhiteList) == 0 {
l.Fatalf("DCA service is enabled, but white list is empty! ")
}

for _, cidr := range config.Cfg.DCAConfig.WhiteList {
if _, _, err := net.ParseCIDR(cidr); err != nil {
if net.ParseIP(cidr) == nil {
l.Fatalf("DCA white list set error, invalid IP: %s", cidr)
}
}
}

if DCAListen != "" {
config.Cfg.DCAConfig.Listen = DCAListen
}

l.Infof("DCA enabled, listen on %s, whiteliste: %s", DCAListen, DCAWhiteList)
}

if PProfListen != "" {
config.Cfg.PProfListen = PProfListen
}
l.Infof("pprof enabled? %v, listen on %s", config.Cfg.EnablePProf, config.Cfg.PProfListen)

// Only supports linux and windows
if LimitDisabled != 1 && (runtime.GOOS == datakit.OSLinux || runtime.GOOS == datakit.OSWindows) {
mc.ResourceLimitOptions.Enable = true

if LimitCPUMax > 0 {
mc.ResourceLimitOptions.CPUMax = LimitCPUMax
}

if LimitMemMax > 0 {
l.Infof("resource limit set max memory to %dMB", LimitMemMax)
mc.ResourceLimitOptions.MemMax = LimitMemMax
} else {
l.Infof("resource limit max memory not set")
}

l.Infof("resource limit enabled under %s, cpu: %f, mem: %dMB",
runtime.GOOS, mc.ResourceLimitOptions.CPUMax, mc.ResourceLimitOptions.MemMax)
} else {
mc.ResourceLimitOptions.Enable = false
l.Infof("resource limit disabled, OS: %s", runtime.GOOS)
}

if HostName != "" {
mc.Environments["ENV_HOSTNAME"] = HostName
l.Infof("set ENV_HOSTNAME to %s", HostName)
}

if GlobalHostTags != "" {
mc.GlobalHostTags = config.ParseGlobalTags(GlobalHostTags)

l.Infof("set global host tags to %+#v", mc.GlobalHostTags)
}

if GlobalElectionTags != "" {
mc.Election.Tags = config.ParseGlobalTags(GlobalElectionTags)
l.Infof("set global election tags to %+#v", mc.Election.Tags)
}

if EnableElection != "" {
mc.Election.Enable = true
l.Infof("election enabled? %v", true)
}

mc.Election.Namespace = ElectionNamespace
l.Infof("set election namespace to %s", mc.Election.Namespace)

mc.HTTPAPI.Listen = fmt.Sprintf("%s:%d", HTTPListen, HTTPPort)
l.Infof("set HTTP listen to %s", mc.HTTPAPI.Listen)

mc.InstallVer = DataKitVersion
l.Infof("install version %s", mc.InstallVer)

if DatakitName != "" {
mc.Name = DatakitName
l.Infof("set datakit name to %s", mc.Name)
}

if CryptoAESKey != "" || CryptoAESKeyFile != "" {
if mc.Crypto != nil {
mc.Crypto.AESKey = CryptoAESKey
mc.Crypto.AESKeyFile = CryptoAESKeyFile
l.Infof("set datakit crypto key=%s or crypto key file=%s", mc.Crypto.AESKey, mc.Crypto.AESKeyFile)
}
}

// 一个 func 最大 230 行
addConfdConfig(mc)

if GitURL != "" {
mc.GitRepos = &config.GitRepost{
PullInterval: GitPullInterval,
Repos: []*config.GitRepository{
{
Enable: true,
URL: GitURL,
SSHPrivateKeyPath: GitKeyPath,
SSHPrivateKeyPassword: GitKeyPW,
Branch: GitBranch,
}, // GitRepository
}, // Repos
} // GitRepost
}

if RumDisable404Page != "" {
l.Infof("set disable 404 page: %v", RumDisable404Page)
mc.HTTPAPI.Disable404Page = true
}

if RumOriginIPHeader != "" {
l.Infof("set rum origin IP header: %s", RumOriginIPHeader)
mc.HTTPAPI.RUMOriginIPHeader = RumOriginIPHeader
}

if LogLevel != "" {
mc.Logging.Level = LogLevel
l.Infof("set log level to %s", LogLevel)
}

if Log != "" {
mc.Logging.Log = Log
l.Infof("set log to %s", Log)
}

if GinLog != "" {
l.Infof("set gin log to %s", GinLog)
mc.Logging.GinLog = GinLog
}

return mc
}
Loading

0 comments on commit 685b089

Please sign in to comment.