提交 0937d2b7 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

upgrade

上级 c705af98
......@@ -9,7 +9,7 @@ import (
func RunUnitTest(cmdStr string) string {
startTime := time2.Now().Unix()
shellUtils.ExeShellWithOutput(cmdStr)
shellUtils.ExeAppWithOutput(cmdStr)
endTime := time2.Now().Unix()
testSuites, resultDir := testingService.RetrieveUnitResult()
......
......@@ -158,7 +158,7 @@ func runCases(cases []string) {
var report = model.TestReport{Env: commonUtils.GetOs(),
Pass: 0, Fail: 0, Total: 0, FuncResult: make([]model.FuncResult, 0)}
report.TestType = "func"
report.TestFrame = "ztf"
report.TestFrame = constant.AppName
pathMaxWidth := 0
numbMaxWidth := 0
......
......@@ -5,9 +5,16 @@ import (
serverUtils "github.com/easysoft/zentaoatf/src/server/utils/common"
serverConst "github.com/easysoft/zentaoatf/src/server/utils/const"
commonUtils "github.com/easysoft/zentaoatf/src/utils/common"
configUtils "github.com/easysoft/zentaoatf/src/utils/config"
constant "github.com/easysoft/zentaoatf/src/utils/const"
fileUtils "github.com/easysoft/zentaoatf/src/utils/file"
logUtils "github.com/easysoft/zentaoatf/src/utils/log"
"github.com/easysoft/zentaoatf/src/utils/vari"
"github.com/fatih/color"
"github.com/mholt/archiver/v3"
"github.com/sirupsen/logrus"
"os"
"os/exec"
"strconv"
"strings"
)
......@@ -28,12 +35,16 @@ func (s *UpgradeService) CheckUpgrade() {
content := strings.TrimSpace(fileUtils.ReadFile(pth))
version, _ := strconv.ParseFloat(content, 64)
if vari.Config.Version < version {
s.Upgrade(version)
versionStr := fmt.Sprintf("%.1f", version)
err := s.DownloadVersion(versionStr)
if err == nil {
s.RestartVersion(versionStr)
}
}
}
func (s *UpgradeService) Upgrade(ver float64) (err error) {
version := fmt.Sprintf("%.1f", ver)
func (s *UpgradeService) DownloadVersion(version string) (err error) {
os := commonUtils.GetOs()
if commonUtils.IsWin() {
......@@ -46,9 +57,59 @@ func (s *UpgradeService) Upgrade(ver float64) (err error) {
err = serverUtils.Download(url, pth)
if err == nil {
fileUtils.RmDir(dir)
fileUtils.MkDirIfNeeded(dir)
archiver.Unarchive(pth, dir)
err = archiver.Unarchive(pth, dir)
}
return
}
func (s *UpgradeService) RestartVersion(version string) (err error) {
currExePath := vari.ZTFDir + constant.AppName
bakExePath := currExePath + "_bak"
newExePath := vari.AgentLogDir + version + constant.PthSep + constant.AppName + constant.PthSep + constant.AppName
if commonUtils.IsWin() {
currExePath += ".exe"
bakExePath += ".exe"
newExePath += ".exe"
}
logrus.Println(currExePath)
if !vari.IsDebug {
err = os.Rename(currExePath, bakExePath)
}
_, err = fileUtils.CopyFile(newExePath, currExePath)
cmdStr := constant.AppName
var cmd *exec.Cmd
if commonUtils.IsWin() {
cmdStr += ".exe"
cmd = exec.Command("cmd", "/C", cmdStr)
} else {
cmd = exec.Command("/bin/bash", "-c", cmdStr)
}
cmd.Dir = vari.ZTFDir
err = cmd.Start()
if err != nil {
logUtils.PrintToWithColor("fail to start new app, err: "+err.Error(), color.FgRed)
return
}
err = cmd.Wait()
if err != nil {
logUtils.PrintToWithColor("fail to start new app, err: "+err.Error(), color.FgRed)
return
} else {
logUtils.PrintToWithColor("success to start update to new version "+version, color.FgCyan)
vari.Config.Version, _ = strconv.ParseFloat(version, 64)
configUtils.SaveConfig(vari.Config)
}
os.Exit(0)
return
}
package serverConst
import constant "github.com/easysoft/zentaoatf/src/utils/const"
const (
HeartBeatInterval = 5
CheckUpgradeInterval = 5
......@@ -7,7 +9,7 @@ const (
AgentRunTime = 30 * 60
AgentLogDir = "log-agent"
QiNiuURL = "https://dl.cnezsoft.com/ztf/"
QiNiuURL = "https://dl.cnezsoft.com/" + constant.AppName + "/"
AgentUpgradeURL = QiNiuURL + "version.txt"
AgentDownloadURL = QiNiuURL + "%s/%s/ztf.zip"
AgentDownloadURL = QiNiuURL + "%s/%s/" + constant.AppName + ".zip"
)
......@@ -57,7 +57,7 @@ func GetAllScriptsInDir(path string, files *[]string) error {
for _, fi := range dir {
name := fi.Name()
if commonUtils.IngoreFile(name) {
if commonUtils.IgnoreFile(name) {
continue
}
......@@ -85,7 +85,7 @@ func GetScriptByIdsInDir(dirPth string, idMap map[int]string, files *[]string) e
sep := string(os.PathSeparator)
if commonUtils.IngoreFile(dirPth) {
if commonUtils.IgnoreFile(dirPth) {
return nil
}
......
......@@ -73,7 +73,7 @@ func AddSlashForUrl(url string) string {
return url
}
func IngoreFile(path string) bool {
func IgnoreFile(path string) bool {
path = filepath.Base(path)
if strings.Index(path, ".") == 0 ||
......
......@@ -21,7 +21,7 @@ import (
)
func InitConfig() {
vari.ZTFDir = fileUtils.GetZTFDir()
vari.ZTFDir, vari.IsDebug = fileUtils.GetZTFDir()
CheckConfigPermission()
vari.ConfigPath = vari.ZTFDir + constant.ConfigFile
......
......@@ -5,13 +5,17 @@ import (
"os"
)
const (
AppName = "ztf"
ConfigVer = 1.0
)
var (
PthSep = string(os.PathSeparator)
ConfigVer = 1.0
ConfigFile = fmt.Sprintf("conf%sztf.conf", string(os.PathSeparator))
ConfigFile = fmt.Sprintf("conf%s%s.conf", string(os.PathSeparator), AppName)
UrlZentaoSettings = "zentaoSettings"
UrlZenTaoSettings = "zentaoSettings"
UrlImportProject = "importProject"
UrlSubmitResult = "submitResults"
UrlReportBug = "reportBug"
......
......@@ -17,7 +17,7 @@ func GetScreenSize() (int, int) {
if commonUtils.IsWin() {
cmd = "mode" // tested for win7
out, _ := shellUtils.ExeShell(cmd)
out, _ := shellUtils.ExeSysCmd(cmd)
//out := `设备状态 CON:
// ---------
......
......@@ -150,25 +150,25 @@ func ReadResData(path string) string {
return jsonStr
}
func GetZTFDir() string { // where ztf command in
var dir string
func GetZTFDir() (dir string, isDebug bool) { // where ztf command in
arg1 := strings.ToLower(os.Args[0])
name := filepath.Base(arg1)
if strings.Index(name, "ztf") == 0 && strings.Index(arg1, "go-build") < 0 {
if strings.Index(name, constant.AppName) == 0 && strings.Index(arg1, "go-build") < 0 {
p, _ := exec.LookPath(os.Args[0])
if strings.Index(p, string(os.PathSeparator)) > -1 {
dir = p[:strings.LastIndex(p, string(os.PathSeparator))]
}
} else { // debug
dir, _ = os.Getwd()
isDebug = true
}
dir, _ = filepath.Abs(dir)
dir = AddPathSepIfNeeded(dir)
//fmt.Printf("Debug: Launch %s in %s \n", arg1, dir)
return dir
return
}
func GetLogDir() string {
......
......@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
commonUtils "github.com/easysoft/zentaoatf/src/utils/common"
constant "github.com/easysoft/zentaoatf/src/utils/const"
fileUtils "github.com/easysoft/zentaoatf/src/utils/file"
"github.com/fatih/color"
"os"
......@@ -20,7 +21,7 @@ func PrintUsage() {
PrintToWithColor("Usage: ", color.FgCyan)
usage := fileUtils.ReadResData(usageFile)
exeFile := "ztf"
exeFile := constant.AppName
if commonUtils.IsWin() {
exeFile += ".exe"
}
......@@ -33,8 +34,8 @@ func PrintUsage() {
regx, _ := regexp.Compile(`\\`)
sample = regx.ReplaceAllString(sample, "/")
regx, _ = regexp.Compile(`ztf.exe`)
sample = regx.ReplaceAllString(sample, "ztf")
regx, _ = regexp.Compile(constant.AppName + `.exe`)
sample = regx.ReplaceAllString(sample, constant.AppName)
regx, _ = regexp.Compile(`/bat/`)
sample = regx.ReplaceAllString(sample, "/shell/")
......
......@@ -16,7 +16,7 @@ import (
"strings"
)
func ExeShell(cmdStr string) (string, error) {
func ExeSysCmd(cmdStr string) (string, error) {
var cmd *exec.Cmd
if commonUtils.IsWin() {
cmd = exec.Command(cmdStr)
......@@ -31,8 +31,27 @@ func ExeShell(cmdStr string) (string, error) {
return out.String(), err
}
func ExeAppInDir(cmdStr string, dir string) (string, error) {
var cmd *exec.Cmd
if commonUtils.IsWin() {
cmd = exec.Command("cmd", "/C", cmdStr)
} else {
cmd = exec.Command("/bin/bash", "-c", cmdStr)
}
if dir != "" {
cmd.Dir = dir
}
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
return out.String(), err
}
func ExeShellWithOutput(cmdStr string) []string {
func ExeAppWithOutput(cmdStr string) []string {
var cmd *exec.Cmd
if commonUtils.IsWin() {
cmd = exec.Command("cmd", "/C", cmdStr)
......
......@@ -6,6 +6,7 @@ import (
)
var (
IsDebug bool
Config = model.Config{}
Cui *gocui.Gui
MainViewHeight int
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册