Skip to content

Commit

Permalink
update upgrade cli
Browse files Browse the repository at this point in the history
  • Loading branch information
710leo committed Mar 9, 2023
1 parent 1426ccc commit 550a12a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
4 changes: 2 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import (
"github.com/ccfos/nightingale/v6/cli/upgrade"
)

func Upgrade(configFile string, sqlFile string) error {
return upgrade.Upgrade(configFile, sqlFile)
func Upgrade(configFile string) error {
return upgrade.Upgrade(configFile)
}
55 changes: 42 additions & 13 deletions cli/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,62 @@ package upgrade

import (
"context"
"fmt"
"os/exec"

"github.com/ccfos/nightingale/v6/models"
"github.com/ccfos/nightingale/v6/pkg/ctx"
"github.com/ccfos/nightingale/v6/storage"

"github.com/go-sql-driver/mysql"
"github.com/toolkits/pkg/logger"
)

func Upgrade(configFile string, sqlFile string) error {
func Upgrade(configFile string) error {
var config Config
Parse(configFile, &config)
dsnConf, _ := mysql.ParseDSN(config.DB.DSN)
passwd := dsnConf.Passwd
user := dsnConf.User

cmd := exec.Command("mysql", fmt.Sprintf("-u%s", user), fmt.Sprintf("-p%s", passwd), "<", sqlFile)
if err := cmd.Run(); err != nil {
return err
}

db, err := storage.New(config.DB)
if err != nil {
return err
}

ctx := ctx.NewContext(context.Background(), db)
for _, cluster := range config.Clusters {
header := make(map[string]string)
headerCount := len(cluster.Headers)
if headerCount > 0 && headerCount%2 == 0 {
for i := 0; i < len(cluster.Headers); i += 2 {
header[cluster.Headers[i]] = cluster.Headers[i+1]
}
}

authJosn := models.Auth{
BasicAuthUser: cluster.BasicAuthUser,
BasicAuthPassword: cluster.BasicAuthPass,
}

httpJson := models.HTTP{
Timeout: cluster.Timeout,
DialTimeout: cluster.DialTimeout,
UseTLS: models.TLS{
SkipTlsVerify: cluster.UseTLS,
},
MaxIdleConnsPerHost: cluster.MaxIdleConnsPerHost,
Url: cluster.Prom,
Headers: header,
}

datasrouce := models.Datasource{
PluginId: 1,
PluginType: "prometheus",
PluginTypeName: "Prometheus Like",
Name: cluster.Name,
HTTPJson: httpJson,
AuthJson: authJosn,
ClusterName: "default",
}
err := datasrouce.Add(ctx)
if err != nil {
logger.Errorf("add datasource %s error: %v", cluster.Name, err)
}
}

datasources, err := models.GetDatasources(ctx)
if err != nil {
Expand Down
8 changes: 1 addition & 7 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
var (
upgrade = flag.Bool("upgrade", false, "Upgrade the database.")
showVersion = flag.Bool("version", false, "Show version.")
sqlFile = flag.String("sql", "", "Specify the sql file to be executed.")
configDir = flag.String("config", "", "Specify configuration directory.(env:N9E_CONFIGS)")
)

Expand All @@ -25,17 +24,12 @@ func main() {
}

if *upgrade {
if *sqlFile == "" {
fmt.Println("Please specify the sql file to be executed.")
os.Exit(1)
}

if *configDir == "" {
fmt.Println("Please specify the configuration directory.")
os.Exit(1)
}

err := cli.Upgrade(*configDir, *sqlFile)
err := cli.Upgrade(*configDir)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
9 changes: 9 additions & 0 deletions etc/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ ClusterName = "default"
Timeout=30000
NotifyConcurrency = 10

[Alert.SMTP]
Host = "smtp.163.com"
Port = 994
User = "username"
Pass = "password"
From = "[email protected]"
InsecureSkipVerify = true
Batch = 5

[Center]
MetricsYamlFile = "./etc/metrics.yaml"
I18NHeaderKey = "X-Language"
Expand Down

0 comments on commit 550a12a

Please sign in to comment.