bsm/cmd/commands/check.go

168 lines
4.7 KiB
Go

package commands
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"git.apinb.com/bsm-sdk/engine/env"
"git.apinb.com/bsm-sdk/engine/utils"
"github.com/spf13/cobra"
)
type LocalServices struct {
Srv Service
LocalVersion string
}
var checkCmd = &cobra.Command{
Use: "check",
Short: "<etc/service/registry> 批量检测微服务和配置并更新.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("check sub command: etc/service/registry")
fmt.Println(" etc: 检测并更新本地已经安装的所有微服务的配置文件")
fmt.Println(" service: 检测并更新本地已经安装的所有微服务")
fmt.Println(" registry: 检测并更新Registry已经发布的所有微服务")
return
}
subcmd := strings.ToLower(args[0])
switch subcmd {
case "etc":
// 检测并更新本地已经安装的所有微服务的配置文件
ls := getLocalServices()
etcExecute(ls)
case "service":
// 检测并更新本地已经安装的所有微服务
ls := getLocalServices()
serviceExecute(ls)
case "registry":
// 检测并更新Registry已经发布的所有微服务
rs := getRegistryServices()
registryExecute(rs)
default:
fmt.Println("check sub command: etc/server/registry ")
return
}
},
}
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)
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))
}
}
}
func getLocalServices() []LocalServices {
reles := getRegistryReleases()
dirFs, err := os.ReadDir(env.MeshEnv.Prefix)
ls := make([]LocalServices, 0)
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: v,
LocalVersion: version,
})
}
}
}
return ls
}
func getRegistryServices() []Service {
reles := getRegistryReleases()
rs := make([]Service, 0)
for _, srv := range reles.Data {
localVersion := getCurrentVersion(srv.ServiceKey)
if localVersion == " - " || srv.Version != localVersion {
rs = append(rs, srv)
}
}
return rs
}
func serviceExecute(ls []LocalServices) {
for _, service := range ls {
if service.LocalVersion == service.Srv.Version {
fmt.Println("Skip [", service.Srv.ServiceKey, "], already latest version :", service.Srv.Version)
} else {
service.Srv.Stop()
downUrl := service.Srv.OssUrl + service.Srv.FilePath
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)
})
service.Srv.Start()
}
}
}
func registryExecute(rs []Service) {
if !utils.PathExists(env.MeshEnv.Prefix) {
utils.CreateDir(env.MeshEnv.Prefix)
}
logsPath := path.Join(env.MeshEnv.Prefix, "logs")
if !utils.PathExists(logsPath) {
utils.CreateDir(logsPath)
}
etcPath := path.Join(env.MeshEnv.Prefix, "etc")
if !utils.PathExists(etcPath) {
utils.CreateDir(etcPath)
}
for _, service := range rs {
service.Stop()
yamlUrl := fmt.Sprintf("%s%s/%s/%s_%s.yaml", service.OssUrl, service.EtcPath, env.MeshEnv.Workspace, service.ServiceKey, env.MeshEnv.RuntimeMode)
body, err := utils.HttpGet(yamlUrl)
checkError(err)
fmt.Println("Found Registry YAML Configure:", yamlUrl, " [OK]")
yamlPath := filepath.Join(env.MeshEnv.Prefix, service.EtcPath, fmt.Sprintf("%s_%s.yaml", service.ServiceKey, env.MeshEnv.RuntimeMode))
utils.StringToFile(yamlPath, string(body))
downUrl := service.OssUrl + service.FilePath
fmt.Println("Found Registry Microservice:", service.ServiceKey, service.Version)
binPath := filepath.Join(env.MeshEnv.Prefix, service.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)
})
fmt.Println("Restart Microservice:", service.ServiceKey)
service.Start()
}
}