150 lines
4.3 KiB
Go
150 lines
4.3 KiB
Go
package cmd
|
|
|
|
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: "check etc/service/registry auto version update or install.",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 1 {
|
|
fmt.Println("Please input the name of the microservice to be updated!")
|
|
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)
|
|
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))
|
|
}
|
|
}
|
|
|
|
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 {
|
|
ls = append(ls, LocalServices{
|
|
Srv: srv,
|
|
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("Check 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()
|
|
|
|
}
|
|
}
|