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 84% rename from cmd/check.go rename to cmd/commands/check.go index 861a452..9f1c657 100644 --- a/cmd/check.go +++ b/cmd/commands/check.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "fmt" @@ -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, }) } @@ -106,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) diff --git a/cmd/ctl.go b/cmd/commands/ctl.go similarity index 95% rename from cmd/ctl.go rename to cmd/commands/ctl.go index c1e19c2..af8f43a 100644 --- a/cmd/ctl.go +++ b/cmd/commands/ctl.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "fmt" @@ -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/ext.go b/cmd/commands/ext.go similarity index 99% rename from cmd/ext.go rename to cmd/commands/ext.go index 8548584..b159dd1 100644 --- a/cmd/ext.go +++ b/cmd/commands/ext.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "encoding/json" diff --git a/cmd/install.go b/cmd/commands/install.go similarity index 97% rename from cmd/install.go rename to cmd/commands/install.go index a8e6723..f465ff1 100644 --- a/cmd/install.go +++ b/cmd/commands/install.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "fmt" @@ -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!") diff --git a/cmd/ps.go b/cmd/commands/ps.go similarity index 99% rename from cmd/ps.go rename to cmd/commands/ps.go index 56c0582..63bcdb8 100644 --- a/cmd/ps.go +++ b/cmd/commands/ps.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "encoding/json" diff --git a/cmd/commands/restart.go b/cmd/commands/restart.go new file mode 100644 index 0000000..8de1ec3 --- /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/root.go b/cmd/commands/root.go similarity index 89% rename from cmd/root.go rename to cmd/commands/root.go index 85f956e..80fe3b5 100644 --- a/cmd/root.go +++ b/cmd/commands/root.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "fmt" @@ -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/types.go b/cmd/commands/types.go new file mode 100644 index 0000000..cdff10d --- /dev/null +++ b/cmd/commands/types.go @@ -0,0 +1 @@ +package commands diff --git a/cmd/update.go b/cmd/commands/update.go similarity index 97% rename from cmd/update.go rename to cmd/commands/update.go index ec50769..c525056 100644 --- a/cmd/update.go +++ b/cmd/commands/update.go @@ -1,4 +1,4 @@ -package cmd +package commands import ( "fmt" @@ -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!") } } diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..d2de3a2 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + + "git.apinb.com/bsm-sdk/engine/vars" + "git.apinb.com/bsm-tools/bsm/cmd/commands" +) + +func main() { + fmt.Printf("Blocks Service Cli Version: %s \n", vars.VERSION) + + commands.Execute() +} diff --git a/cmd/types.go b/cmd/types.go deleted file mode 100644 index 1d619dd..0000000 --- a/cmd/types.go +++ /dev/null @@ -1 +0,0 @@ -package cmd 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() -}