2016-05-02 19:21:25 +00:00
|
|
|
package server
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
import (
|
2016-09-26 05:39:28 +00:00
|
|
|
"fmt"
|
2015-09-30 01:21:17 +00:00
|
|
|
"strconv"
|
2016-07-08 22:40:29 +00:00
|
|
|
"time"
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2016-07-08 22:40:29 +00:00
|
|
|
"github.com/drone/drone/cache"
|
2016-04-28 21:10:32 +00:00
|
|
|
"github.com/drone/drone/model"
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/drone/drone/router/middleware/session"
|
2015-10-21 23:14:02 +00:00
|
|
|
"github.com/drone/drone/store"
|
2016-09-26 05:39:28 +00:00
|
|
|
"github.com/drone/mq/stomp"
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2016-07-08 22:40:29 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gorilla/websocket"
|
2015-09-30 01:21:17 +00:00
|
|
|
)
|
|
|
|
|
2016-07-08 22:40:29 +00:00
|
|
|
var (
|
|
|
|
// Time allowed to write the file to the client.
|
|
|
|
writeWait = 5 * 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 = 30 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
// LogStream streams the build log output to the client.
|
|
|
|
func LogStream(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
buildn, _ := strconv.Atoi(c.Param("build"))
|
|
|
|
jobn, _ := strconv.Atoi(c.Param("number"))
|
|
|
|
|
|
|
|
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
|
|
|
|
build, err := store.GetBuildNumber(c, repo, buildn)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugln("stream cannot get build number.", err)
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
job, err := store.GetJobNumber(c, build, jobn)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugln("stream cannot get job number.", err)
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if job.Status != model.StatusRunning {
|
|
|
|
logrus.Debugln("stream not found.")
|
|
|
|
c.AbortWithStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(websocket.HandshakeError); !ok {
|
|
|
|
logrus.Errorf("Cannot upgrade websocket. %s", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logrus.Debugf("Successfull upgraded websocket")
|
|
|
|
|
|
|
|
ticker := time.NewTicker(pingPeriod)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2016-09-28 00:33:13 +00:00
|
|
|
logs := make(chan []byte)
|
2016-09-26 05:39:28 +00:00
|
|
|
done := make(chan bool)
|
2016-09-26 08:29:05 +00:00
|
|
|
dest := fmt.Sprintf("/topic/logs.%d", job.ID)
|
2016-09-26 05:39:28 +00:00
|
|
|
client, _ := stomp.FromContext(c)
|
|
|
|
sub, err := client.Subscribe(dest, stomp.HandlerFunc(func(m *stomp.Message) {
|
2016-09-26 08:29:05 +00:00
|
|
|
if m.Header.GetBool("eof") {
|
2016-09-26 05:39:28 +00:00
|
|
|
done <- true
|
2016-09-28 00:33:13 +00:00
|
|
|
} else {
|
|
|
|
logs <- m.Body
|
2016-09-26 05:39:28 +00:00
|
|
|
}
|
2016-09-28 00:33:13 +00:00
|
|
|
m.Release()
|
2016-09-26 05:39:28 +00:00
|
|
|
}))
|
2016-07-08 22:40:29 +00:00
|
|
|
if err != nil {
|
2016-09-26 05:39:28 +00:00
|
|
|
logrus.Errorf("Unable to read logs from broker. %s", err)
|
2016-07-08 22:40:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
2016-09-26 05:39:28 +00:00
|
|
|
client.Unsubscribe(sub)
|
2016-09-28 00:33:13 +00:00
|
|
|
close(done)
|
|
|
|
close(logs)
|
2016-07-08 22:40:29 +00:00
|
|
|
}()
|
|
|
|
|
2016-09-26 05:39:28 +00:00
|
|
|
for {
|
|
|
|
select {
|
2016-09-28 00:33:13 +00:00
|
|
|
case buf := <-logs:
|
|
|
|
ws.SetWriteDeadline(time.Now().Add(writeWait))
|
|
|
|
ws.WriteMessage(websocket.TextMessage, buf)
|
2016-09-26 05:39:28 +00:00
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(writeWait))
|
|
|
|
if err != nil {
|
2016-07-08 22:40:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventStream produces the User event stream, sending all repository, build
|
|
|
|
// and agent events to the client.
|
|
|
|
func EventStream(c *gin.Context) {
|
|
|
|
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(websocket.HandshakeError); !ok {
|
|
|
|
logrus.Errorf("Cannot upgrade websocket. %s", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logrus.Debugf("Successfull upgraded websocket")
|
|
|
|
|
|
|
|
user := session.User(c)
|
|
|
|
repo := map[string]bool{}
|
|
|
|
if user != nil {
|
|
|
|
repo, _ = cache.GetRepoMap(c, user)
|
|
|
|
}
|
|
|
|
|
2016-09-26 05:39:28 +00:00
|
|
|
eventc := make(chan []byte, 10)
|
2016-07-08 22:40:29 +00:00
|
|
|
quitc := make(chan bool)
|
2016-09-26 05:39:28 +00:00
|
|
|
tick := time.NewTicker(pingPeriod)
|
2016-07-08 22:40:29 +00:00
|
|
|
defer func() {
|
2016-09-26 05:39:28 +00:00
|
|
|
tick.Stop()
|
2016-07-08 22:40:29 +00:00
|
|
|
ws.Close()
|
|
|
|
logrus.Debug("Successfully closed websocket")
|
|
|
|
}()
|
|
|
|
|
2016-09-26 05:39:28 +00:00
|
|
|
client := stomp.MustFromContext(c)
|
|
|
|
sub, err := client.Subscribe("/topic/events", stomp.HandlerFunc(func(m *stomp.Message) {
|
|
|
|
name := m.Header.GetString("repo")
|
|
|
|
priv := m.Header.GetBool("private")
|
|
|
|
if repo[name] || !priv {
|
|
|
|
eventc <- m.Body
|
|
|
|
}
|
|
|
|
m.Release()
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Unable to read logs from broker. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
2016-09-28 00:33:13 +00:00
|
|
|
client.Unsubscribe(sub)
|
2016-09-26 05:39:28 +00:00
|
|
|
close(quitc)
|
|
|
|
close(eventc)
|
|
|
|
}()
|
|
|
|
|
2016-07-08 22:40:29 +00:00
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
recover()
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-quitc:
|
|
|
|
return
|
2016-09-26 05:39:28 +00:00
|
|
|
case event, ok := <-eventc:
|
|
|
|
if !ok {
|
2016-07-08 22:40:29 +00:00
|
|
|
return
|
|
|
|
}
|
2016-09-26 05:39:28 +00:00
|
|
|
ws.SetWriteDeadline(time.Now().Add(writeWait))
|
|
|
|
ws.WriteMessage(websocket.TextMessage, event)
|
|
|
|
case <-tick.C:
|
2016-07-08 22:40:29 +00:00
|
|
|
err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(writeWait))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
reader(ws)
|
|
|
|
}
|
|
|
|
|
|
|
|
func reader(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
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|