From 502f9f3286dd01fb9e88ea4d1ec116bbadb4664a Mon Sep 17 00:00:00 2001 From: yanweidong Date: Fri, 15 Mar 2024 09:48:17 +0800 Subject: [PATCH 1/9] dev --- .actions | 8 ++++++++ cmd/{ => commands}/check.go | 0 cmd/{ => commands}/ctl.go | 0 cmd/{ => commands}/ext.go | 0 cmd/{ => commands}/install.go | 0 cmd/{ => commands}/ps.go | 0 cmd/{ => commands}/root.go | 0 cmd/{ => commands}/types.go | 0 cmd/{ => commands}/update.go | 2 +- cmd/main.go | 7 +++++++ main.go | 7 ------- 11 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 .actions rename cmd/{ => commands}/check.go (100%) rename cmd/{ => commands}/ctl.go (100%) rename cmd/{ => commands}/ext.go (100%) rename cmd/{ => commands}/install.go (100%) rename cmd/{ => commands}/ps.go (100%) rename cmd/{ => commands}/root.go (100%) rename cmd/{ => commands}/types.go (100%) rename cmd/{ => commands}/update.go (99%) create mode 100644 cmd/main.go delete mode 100644 main.go diff --git a/.actions b/.actions new file mode 100644 index 0000000..be26f95 --- /dev/null +++ b/.actions @@ -0,0 +1,8 @@ +# Actions 编译部署配置文件,文件格式:YAML +# +# 微服务相关配置 +service: + origin: bsm + image: golang:1.22.0-bookworm + describe: system cli cmd + diff --git a/cmd/check.go b/cmd/commands/check.go similarity index 100% rename from cmd/check.go rename to cmd/commands/check.go diff --git a/cmd/ctl.go b/cmd/commands/ctl.go similarity index 100% rename from cmd/ctl.go rename to cmd/commands/ctl.go diff --git a/cmd/ext.go b/cmd/commands/ext.go similarity index 100% rename from cmd/ext.go rename to cmd/commands/ext.go diff --git a/cmd/install.go b/cmd/commands/install.go similarity index 100% rename from cmd/install.go rename to cmd/commands/install.go diff --git a/cmd/ps.go b/cmd/commands/ps.go similarity index 100% rename from cmd/ps.go rename to cmd/commands/ps.go diff --git a/cmd/root.go b/cmd/commands/root.go similarity index 100% rename from cmd/root.go rename to cmd/commands/root.go diff --git a/cmd/types.go b/cmd/commands/types.go similarity index 100% rename from cmd/types.go rename to cmd/commands/types.go diff --git a/cmd/update.go b/cmd/commands/update.go similarity index 99% rename from cmd/update.go rename to cmd/commands/update.go index ec50769..75304a1 100644 --- a/cmd/update.go +++ b/cmd/commands/update.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "fmt" diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..0696470 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,7 @@ +package main + +import "git.apinb.com/bsm-tools/bsm/cmd/commands" + +func main() { + commands.Execute() +} diff --git a/main.go b/main.go deleted file mode 100644 index d1d790d..0000000 --- a/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "git.apinb.com/bsm-tools/bsm/cmd" - -func main() { - cmd.Execute() -} From 1ed74aa7121f9c4fc55ba99638405c2d054cce49 Mon Sep 17 00:00:00 2001 From: yanweidong Date: Fri, 15 Mar 2024 09:51:15 +0800 Subject: [PATCH 2/9] dev --- cmd/commands/check.go | 2 +- cmd/commands/ctl.go | 2 +- cmd/commands/ext.go | 2 +- cmd/commands/install.go | 2 +- cmd/commands/ps.go | 2 +- cmd/commands/root.go | 2 +- cmd/commands/types.go | 2 +- cmd/main.go | 7 ++++++- 8 files changed, 13 insertions(+), 8 deletions(-) diff --git a/cmd/commands/check.go b/cmd/commands/check.go index 861a452..02965ce 100644 --- a/cmd/commands/check.go +++ b/cmd/commands/check.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "fmt" diff --git a/cmd/commands/ctl.go b/cmd/commands/ctl.go index c1e19c2..caf42c5 100644 --- a/cmd/commands/ctl.go +++ b/cmd/commands/ctl.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "fmt" diff --git a/cmd/commands/ext.go b/cmd/commands/ext.go index 8548584..b159dd1 100644 --- a/cmd/commands/ext.go +++ b/cmd/commands/ext.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "encoding/json" diff --git a/cmd/commands/install.go b/cmd/commands/install.go index a8e6723..aabdb88 100644 --- a/cmd/commands/install.go +++ b/cmd/commands/install.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "fmt" diff --git a/cmd/commands/ps.go b/cmd/commands/ps.go index 56c0582..63bcdb8 100644 --- a/cmd/commands/ps.go +++ b/cmd/commands/ps.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "encoding/json" diff --git a/cmd/commands/root.go b/cmd/commands/root.go index 85f956e..e06ce12 100644 --- a/cmd/commands/root.go +++ b/cmd/commands/root.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "fmt" diff --git a/cmd/commands/types.go b/cmd/commands/types.go index 1d619dd..cdff10d 100644 --- a/cmd/commands/types.go +++ b/cmd/commands/types.go @@ -1 +1 @@ -package cmd +package commands diff --git a/cmd/main.go b/cmd/main.go index 0696470..8428096 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,7 +1,12 @@ package main -import "git.apinb.com/bsm-tools/bsm/cmd/commands" +import ( + "git.apinb.com/bsm-sdk/engine/cmd" + "git.apinb.com/bsm-tools/bsm/cmd/commands" +) func main() { + cmd.NewCmd() + commands.Execute() } From 5f31c684432941ce2b99decde68f239799e7a0a3 Mon Sep 17 00:00:00 2001 From: yanweidong Date: Fri, 15 Mar 2024 09:57:48 +0800 Subject: [PATCH 3/9] dev --- cmd/main.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 8428096..8eb1136 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,12 +1,14 @@ package main import ( - "git.apinb.com/bsm-sdk/engine/cmd" + "fmt" + + "git.apinb.com/bsm-sdk/engine/vars" "git.apinb.com/bsm-tools/bsm/cmd/commands" ) func main() { - cmd.NewCmd() + fmt.Printf("[Blocks Service Cli: %s] Version: %s \n", vars.ServiceKey, vars.VERSION) commands.Execute() } From 2b22041ed84de4fa44d87799e375f67344b1a25c Mon Sep 17 00:00:00 2001 From: yanweidong Date: Fri, 15 Mar 2024 09:59:30 +0800 Subject: [PATCH 4/9] dev --- cmd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/main.go b/cmd/main.go index 8eb1136..d2de3a2 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -8,7 +8,7 @@ import ( ) func main() { - fmt.Printf("[Blocks Service Cli: %s] Version: %s \n", vars.ServiceKey, vars.VERSION) + fmt.Printf("Blocks Service Cli Version: %s \n", vars.VERSION) commands.Execute() } From a893f6ac8616956d6a7aab8cd01a9ab99475381f Mon Sep 17 00:00:00 2001 From: yanweidong Date: Thu, 21 Mar 2024 18:15:36 +0800 Subject: [PATCH 5/9] dev --- cmd/commands/check.go | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/cmd/commands/check.go b/cmd/commands/check.go index 02965ce..2e96683 100644 --- a/cmd/commands/check.go +++ b/cmd/commands/check.go @@ -55,10 +55,13 @@ func etcExecute(ls []LocalServices) { for _, service := range ls { yamlUrl := fmt.Sprintf("%s%s/%s/%s_%s.yaml", service.Srv.OssUrl, service.Srv.EtcPath, env.MeshEnv.Workspace, service.Srv.ServiceKey, env.MeshEnv.RuntimeMode) body, err := utils.HttpGet(yamlUrl) - checkError(err) - fmt.Println("Check YAML Configure:", yamlUrl, " [OK]") - yamlPath := filepath.Join(env.MeshEnv.Prefix, service.Srv.EtcPath, fmt.Sprintf("%s_%s.yaml", service.Srv.ServiceKey, env.MeshEnv.RuntimeMode)) - utils.StringToFile(yamlPath, string(body)) + if err != nil { + fmt.Println("Check YAML Configure:", yamlUrl, " [ERROR]", err.Error()) + } else { + fmt.Println("Check YAML Configure:", yamlUrl, " [OK]") + yamlPath := filepath.Join(env.MeshEnv.Prefix, service.Srv.EtcPath, fmt.Sprintf("%s_%s.yaml", service.Srv.ServiceKey, env.MeshEnv.RuntimeMode)) + utils.StringToFile(yamlPath, string(body)) + } } } @@ -66,15 +69,26 @@ func getLocalServices() []LocalServices { reles := getRegistryReleases() dirFs, err := os.ReadDir(env.MeshEnv.Prefix) - checkError(err) ls := make([]LocalServices, 0) - for _, v := range dirFs { - version := getCurrentVersion(v.Name()) - if version != " - " { - for _, srv := range reles.Data { + + checkError(err) + + for _, v := range reles.Data { + var check bool = false + for _, srv := range dirFs { + if v.ServiceKey == srv.Name() { + check = true + break + } + } + + if check { + version := getCurrentVersion(v.ServiceKey) + if version != " - " { + ls = append(ls, LocalServices{ - Srv: srv, + Srv: v, LocalVersion: version, }) } From 89e10173c2bca1938de96f2bdda2ed1d546e0246 Mon Sep 17 00:00:00 2001 From: yanweidong Date: Thu, 21 Mar 2024 18:17:51 +0800 Subject: [PATCH 6/9] fix --- cmd/commands/ctl.go | 2 +- cmd/commands/install.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/commands/ctl.go b/cmd/commands/ctl.go index caf42c5..af8f43a 100644 --- a/cmd/commands/ctl.go +++ b/cmd/commands/ctl.go @@ -10,7 +10,7 @@ import ( func getFilePath(srv *Service) (binPath, logsPath string) { binPath = filepath.Join(env.MeshEnv.Prefix, srv.ServiceKey) - logsPath = filepath.Join(env.MeshEnv.Prefix, "logs", srv.ServiceKey+"@"+srv.Version+".log") + logsPath = filepath.Join(env.MeshEnv.Prefix, "logs", srv.ServiceKey+"-"+srv.Version+".log") return } diff --git a/cmd/commands/install.go b/cmd/commands/install.go index aabdb88..f465ff1 100644 --- a/cmd/commands/install.go +++ b/cmd/commands/install.go @@ -64,7 +64,7 @@ func installExecute(srv string) { DownloadFile(downUrl, binPath, func(length, downLen int64) { fmt.Fprintf(os.Stdout, "Total:%d KB, Current:%d KB, Percent:%.2f%%\r", length, downLen, (float32(downLen)/float32(length))*100) }) - + fmt.Println("[6/6] Download Success!") fmt.Println("[6/6] Restart Microservice:", srv) service.Start() fmt.Println("Install Successful!") From 65cdca43c12eccb72d8970a02bc7c76e92dfc9ff Mon Sep 17 00:00:00 2001 From: yanweidong Date: Thu, 21 Mar 2024 18:19:05 +0800 Subject: [PATCH 7/9] fix --- cmd/commands/check.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/commands/check.go b/cmd/commands/check.go index 2e96683..9f1c657 100644 --- a/cmd/commands/check.go +++ b/cmd/commands/check.go @@ -120,7 +120,7 @@ func serviceExecute(ls []LocalServices) { } else { service.Srv.Stop() downUrl := service.Srv.OssUrl + service.Srv.FilePath - fmt.Println("Check Microservice", service.Srv.ServiceKey, service.Srv.Version, downUrl) + fmt.Println("Update Microservice", service.Srv.ServiceKey, service.Srv.Version, downUrl) binPath := filepath.Join(env.MeshEnv.Prefix, service.Srv.ServiceKey) DownloadFile(downUrl, binPath, func(length, downLen int64) { fmt.Fprintf(os.Stdout, "Total:%d KB, Current:%d KB, Percent:%.2f%%\r", length, downLen, (float32(downLen)/float32(length))*100) From 78dbac4b2e0e79e25974ad5de64ee17031dabdd0 Mon Sep 17 00:00:00 2001 From: yanweidong Date: Fri, 22 Mar 2024 16:19:31 +0800 Subject: [PATCH 8/9] dev --- cmd/commands/restart.go | 72 +++++++++++++++++++++++++++++++++++++++++ cmd/commands/root.go | 2 +- cmd/commands/update.go | 2 +- 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 cmd/commands/restart.go diff --git a/cmd/commands/restart.go b/cmd/commands/restart.go new file mode 100644 index 0000000..5b8ec8d --- /dev/null +++ b/cmd/commands/restart.go @@ -0,0 +1,72 @@ +package commands + +import ( + "fmt" + "os" + "path/filepath" + + "git.apinb.com/bsm-sdk/engine/env" + "git.apinb.com/bsm-sdk/engine/utils" + "github.com/spf13/cobra" +) + +var restartCmd = &cobra.Command{ + Use: "restart", + Short: " 更新一个微服务服务,并重启该服务.", + Run: func(cmd *cobra.Command, args []string) { + if len(args) != 1 { + fmt.Println("Update a microservice service and restart it!") + return + } + + srv := args[0] + execbin := filepath.Join(env.MeshEnv.Prefix, srv) + binExists := utils.PathExists(execbin) + if !binExists { + fmt.Println(srv, "microservice not install") + } else { + restartExecute(srv) + } + }, +} + +func restartExecute(srv string) { + localVersion := getCurrentVersion(srv) + fmt.Println("[1/5] Check local microservice version:", localVersion) + + service := getService(srv) + if service == nil { + fmt.Println("ERR:", srv, "Not Found!") + os.Exit(0) + } + + fmt.Println("[2/5] Check registry microservice version:", service.Version) + + service.Stop() + + yamlUrl := fmt.Sprintf("%s%s/%s/%s_%s.yaml", service.OssUrl, service.EtcPath, env.MeshEnv.Workspace, srv, env.MeshEnv.RuntimeMode) + body, err := utils.HttpGet(yamlUrl) + checkError(err) + fmt.Println("[3/5] Download YAML Configure:", yamlUrl, " [OK]") + yamlPath := filepath.Join(env.MeshEnv.Prefix, service.EtcPath, fmt.Sprintf("%s_%s.yaml", srv, env.MeshEnv.RuntimeMode)) + utils.StringToFile(yamlPath, string(body)) + + if localVersion != service.Version { + downUrl := service.OssUrl + service.FilePath + fmt.Println("[4/5] Download Binrary File:", downUrl) + binPath := filepath.Join(env.MeshEnv.Prefix, srv) + DownloadFile(downUrl, binPath, func(length, downLen int64) { + fmt.Fprintf(os.Stdout, "Total:%d KB, Current:%d KB, Percent:%.2f%%\r", length, downLen, (float32(downLen)/float32(length))*100) + }) + + fmt.Println("[5/5] Start Microservice:", srv) + service.Start() + fmt.Println("Updated Success!") + } else { + fmt.Println("[4/5] "+srv+" Already the latest version:", localVersion) + fmt.Println("[5/5] Restart Microservice:", srv) + service.Start() + fmt.Println("Restart Success!") + } + +} diff --git a/cmd/commands/root.go b/cmd/commands/root.go index e06ce12..80fe3b5 100644 --- a/cmd/commands/root.go +++ b/cmd/commands/root.go @@ -22,7 +22,7 @@ func init() { registryUrl = env.GetEnvDefault("BlocksMesh_Registry", "http://registry.apinb.com") } - rootCmd.AddCommand(psCmd, installCmd, updateCmd, checkCmd) + rootCmd.AddCommand(psCmd, installCmd, updateCmd, checkCmd, restartCmd) } var rootCmd = &cobra.Command{ diff --git a/cmd/commands/update.go b/cmd/commands/update.go index 75304a1..c525056 100644 --- a/cmd/commands/update.go +++ b/cmd/commands/update.go @@ -61,7 +61,7 @@ func updateExecute(srv string) { fmt.Println("[5/5] Start Microservice:", srv) service.Start() - fmt.Println("Install Successful!") + fmt.Println("Updated Success!") } } From 2a0d754557e20ca57e0512884c557144c8cb010d Mon Sep 17 00:00:00 2001 From: yanweidong Date: Fri, 22 Mar 2024 16:19:56 +0800 Subject: [PATCH 9/9] dev --- cmd/commands/restart.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/commands/restart.go b/cmd/commands/restart.go index 5b8ec8d..8de1ec3 100644 --- a/cmd/commands/restart.go +++ b/cmd/commands/restart.go @@ -12,7 +12,7 @@ import ( var restartCmd = &cobra.Command{ Use: "restart", - Short: " 更新一个微服务服务,并重启该服务.", + Short: " 更新一个微服务的服务,并重启该服务.", Run: func(cmd *cobra.Command, args []string) { if len(args) != 1 { fmt.Println("Update a microservice service and restart it!")