service logs: Support no-follow mode

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
(cherry picked from commit c2d435e4f0)
Signed-off-by: Victor Vieux <victorvieux@gmail.com>
This commit is contained in:
Andrea Luzzardi 2016-11-18 12:38:36 -08:00 committed by Victor Vieux
parent af08f44640
commit 43e6479fd4
4 changed files with 44 additions and 13 deletions

View file

@ -237,10 +237,6 @@ func (sr *swarmRouter) getServiceLogs(ctx context.Context, w http.ResponseWriter
OutStream: w,
}
if !logsConfig.Follow {
return fmt.Errorf("Bad parameters: Only follow mode is currently supported")
}
if logsConfig.Details {
return fmt.Errorf("Bad parameters: details is not currently supported")
}

View file

@ -1278,7 +1278,7 @@ func (c *Cluster) ServiceLogs(ctx context.Context, input string, config *backend
ServiceIDs: []string{service.ID},
},
Options: &swarmapi.LogSubscriptionOptions{
Follow: true,
Follow: config.Follow,
},
})
if err != nil {

View file

@ -419,7 +419,11 @@ func (c *containerAdapter) logs(ctx context.Context, options api.LogSubscription
}
chStarted := make(chan struct{})
go c.backend.ContainerLogs(ctx, c.container.name(), apiOptions, chStarted)
go func() {
defer writer.Close()
c.backend.ContainerLogs(ctx, c.container.name(), apiOptions, chStarted)
}()
return reader, nil
}

View file

@ -4,6 +4,7 @@ package main
import (
"bufio"
"fmt"
"io"
"os/exec"
"strings"
@ -24,7 +25,27 @@ func (s *DockerSwarmSuite) TestServiceLogs(c *check.C) {
name := "TestServiceLogs"
out, err := d.Cmd("service", "create", "--name", name, "busybox", "sh", "-c", "while true; do echo log test; sleep 1; done")
out, err := d.Cmd("service", "create", "--name", name, "--restart-condition", "none", "busybox", "sh", "-c", "echo hello world")
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
// make sure task has been deployed.
waitAndAssert(c, defaultReconciliationTimeout, d.checkActiveContainerCount, checker.Equals, 1)
out, err = d.Cmd("service", "logs", name)
fmt.Println(out)
c.Assert(err, checker.IsNil)
c.Assert(out, checker.Contains, "hello world")
}
func (s *DockerSwarmSuite) TestServiceLogsFollow(c *check.C) {
testRequires(c, ExperimentalDaemon)
d := s.AddDaemon(c, true, true)
name := "TestServiceLogsFollow"
out, err := d.Cmd("service", "create", "--name", name, "busybox", "sh", "-c", "while true; do echo log test; sleep 0.1; done")
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
@ -40,16 +61,26 @@ func (s *DockerSwarmSuite) TestServiceLogs(c *check.C) {
// Make sure pipe is written to
ch := make(chan *logMessage)
done := make(chan struct{})
go func() {
reader := bufio.NewReader(r)
for {
msg := &logMessage{}
msg.data, _, msg.err = reader.ReadLine()
ch <- msg
select {
case ch <- msg:
case <-done:
return
}
}
}()
for i := 0; i < 3; i++ {
msg := <-ch
c.Assert(msg.err, checker.IsNil)
c.Assert(string(msg.data), checker.Contains, "log test")
}
close(done)
c.Assert(cmd.Process.Kill(), checker.IsNil)
}