2015-04-08 22:43:59 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-05-06 07:56:06 +00:00
|
|
|
"bufio"
|
2015-05-17 03:08:32 +00:00
|
|
|
"encoding/json"
|
2015-05-17 02:46:12 +00:00
|
|
|
"io"
|
2015-04-30 07:24:39 +00:00
|
|
|
"net/http"
|
2015-04-30 02:57:43 +00:00
|
|
|
"strconv"
|
2015-04-08 22:43:59 +00:00
|
|
|
"time"
|
|
|
|
|
2015-05-17 18:10:43 +00:00
|
|
|
"github.com/drone/drone/pkg/bus"
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-04-25 00:06:46 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-04-08 22:43:59 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gorilla/websocket"
|
2015-05-06 07:56:06 +00:00
|
|
|
|
|
|
|
// "github.com/koding/websocketproxy"
|
2015-04-08 22:43:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Time allowed to write the message to the client.
|
|
|
|
writeWait = 10 * time.Second
|
|
|
|
|
|
|
|
// Time allowed to read the next pong message from the client.
|
|
|
|
pongWait = 60 * time.Second
|
|
|
|
|
|
|
|
// Send pings to client with this period. Must be less than pongWait.
|
|
|
|
pingPeriod = (pongWait * 9) / 10
|
|
|
|
)
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
2015-04-30 07:24:39 +00:00
|
|
|
CheckOrigin: func(r *http.Request) bool { return true },
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 02:46:26 +00:00
|
|
|
// GetRepoEvents will upgrade the connection to a Websocket and will stream
|
|
|
|
// event updates to the browser.
|
|
|
|
func GetRepoEvents(c *gin.Context) {
|
2015-05-17 18:10:43 +00:00
|
|
|
bus_ := ToBus(c)
|
2015-05-06 02:46:26 +00:00
|
|
|
repo := ToRepo(c)
|
2015-05-17 02:46:12 +00:00
|
|
|
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
2015-04-25 00:06:46 +00:00
|
|
|
|
2015-05-17 18:10:43 +00:00
|
|
|
eventc := make(chan *bus.Event, 1)
|
|
|
|
bus_.Subscribe(eventc)
|
2015-04-08 22:43:59 +00:00
|
|
|
defer func() {
|
2015-05-17 18:10:43 +00:00
|
|
|
bus_.Unsubscribe(eventc)
|
2015-05-17 02:46:12 +00:00
|
|
|
log.Infof("closed event stream")
|
2015-04-08 22:43:59 +00:00
|
|
|
}()
|
|
|
|
|
2015-05-17 02:46:12 +00:00
|
|
|
c.Stream(func(w io.Writer) bool {
|
2015-05-17 03:35:41 +00:00
|
|
|
select {
|
|
|
|
case event := <-eventc:
|
|
|
|
if event == nil {
|
|
|
|
log.Infof("nil event received")
|
|
|
|
return false
|
|
|
|
}
|
2015-05-17 18:10:43 +00:00
|
|
|
if event.Kind == bus.EventRepo &&
|
2015-05-17 03:35:41 +00:00
|
|
|
event.Name == repo.FullName {
|
|
|
|
d := map[string]interface{}{}
|
|
|
|
json.Unmarshal(event.Msg, &d)
|
|
|
|
c.SSEvent("message", d)
|
|
|
|
}
|
|
|
|
case <-c.Writer.CloseNotify():
|
2015-05-17 02:46:12 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 02:57:43 +00:00
|
|
|
func GetStream(c *gin.Context) {
|
2015-05-11 07:45:31 +00:00
|
|
|
store := ToDatastore(c)
|
2015-04-30 02:57:43 +00:00
|
|
|
repo := ToRepo(c)
|
2015-05-06 07:56:06 +00:00
|
|
|
runner := ToRunner(c)
|
2015-05-11 07:45:31 +00:00
|
|
|
commitseq, _ := strconv.Atoi(c.Params.ByName("build"))
|
|
|
|
buildseq, _ := strconv.Atoi(c.Params.ByName("number"))
|
2015-04-30 02:57:43 +00:00
|
|
|
|
2015-05-06 07:56:06 +00:00
|
|
|
// agent, err := store.BuildAgent(repo.FullName, build)
|
|
|
|
// if err != nil {
|
|
|
|
// c.Fail(404, err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
commit, err := store.CommitSeq(repo, commitseq)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
build, err := store.BuildSeq(commit, buildseq)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rc, err := runner.Logs(build)
|
2015-04-30 02:57:43 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-06 07:56:06 +00:00
|
|
|
// upgrade the websocket
|
|
|
|
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
2015-04-30 02:57:43 +00:00
|
|
|
if err != nil {
|
2015-05-06 07:56:06 +00:00
|
|
|
c.Fail(400, err)
|
2015-04-30 02:57:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-06 07:56:06 +00:00
|
|
|
var ticker = time.NewTicker(pingPeriod)
|
|
|
|
var out = make(chan []byte)
|
|
|
|
defer func() {
|
|
|
|
log.Infof("closed stdout websocket")
|
|
|
|
ticker.Stop()
|
|
|
|
rc.Close()
|
|
|
|
ws.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.Writer.CloseNotify():
|
|
|
|
rc.Close()
|
|
|
|
ws.Close()
|
|
|
|
return
|
|
|
|
case line := <-out:
|
|
|
|
ws.WriteMessage(websocket.TextMessage, line)
|
|
|
|
case <-ticker.C:
|
|
|
|
ws.SetWriteDeadline(time.Now().Add(writeWait))
|
|
|
|
err := ws.WriteMessage(websocket.PingMessage, []byte{})
|
|
|
|
if err != nil {
|
|
|
|
rc.Close()
|
|
|
|
ws.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
rd := bufio.NewReader(rc)
|
|
|
|
for {
|
|
|
|
str, err := rd.ReadBytes('\n')
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(str) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
out <- str
|
|
|
|
}
|
|
|
|
rc.Close()
|
|
|
|
ws.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
readWebsocket(ws)
|
|
|
|
|
|
|
|
// url_, err := url.Parse("ws://" + agent.Addr)
|
|
|
|
// if err != nil {
|
|
|
|
// c.Fail(500, err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// url_.Path = fmt.Sprintf("/stream/%s/%v/%v", repo.FullName, build, task)
|
|
|
|
// proxy := websocketproxy.NewProxy(url_)
|
|
|
|
// proxy.ServeHTTP(c.Writer, c.Request)
|
|
|
|
|
|
|
|
// log.Debugf("closed websocket")
|
2015-04-30 02:57:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 22:43:59 +00:00
|
|
|
// readWebsocket will block while reading the websocket data
|
|
|
|
func readWebsocket(ws *websocket.Conn) {
|
|
|
|
defer ws.Close()
|
|
|
|
ws.SetReadLimit(512)
|
|
|
|
ws.SetReadDeadline(time.Now().Add(pongWait))
|
|
|
|
ws.SetPongHandler(func(string) error {
|
|
|
|
ws.SetReadDeadline(time.Now().Add(pongWait))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
for {
|
|
|
|
_, _, err := ws.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|