Skip to content

Commit

Permalink
Merge branch 'dashboard' of https://github.com/unarxiv/cvpm into dash…
Browse files Browse the repository at this point in the history
…board
  • Loading branch information
xzyaoi committed Dec 5, 2018
2 parents 39878a8 + e12951e commit 16b0375
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 7 deletions.
25 changes: 19 additions & 6 deletions cli/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -54,24 +67,23 @@ 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)
}
}

// watched log source
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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions cli/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
43 changes: 43 additions & 0 deletions cli/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
22 changes: 21 additions & 1 deletion cli/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
43 changes: 43 additions & 0 deletions cli/utils.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 16b0375

Please sign in to comment.