From a4575bf620d06f7927626d119ee4df1a9840c609 Mon Sep 17 00:00:00 2001 From: aaron <462826@qq.com> Date: Thu, 17 Dec 2020 11:50:43 +0800 Subject: [PATCH] close task#8649 --- src/action/bug.go | 2 +- src/action/ztfResult.go | 2 +- src/server/model/sysInfo.go | 2 + src/server/server.go | 14 +++++- src/server/utils/{ => common}/request.go | 0 src/server/utils/common/sys.go | 61 +++++++++++++++++++++++- src/server/utils/const/const.go | 2 + src/utils/const/const.go | 2 + src/utils/file/file.go | 8 ++-- src/utils/vari/var.go | 7 +-- src/utils/zentao/zentao.go | 2 +- src/ztf.go | 2 + 12 files changed, 92 insertions(+), 12 deletions(-) rename src/server/utils/{ => common}/request.go (100%) diff --git a/src/action/bug.go b/src/action/bug.go index f67d8352..9c0d278d 100644 --- a/src/action/bug.go +++ b/src/action/bug.go @@ -23,7 +23,7 @@ func CommitBug(files []string) { } else { stdinUtils.InputForDir(&resultDir, "", "result") } - resultDir = fileUtils.UpdateDir(resultDir) + resultDir = fileUtils.AddPathSepIfNeeded(resultDir) report := testingService.GetZTFTestReportForSubmit(resultDir) diff --git a/src/action/ztfResult.go b/src/action/ztfResult.go index 6b97d100..c57948b3 100644 --- a/src/action/ztfResult.go +++ b/src/action/ztfResult.go @@ -14,6 +14,6 @@ func CommitZTFTestResult(files []string, productId string, taskId string, noNeed stdinUtils.InputForDir(&resultDir, "", "result") } - resultDir = fileUtils.UpdateDir(resultDir) + resultDir = fileUtils.AddPathSepIfNeeded(resultDir) zentaoService.CommitZTFTestResult(resultDir, productId, taskId, noNeedConfirm) } diff --git a/src/server/model/sysInfo.go b/src/server/model/sysInfo.go index 12811ea5..fecf1fcf 100644 --- a/src/server/model/sysInfo.go +++ b/src/server/model/sysInfo.go @@ -1,6 +1,8 @@ package serverModel type SysInfo struct { + AgentDir string `json:"agentDir"` + SysArch string `json:"sysArch"` SysCores int `json:"sysCores"` diff --git a/src/server/server.go b/src/server/server.go index f238e76f..1fad624b 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -5,7 +5,10 @@ import ( "fmt" serverModel "github.com/easysoft/zentaoatf/src/server/model" "github.com/easysoft/zentaoatf/src/server/service" - serverUtils "github.com/easysoft/zentaoatf/src/server/utils" + serverUtils "github.com/easysoft/zentaoatf/src/server/utils/common" + serverConst "github.com/easysoft/zentaoatf/src/server/utils/const" + constant "github.com/easysoft/zentaoatf/src/utils/const" + fileUtils "github.com/easysoft/zentaoatf/src/utils/file" "github.com/easysoft/zentaoatf/src/utils/vari" "io" "io/ioutil" @@ -27,6 +30,15 @@ func NewServer() *Server { return &Server{commonService: commonService, agentService: agentService, cronService: cronService} } +func (s *Server) Init() { + if vari.AgentDir != "" { + return + } + + home, _ := serverUtils.GetUserHome() + vari.AgentDir = fileUtils.AddPathSepIfNeeded(home + constant.PthSep + serverConst.AgentDir) +} + func (s *Server) Run() { httpServer := &http.Server{ Addr: fmt.Sprintf(":%d", vari.Port), diff --git a/src/server/utils/request.go b/src/server/utils/common/request.go similarity index 100% rename from src/server/utils/request.go rename to src/server/utils/common/request.go diff --git a/src/server/utils/common/sys.go b/src/server/utils/common/sys.go index 59891abe..3c434163 100644 --- a/src/server/utils/common/sys.go +++ b/src/server/utils/common/sys.go @@ -1,13 +1,20 @@ -package commonUtils +package serverUtils import ( + "bytes" + "errors" serverModel "github.com/easysoft/zentaoatf/src/server/model" + "github.com/easysoft/zentaoatf/src/utils/vari" "os" + "os/exec" + "os/user" "runtime" "strings" ) func GetSysInfo() (info serverModel.SysInfo) { + info.AgentDir = vari.AgentDir + info.SysArch = runtime.GOARCH info.SysCores = runtime.GOMAXPROCS(0) @@ -23,3 +30,55 @@ func GetSysInfo() (info serverModel.SysInfo) { return } + +func GetUserHome() (string, error) { + user, err := user.Current() + if nil == err { + return user.HomeDir, nil + } + + // cross compile support + + if "windows" == runtime.GOOS { + return homeWindows() + } + + // Unix-like system, so just assume Unix + return homeUnix() +} + +func homeUnix() (string, error) { + // First prefer the HOME environmental variable + if home := os.Getenv("HOME"); home != "" { + return home, nil + } + + // If that fails, try the shell + var stdout bytes.Buffer + cmd := exec.Command("sh", "-c", "eval echo ~$USER") + cmd.Stdout = &stdout + if err := cmd.Run(); err != nil { + return "", err + } + + result := strings.TrimSpace(stdout.String()) + if result == "" { + return "", errors.New("blank output when reading home directory") + } + + return result, nil +} + +func homeWindows() (string, error) { + drive := os.Getenv("HOMEDRIVE") + path := os.Getenv("HOMEPATH") + home := drive + path + if drive == "" || path == "" { + home = os.Getenv("USERPROFILE") + } + if home == "" { + return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank") + } + + return home, nil +} diff --git a/src/server/utils/const/const.go b/src/server/utils/const/const.go index 5b2c05fc..37a52f47 100644 --- a/src/server/utils/const/const.go +++ b/src/server/utils/const/const.go @@ -2,4 +2,6 @@ package serverConst const ( HeartBeatInterval = 5 + + AgentDir = "ztf_agent" ) diff --git a/src/utils/const/const.go b/src/utils/const/const.go index e840c8e0..41778f6b 100644 --- a/src/utils/const/const.go +++ b/src/utils/const/const.go @@ -6,6 +6,8 @@ import ( ) var ( + PthSep = string(os.PathSeparator) + ConfigVer = 1 ConfigFile = fmt.Sprintf("conf%sztf.conf", string(os.PathSeparator)) diff --git a/src/utils/file/file.go b/src/utils/file/file.go index 4ad23d8b..0f7a42f0 100644 --- a/src/utils/file/file.go +++ b/src/utils/file/file.go @@ -78,7 +78,7 @@ func AbosutePath(pth string) string { pth, _ = filepath.Abs(pth) } - pth = UpdateDir(pth) + pth = AddPathSepIfNeeded(pth) return pth } @@ -88,7 +88,7 @@ func IsAbosutePath(pth string) bool { strings.Index(pth, ":") == 1 // windows } -func UpdateDir(pth string) string { +func AddPathSepIfNeeded(pth string) string { sepa := string(os.PathSeparator) if strings.LastIndex(pth, sepa) < len(pth)-1 { @@ -148,7 +148,7 @@ func GetZTFDir() string { // where ztf command in } dir, _ = filepath.Abs(dir) - dir = UpdateDir(dir) + dir = AddPathSepIfNeeded(dir) //fmt.Printf("Debug: Launch %s in %s \n", arg1, dir) return dir @@ -195,7 +195,7 @@ func GetLogDir() string { ret := getLogNumb(numb + 1) - return UpdateDir(path + ret) + return AddPathSepIfNeeded(path + ret) } func getLogNumb(numb int) string { diff --git a/src/utils/vari/var.go b/src/utils/vari/var.go index 16b2d9d0..599f9090 100644 --- a/src/utils/vari/var.go +++ b/src/utils/vari/var.go @@ -40,7 +40,8 @@ var ( Interpreter string // server - RunMode string - IP string - Port int + RunMode string + IP string + Port int + AgentDir string ) diff --git a/src/utils/zentao/zentao.go b/src/utils/zentao/zentao.go index 56768f95..3659d518 100644 --- a/src/utils/zentao/zentao.go +++ b/src/utils/zentao/zentao.go @@ -290,7 +290,7 @@ func ReadCaseId(content string) string { } func GetDependentExpect(file string) (bool, string) { - dir := fileUtils.UpdateDir(filepath.Dir(file)) + dir := fileUtils.AddPathSepIfNeeded(filepath.Dir(file)) name := strings.Replace(filepath.Base(file), path.Ext(file), ".exp", -1) expectIndependentFile := dir + name diff --git a/src/ztf.go b/src/ztf.go index 28f83ca2..b4d22fb2 100644 --- a/src/ztf.go +++ b/src/ztf.go @@ -75,6 +75,7 @@ func main() { flagSet.IntVar(&vari.Port, "P", 8848, "") flagSet.IntVar(&vari.Port, "port", 8848, "") + flagSet.StringVar(&vari.AgentDir, "w", "", "") var placeholder string flagSet.StringVar(&placeholder, "h", "", "") @@ -227,6 +228,7 @@ func startServer() { logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("start_server", vari.IP, strconv.Itoa(vari.Port)), color.FgCyan) server := server.NewServer() + server.Init() server.Run() return -- GitLab