This commit is contained in:
david 2024-09-18 01:13:32 +08:00
commit 3849568b18
13 changed files with 131 additions and 30 deletions

8
.actions Normal file
View File

@ -0,0 +1,8 @@
# Actions 编译部署配置文件,文件格式:YAML
#
# 微服务相关配置
service:
origin: bsm
image: golang:1.22.0-bookworm
describe: system cli cmd

View File

@ -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)

View File

@ -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
}

View File

@ -1,4 +1,4 @@
package cmd
package commands
import (
"encoding/json"

View File

@ -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!")

View File

@ -1,4 +1,4 @@
package cmd
package commands
import (
"encoding/json"

72
cmd/commands/restart.go Normal file
View File

@ -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: "<ServiceKey> 更新一个微服务的服务,并重启该服务.",
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!")
}
}

View File

@ -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{

1
cmd/commands/types.go Normal file
View File

@ -0,0 +1 @@
package commands

View File

@ -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!")
}
}

14
cmd/main.go Normal file
View File

@ -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()
}

View File

@ -1 +0,0 @@
package cmd

View File

@ -1,7 +0,0 @@
package main
import "git.apinb.com/bsm-tools/bsm/cmd"
func main() {
cmd.Execute()
}