feat: component super config file

pull/912/head
Xinwei Xiong 2 years ago committed by GitHub
parent 90659a30b6
commit 359739336d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"database/sql" "database/sql"
"flag"
"fmt" "fmt"
"net" "net"
"net/url" "net/url"
@ -29,7 +30,8 @@ import (
) )
const ( const (
cfgPath = "../../../../../config/config.yaml" // defaultCfgPath is the default path of the configuration file
defaultCfgPath = "../../../../../config/config.yaml"
minioHealthCheckDuration = 1 minioHealthCheckDuration = 1
maxRetry = 100 maxRetry = 100
componentStartErrCode = 6000 componentStartErrCode = 6000
@ -37,84 +39,64 @@ const (
) )
var ( var (
cfgPath = flag.String("c", defaultCfgPath, "Path to the configuration file")
ErrComponentStart = errs.NewCodeError(componentStartErrCode, "ComponentStartErr") ErrComponentStart = errs.NewCodeError(componentStartErrCode, "ComponentStartErr")
ErrConfig = errs.NewCodeError(configErrCode, "Config file is incorrect") ErrConfig = errs.NewCodeError(configErrCode, "Config file is incorrect")
) )
func initCfg() error { func initCfg() error {
data, err := os.ReadFile(cfgPath) data, err := os.ReadFile(*cfgPath)
if err != nil { if err != nil {
return err return err
} }
if err = yaml.Unmarshal(data, &config.Config); err != nil { return yaml.Unmarshal(data, &config.Config)
return err }
}
return nil type checkFunc struct {
name string
function func() error
} }
func main() { func main() {
err := initCfg() flag.Parse()
if err != nil {
fmt.Printf("Read config failed: %v", err.Error()) if err := initCfg(); err != nil {
fmt.Printf("Read config failed: %v\n", err)
return
}
checks := []checkFunc{
{name: "Mysql", function: checkMysql},
{name: "Mongo", function: checkMongo},
{name: "Minio", function: checkMinio},
{name: "Redis", function: checkRedis},
{name: "Zookeeper", function: checkZookeeper},
{name: "Kafka", function: checkKafka},
} }
for i := 0; i < maxRetry; i++ { for i := 0; i < maxRetry; i++ {
if i != 0 { if i != 0 {
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
} }
fmt.Printf("Checking components Round %v......\n", i+1) fmt.Printf("Checking components Round %v...\n", i+1)
// Check MySQL
if err := checkMysql(); err != nil {
errorPrint(fmt.Sprintf("Starting Mysql failed: %v. Please make sure your mysql service has started", err.Error()))
continue
} else {
successPrint(fmt.Sprint("Mysql starts successfully"))
}
// Check MongoDB
if err := checkMongo(); err != nil {
errorPrint(fmt.Sprintf("Starting Mongo failed: %v. Please make sure your monngo service has started", err.Error()))
continue
} else {
successPrint(fmt.Sprint("Mongo starts successfully"))
}
// Check Minio allSuccess := true
if err := checkMinio(); err != nil { for _, check := range checks {
if index := strings.Index(err.Error(), utils.IntToString(configErrCode)); index != -1 { err := check.function()
successPrint(fmt.Sprint("Minio starts successfully")) if err != nil {
warningPrint(fmt.Sprintf("%v. Please modify your config file", err.Error())) errorPrint(fmt.Sprintf("Starting %s failed: %v", check.name, err))
allSuccess = false
break
} else { } else {
errorPrint(fmt.Sprintf("Starting Minio failed: %v. Please make sure your Minio service has started", err.Error())) successPrint(fmt.Sprintf("%s starts successfully", check.name))
continue
} }
} else {
successPrint(fmt.Sprint("Minio starts successfully"))
}
// Check Redis
if err := checkRedis(); err != nil {
errorPrint(fmt.Sprintf("Starting Redis failed: %v.Please make sure your Redis service has started", err.Error()))
continue
} else {
successPrint(fmt.Sprint("Redis starts successfully"))
} }
// Check Zookeeper if allSuccess {
if err := checkZookeeper(); err != nil { successPrint("All components started successfully!")
errorPrint(fmt.Sprintf("Starting Zookeeper failed: %v.Please make sure your Zookeeper service has started", err.Error())) return
continue
} else {
successPrint(fmt.Sprint("Zookeeper starts successfully"))
}
// Check Kafka
if err := checkKafka(); err != nil {
errorPrint(fmt.Sprintf("Starting Kafka failed: %v.Please make sure your Kafka service has started", err.Error()))
continue
} else {
successPrint(fmt.Sprint("Kafka starts successfully"))
} }
successPrint(fmt.Sprint("All components starts successfully"))
os.Exit(0)
} }
os.Exit(1) os.Exit(1)
} }

Loading…
Cancel
Save