wide/output/outputs.go

966 lines
21 KiB
Go
Raw Normal View History

2014-11-12 18:13:14 +03:00
// Copyright (c) 2014, B3log
2014-11-20 06:30:18 +03:00
//
2014-11-12 18:13:14 +03:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-11-20 06:30:18 +03:00
//
2014-11-12 18:13:14 +03:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-11-20 06:30:18 +03:00
//
2014-11-12 18:13:14 +03:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2014-12-07 06:33:56 +03:00
// Package output includes build, run and go tool related manipulations.
2014-08-18 17:45:43 +04:00
package output
import (
2014-10-23 10:44:06 +04:00
"bufio"
2014-08-18 17:45:43 +04:00
"encoding/json"
2014-12-17 12:58:31 +03:00
"fmt"
2014-08-18 17:45:43 +04:00
"io"
2014-10-23 11:02:33 +04:00
"io/ioutil"
2014-08-18 17:45:43 +04:00
"math/rand"
"net/http"
"os"
"os/exec"
"path/filepath"
2014-08-22 07:57:05 +04:00
"runtime"
2014-09-09 11:33:12 +04:00
"strconv"
2014-08-22 07:57:05 +04:00
"strings"
"syscall"
2014-09-16 11:06:52 +04:00
"time"
2014-09-08 07:37:34 +04:00
"github.com/b3log/wide/conf"
2014-10-22 17:03:34 +04:00
"github.com/b3log/wide/i18n"
2014-12-13 13:47:41 +03:00
"github.com/b3log/wide/log"
2014-09-17 10:35:48 +04:00
"github.com/b3log/wide/session"
2014-09-08 07:37:34 +04:00
"github.com/b3log/wide/util"
"github.com/gorilla/websocket"
2014-08-18 17:45:43 +04:00
)
2014-09-26 13:36:12 +04:00
const (
2014-10-29 13:15:18 +03:00
lintSeverityError = "error" // lint severity: error
lintSeverityWarn = "warning" // lint severity: warning
2014-09-26 13:36:12 +04:00
)
2014-12-13 13:47:41 +03:00
// Logger.
var logger = log.NewLogger(os.Stdout)
2014-12-07 06:33:56 +03:00
// Lint represents a code lint.
2014-09-26 13:36:12 +04:00
type Lint struct {
File string `json:"file"`
LineNo int `json:"lineNo"`
Severity string `json:"severity"`
Msg string `json:"msg"`
2014-09-26 13:36:12 +04:00
}
2014-10-29 13:15:18 +03:00
// WSHandler handles request of creating output channel.
2014-08-18 17:45:43 +04:00
func WSHandler(w http.ResponseWriter, r *http.Request) {
2014-09-17 06:54:57 +04:00
sid := r.URL.Query()["sid"][0]
2014-08-18 17:45:43 +04:00
2014-09-16 11:06:52 +04:00
conn, _ := websocket.Upgrade(w, r, nil, 1024, 1024)
wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}
2014-08-18 17:45:43 +04:00
ret := map[string]interface{}{"output": "Ouput initialized", "cmd": "init-output"}
2014-11-20 08:59:08 +03:00
err := wsChan.WriteJSON(&ret)
2014-11-20 06:30:18 +03:00
if nil != err {
return
}
session.OutputWS[sid] = &wsChan
2014-08-18 17:45:43 +04:00
2014-12-14 18:05:54 +03:00
logger.Tracef("Open a new [Output] with session [%s], %d", sid, len(session.OutputWS))
2014-08-18 17:45:43 +04:00
}
2014-10-29 13:15:18 +03:00
// RunHandler handles request of executing a binary file.
2014-08-18 17:45:43 +04:00
func RunHandler(w http.ResponseWriter, r *http.Request) {
2014-09-01 16:50:51 +04:00
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
2014-08-18 17:45:43 +04:00
var args map[string]interface{}
2014-10-27 18:02:15 +03:00
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
}
2014-09-17 06:54:57 +04:00
sid := args["sid"].(string)
2014-09-19 15:21:13 +04:00
wSession := session.WideSessions.Get(sid)
if nil == wSession {
data["succ"] = false
}
2014-09-17 06:54:57 +04:00
2014-08-22 07:57:05 +04:00
filePath := args["executable"].(string)
curDir := filepath.Dir(filePath)
2014-08-22 07:57:05 +04:00
2014-12-17 12:58:31 +03:00
cmd1 := exec.Command("cat", "/etc/passwd")
output, err := cmd1.Output()
if nil != err {
fmt.Println("err")
fmt.Println(err)
}
fmt.Println(string(output))
2014-08-22 07:57:05 +04:00
cmd := exec.Command(filePath)
cmd.Dir = curDir
// XXX: keep move with Go 1.4 and later's
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Cloneflags = syscall.CLONE_NEWUSER | syscall.CLONE_NEWNS | syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID | syscall.CLONE_NEWIPC | syscall.CLONE_NEWNET
cmd.SysProcAttr.Credential = &syscall.Credential{
2014-12-18 06:11:56 +03:00
Uid: 1000,
Gid: 1000,
}
2014-12-18 06:11:56 +03:00
cmd.SysProcAttr.UidMappings = []syscall.SysProcIDMap{{ContainerID: 1000, HostID: 1000, Size: 1}}
cmd.SysProcAttr.GidMappings = []syscall.SysProcIDMap{{ContainerID: 1000, HostID: 1000, Size: 1}}
2014-08-22 07:57:05 +04:00
stdout, err := cmd.StdoutPipe()
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-22 07:57:05 +04:00
}
stderr, err := cmd.StderrPipe()
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-22 07:57:05 +04:00
}
2014-11-07 16:30:06 +03:00
outReader := util.NewReader(stdout)
2014-11-28 10:02:32 +03:00
errReader := util.NewReader(stderr)
2014-08-22 07:57:05 +04:00
2014-09-16 13:32:55 +04:00
if err := cmd.Start(); nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-16 13:32:55 +04:00
data["succ"] = false
2014-11-06 12:48:03 +03:00
}
wsChannel := session.OutputWS[sid]
channelRet := map[string]interface{}{}
if !data["succ"].(bool) {
if nil != wsChannel {
channelRet["cmd"] = "run-done"
channelRet["output"] = ""
2014-11-20 08:59:08 +03:00
err := wsChannel.WriteJSON(&channelRet)
2014-11-06 12:48:03 +03:00
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-11-06 12:48:03 +03:00
return
}
wsChannel.Refresh()
}
2014-09-16 13:32:55 +04:00
return
}
2014-11-06 12:48:03 +03:00
channelRet["pid"] = cmd.Process.Pid
2014-10-29 13:15:18 +03:00
// add the process to user's process set
2014-09-19 15:21:13 +04:00
processes.add(wSession, cmd.Process)
2014-08-22 07:57:05 +04:00
go func(runningId int) {
2014-09-26 13:36:12 +04:00
defer util.Recover()
2014-10-16 06:39:09 +04:00
defer cmd.Wait()
2014-09-26 13:36:12 +04:00
2014-12-13 13:47:41 +03:00
logger.Debugf("Session [%s] is running [id=%d, file=%s]", sid, runningId, filePath)
2014-08-22 07:57:05 +04:00
2014-10-29 13:15:18 +03:00
// push once for front-end to get the 'run' state and pid
2014-11-06 12:48:03 +03:00
if nil != wsChannel {
2014-10-16 06:39:09 +04:00
channelRet["cmd"] = "run"
channelRet["output"] = ""
2014-11-20 08:59:08 +03:00
err := wsChannel.WriteJSON(&channelRet)
2014-10-16 06:39:09 +04:00
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-16 06:39:09 +04:00
return
}
2014-10-29 13:15:18 +03:00
wsChannel.Refresh()
2014-10-16 06:39:09 +04:00
}
2014-10-31 08:46:51 +03:00
go func() {
for {
2014-11-07 16:30:06 +03:00
buf, err := outReader.ReadData()
buf = strings.Replace(buf, "<", "&lt;", -1)
buf = strings.Replace(buf, ">", "&gt;", -1)
2014-10-31 08:46:51 +03:00
2014-11-01 12:38:12 +03:00
// TODO: fix the duplicated error
2014-10-31 08:46:51 +03:00
if nil != err {
// remove the exited process from user process set
processes.remove(wSession, cmd.Process)
2014-12-13 13:47:41 +03:00
logger.Debugf("Session [%s] 's running [id=%d, file=%s] has done [stdout err]", sid, runningId, filePath)
2014-10-31 08:46:51 +03:00
2014-11-06 12:48:03 +03:00
if nil != wsChannel {
2014-10-31 08:46:51 +03:00
channelRet["cmd"] = "run-done"
2014-11-11 18:55:23 +03:00
channelRet["output"] = buf
2014-11-20 08:59:08 +03:00
err := wsChannel.WriteJSON(&channelRet)
2014-10-31 08:46:51 +03:00
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-31 08:46:51 +03:00
break
}
wsChannel.Refresh()
}
break
} else {
2014-11-06 12:48:03 +03:00
if nil != wsChannel {
2014-10-31 08:46:51 +03:00
channelRet["cmd"] = "run"
2014-11-11 18:55:23 +03:00
channelRet["output"] = buf
2014-11-20 08:59:08 +03:00
err := wsChannel.WriteJSON(&channelRet)
2014-10-31 08:46:51 +03:00
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-31 08:46:51 +03:00
break
}
wsChannel.Refresh()
}
}
}
}()
2014-08-22 07:57:05 +04:00
for {
2014-11-28 10:02:32 +03:00
buf, err := errReader.ReadData()
buf = strings.Replace(buf, "<", "&lt;", -1)
buf = strings.Replace(buf, ">", "&gt;", -1)
2014-08-22 07:57:05 +04:00
2014-11-28 10:02:32 +03:00
if nil == session.OutputWS[sid] {
break
}
wsChannel := session.OutputWS[sid]
2014-10-31 08:46:51 +03:00
if nil != err {
2014-10-29 13:15:18 +03:00
// remove the exited process from user process set
2014-09-19 15:21:13 +04:00
processes.remove(wSession, cmd.Process)
2014-09-16 13:32:55 +04:00
2014-12-13 13:47:41 +03:00
logger.Debugf("Session [%s] 's running [id=%d, file=%s] has done [stderr err]", sid, runningId, filePath)
2014-08-22 07:57:05 +04:00
2014-11-28 10:02:32 +03:00
channelRet["cmd"] = "run-done"
channelRet["output"] = "<span class='stderr'>" + buf + "</span>"
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-11-28 10:02:32 +03:00
break
2014-09-16 13:32:55 +04:00
}
2014-11-28 10:02:32 +03:00
wsChannel.Refresh()
2014-08-22 07:57:05 +04:00
break
} else {
2014-11-28 10:02:32 +03:00
channelRet["cmd"] = "run"
channelRet["output"] = "<span class='stderr'>" + buf + "</span>"
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-11-28 10:02:32 +03:00
break
2014-08-22 07:57:05 +04:00
}
2014-11-28 10:02:32 +03:00
wsChannel.Refresh()
2014-08-22 07:57:05 +04:00
}
}
}(rand.Int())
}
2014-10-29 13:15:18 +03:00
// BuildHandler handles request of building.
2014-08-22 07:57:05 +04:00
func BuildHandler(w http.ResponseWriter, r *http.Request) {
2014-09-01 16:50:51 +04:00
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
2014-09-17 10:35:48 +04:00
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
2014-11-26 08:49:27 +03:00
if httpSession.IsNew {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
2014-09-17 06:54:57 +04:00
username := httpSession.Values["username"].(string)
2014-10-23 17:43:35 +04:00
locale := conf.Wide.GetUser(username).Locale
2014-08-22 07:57:05 +04:00
var args map[string]interface{}
2014-10-27 18:02:15 +03:00
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-22 07:57:05 +04:00
return
}
2014-09-17 06:54:57 +04:00
sid := args["sid"].(string)
2014-08-18 17:45:43 +04:00
filePath := args["file"].(string)
curDir := filepath.Dir(filePath)
2014-08-18 17:45:43 +04:00
fout, err := os.Create(filePath)
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
return
}
code := args["code"].(string)
fout.WriteString(code)
if err := fout.Close(); nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
return
}
2014-08-22 07:57:05 +04:00
suffix := ""
2014-10-26 10:41:34 +03:00
if util.OS.IsWindows() {
2014-08-22 07:57:05 +04:00
suffix = ".exe"
}
2014-08-18 17:45:43 +04:00
2014-11-06 12:48:03 +03:00
cmd := exec.Command("go", "build")
2014-08-22 07:57:05 +04:00
cmd.Dir = curDir
2014-09-02 20:26:10 +04:00
setCmdEnv(cmd, username)
2014-08-28 06:19:00 +04:00
2014-11-06 12:48:03 +03:00
executable := filepath.Base(curDir) + suffix
2014-12-13 13:47:41 +03:00
logger.Debugf("go build for [%s]", executable)
2014-08-22 11:37:26 +04:00
executable = filepath.Join(curDir, executable)
2014-08-22 11:37:26 +04:00
2014-08-18 17:45:43 +04:00
stdout, err := cmd.StdoutPipe()
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-01 14:55:11 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
return
}
stderr, err := cmd.StderrPipe()
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-01 14:55:11 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
return
}
2014-10-22 13:47:53 +04:00
if !data["succ"].(bool) {
return
}
2014-08-18 17:45:43 +04:00
2014-10-22 13:47:53 +04:00
channelRet := map[string]interface{}{}
if nil != session.OutputWS[sid] {
2014-10-29 13:15:18 +03:00
// display "START [go build]" in front-end browser
2014-10-22 13:47:53 +04:00
2014-10-23 17:43:35 +04:00
channelRet["output"] = "<span class='start-build'>" + i18n.Get(locale, "start-build").(string) + "</span>\n"
2014-10-22 17:41:14 +04:00
channelRet["cmd"] = "start-build"
2014-09-16 13:32:55 +04:00
2014-10-22 13:47:53 +04:00
wsChannel := session.OutputWS[sid]
2014-11-20 08:59:08 +03:00
err := wsChannel.WriteJSON(&channelRet)
2014-10-22 13:47:53 +04:00
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-16 13:32:55 +04:00
return
}
2014-08-18 17:45:43 +04:00
2014-10-29 13:15:18 +03:00
wsChannel.Refresh()
2014-10-22 13:47:53 +04:00
}
2014-09-26 13:36:12 +04:00
2014-10-23 11:02:33 +04:00
reader := bufio.NewReader(io.MultiReader(stdout, stderr))
2014-08-18 17:45:43 +04:00
2014-10-22 13:47:53 +04:00
if err := cmd.Start(); nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-22 13:47:53 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
2014-10-22 13:47:53 +04:00
return
}
2014-09-09 11:33:12 +04:00
2014-10-22 13:47:53 +04:00
go func(runningId int) {
defer util.Recover()
defer cmd.Wait()
2014-09-16 18:39:22 +04:00
2014-12-14 18:05:54 +03:00
// logger.Debugf("Session [%s] is building [id=%d, dir=%s]", sid, runningId, curDir)
2014-09-04 19:48:49 +04:00
2014-10-29 13:15:18 +03:00
// read all
2014-10-23 11:02:33 +04:00
buf, _ := ioutil.ReadAll(reader)
2014-09-04 19:48:49 +04:00
2014-10-22 13:47:53 +04:00
channelRet := map[string]interface{}{}
channelRet["cmd"] = "build"
channelRet["executable"] = executable
2014-09-26 13:36:12 +04:00
2014-10-29 13:15:18 +03:00
if 0 == len(buf) { // build success
2014-10-22 13:47:53 +04:00
channelRet["nextCmd"] = args["nextCmd"]
2014-10-23 17:43:35 +04:00
channelRet["output"] = "<span class='build-succ'>" + i18n.Get(locale, "build-succ").(string) + "</span>\n"
2014-09-09 09:53:25 +04:00
2014-10-29 13:15:18 +03:00
go func() { // go install, for subsequent gocode lib-path
2014-10-22 13:47:53 +04:00
cmd := exec.Command("go", "install")
cmd.Dir = curDir
2014-09-26 13:36:12 +04:00
2014-10-22 13:47:53 +04:00
setCmdEnv(cmd, username)
2014-09-26 13:36:12 +04:00
2014-10-22 13:47:53 +04:00
out, _ := cmd.CombinedOutput()
if len(out) > 0 {
2014-12-13 13:47:41 +03:00
logger.Warn(string(out))
2014-10-22 13:47:53 +04:00
}
}()
2014-10-29 13:15:18 +03:00
} else { // build error
// build gutter lint
2014-10-23 11:02:33 +04:00
errOut := string(buf)
2014-12-03 10:26:52 +03:00
lines := strings.Split(errOut, "\n")
// path process
var errOutWithPath string
for _, line := range lines {
errOutWithPath += parsePath(curDir, line) + "\n"
}
2014-11-28 10:02:32 +03:00
channelRet["output"] = "<span class='build-error'>" + i18n.Get(locale, "build-error").(string) + "</span>\n" +
2014-12-03 10:26:52 +03:00
"<span class='stderr'>" + errOutWithPath + "</span>"
2014-10-22 13:55:22 +04:00
2014-12-03 10:26:52 +03:00
// lint process
2014-10-22 13:47:53 +04:00
if lines[0][0] == '#' {
2014-10-29 13:15:18 +03:00
lines = lines[1:] // skip the first line
2014-10-22 13:47:53 +04:00
}
2014-09-26 13:36:12 +04:00
2014-10-22 13:47:53 +04:00
lints := []*Lint{}
for _, line := range lines {
if len(line) < 1 {
continue
}
2014-09-04 19:48:49 +04:00
2014-10-22 13:47:53 +04:00
if line[0] == '\t' {
2014-10-29 13:15:18 +03:00
// append to the last lint
2014-10-22 13:47:53 +04:00
last := len(lints)
msg := lints[last-1].Msg
msg += line
2014-08-18 17:45:43 +04:00
2014-10-22 13:47:53 +04:00
lints[last-1].Msg = msg
2014-09-26 13:36:12 +04:00
2014-10-22 13:47:53 +04:00
continue
2014-09-09 11:33:12 +04:00
}
2014-10-22 13:47:53 +04:00
file := line[:strings.Index(line, ":")]
left := line[strings.Index(line, ":")+1:]
index := strings.Index(left, ":")
lineNo := 0
msg := left
if index >= 0 {
2014-12-03 10:26:52 +03:00
lineNo, err = strconv.Atoi(left[:index])
if nil != err {
continue
}
msg = left[index+2:]
}
2014-10-22 13:47:53 +04:00
lint := &Lint{
2014-12-13 13:47:41 +03:00
File: filepath.Join(curDir, file),
2014-10-22 13:47:53 +04:00
LineNo: lineNo - 1,
Severity: lintSeverityError,
Msg: msg,
}
lints = append(lints, lint)
2014-09-09 11:33:12 +04:00
}
2014-08-18 17:45:43 +04:00
2014-10-22 13:47:53 +04:00
channelRet["lints"] = lints
}
2014-08-22 07:57:05 +04:00
2014-10-22 13:47:53 +04:00
if nil != session.OutputWS[sid] {
2014-12-14 18:05:54 +03:00
// logger.Debugf("Session [%s] 's build [id=%d, dir=%s] has done", sid, runningId, curDir)
2014-09-16 11:11:14 +04:00
2014-10-22 13:47:53 +04:00
wsChannel := session.OutputWS[sid]
2014-11-20 08:59:08 +03:00
err := wsChannel.WriteJSON(&channelRet)
2014-10-22 13:47:53 +04:00
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-08-18 17:45:43 +04:00
}
2014-10-29 13:15:18 +03:00
wsChannel.Refresh()
2014-10-22 13:47:53 +04:00
}
}(rand.Int())
2014-08-18 17:45:43 +04:00
}
2014-08-28 06:19:00 +04:00
2014-12-03 10:26:52 +03:00
// parsePath parses file path in the specified outputLine, and returns new line with front-end friendly.
func parsePath(curDir, outputLine string) string {
index := strings.Index(outputLine, " ")
if -1 == index || index >= len(outputLine) {
return outputLine
}
pathPart := outputLine[:index]
msgPart := outputLine[index:]
parts := strings.Split(pathPart, ":")
if len(parts) < 2 { // no file path info (line & column) found
return outputLine
}
file := parts[0]
line := parts[1]
if _, err := strconv.Atoi(line); nil != err {
return outputLine
}
column := "0"
hasColumn := 4 == len(parts)
if hasColumn {
column = parts[2]
}
2014-12-03 12:00:24 +03:00
tagStart := `<span class="path" data-path="` + filepath.Join(curDir, file) + `" data-line="` + line +
2014-12-03 10:26:52 +03:00
`" data-column="` + column + `">`
text := file + ":" + line
if hasColumn {
text += ":" + column
}
tagEnd := "</span>:"
return tagStart + text + tagEnd + msgPart
}
2014-10-29 13:15:18 +03:00
// GoTestHandler handles request of go test.
2014-10-27 18:02:15 +03:00
func GoTestHandler(w http.ResponseWriter, r *http.Request) {
2014-09-09 13:01:22 +04:00
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
2014-09-17 10:35:48 +04:00
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
2014-11-26 08:49:27 +03:00
if httpSession.IsNew {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
2014-09-17 06:54:57 +04:00
username := httpSession.Values["username"].(string)
2014-10-23 17:43:35 +04:00
locale := conf.Wide.GetUser(username).Locale
2014-09-09 13:01:22 +04:00
var args map[string]interface{}
2014-10-27 18:02:15 +03:00
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-09 13:01:22 +04:00
data["succ"] = false
return
}
2014-09-17 06:54:57 +04:00
sid := args["sid"].(string)
2014-09-09 13:01:22 +04:00
filePath := args["file"].(string)
curDir := filepath.Dir(filePath)
2014-09-09 13:01:22 +04:00
2014-10-27 18:02:15 +03:00
cmd := exec.Command("go", "test", "-v")
cmd.Dir = curDir
setCmdEnv(cmd, username)
2014-09-09 13:01:22 +04:00
2014-10-27 18:02:15 +03:00
stdout, err := cmd.StdoutPipe()
2014-09-09 13:01:22 +04:00
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-09 13:01:22 +04:00
data["succ"] = false
return
}
2014-10-27 18:02:15 +03:00
stderr, err := cmd.StderrPipe()
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-27 18:02:15 +03:00
data["succ"] = false
2014-09-09 13:01:22 +04:00
2014-10-27 18:02:15 +03:00
return
}
2014-09-09 13:01:22 +04:00
2014-10-27 18:02:15 +03:00
if !data["succ"].(bool) {
return
}
channelRet := map[string]interface{}{}
if nil != session.OutputWS[sid] {
2014-10-29 13:15:18 +03:00
// display "START [go test]" in front-end browser
2014-10-27 18:02:15 +03:00
channelRet["output"] = "<span class='start-test'>" + i18n.Get(locale, "start-test").(string) + "</span>\n"
channelRet["cmd"] = "start-test"
wsChannel := session.OutputWS[sid]
2014-11-20 08:59:08 +03:00
err := wsChannel.WriteJSON(&channelRet)
2014-10-27 18:02:15 +03:00
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-27 18:02:15 +03:00
return
}
2014-10-29 13:15:18 +03:00
wsChannel.Refresh()
2014-10-27 18:02:15 +03:00
}
reader := bufio.NewReader(io.MultiReader(stdout, stderr))
if err := cmd.Start(); nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-09 13:01:22 +04:00
data["succ"] = false
return
}
2014-10-27 18:02:15 +03:00
go func(runningId int) {
defer util.Recover()
2014-12-13 13:47:41 +03:00
logger.Debugf("Session [%s] is running [go test] [runningId=%d]", sid, runningId)
2014-10-27 18:02:15 +03:00
channelRet := map[string]interface{}{}
channelRet["cmd"] = "go test"
2014-10-29 13:15:18 +03:00
// read all
2014-10-27 18:02:15 +03:00
buf, _ := ioutil.ReadAll(reader)
2014-10-29 13:15:18 +03:00
// waiting for go test finished
2014-10-27 18:02:15 +03:00
cmd.Wait()
if !cmd.ProcessState.Success() {
2014-12-13 13:47:41 +03:00
logger.Debugf("Session [%s] 's running [go test] [runningId=%d] has done (with error)", sid, runningId)
2014-10-27 18:02:15 +03:00
2014-10-27 18:58:10 +03:00
channelRet["output"] = "<span class='test-error'>" + i18n.Get(locale, "test-error").(string) + "</span>\n" + string(buf)
2014-10-27 18:02:15 +03:00
} else {
2014-12-13 13:47:41 +03:00
logger.Debugf("Session [%s] 's running [go test] [runningId=%d] has done", sid, runningId)
2014-10-27 18:02:15 +03:00
channelRet["output"] = "<span class='test-succ'>" + i18n.Get(locale, "test-succ").(string) + "</span>\n" + string(buf)
}
if nil != session.OutputWS[sid] {
wsChannel := session.OutputWS[sid]
2014-11-20 08:59:08 +03:00
err := wsChannel.WriteJSON(&channelRet)
2014-10-27 18:02:15 +03:00
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-27 18:02:15 +03:00
}
2014-10-29 13:15:18 +03:00
wsChannel.Refresh()
2014-10-27 18:02:15 +03:00
}
}(rand.Int())
}
2014-10-29 13:15:18 +03:00
// GoInstallHandler handles request of go install.
2014-10-27 18:02:15 +03:00
func GoInstallHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
2014-11-26 08:49:27 +03:00
if httpSession.IsNew {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
2014-10-27 18:02:15 +03:00
username := httpSession.Values["username"].(string)
locale := conf.Wide.GetUser(username).Locale
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-27 18:02:15 +03:00
data["succ"] = false
return
}
sid := args["sid"].(string)
filePath := args["file"].(string)
curDir := filepath.Dir(filePath)
2014-10-27 18:02:15 +03:00
2014-09-09 13:01:22 +04:00
cmd := exec.Command("go", "install")
cmd.Dir = curDir
setCmdEnv(cmd, username)
2014-12-13 13:47:41 +03:00
logger.Debugf("go install %s", curDir)
2014-09-09 13:01:22 +04:00
stdout, err := cmd.StdoutPipe()
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-09 13:01:22 +04:00
data["succ"] = false
return
}
stderr, err := cmd.StderrPipe()
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-09 13:01:22 +04:00
data["succ"] = false
return
}
2014-10-22 17:57:21 +04:00
if !data["succ"].(bool) {
return
}
2014-09-09 13:01:22 +04:00
2014-10-22 17:57:21 +04:00
channelRet := map[string]interface{}{}
2014-09-09 13:01:22 +04:00
2014-10-22 17:57:21 +04:00
if nil != session.OutputWS[sid] {
2014-10-29 13:15:18 +03:00
// display "START [go install]" in front-end browser
2014-09-26 13:36:12 +04:00
2014-10-23 17:43:35 +04:00
channelRet["output"] = "<span class='start-install'>" + i18n.Get(locale, "start-install").(string) + "</span>\n"
2014-10-22 17:57:21 +04:00
channelRet["cmd"] = "start-install"
2014-09-09 13:01:22 +04:00
2014-10-22 17:57:21 +04:00
wsChannel := session.OutputWS[sid]
2014-09-09 13:01:22 +04:00
2014-11-20 08:59:08 +03:00
err := wsChannel.WriteJSON(&channelRet)
2014-10-22 17:57:21 +04:00
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-22 17:57:21 +04:00
return
}
2014-09-09 13:01:22 +04:00
2014-10-29 13:15:18 +03:00
wsChannel.Refresh()
2014-10-22 17:57:21 +04:00
}
2014-09-26 13:36:12 +04:00
2014-10-23 11:22:15 +04:00
reader := bufio.NewReader(io.MultiReader(stdout, stderr))
2014-09-26 13:36:12 +04:00
2014-10-22 17:57:21 +04:00
if err := cmd.Start(); nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-22 17:57:21 +04:00
data["succ"] = false
2014-09-09 13:01:22 +04:00
2014-10-22 17:57:21 +04:00
return
}
2014-09-26 13:36:12 +04:00
2014-10-22 17:57:21 +04:00
go func(runningId int) {
defer util.Recover()
defer cmd.Wait()
2014-09-26 13:36:12 +04:00
2014-12-13 13:47:41 +03:00
logger.Debugf("Session [%s] is running [go install] [id=%d, dir=%s]", sid, runningId, curDir)
2014-09-26 13:36:12 +04:00
2014-10-29 13:15:18 +03:00
// read all
2014-10-23 11:22:15 +04:00
buf, _ := ioutil.ReadAll(reader)
2014-09-09 13:01:22 +04:00
2014-10-22 17:57:21 +04:00
channelRet := map[string]interface{}{}
channelRet["cmd"] = "go install"
2014-09-09 13:01:22 +04:00
2014-10-29 13:15:18 +03:00
if 0 != len(buf) { // build error
// build gutter lint
2014-10-23 11:22:15 +04:00
errOut := string(buf)
2014-10-22 17:57:21 +04:00
lines := strings.Split(errOut, "\n")
if lines[0][0] == '#' {
2014-10-29 13:15:18 +03:00
lines = lines[1:] // skip the first line
2014-09-09 13:01:22 +04:00
}
2014-10-22 17:57:21 +04:00
lints := []*Lint{}
2014-09-09 13:01:22 +04:00
2014-10-22 17:57:21 +04:00
for _, line := range lines {
if len(line) < 1 {
continue
2014-09-09 13:01:22 +04:00
}
2014-09-16 11:11:14 +04:00
2014-10-22 17:57:21 +04:00
if line[0] == '\t' {
2014-10-29 13:15:18 +03:00
// append to the last lint
2014-10-22 17:57:21 +04:00
last := len(lints)
msg := lints[last-1].Msg
msg += line
lints[last-1].Msg = msg
continue
}
file := line[:strings.Index(line, ":")]
left := line[strings.Index(line, ":")+1:]
index := strings.Index(left, ":")
lineNo := 0
msg := left
if index >= 0 {
lineNo, _ = strconv.Atoi(left[:index])
msg = left[index+2:]
}
2014-10-22 17:57:21 +04:00
lint := &Lint{
File: file,
LineNo: lineNo - 1,
Severity: lintSeverityError,
Msg: msg,
}
lints = append(lints, lint)
2014-09-09 13:01:22 +04:00
}
2014-10-22 17:57:21 +04:00
channelRet["lints"] = lints
2014-10-27 18:58:10 +03:00
channelRet["output"] = "<span class='install-error'>" + i18n.Get(locale, "install-error").(string) + "</span>\n" + errOut
2014-10-22 17:57:21 +04:00
} else {
2014-10-23 17:43:35 +04:00
channelRet["output"] = "<span class='install-succ'>" + i18n.Get(locale, "install-succ").(string) + "</span>\n"
2014-10-22 17:57:21 +04:00
}
if nil != session.OutputWS[sid] {
2014-12-13 13:47:41 +03:00
logger.Debugf("Session [%s] 's running [go install] [id=%d, dir=%s] has done", sid, runningId, curDir)
2014-10-22 17:57:21 +04:00
wsChannel := session.OutputWS[sid]
2014-11-20 08:59:08 +03:00
err := wsChannel.WriteJSON(&channelRet)
2014-10-22 17:57:21 +04:00
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-22 17:57:21 +04:00
}
2014-10-29 13:15:18 +03:00
wsChannel.Refresh()
2014-10-22 17:57:21 +04:00
}
}(rand.Int())
2014-09-09 13:01:22 +04:00
}
2014-10-29 13:15:18 +03:00
// GoGetHandler handles request of go get.
2014-09-05 08:18:50 +04:00
func GoGetHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
2014-09-17 10:35:48 +04:00
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
2014-11-26 08:49:27 +03:00
if httpSession.IsNew {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
2014-09-17 06:54:57 +04:00
username := httpSession.Values["username"].(string)
2014-10-23 17:43:35 +04:00
locale := conf.Wide.GetUser(username).Locale
2014-09-05 08:18:50 +04:00
var args map[string]interface{}
2014-10-27 18:02:15 +03:00
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-05 08:18:50 +04:00
data["succ"] = false
return
}
2014-09-17 06:54:57 +04:00
sid := args["sid"].(string)
2014-09-05 08:18:50 +04:00
filePath := args["file"].(string)
curDir := filepath.Dir(filePath)
2014-09-05 08:18:50 +04:00
cmd := exec.Command("go", "get")
cmd.Dir = curDir
setCmdEnv(cmd, username)
stdout, err := cmd.StdoutPipe()
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-05 08:18:50 +04:00
data["succ"] = false
return
}
stderr, err := cmd.StderrPipe()
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-05 08:18:50 +04:00
data["succ"] = false
return
}
2014-10-22 18:35:14 +04:00
if !data["succ"].(bool) {
return
}
channelRet := map[string]interface{}{}
if nil != session.OutputWS[sid] {
2014-10-29 13:15:18 +03:00
// display "START [go get]" in front-end browser
2014-10-22 18:35:14 +04:00
2014-10-23 17:43:35 +04:00
channelRet["output"] = "<span class='start-get'>" + i18n.Get(locale, "start-get").(string) + "</span>\n"
2014-10-22 18:35:14 +04:00
channelRet["cmd"] = "start-get"
wsChannel := session.OutputWS[sid]
2014-11-20 08:59:08 +03:00
err := wsChannel.WriteJSON(&channelRet)
2014-10-22 18:35:14 +04:00
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-22 18:35:14 +04:00
return
}
2014-10-29 13:15:18 +03:00
wsChannel.Refresh()
2014-10-22 18:35:14 +04:00
}
2014-10-23 11:22:15 +04:00
reader := bufio.NewReader(io.MultiReader(stdout, stderr))
2014-09-05 08:18:50 +04:00
2014-10-22 18:35:14 +04:00
if err := cmd.Start(); nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-22 18:35:14 +04:00
data["succ"] = false
2014-09-05 08:18:50 +04:00
2014-10-22 18:35:14 +04:00
return
}
2014-09-05 08:18:50 +04:00
go func(runningId int) {
2014-09-26 13:36:12 +04:00
defer util.Recover()
2014-09-30 13:00:51 +04:00
defer cmd.Wait()
2014-09-26 13:36:12 +04:00
2014-12-13 13:47:41 +03:00
logger.Debugf("Session [%s] is running [go get] [runningId=%d]", sid, runningId)
2014-09-05 08:18:50 +04:00
2014-10-22 18:35:14 +04:00
channelRet := map[string]interface{}{}
channelRet["cmd"] = "go get"
2014-10-29 13:15:18 +03:00
// read all
2014-10-23 12:05:21 +04:00
buf, _ := ioutil.ReadAll(reader)
2014-10-22 18:35:14 +04:00
2014-10-23 12:05:21 +04:00
if 0 != len(buf) {
2014-12-13 13:47:41 +03:00
logger.Debugf("Session [%s] 's running [go get] [runningId=%d] has done (with error)", sid, runningId)
2014-10-22 18:35:14 +04:00
2014-10-27 18:58:10 +03:00
channelRet["output"] = "<span class='get-error'>" + i18n.Get(locale, "get-error").(string) + "</span>\n" + string(buf)
2014-10-23 12:05:21 +04:00
} else {
2014-12-13 13:47:41 +03:00
logger.Debugf("Session [%s] 's running [go get] [runningId=%d] has done", sid, runningId)
2014-09-05 08:18:50 +04:00
2014-10-23 17:43:35 +04:00
channelRet["output"] = "<span class='get-succ'>" + i18n.Get(locale, "get-succ").(string) + "</span>\n"
2014-09-16 11:06:52 +04:00
2014-10-23 12:05:21 +04:00
}
2014-09-16 11:11:14 +04:00
2014-10-23 12:05:21 +04:00
if nil != session.OutputWS[sid] {
wsChannel := session.OutputWS[sid]
2014-10-22 18:35:14 +04:00
2014-11-20 08:59:08 +03:00
err := wsChannel.WriteJSON(&channelRet)
2014-10-23 12:05:21 +04:00
if nil != err {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-22 18:35:14 +04:00
}
2014-10-29 13:15:18 +03:00
wsChannel.Refresh()
2014-09-05 08:18:50 +04:00
}
}(rand.Int())
}
2014-10-29 13:15:18 +03:00
// StopHandler handles request of stoping a running process.
2014-09-22 13:44:07 +04:00
func StopHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-09-22 13:44:07 +04:00
data["succ"] = false
return
}
sid := args["sid"].(string)
2014-09-23 05:45:56 +04:00
pid := int(args["pid"].(float64))
2014-09-22 13:44:07 +04:00
wSession := session.WideSessions.Get(sid)
if nil == wSession {
data["succ"] = false
return
}
processes.kill(wSession, pid)
}
2014-09-02 20:26:10 +04:00
func setCmdEnv(cmd *exec.Cmd, username string) {
2014-09-05 07:24:53 +04:00
userWorkspace := conf.Wide.GetUserWorkspace(username)
2014-09-02 20:26:10 +04:00
cmd.Env = append(cmd.Env,
2014-11-12 09:49:14 +03:00
"GOPATH="+userWorkspace,
2014-09-04 19:48:49 +04:00
"GOOS="+runtime.GOOS,
"GOARCH="+runtime.GOARCH,
2014-09-05 08:18:50 +04:00
"GOROOT="+runtime.GOROOT(),
"PATH="+os.Getenv("PATH"))
2014-08-28 06:19:00 +04:00
}