2015-04-08 22:43:59 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-05-19 04:53:34 +00:00
|
|
|
"fmt"
|
2015-05-17 02:46:12 +00:00
|
|
|
"io"
|
2015-05-18 22:47:13 +00:00
|
|
|
"net/http"
|
2015-04-30 02:57:43 +00:00
|
|
|
"strconv"
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-05-17 18:10:43 +00:00
|
|
|
"github.com/drone/drone/pkg/bus"
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
log "github.com/drone/drone/Godeps/_workspace/src/github.com/Sirupsen/logrus"
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin"
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/manucorporat/sse"
|
2015-05-18 06:07:10 +00:00
|
|
|
"github.com/drone/drone/pkg/docker"
|
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-18 06:07:10 +00:00
|
|
|
close(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 {
|
2015-05-18 06:07:10 +00:00
|
|
|
sse.Encode(w, sse.Event{
|
|
|
|
Event: "message",
|
|
|
|
Data: string(event.Msg),
|
|
|
|
})
|
2015-05-17 03:35:41 +00:00
|
|
|
}
|
|
|
|
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-19 04:53:34 +00:00
|
|
|
conf := ToSettings(c)
|
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"))
|
2015-06-19 00:36:52 +00:00
|
|
|
jobnum, _ := strconv.Atoi(c.Params.ByName("number"))
|
2015-04-30 02:57:43 +00:00
|
|
|
|
2015-05-18 06:07:10 +00:00
|
|
|
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
2015-05-06 07:56:06 +00:00
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
build, err := store.BuildNumber(repo, commitseq)
|
2015-05-11 07:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
job, err := store.JobNumber(build, jobnum)
|
2015-05-11 07:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-18 22:47:13 +00:00
|
|
|
var rc io.ReadCloser
|
|
|
|
|
2015-05-19 03:44:44 +00:00
|
|
|
// if the commit is being executed by an agent
|
|
|
|
// we'll proxy the build output directly to the
|
|
|
|
// remote Docker client, through the agent.
|
2015-05-28 07:18:46 +00:00
|
|
|
if conf.Agents.Secret != "" {
|
2015-06-23 03:45:08 +00:00
|
|
|
addr, err := store.Agent(build)
|
2015-05-19 04:53:34 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
2015-06-19 00:36:52 +00:00
|
|
|
url := fmt.Sprintf("http://%s/stream/%d?token=%s", addr, job.ID, conf.Agents.Secret)
|
2015-05-19 04:53:34 +00:00
|
|
|
resp, err := http.Get(url)
|
2015-05-18 22:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
} else if resp.StatusCode != 200 {
|
|
|
|
resp.Body.Close()
|
|
|
|
c.AbortWithStatus(resp.StatusCode)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rc = resp.Body
|
2015-05-19 03:44:44 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// else if the commit is not being executed
|
|
|
|
// by the build agent we can use the local runner
|
2015-06-19 00:36:52 +00:00
|
|
|
rc, err = runner.Logs(job)
|
2015-05-19 03:44:44 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
2015-04-30 02:57:43 +00:00
|
|
|
}
|
2015-05-18 22:47:13 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
rc.Close()
|
|
|
|
}()
|
|
|
|
|
2015-05-06 07:56:06 +00:00
|
|
|
go func() {
|
2015-05-18 06:07:10 +00:00
|
|
|
<-c.Writer.CloseNotify()
|
|
|
|
rc.Close()
|
2015-05-06 07:56:06 +00:00
|
|
|
}()
|
|
|
|
|
2015-05-18 06:07:10 +00:00
|
|
|
rw := &StreamWriter{c.Writer, 0}
|
2015-05-06 07:56:06 +00:00
|
|
|
|
2015-05-18 06:07:10 +00:00
|
|
|
docker.StdCopy(rw, rw, rc)
|
|
|
|
}
|
2015-05-06 07:56:06 +00:00
|
|
|
|
2015-05-18 06:07:10 +00:00
|
|
|
type StreamWriter struct {
|
|
|
|
writer gin.ResponseWriter
|
|
|
|
count int
|
2015-04-30 02:57:43 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 06:07:10 +00:00
|
|
|
func (w *StreamWriter) Write(data []byte) (int, error) {
|
|
|
|
var err = sse.Encode(w.writer, sse.Event{
|
|
|
|
Id: strconv.Itoa(w.count),
|
|
|
|
Event: "message",
|
|
|
|
Data: string(data),
|
2015-04-08 22:43:59 +00:00
|
|
|
})
|
2015-05-18 06:07:10 +00:00
|
|
|
w.writer.Flush()
|
|
|
|
w.count += len(data)
|
|
|
|
return len(data), err
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|