From 8a53374534d93976683be8bc511035b4dc88b104 Mon Sep 17 00:00:00 2001 From: 9547 Date: Sat, 16 Jan 2021 13:29:28 +0800 Subject: [PATCH] feat(cluster/spec): check alertmanager,prometheus's config --- pkg/cluster/spec/alertmanager.go | 5 ++- pkg/cluster/spec/prometheus.go | 6 +++- pkg/cluster/spec/server_config.go | 58 ++++++++++++++++++------------- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/pkg/cluster/spec/alertmanager.go b/pkg/cluster/spec/alertmanager.go index 7aceb58a27..a2ad1c7394 100644 --- a/pkg/cluster/spec/alertmanager.go +++ b/pkg/cluster/spec/alertmanager.go @@ -159,7 +159,10 @@ func (i *AlertManagerInstance) InitConfig( if err := config.NewAlertManagerConfig().ConfigToFile(configPath); err != nil { return err } - return i.TransferLocalConfigFile(ctx, e, configPath, dst) + if err := i.TransferLocalConfigFile(ctx, e, configPath, dst); err != nil { + return err + } + return checkConfig(ctx, e, i.ComponentName(), clusterVersion, i.OS(), i.Arch(), i.ComponentName()+".yml", paths, nil) } // ScaleConfig deploy temporary config on scaling diff --git a/pkg/cluster/spec/prometheus.go b/pkg/cluster/spec/prometheus.go index a4b93b912c..c61b728db4 100644 --- a/pkg/cluster/spec/prometheus.go +++ b/pkg/cluster/spec/prometheus.go @@ -276,7 +276,11 @@ func (i *MonitorInstance) InitConfig( return err } dst = filepath.Join(paths.Deploy, "conf", "prometheus.yml") - return e.Transfer(ctx, fp, dst, false) + if err := e.Transfer(ctx, fp, dst, false); err != nil { + return err + } + + return checkConfig(ctx, e, i.ComponentName(), clusterVersion, i.OS(), i.Arch(), i.ComponentName()+".yml", paths, nil) } // We only really installRules for dm cluster because the rules(*.rules.yml) packed with the prometheus diff --git a/pkg/cluster/spec/server_config.go b/pkg/cluster/spec/server_config.go index 5abab8c2fa..fd5c3fa8da 100644 --- a/pkg/cluster/spec/server_config.go +++ b/pkg/cluster/spec/server_config.go @@ -27,6 +27,7 @@ import ( perrs "github.com/pingcap/errors" "github.com/pingcap/tiup/pkg/cluster/clusterutil" "github.com/pingcap/tiup/pkg/cluster/ctxt" + "github.com/pingcap/tiup/pkg/cluster/executor" "github.com/pingcap/tiup/pkg/logger/log" "github.com/pingcap/tiup/pkg/meta" "github.com/pingcap/tiup/pkg/utils" @@ -235,36 +236,45 @@ func mergeImported(importConfig []byte, specConfigs ...map[string]interface{}) ( // BindVersion map the cluster version to the third components binding version. type BindVersion func(comp string, version string) (bindVersion string) -func checkConfig(ctx context.Context, e ctxt.Executor, componentName, clusterVersion, nodeOS, arch, config string, paths meta.DirPaths, bindVersion BindVersion) error { - repo, err := clusterutil.NewRepository(nodeOS, arch) - if err != nil { - return perrs.Annotate(ErrorCheckConfig, err.Error()) - } +func checkConfig(ctx context.Context, e executor.Executor, componentName, clusterVersion, nodeOS, arch, config string, paths meta.DirPaths, bindVersion BindVersion) error { + var cmd string + configPath := path.Join(paths.Deploy, "conf", config) + switch componentName { + case ComponentPrometheus: + cmd = fmt.Sprintf("%s/bin/prometheus/promtool check config %s", paths.Deploy, configPath) + case ComponentAlertmanager: + cmd = fmt.Sprintf("%s/bin/alertmanager/amtool check-config %s", paths.Deploy, configPath) + default: + repo, err := clusterutil.NewRepository(nodeOS, arch) + if err != nil { + return perrs.Annotate(ErrorCheckConfig, err.Error()) + } - ver := clusterVersion - if bindVersion != nil { - ver = bindVersion(componentName, clusterVersion) - } + ver := clusterVersion + if bindVersion != nil { + ver = bindVersion(componentName, clusterVersion) + } - entry, err := repo.ComponentBinEntry(componentName, ver) - if err != nil { - return perrs.Annotate(ErrorCheckConfig, err.Error()) - } + entry, err := repo.ComponentBinEntry(componentName, ver) + if err != nil { + return perrs.Annotate(ErrorCheckConfig, err.Error()) + } + binPath := path.Join(paths.Deploy, "bin", entry) - binPath := path.Join(paths.Deploy, "bin", entry) - // Skip old versions - if !hasConfigCheckFlag(ctx, e, binPath) { - return nil - } + // Skip old versions + if !hasConfigCheckFlag(ctx, e, binPath) { + return nil + } - // Hack tikv --pd flag - extra := "" - if componentName == ComponentTiKV { - extra = `--pd=""` + // Hack tikv --pd flag + extra := "" + if componentName == ComponentTiKV { + extra = `--pd=""` + } + cmd = fmt.Sprintf("%s --config-check --config=%s %s", binPath, configPath, extra) } - configPath := path.Join(paths.Deploy, "conf", config) - _, _, err = e.Execute(ctx, fmt.Sprintf("%s --config-check --config=%s %s", binPath, configPath, extra), false) + _, _, err := e.Execute(cmd, false) if err != nil { return perrs.Annotate(ErrorCheckConfig, err.Error()) }