diff --git a/backend/constants/message.go b/backend/constants/message.go index 34974e1e..89701b33 100644 --- a/backend/constants/message.go +++ b/backend/constants/message.go @@ -2,6 +2,6 @@ package constants const ( MsgTypeGetLog = "get-log" - MsgTypeGetEnv = "get-env" + MsgTypeGetSystemInfo = "get-sys-info" MsgTypeCancelTask = "cancel-task" ) diff --git a/backend/main.go b/backend/main.go index 1b4a29c1..5ee066e7 100644 --- a/backend/main.go +++ b/backend/main.go @@ -83,6 +83,7 @@ func main() { app.GET("/nodes/:id", routes.GetNode) // 节点详情 app.POST("/nodes/:id", routes.PostNode) // 修改节点 app.GET("/nodes/:id/tasks", routes.GetNodeTaskList) // 节点任务列表 + app.GET("/nodes/:id/system", routes.GetSystemInfo) // 节点任务列表 // 爬虫 app.GET("/spiders", routes.GetSpiderList) // 爬虫列表 app.GET("/spiders/:id", routes.GetSpider) // 爬虫详情 diff --git a/backend/model/system.go b/backend/model/system.go new file mode 100644 index 00000000..c4865a24 --- /dev/null +++ b/backend/model/system.go @@ -0,0 +1,15 @@ +package model + +type SystemInfo struct { + ARCH string `json:"arch"` + OS string `json:"os"` + Hostname string `json:"host_name"` + NumCpu int `json:"num_cpu"` + Executables []Executable `json:"executables"` +} + +type Executable struct { + Path string `json:"path"` + FileName string `json:"file_name"` + DisplayName string `json:"display_name"` +} diff --git a/backend/routes/node.go b/backend/routes/node.go index c2d7d66c..cc30cae5 100644 --- a/backend/routes/node.go +++ b/backend/routes/node.go @@ -91,3 +91,13 @@ func GetNodeTaskList(c *gin.Context) { Data: tasks, }) } + +func GetSystemInfo(c *gin.Context) { + sysInfo, _ := services.GetSystemInfo() + + c.JSON(http.StatusOK, Response{ + Status: "ok", + Message: "success", + Data: sysInfo, + }) +} diff --git a/backend/services/system.go b/backend/services/system.go new file mode 100644 index 00000000..6fc67102 --- /dev/null +++ b/backend/services/system.go @@ -0,0 +1,91 @@ +package services + +import ( + "crawlab/model" + "io/ioutil" + "os" + "path/filepath" + "runtime" + "strings" +) + +var executableNameMap = map[string]string{ + // python + "python": "Python", + "python2": "Python 2", + "python2.7": "Python 2.7", + "python3": "Python 3", + "python3.5": "Python 3.5", + "python3.6": "Python 3.6", + "python3.7": "Python 3.7", + "python3.8": "Python 3.8", + // java + "java": "Java", + // go + "go": "Go", + // node + "node": "NodeJS", + // windows command + "cmd": "Windows Command Prompt", + // linux shell + "sh": "Shell", + "bash": "bash", +} + +func GetSystemEnv(key string) string { + return os.Getenv(key) +} + +func GetPathValues() (paths []string) { + pathEnv := GetSystemEnv("PATH") + return strings.Split(pathEnv, ":") +} + +func GetExecutables() (executables []model.Executable, err error) { + pathValues := GetPathValues() + + cache := map[string]string{} + + for _, path := range pathValues { + fileList, err := ioutil.ReadDir(path) + if err != nil { + return executables, err + } + + for _, file := range fileList { + displayName := executableNameMap[file.Name()] + filePath := filepath.Join(path, file.Name()) + + if cache[filePath] == "" { + if displayName != "" { + executables = append(executables, model.Executable{ + Path: filePath, + FileName: file.Name(), + DisplayName: displayName, + }) + } + cache[filePath] = filePath + } + } + } + return executables, nil +} + +func GetSystemInfo() (sysInfo model.SystemInfo, err error) { + executables, err := GetExecutables() + if err != nil { + return sysInfo, err + } + hostname, err := os.Hostname() + if err != nil { + return sysInfo, err + } + + return model.SystemInfo{ + ARCH: runtime.GOARCH, + OS: runtime.GOOS, + NumCpu: runtime.GOMAXPROCS(0), + Hostname: hostname, + Executables: executables, + }, nil +}