From 69bb2d222e4ca7bd0497a6f9832fdcb737df21a7 Mon Sep 17 00:00:00 2001 From: xzyaoi Date: Wed, 5 Dec 2018 21:33:47 +0800 Subject: [PATCH] some utilization for dashboard --- cli/daemon.go | 25 +++++++++++++++++++------ cli/handler.go | 4 ++++ cli/repository.go | 43 +++++++++++++++++++++++++++++++++++++++++++ cli/shell.go | 22 +++++++++++++++++++++- cli/utils.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+), 7 deletions(-) diff --git a/cli/daemon.go b/cli/daemon.go index b0271a312..620883d0b 100644 --- a/cli/daemon.go +++ b/cli/daemon.go @@ -41,10 +41,23 @@ func PostRepoHandler(c *gin.Context) { } // Handle Get Request -> Get Running Repo -func GetReposHandler(c *gin.Context) { +func GetRunningReposHandler(c *gin.Context) { c.JSON(http.StatusOK, RunningRepos) } +// Handle Get Request -> Get All Repos +func GetReposHandler(c *gin.Context) { + config := readConfig() + c.JSON(http.StatusOK, config.Repositories) +} + +// Handle Get Repository Meta Info +func GetRepoMetaHandler(c *gin.Context) { + vendor := c.Param("vendor") + name := c.Param("name") + c.JSON(http.StatusOK, GetMetaInfo(vendor, name)) +} + // Handle Socket Request func socketHandler(c *gin.Context) { socketServer.On("connection", func(so socketio.Socket) { @@ -54,15 +67,14 @@ func socketHandler(c *gin.Context) { } // write log to socket stream -func writeLog(filepath string, server *socketio.Server) { +func writeLog(filepath string, server *socketio.Server, eventName string) { log.Println("Writing Logs") t, err := tail.TailFile(filepath, tail.Config{Follow: true}) if err != nil { panic(err) } for line := range t.Lines { - log.Println("Broadcasting") - server.BroadcastTo("cvpm-webtail", "logevent", line.Text) + server.BroadcastTo("cvpm-webtail", eventName, line.Text) } } @@ -70,8 +82,8 @@ func writeLog(filepath string, server *socketio.Server) { func watchLogs(server *socketio.Server) { // System Log cvpmLogsLocation := getLogsLocation() - log.Println(cvpmLogsLocation) - go writeLog(filepath.Join(cvpmLogsLocation, "system.log"), server) + go writeLog(filepath.Join(cvpmLogsLocation, "system.log"), server, "system") + go writeLog(filepath.Join(cvpmLogsLocation, "package.log"), server, "package") } // global header @@ -104,6 +116,7 @@ func runServer(port string) { "daemon": "running", }) }) + r.GET("/repo/meta/:vendor/:name", GetRepoMetaHandler) r.POST("/repo", PostRepoHandler) r.GET("/repos", GetReposHandler) r.GET("/socket.io/", socketHandler) diff --git a/cli/handler.go b/cli/handler.go index 3dfe566f8..473af6e0b 100644 --- a/cli/handler.go +++ b/cli/handler.go @@ -100,6 +100,10 @@ func RepoHandler(c *cli.Context) { case "run": solverstring := c.Args().Get(1) runningPort := c.Args().Get(2) + if (runningPort == "") { + runningPort = findNextOpenPort(8080) + color.Red("No Running Port specified! Server will listen on: " + runningPort) + } runParams := strings.Split(solverstring, "/") color.Cyan("Running " + runParams[0] + "/" + runParams[1] + "/" + runParams[2]) requestParams := map[string]string{ diff --git a/cli/repository.go b/cli/repository.go index a7a75496b..679dc6fba 100644 --- a/cli/repository.go +++ b/cli/repository.go @@ -17,6 +17,14 @@ type Repository struct { Name string LocalFolder string Vendor string + Port string +} + +type RepositoryMetaInfo struct { + Config string + Dependency string + DiskSize float64 + Readme string } type solver struct { @@ -106,6 +114,7 @@ func InstallDependencies(localFolder string) { pip([]string{"install", "-r", filepath.Join(localFolder, "requirements.txt"), "--user"}) } +// Generating Runners for future use func GeneratingRunners(localFolder string) { var mySolvers solvers cvpmFile := filepath.Join(localFolder, "cvpm.toml") @@ -114,3 +123,37 @@ func GeneratingRunners(localFolder string) { } renderRunnerTpl(localFolder, mySolvers) } + +// After Installation + +// Return Repository Meta Info: Dependency, Config, Disk Size and Readme +func GetMetaInfo(Vendor string, Name string) RepositoryMetaInfo { + repos := readRepos() + repositoryMeta := RepositoryMetaInfo{} + for _, existed_repo := range repos { + if existed_repo.Name == Name && existed_repo.Vendor == Vendor { + // Read config file etc + byte_config, err := ioutil.ReadFile(filepath.Join(existed_repo.LocalFolder, "cvpm.toml")) + if err != nil { + repositoryMeta.Config = "Read cvpm.toml failed" + } else { + repositoryMeta.Config = string(byte_config) + } + byte_dependency, err := ioutil.ReadFile(filepath.Join(existed_repo.LocalFolder, "requirements.txt")) + if err != nil { + repositoryMeta.Dependency = "Read requirements.txt failed" + } else { + repositoryMeta.Dependency = string(byte_dependency) + } + byte_readme, err := ioutil.ReadFile(filepath.Join(existed_repo.LocalFolder, "README.md")) + if err != nil { + repositoryMeta.Readme = "Read Readme.md failed" + } else { + repositoryMeta.Readme = string(byte_readme) + } + packageSize := getDirSizeMB(existed_repo.LocalFolder) + repositoryMeta.DiskSize = packageSize + } + } + return repositoryMeta +} diff --git a/cli/shell.go b/cli/shell.go index 41523a7db..062b7020c 100644 --- a/cli/shell.go +++ b/cli/shell.go @@ -19,12 +19,14 @@ func pip(args []string) { proc := NewProcess(localPip, args...) out := proc.StreamOutput() logFile := filepath.Join(getLogsLocation(), "system.log") + log.Println(logFile) file, err := os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND, 0644) if err != nil { log.Fatalf("failed opening file: %s", err) } go func() { for out.Scan() { + log.Println(out.Text()) _, err := file.WriteString(out.Text() + "\n") if err != nil { log.Fatalf("failed writing to file: %s", err) @@ -39,7 +41,25 @@ func pip(args []string) { func python(args []string) { config := readConfig() localPython := config.Local.Python - _ = _execCommand(localPython, args) + //_ = _execCommand(localPython, args) + proc := NewProcess(localPython, args...) + out := proc.StreamOutput() + logFile := filepath.Join(getLogsLocation(), "package.log") + file, err := os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND, 0644) + if err != nil { + log.Fatalf("failed opening file: %s", err) + } + go func() { + for out.Scan() { + _, err := file.WriteString(out.Text() + "\n") + if err != nil { + log.Fatalf("failed writing to file: %s", err) + } + defer file.Close() + log.Println(out.Text()) + } + }() + proc.Start() } func _execCommand(commandName string, params []string) bool { diff --git a/cli/utils.go b/cli/utils.go index df60e0f6f..090cf1389 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -1,8 +1,16 @@ package main import ( + "strconv" + "net" "os" "os/user" + "path/filepath" + "time" +) + +const ( + defaultTimeout = 5 * time.Second ) func isExists(path string) bool { @@ -35,3 +43,38 @@ func isRoot() bool { return false } } + +func getDirSizeMB(path string) float64 { + var dirSize int64 = 0 + readSize := func(path string, file os.FileInfo, err error) error { + if !file.IsDir() { + dirSize += file.Size() + } + return nil + } + filepath.Walk(path, readSize) + sizeMB := float64(dirSize) / 1024.0 / 1024.0 + return sizeMB +} + +func isPortOpen(port string, timeout time.Duration) bool { + conn, _ := net.DialTimeout("tcp", net.JoinHostPort("", port), timeout) + if conn != nil { + conn.Close() + return false + } else { + return true + } +} + +func findNextOpenPort(port int) string { + var hasFound bool = false + var str_port string + for ; !hasFound; port++ { + str_port = strconv.Itoa(port) + if isPortOpen(str_port, defaultTimeout) { + hasFound = true + } + } + return str_port +}