From 21ebdb558cb939176d862e12bec99f34a1e5d4ba Mon Sep 17 00:00:00 2001 From: baude Date: Fri, 4 May 2018 10:39:34 -0500 Subject: Allow streaming on some varlink container methods The following methods should support streaming requests from the client: * GetContainerLogs A reference for a python stream implementation can be found here: https://github.com/varlink/python/blob/master/varlink/tests/test_orgexamplemore.py#L29-L42 Signed-off-by: baude Closes: #724 Approved by: mheon --- pkg/varlinkapi/containers.go | 33 ++++++++++++++++++++++++++++++--- pkg/varlinkapi/system.go | 2 +- 2 files changed, 31 insertions(+), 4 deletions(-) (limited to 'pkg/varlinkapi') diff --git a/pkg/varlinkapi/containers.go b/pkg/varlinkapi/containers.go index ece33556b..79eea4aa7 100644 --- a/pkg/varlinkapi/containers.go +++ b/pkg/varlinkapi/containers.go @@ -4,8 +4,10 @@ import ( "bufio" "encoding/json" "fmt" + "io" "os" "syscall" + "time" "github.com/pkg/errors" "github.com/projectatomic/libpod/cmd/podman/batchcontainer" @@ -117,6 +119,7 @@ func (i *LibpodAPI) ListContainerProcesses(call ioprojectatomicpodman.VarlinkCal if err != nil { return call.ReplyErrorOccurred(err.Error()) } + return call.ReplyListContainerProcesses(psOutput) } @@ -148,12 +151,36 @@ func (i *LibpodAPI) GetContainerLogs(call ioprojectatomicpodman.VarlinkCall, nam } defer file.Close() reader := bufio.NewReader(file) + if call.WantsMore() { + call.Continues = true + } for { line, err := reader.ReadString('\n') - if err != nil { - break + // We've read the entire file + if err == io.EOF { + if !call.WantsMore() { + // If this is a non-following log request, we return what we have + break + } else { + // If we want to follow, return what we have, wipe the slice, and make + // sure the container is still running before iterating. + call.ReplyGetContainerLogs(logs) + logs = []string{} + time.Sleep(1 * time.Second) + state, err := ctr.State() + if err != nil { + return call.ReplyErrorOccurred(err.Error()) + } + if state != libpod.ContainerStateRunning && state != libpod.ContainerStatePaused { + return call.ReplyErrorOccurred(fmt.Sprintf("%s is no longer running", ctr.ID())) + } + + } + } else if err != nil { + return call.ReplyErrorOccurred(err.Error()) + } else { + logs = append(logs, line) } - logs = append(logs, line) } return call.ReplyGetContainerLogs(logs) } diff --git a/pkg/varlinkapi/system.go b/pkg/varlinkapi/system.go index 976dfc682..418db6445 100644 --- a/pkg/varlinkapi/system.go +++ b/pkg/varlinkapi/system.go @@ -1,7 +1,7 @@ package varlinkapi import ( - ioprojectatomicpodman "github.com/projectatomic/libpod/cmd/podman/varlink" + "github.com/projectatomic/libpod/cmd/podman/varlink" "github.com/projectatomic/libpod/libpod" ) -- cgit v1.2.3-54-g00ecf