68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
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 updateCmd = &cobra.Command{
|
|
Use: "update",
|
|
Short: "<ServiceKey> 更新一个微服务至最新版本.",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) != 1 {
|
|
fmt.Println("Please input the name of the microservice to be updated!")
|
|
return
|
|
}
|
|
|
|
srv := args[0]
|
|
execbin := filepath.Join(env.MeshEnv.Prefix, srv)
|
|
binExists := utils.PathExists(execbin)
|
|
if !binExists {
|
|
fmt.Println(srv, "microservice not install")
|
|
} else {
|
|
updateExecute(srv)
|
|
}
|
|
},
|
|
}
|
|
|
|
func updateExecute(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)
|
|
|
|
if localVersion != 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))
|
|
|
|
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!")
|
|
}
|
|
|
|
}
|