wide/output/outputs.go

536 lines
11 KiB
Go
Raw Normal View History

2014-09-12 13:10:58 +04:00
// 构建、运行、go tool 操作.
2014-08-18 17:45:43 +04:00
package output
import (
"encoding/json"
"io"
"math/rand"
"net/http"
"os"
"os/exec"
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"
2014-09-16 11:06:52 +04:00
"time"
2014-09-08 07:37:34 +04:00
"github.com/b3log/wide/conf"
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/golang/glog"
"github.com/gorilla/websocket"
2014-08-18 17:45:43 +04:00
)
2014-09-16 11:06:52 +04:00
// 建立输出通道.
2014-08-18 17:45:43 +04:00
func WSHandler(w http.ResponseWriter, r *http.Request) {
2014-09-17 06:54:57 +04:00
// TODO: 会话校验
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-09-20 06:39:29 +04:00
session.OutputWS[sid] = &wsChan
2014-08-18 17:45:43 +04:00
ret := map[string]interface{}{"output": "Ouput initialized", "cmd": "init-output"}
2014-09-16 11:06:52 +04:00
wsChan.Conn.WriteJSON(&ret)
2014-08-18 17:45:43 +04:00
2014-09-20 06:39:29 +04:00
glog.V(4).Infof("Open a new [Output] with session [%s], %d", sid, len(session.OutputWS))
2014-08-18 17:45:43 +04:00
}
2014-09-12 13:10:58 +04:00
// 运行一个可执行文件.
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
decoder := json.NewDecoder(r.Body)
var args map[string]interface{}
if err := decoder.Decode(&args); err != nil {
glog.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
return
}
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
return
}
2014-09-17 06:54:57 +04:00
2014-08-22 07:57:05 +04:00
filePath := args["executable"].(string)
curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))]
cmd := exec.Command(filePath)
cmd.Dir = curDir
stdout, err := cmd.StdoutPipe()
if nil != err {
glog.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-22 07:57:05 +04:00
return
}
stderr, err := cmd.StderrPipe()
if nil != err {
glog.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-22 07:57:05 +04:00
return
}
reader := io.MultiReader(stdout, stderr)
2014-09-16 13:32:55 +04:00
if err := cmd.Start(); nil != err {
glog.Error(err)
data["succ"] = false
return
}
// 添加到用户进程集中
2014-09-19 15:21:13 +04:00
processes.add(wSession, cmd.Process)
2014-08-22 07:57:05 +04:00
2014-08-22 11:37:26 +04:00
channelRet := map[string]interface{}{}
2014-09-16 13:32:55 +04:00
channelRet["pid"] = cmd.Process.Pid
2014-08-22 07:57:05 +04:00
go func(runningId int) {
2014-09-08 07:37:34 +04:00
glog.V(3).Infof("Session [%s] is running [id=%d, file=%s]", sid, runningId, filePath)
2014-08-22 07:57:05 +04:00
for {
buf := make([]byte, 1024)
count, err := reader.Read(buf)
if nil != err || 0 == count {
2014-09-16 13:32:55 +04:00
// 从用户进程集中移除这个执行完毕的进程
2014-09-19 15:21:13 +04:00
processes.remove(wSession, cmd.Process)
2014-09-16 13:32:55 +04:00
2014-09-08 07:37:34 +04:00
glog.V(3).Infof("Session [%s] 's running [id=%d, file=%s] has done", sid, runningId, filePath)
2014-08-22 07:57:05 +04:00
2014-09-20 06:39:29 +04:00
if nil != session.OutputWS[sid] {
wsChannel := session.OutputWS[sid]
2014-09-16 13:32:55 +04:00
channelRet["cmd"] = "run-done"
channelRet["output"] = string(buf[:count])
err := wsChannel.Conn.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
break
}
// 更新通道最近使用时间
wsChannel.Time = time.Now()
}
2014-08-22 07:57:05 +04:00
break
} else {
2014-09-20 06:39:29 +04:00
if nil != session.OutputWS[sid] {
wsChannel := session.OutputWS[sid]
2014-09-16 11:06:52 +04:00
2014-09-16 13:32:55 +04:00
channelRet["cmd"] = "run"
channelRet["output"] = string(buf[:count])
2014-09-16 11:06:52 +04:00
err := wsChannel.Conn.WriteJSON(&channelRet)
2014-08-22 07:57:05 +04:00
if nil != err {
glog.Error(err)
break
}
2014-09-16 11:11:14 +04:00
// 更新通道最近使用时间
wsChannel.Time = time.Now()
2014-08-22 07:57:05 +04:00
}
}
}
}(rand.Int())
}
2014-09-12 13:10:58 +04:00
// 构建可执行文件.
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-09-17 06:54:57 +04:00
username := httpSession.Values["username"].(string)
2014-08-22 07:57:05 +04:00
decoder := json.NewDecoder(r.Body)
var args map[string]interface{}
if err := decoder.Decode(&args); err != nil {
glog.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
// TODO: 会话校验
sid := args["sid"].(string)
2014-08-18 17:45:43 +04:00
filePath := args["file"].(string)
2014-08-22 07:57:05 +04:00
curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))]
2014-08-18 17:45:43 +04:00
fout, err := os.Create(filePath)
if nil != err {
glog.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 {
glog.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 := ""
if "windows" == runtime.GOOS {
suffix = ".exe"
}
executable := "main" + suffix
argv := []string{"build", "-o", executable, filePath}
2014-08-18 17:45:43 +04:00
cmd := exec.Command("go", argv...)
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-09-08 07:37:34 +04:00
glog.V(5).Infof("go build -o %s %s", executable, filePath)
2014-08-22 11:37:26 +04:00
executable = curDir + string(os.PathSeparator) + executable
// 先把可执行文件删了
2014-09-01 16:50:51 +04:00
err = os.RemoveAll(executable)
if nil != err {
glog.Info(err)
data["succ"] = false
2014-08-18 17:45:43 +04:00
2014-09-01 16:50:51 +04:00
return
}
2014-09-01 14:55:11 +04:00
2014-08-18 17:45:43 +04:00
stdout, err := cmd.StdoutPipe()
if nil != err {
glog.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 {
glog.Error(err)
2014-09-01 14:55:11 +04:00
data["succ"] = false
2014-08-18 17:45:43 +04:00
return
}
2014-09-01 16:50:51 +04:00
if data["succ"].(bool) {
reader := io.MultiReader(stdout, stderr)
2014-08-18 17:45:43 +04:00
2014-09-16 13:32:55 +04:00
if err := cmd.Start(); nil != err {
glog.Error(err)
data["succ"] = false
return
}
2014-08-18 17:45:43 +04:00
2014-09-01 16:50:51 +04:00
go func(runningId int) {
2014-09-08 07:37:34 +04:00
glog.V(3).Infof("Session [%s] is building [id=%d, file=%s]", sid, runningId, filePath)
2014-08-18 17:45:43 +04:00
2014-09-01 16:50:51 +04:00
// 一次性读取
buf := make([]byte, 1024*8)
count, _ := reader.Read(buf)
2014-08-18 17:45:43 +04:00
2014-09-09 11:33:12 +04:00
channelRet := map[string]interface{}{}
channelRet["output"] = string(buf[:count])
channelRet["cmd"] = "build"
channelRet["executable"] = executable
2014-09-04 19:48:49 +04:00
if 0 == count { // 说明构建成功,没有错误信息输出
2014-09-16 18:39:22 +04:00
// 设置下一次执行命令(前端会根据这个发送请求)
channelRet["nextCmd"] = "run"
2014-09-04 19:48:49 +04:00
go func() { // 运行 go install生成的库用于 gocode lib-path
cmd := exec.Command("go", "install")
cmd.Dir = curDir
setCmdEnv(cmd, username)
2014-09-05 07:42:45 +04:00
out, _ := cmd.CombinedOutput()
if len(out) > 0 {
glog.Warning(string(out))
}
2014-09-04 19:48:49 +04:00
}()
2014-09-09 09:53:25 +04:00
} else { // 构建失败
// 解析错误信息,返回给编辑器 gutter lint
2014-09-09 11:33:12 +04:00
lines := strings.Split(string(buf[:count]), "\n")[1:]
lints := []map[string]interface{}{}
2014-09-09 09:53:25 +04:00
2014-09-09 11:33:12 +04:00
for _, line := range lines {
if len(line) <= 1 {
continue
}
2014-09-04 19:48:49 +04:00
2014-09-09 11:33:12 +04:00
file := line[:strings.Index(line, ":")]
left := line[strings.Index(line, ":")+1:]
lineNo, _ := strconv.Atoi(left[:strings.Index(left, ":")])
msg := left[strings.Index(left, ":")+2:]
2014-08-18 17:45:43 +04:00
2014-09-09 11:33:12 +04:00
lint := map[string]interface{}{}
lint["file"] = file
lint["lineNo"] = lineNo - 1
lint["msg"] = msg
2014-09-09 12:33:48 +04:00
lint["severity"] = "error" // warning
2014-09-09 11:33:12 +04:00
lints = append(lints, lint)
}
channelRet["lints"] = lints
}
2014-08-18 17:45:43 +04:00
2014-09-20 06:39:29 +04:00
if nil != session.OutputWS[sid] {
2014-09-08 07:37:34 +04:00
glog.V(3).Infof("Session [%s] 's build [id=%d, file=%s] has done", sid, runningId, filePath)
2014-08-22 07:57:05 +04:00
2014-09-20 06:39:29 +04:00
wsChannel := session.OutputWS[sid]
2014-09-16 11:06:52 +04:00
err := wsChannel.Conn.WriteJSON(&channelRet)
2014-09-01 16:50:51 +04:00
if nil != err {
glog.Error(err)
}
2014-09-16 11:11:14 +04:00
// 更新通道最近使用时间
wsChannel.Time = time.Now()
2014-08-18 17:45:43 +04:00
}
2014-09-01 16:50:51 +04:00
}(rand.Int())
}
2014-08-18 17:45:43 +04:00
}
2014-08-28 06:19:00 +04:00
2014-09-12 13:10:58 +04:00
// go install.
2014-09-09 13:01:22 +04:00
func GoInstallHandler(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-09-17 06:54:57 +04:00
username := httpSession.Values["username"].(string)
2014-09-09 13:01:22 +04:00
decoder := json.NewDecoder(r.Body)
var args map[string]interface{}
if err := decoder.Decode(&args); err != nil {
glog.Error(err)
data["succ"] = false
return
}
2014-09-17 06:54:57 +04:00
// TODO: 会话校验
sid := args["sid"].(string)
2014-09-09 13:01:22 +04:00
filePath := args["file"].(string)
curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))]
fout, err := os.Create(filePath)
if nil != err {
glog.Error(err)
data["succ"] = false
return
}
code := args["code"].(string)
fout.WriteString(code)
if err := fout.Close(); nil != err {
glog.Error(err)
data["succ"] = false
return
}
cmd := exec.Command("go", "install")
cmd.Dir = curDir
setCmdEnv(cmd, username)
glog.V(5).Infof("go install %s", curDir)
stdout, err := cmd.StdoutPipe()
if nil != err {
glog.Error(err)
data["succ"] = false
return
}
stderr, err := cmd.StderrPipe()
if nil != err {
glog.Error(err)
data["succ"] = false
return
}
if data["succ"].(bool) {
reader := io.MultiReader(stdout, stderr)
cmd.Start()
go func(runningId int) {
glog.V(3).Infof("Session [%s] is running [go install] [id=%d, dir=%s]", sid, runningId, curDir)
// 一次性读取
buf := make([]byte, 1024*8)
count, _ := reader.Read(buf)
channelRet := map[string]interface{}{}
channelRet["output"] = string(buf[:count])
channelRet["cmd"] = "go install"
2014-09-09 13:02:08 +04:00
if 0 != count { // 构建失败
2014-09-09 13:01:22 +04:00
// 解析错误信息,返回给编辑器 gutter lint
lines := strings.Split(string(buf[:count]), "\n")[1:]
lints := []map[string]interface{}{}
for _, line := range lines {
if len(line) <= 1 {
continue
}
file := line[:strings.Index(line, ":")]
left := line[strings.Index(line, ":")+1:]
lineNo, _ := strconv.Atoi(left[:strings.Index(left, ":")])
msg := left[strings.Index(left, ":")+2:]
lint := map[string]interface{}{}
lint["file"] = file
lint["lineNo"] = lineNo - 1
lint["msg"] = msg
lint["severity"] = "error" // warning
lints = append(lints, lint)
}
channelRet["lints"] = lints
}
2014-09-20 06:39:29 +04:00
if nil != session.OutputWS[sid] {
2014-09-09 13:01:22 +04:00
glog.V(3).Infof("Session [%s] 's running [go install] [id=%d, dir=%s] has done", sid, runningId, curDir)
2014-09-20 06:39:29 +04:00
wsChannel := session.OutputWS[sid]
2014-09-16 11:06:52 +04:00
err := wsChannel.Conn.WriteJSON(&channelRet)
2014-09-09 13:01:22 +04:00
if nil != err {
glog.Error(err)
}
2014-09-16 11:11:14 +04:00
// 更新通道最近使用时间
wsChannel.Time = time.Now()
2014-09-09 13:01:22 +04:00
}
}(rand.Int())
}
}
2014-09-12 13:10:58 +04:00
// 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-09-17 06:54:57 +04:00
username := httpSession.Values["username"].(string)
2014-09-05 08:18:50 +04:00
decoder := json.NewDecoder(r.Body)
var args map[string]interface{}
if err := decoder.Decode(&args); err != nil {
glog.Error(err)
data["succ"] = false
return
}
2014-09-17 06:54:57 +04:00
// TODO: 会话校验
sid := args["sid"].(string)
2014-09-05 08:18:50 +04:00
filePath := args["file"].(string)
curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))]
cmd := exec.Command("go", "get")
cmd.Dir = curDir
setCmdEnv(cmd, username)
stdout, err := cmd.StdoutPipe()
if nil != err {
glog.Error(err)
data["succ"] = false
return
}
stderr, err := cmd.StderrPipe()
if nil != err {
glog.Error(err)
data["succ"] = false
return
}
reader := io.MultiReader(stdout, stderr)
cmd.Start()
channelRet := map[string]interface{}{}
go func(runningId int) {
2014-09-08 07:37:34 +04:00
glog.V(3).Infof("Session [%s] is running [go get] [runningId=%d]", sid, runningId)
2014-09-05 08:18:50 +04:00
for {
buf := make([]byte, 1024)
count, err := reader.Read(buf)
if nil != err || 0 == count {
2014-09-08 07:37:34 +04:00
glog.V(3).Infof("Session [%s] 's running [go get] [runningId=%d] has done", sid, runningId)
2014-09-05 08:18:50 +04:00
break
} else {
channelRet["output"] = string(buf[:count])
2014-09-05 09:22:25 +04:00
channelRet["cmd"] = "go get"
2014-09-05 08:18:50 +04:00
2014-09-20 06:39:29 +04:00
if nil != session.OutputWS[sid] {
wsChannel := session.OutputWS[sid]
2014-09-16 11:06:52 +04:00
err := wsChannel.Conn.WriteJSON(&channelRet)
2014-09-05 08:18:50 +04:00
if nil != err {
glog.Error(err)
break
}
2014-09-16 11:11:14 +04:00
// 更新通道最近使用时间
wsChannel.Time = time.Now()
2014-09-05 08:18:50 +04:00
}
}
}
}(rand.Int())
}
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-09-04 19:48:49 +04:00
"GOPATH="+userWorkspace,
"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
}