From 123d50b3c9c0f2ff8af43b3f05f03604dfca87b2 Mon Sep 17 00:00:00 2001 From: zzw <2318266924@qq.com> Date: Thu, 25 Apr 2024 16:49:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BC=98=E5=8C=96=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/apisix.go | 63 ++++++++++++++ cmd/cmd.go | 225 +------------------------------------------------- cmd/consul.go | 33 ++++++++ cmd/file.go | 147 +++++++++++++++++++++++++++++++++ 4 files changed, 247 insertions(+), 221 deletions(-) create mode 100644 cmd/apisix.go create mode 100644 cmd/consul.go create mode 100644 cmd/file.go diff --git a/cmd/apisix.go b/cmd/apisix.go new file mode 100644 index 0000000..4f9ce7b --- /dev/null +++ b/cmd/apisix.go @@ -0,0 +1,63 @@ +package cmd + +import ( + "fmt" + "github.com/cossim/coss-cli/pkg/apisix" + "github.com/urfave/cli/v2" + "io/ioutil" +) + +func initRoute(context *cli.Context) error { + apiKey := context.String("key") + host := context.String("host") + direct := context.Bool("direct") + domain := context.String("domain") + livekitDomain := context.String("livekit") + routeHost := context.String("route-host") + + baseURL := host + "/apisix/admin/routes/" + + client := apisix.NewApiClient(apiKey, baseURL) + + route := client.GetRoutes(domain, routeHost, livekitDomain, direct) + + for i, route := range route { + resp, err := client.SendRequest("PUT", fmt.Sprintf("%d", i+1), route) + if err != nil { + fmt.Printf("Error sending request for route %d: %v\n", i+1, err) + continue + } + fmt.Printf("Route %d created successfully: %s\n", i+1, resp) + } + return nil +} + +func uploadSSL(context *cli.Context) error { + certPath := context.String("cert") + keyPath := context.String("private_key") + domain := context.String("domain") + apiKey := context.String("key") + host := context.String("host") + num := context.Int("num") + + cert, err := ioutil.ReadFile(certPath) + if err != nil { + return fmt.Errorf("failed to read certificate file: %v", err) + } + + key, err := ioutil.ReadFile(keyPath) + if err != nil { + return fmt.Errorf("failed to read key file: %v", err) + } + + if domain == "" { + return fmt.Errorf("domain name is required") + } + client := apisix.NewApiClient(apiKey, host) + if err := client.UpdateSSL(cert, key, []string{domain}, num); err != nil { + fmt.Printf("Error updating SSL: %v\n", err) + } else { + fmt.Printf("SSL updated successfully\n") + } + return nil +} diff --git a/cmd/cmd.go b/cmd/cmd.go index 40d4165..5d3975d 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -1,16 +1,7 @@ package cmd import ( - "fmt" - "github.com/cossim/coss-cli/config" - "github.com/cossim/coss-cli/pkg/apisix" - "github.com/cossim/coss-cli/pkg/consul" - "github.com/cossim/coss-cli/pkg/pgp" "github.com/urfave/cli/v2" - "io/ioutil" - "os" - "path/filepath" - "strings" ) const VERSION = "v1.0.0" @@ -52,30 +43,7 @@ var App = &cli.App{ Usage: "consul enable ssl", }, }, - Action: func(cCtx *cli.Context) error { - if cCtx.String("path") == "" { - return fmt.Errorf("path is empty") - } - paths := strings.Split(cCtx.String("path"), ",") - if len(paths) == 0 { - return fmt.Errorf("no paths provided") - } - nameSpace := cCtx.String("namespace") - host := cCtx.String("host") - token := cCtx.String("token") - token = token + "/" - client := consul.NewConsulClient(host, nameSpace, "", token) - - for _, path := range paths { - client.SetPath(path) - err := client.PutConfig() - if err != nil { - return err - } - } - - return nil - }, + Action: initConfig, }, { Name: "gen", @@ -108,141 +76,7 @@ var App = &cli.App{ Usage: "consul enable ssl", }, }, - Action: func(cCtx *cli.Context) error { - direct := cCtx.Bool("direct") - outputDir := cCtx.String("path") - env := cCtx.String("env") - domain := cCtx.String("domain") - enableSsl := cCtx.Bool("ssl") - cacheDir := "./config" - - if _, err := os.Stat(cacheDir); os.IsNotExist(err) { - err := os.Mkdir(cacheDir, 0755) // 创建文件夹并设置权限 - if err != nil { - return err - } - } - - cacheDir = "./config/service" - if _, err := os.Stat(cacheDir); os.IsNotExist(err) { - err := os.Mkdir(cacheDir, 0755) // 创建文件夹并设置权限 - if err != nil { - return err - } - } - - cacheDir = "./config/common" - if _, err := os.Stat(cacheDir); os.IsNotExist(err) { - err := os.Mkdir(cacheDir, 0755) // 创建文件夹并设置权限 - if err != nil { - return err - } - } - - if direct { - // 生成服务配置文件 - for _, name := range config.ServiceList { - - httpname := config.HttpName[name] - grpcname := config.GrpcName[name] - httpport := config.HttpPort[httpname] - grpcport := config.GrpcPort[grpcname] - - configStr := config.GenServiceConfig(httpname, grpcname, httpport, grpcport, env, enableSsl, domain) - filePath := filepath.Join(outputDir+"/config/service/", fmt.Sprintf("%s.yaml", name)) - err := ioutil.WriteFile(filePath, []byte(configStr), 0644) - if err != nil { - fmt.Printf("写入文件 %s 失败:%v\n", filePath, err) - } else { - fmt.Printf("生成 %s 成功\n", filePath) - } - } - - } else { - fmt.Println("生成 consul 配置文件") - - // 生成服务配置文件 - for _, name := range config.ServiceList { - - httpname := config.HttpName[name] - grpcname := config.GrpcName[name] - httpport := config.HttpPort[httpname] - grpcport := config.GrpcPort[grpcname] - - configStr := config.GenConsulServiceConfig(httpname, grpcname, httpport, grpcport, env, enableSsl, domain) - filePath := filepath.Join(outputDir+"/config/service/", fmt.Sprintf("%s.yaml", name)) - err := ioutil.WriteFile(filePath, []byte(configStr), 0644) - if err != nil { - fmt.Printf("写入文件 %s 失败:%v\n", filePath, err) - } else { - fmt.Printf("生成 %s 成功\n", filePath) - } - } - - //生成公共配置 - for _, name := range config.ConsulCommonList { - configStr := config.GenConsulCommonConfig(name) - filePath := filepath.Join(outputDir+"/config/common/", fmt.Sprintf("%s.yaml", name)) - err := ioutil.WriteFile(filePath, []byte(configStr), 0644) - if err != nil { - fmt.Printf("写入文件 %s 失败:%v\n", filePath, err) - } else { - fmt.Printf("生成 %s 成功\n", filePath) - } - } - } - - // 生成公共配置文件 - for _, name := range config.CommonClist { - configStr := config.GenCommonConfig(name) - - if name == "consul" { - filePath := filepath.Join(outputDir+"/config/common/", fmt.Sprintf("%s.json", name)) - err := ioutil.WriteFile(filePath, []byte(configStr), 0644) - if err != nil { - fmt.Printf("写入文件 %s 失败:%v\n", filePath, err) - } else { - fmt.Printf("生成 %s 成功\n", filePath) - } - - } else { - filePath := filepath.Join(outputDir+"/config/common/", fmt.Sprintf("%s.yaml", name)) - err := ioutil.WriteFile(filePath, []byte(configStr), 0644) - if err != nil { - fmt.Printf("写入文件 %s 失败:%v\n", filePath, err) - } else { - fmt.Printf("生成 %s 成功\n", filePath) - } - } - - } - - //生成docker-compose - configStr := "" - if direct { - configStr = config.GenDockerCompose(false) - } else { - configStr = config.GenDockerCompose(true) - } - - filePath := filepath.Join(outputDir, "docker-compose.yaml") - - err := ioutil.WriteFile(filePath, []byte(configStr), 0644) - if err != nil { - fmt.Printf("写入文件 %s 失败:%v\n", filePath, err) - return err - } else { - fmt.Printf("生成 %s 成功\n", filePath) - } - - //生成pgp公私钥 - err = pgp.GenerateKeyPair() - if err != nil { - return err - } - - return nil - }, + Action: genFile, }, { Name: "route", @@ -280,30 +114,7 @@ var App = &cli.App{ Usage: "route host", }, }, - Action: func(context *cli.Context) error { - apiKey := context.String("key") - host := context.String("host") - direct := context.Bool("direct") - domain := context.String("domain") - livekitDomain := context.String("livekit") - routeHost := context.String("route-host") - - baseURL := host + "/apisix/admin/routes/" - - client := apisix.NewApiClient(apiKey, baseURL) - - route := client.GetRoutes(domain, routeHost, livekitDomain, direct) - - for i, route := range route { - resp, err := client.SendRequest("PUT", fmt.Sprintf("%d", i+1), route) - if err != nil { - fmt.Printf("Error sending request for route %d: %v\n", i+1, err) - continue - } - fmt.Printf("Route %d created successfully: %s\n", i+1, resp) - } - return nil - }, + Action: initRoute, }, { Name: "ssl", @@ -340,35 +151,7 @@ var App = &cli.App{ Usage: "apisix host", }, }, - Action: func(context *cli.Context) error { - certPath := context.String("cert") - keyPath := context.String("private_key") - domain := context.String("domain") - apiKey := context.String("key") - host := context.String("host") - num := context.Int("num") - - cert, err := ioutil.ReadFile(certPath) - if err != nil { - return fmt.Errorf("failed to read certificate file: %v", err) - } - - key, err := ioutil.ReadFile(keyPath) - if err != nil { - return fmt.Errorf("failed to read key file: %v", err) - } - - if domain == "" { - return fmt.Errorf("domain name is required") - } - client := apisix.NewApiClient(apiKey, host) - if err := client.UpdateSSL(cert, key, []string{domain}, num); err != nil { - fmt.Printf("Error updating SSL: %v\n", err) - } else { - fmt.Printf("SSL updated successfully\n") - } - return nil - }, + Action: uploadSSL, }, }, } diff --git a/cmd/consul.go b/cmd/consul.go new file mode 100644 index 0000000..ef07312 --- /dev/null +++ b/cmd/consul.go @@ -0,0 +1,33 @@ +package cmd + +import ( + "fmt" + "github.com/cossim/coss-cli/pkg/consul" + "github.com/urfave/cli/v2" + "strings" +) + +func initConfig(cCtx *cli.Context) error { + if cCtx.String("path") == "" { + return fmt.Errorf("path is empty") + } + paths := strings.Split(cCtx.String("path"), ",") + if len(paths) == 0 { + return fmt.Errorf("no paths provided") + } + nameSpace := cCtx.String("namespace") + host := cCtx.String("host") + token := cCtx.String("token") + token = token + "/" + client := consul.NewConsulClient(host, nameSpace, "", token) + + for _, path := range paths { + client.SetPath(path) + err := client.PutConfig() + if err != nil { + return err + } + } + + return nil +} diff --git a/cmd/file.go b/cmd/file.go new file mode 100644 index 0000000..4ab5345 --- /dev/null +++ b/cmd/file.go @@ -0,0 +1,147 @@ +package cmd + +import ( + "fmt" + "github.com/cossim/coss-cli/config" + "github.com/cossim/coss-cli/pkg/pgp" + "github.com/urfave/cli/v2" + "io/ioutil" + "os" + "path/filepath" +) + +func genFile(cCtx *cli.Context) error { + direct := cCtx.Bool("direct") + outputDir := cCtx.String("path") + env := cCtx.String("env") + domain := cCtx.String("domain") + enableSsl := cCtx.Bool("ssl") + cacheDir := "./config" + + if _, err := os.Stat(cacheDir); os.IsNotExist(err) { + err := os.Mkdir(cacheDir, 0755) // 创建文件夹并设置权限 + if err != nil { + return err + } + } + + cacheDir = "./config/service" + if _, err := os.Stat(cacheDir); os.IsNotExist(err) { + err := os.Mkdir(cacheDir, 0755) // 创建文件夹并设置权限 + if err != nil { + return err + } + } + + cacheDir = "./config/common" + if _, err := os.Stat(cacheDir); os.IsNotExist(err) { + err := os.Mkdir(cacheDir, 0755) // 创建文件夹并设置权限 + if err != nil { + return err + } + } + + if direct { + // 生成服务配置文件 + for _, name := range config.ServiceList { + + httpname := config.HttpName[name] + grpcname := config.GrpcName[name] + httpport := config.HttpPort[httpname] + grpcport := config.GrpcPort[grpcname] + + configStr := config.GenServiceConfig(httpname, grpcname, httpport, grpcport, env, enableSsl, domain) + filePath := filepath.Join(outputDir+"/config/service/", fmt.Sprintf("%s.yaml", name)) + err := ioutil.WriteFile(filePath, []byte(configStr), 0644) + if err != nil { + fmt.Printf("写入文件 %s 失败:%v\n", filePath, err) + } else { + fmt.Printf("生成 %s 成功\n", filePath) + } + } + + } else { + fmt.Println("生成 consul 配置文件") + + // 生成服务配置文件 + for _, name := range config.ServiceList { + + httpname := config.HttpName[name] + grpcname := config.GrpcName[name] + httpport := config.HttpPort[httpname] + grpcport := config.GrpcPort[grpcname] + + configStr := config.GenConsulServiceConfig(httpname, grpcname, httpport, grpcport, env, enableSsl, domain) + filePath := filepath.Join(outputDir+"/config/service/", fmt.Sprintf("%s.yaml", name)) + err := ioutil.WriteFile(filePath, []byte(configStr), 0644) + if err != nil { + fmt.Printf("写入文件 %s 失败:%v\n", filePath, err) + } else { + fmt.Printf("生成 %s 成功\n", filePath) + } + } + + //生成公共配置 + for _, name := range config.ConsulCommonList { + configStr := config.GenConsulCommonConfig(name) + filePath := filepath.Join(outputDir+"/config/common/", fmt.Sprintf("%s.yaml", name)) + err := ioutil.WriteFile(filePath, []byte(configStr), 0644) + if err != nil { + fmt.Printf("写入文件 %s 失败:%v\n", filePath, err) + } else { + fmt.Printf("生成 %s 成功\n", filePath) + } + } + } + + // 生成公共配置文件 + for _, name := range config.CommonClist { + configStr := config.GenCommonConfig(name) + + if name == "consul" { + filePath := filepath.Join(outputDir+"/config/common/", fmt.Sprintf("%s.json", name)) + err := ioutil.WriteFile(filePath, []byte(configStr), 0644) + if err != nil { + fmt.Printf("写入文件 %s 失败:%v\n", filePath, err) + } else { + fmt.Printf("生成 %s 成功\n", filePath) + } + + } else { + filePath := filepath.Join(outputDir+"/config/common/", fmt.Sprintf("%s.yaml", name)) + err := ioutil.WriteFile(filePath, []byte(configStr), 0644) + if err != nil { + fmt.Printf("写入文件 %s 失败:%v\n", filePath, err) + } else { + fmt.Printf("生成 %s 成功\n", filePath) + } + } + + } + + //生成docker-compose + configStr := "" + if direct { + configStr = config.GenDockerCompose(false) + } else { + configStr = config.GenDockerCompose(true) + } + + filePath := filepath.Join(outputDir, "docker-compose.yaml") + + err := ioutil.WriteFile(filePath, []byte(configStr), 0644) + if err != nil { + fmt.Printf("写入文件 %s 失败:%v\n", filePath, err) + return err + } else { + fmt.Printf("生成 %s 成功\n", filePath) + } + + //生成pgp公私钥 + err = pgp.GenerateKeyPair() + if err != nil { + return err + } + + return nil +}