summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-rw-r--r--pkg/adapter/containers.go81
-rw-r--r--pkg/adapter/containers_remote.go109
-rw-r--r--pkg/adapter/runtime.go9
-rw-r--r--pkg/adapter/runtime_remote.go76
-rw-r--r--pkg/adapter/shortcuts/shortcuts.go27
-rw-r--r--pkg/logs/logs.go22
-rw-r--r--pkg/varlinkapi/containers.go16
7 files changed, 248 insertions, 92 deletions
diff --git a/pkg/adapter/containers.go b/pkg/adapter/containers.go
new file mode 100644
index 000000000..7514f30d2
--- /dev/null
+++ b/pkg/adapter/containers.go
@@ -0,0 +1,81 @@
+// +build !remoteclient
+
+package adapter
+
+import (
+ "context"
+
+ "github.com/containers/libpod/cmd/podman/cliconfig"
+ "github.com/containers/libpod/libpod"
+ "github.com/containers/libpod/pkg/adapter/shortcuts"
+ "github.com/pkg/errors"
+ "github.com/sirupsen/logrus"
+)
+
+// GetLatestContainer gets the latest Container and wraps it in an adapter Container
+func (r *LocalRuntime) GetLatestContainer() (*Container, error) {
+ Container := Container{}
+ c, err := r.Runtime.GetLatestContainer()
+ Container.Container = c
+ return &Container, err
+}
+
+// GetAllContainers gets all Containers and wraps each one in an adapter Container
+func (r *LocalRuntime) GetAllContainers() ([]*Container, error) {
+ var containers []*Container
+ allContainers, err := r.Runtime.GetAllContainers()
+ if err != nil {
+ return nil, err
+ }
+
+ for _, c := range allContainers {
+ containers = append(containers, &Container{c})
+ }
+ return containers, nil
+}
+
+// LookupContainer gets a Container by name or id and wraps it in an adapter Container
+func (r *LocalRuntime) LookupContainer(idOrName string) (*Container, error) {
+ ctr, err := r.Runtime.LookupContainer(idOrName)
+ if err != nil {
+ return nil, err
+ }
+ return &Container{ctr}, nil
+}
+
+// StopContainers stops container(s) based on CLI inputs.
+// Returns list of successful id(s), map of failed id(s) + error, or error not from container
+func (r *LocalRuntime) StopContainers(ctx context.Context, cli *cliconfig.StopValues) ([]string, map[string]error, error) {
+ var timeout *uint
+ if cli.Flags().Changed("timeout") || cli.Flags().Changed("time") {
+ t := uint(cli.Timeout)
+ timeout = &t
+ }
+
+ var (
+ ok = []string{}
+ failures = map[string]error{}
+ )
+
+ ctrs, err := shortcuts.GetContainersByContext(cli.All, cli.Latest, cli.InputArgs, r.Runtime)
+ if err != nil {
+ return ok, failures, err
+ }
+
+ for _, c := range ctrs {
+ if timeout == nil {
+ t := c.StopTimeout()
+ timeout = &t
+ logrus.Debugf("Set timeout to container %s default (%d)", c.ID(), *timeout)
+ }
+ if err := c.StopWithTimeout(*timeout); err == nil {
+ ok = append(ok, c.ID())
+ } else if errors.Cause(err) == libpod.ErrCtrStopped {
+ ok = append(ok, c.ID())
+ logrus.Debugf("Container %s is already stopped", c.ID())
+ } else {
+ failures[c.ID()] = err
+ }
+ }
+ return ok, failures, nil
+}
diff --git a/pkg/adapter/containers_remote.go b/pkg/adapter/containers_remote.go
index 3f43a6905..df40c8efd 100644
--- a/pkg/adapter/containers_remote.go
+++ b/pkg/adapter/containers_remote.go
@@ -3,8 +3,13 @@
package adapter
import (
+ "context"
"encoding/json"
+ "errors"
+
+ "github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/shared"
+ "github.com/sirupsen/logrus"
iopodman "github.com/containers/libpod/cmd/podman/varlink"
"github.com/containers/libpod/libpod"
@@ -29,6 +34,70 @@ func (c *Container) ID() string {
return c.config.ID
}
+// Config returns a container config
+func (r *LocalRuntime) Config(name string) *libpod.ContainerConfig {
+ // TODO the Spec being returned is not populated. Matt and I could not figure out why. Will defer
+ // further looking into it for after devconf.
+ // The libpod function for this has no errors so we are kind of in a tough
+ // spot here. Logging the errors for now.
+ reply, err := iopodman.ContainerConfig().Call(r.Conn, name)
+ if err != nil {
+ logrus.Error("call to container.config failed")
+ }
+ data := libpod.ContainerConfig{}
+ if err := json.Unmarshal([]byte(reply), &data); err != nil {
+ logrus.Error("failed to unmarshal container inspect data")
+ }
+ return &data
+
+}
+
+// ContainerState returns the "state" of the container.
+func (r *LocalRuntime) ContainerState(name string) (*libpod.ContainerState, error) { // no-lint
+ reply, err := iopodman.ContainerStateData().Call(r.Conn, name)
+ if err != nil {
+ return nil, err
+ }
+ data := libpod.ContainerState{}
+ if err := json.Unmarshal([]byte(reply), &data); err != nil {
+ return nil, err
+ }
+ return &data, err
+
+}
+
+// LookupContainer gets basic information about container over a varlink
+// connection and then translates it to a *Container
+func (r *LocalRuntime) LookupContainer(idOrName string) (*Container, error) {
+ state, err := r.ContainerState(idOrName)
+ if err != nil {
+ return nil, err
+ }
+ config := r.Config(idOrName)
+ if err != nil {
+ return nil, err
+ }
+
+ return &Container{
+ remoteContainer{
+ r,
+ config,
+ state,
+ },
+ }, nil
+}
+
+func (r *LocalRuntime) GetLatestContainer() (*Container, error) {
+ reply, err := iopodman.GetContainersByContext().Call(r.Conn, false, true, nil)
+ if err != nil {
+ return nil, err
+ }
+ if len(reply) > 0 {
+ return r.LookupContainer(reply[0])
+ }
+ return nil, errors.New("no containers exist")
+}
+
// GetArtifact returns a container's artifacts
func (c *Container) GetArtifact(name string) ([]byte, error) {
var data []byte
@@ -55,18 +124,42 @@ func (c *Container) Name() string {
return c.config.Name
}
+// StopContainers stops requested containers using CLI inputs.
+// Returns the list of stopped container ids, map of failed to stop container ids + errors, or any non-container error
+func (r *LocalRuntime) StopContainers(ctx context.Context, cli *cliconfig.StopValues) ([]string, map[string]error, error) {
+ var (
+ ok = []string{}
+ failures = map[string]error{}
+ )
+
+ ids, err := iopodman.GetContainersByContext().Call(r.Conn, cli.All, cli.Latest, cli.InputArgs)
+ if err != nil {
+ return ok, failures, err
+ }
+
+ for _, id := range ids {
+ stopped, err := iopodman.StopContainer().Call(r.Conn, id, int64(cli.Timeout))
+ if err != nil {
+ failures[id] = err
+ } else {
+ ok = append(ok, stopped)
+ }
+ }
+ return ok, failures, nil
+}
+
// BatchContainerOp is wrapper func to mimic shared's function with a similar name meant for libpod
func BatchContainerOp(ctr *Container, opts shared.PsOptions) (shared.BatchContainerStruct, error) {
// TODO If pod ps ever shows container's sizes, re-enable this code; otherwise it isn't needed
// and would be a perf hit
- //data, err := ctr.Inspect(true)
- //if err != nil {
- // return shared.BatchContainerStruct{}, err
- //}
+ // data, err := ctr.Inspect(true)
+ // if err != nil {
+ // return shared.BatchContainerStruct{}, err
+ // }
//
- //size := new(shared.ContainerSize)
- //size.RootFsSize = data.SizeRootFs
- //size.RwSize = data.SizeRw
+ // size := new(shared.ContainerSize)
+ // size.RootFsSize = data.SizeRootFs
+ // size.RwSize = data.SizeRw
bcs := shared.BatchContainerStruct{
ConConfig: ctr.config,
@@ -75,7 +168,7 @@ func BatchContainerOp(ctr *Container, opts shared.PsOptions) (shared.BatchContai
Pid: ctr.state.PID,
StartedTime: ctr.state.StartedTime,
ExitedTime: ctr.state.FinishedTime,
- //Size: size,
+ // Size: size,
}
return bcs, nil
}
diff --git a/pkg/adapter/runtime.go b/pkg/adapter/runtime.go
index 8624981b1..5be2ca150 100644
--- a/pkg/adapter/runtime.go
+++ b/pkg/adapter/runtime.go
@@ -108,15 +108,6 @@ func (r *LocalRuntime) RemoveImage(ctx context.Context, img *ContainerImage, for
return r.Runtime.RemoveImage(ctx, img.Image, force)
}
-// LookupContainer ...
-func (r *LocalRuntime) LookupContainer(idOrName string) (*Container, error) {
- ctr, err := r.Runtime.LookupContainer(idOrName)
- if err != nil {
- return nil, err
- }
- return &Container{ctr}, nil
-}
-
// PruneImages is wrapper into PruneImages within the image pkg
func (r *LocalRuntime) PruneImages(all bool) ([]string, error) {
return r.ImageRuntime().PruneImages(all)
diff --git a/pkg/adapter/runtime_remote.go b/pkg/adapter/runtime_remote.go
index 29b43e9b0..14cda7bff 100644
--- a/pkg/adapter/runtime_remote.go
+++ b/pkg/adapter/runtime_remote.go
@@ -5,7 +5,6 @@ package adapter
import (
"bufio"
"context"
- "encoding/json"
"fmt"
"io"
"io/ioutil"
@@ -49,14 +48,13 @@ func GetRuntime(c *cliconfig.PodmanCommand) (*LocalRuntime, error) {
if err != nil {
return nil, err
}
- rr := RemoteRuntime{
- Conn: conn,
- Remote: true,
- }
- foo := LocalRuntime{
- &rr,
- }
- return &foo, nil
+
+ return &LocalRuntime{
+ &RemoteRuntime{
+ Conn: conn,
+ Remote: true,
+ },
+ }, nil
}
// Shutdown is a bogus wrapper for compat with the libpod runtime
@@ -315,66 +313,6 @@ func (ci *ContainerImage) History(ctx context.Context) ([]*image.History, error)
return imageHistories, nil
}
-// LookupContainer gets basic information about container over a varlink
-// connection and then translates it to a *Container
-func (r *LocalRuntime) LookupContainer(idOrName string) (*Container, error) {
- state, err := r.ContainerState(idOrName)
- if err != nil {
- return nil, err
- }
- config := r.Config(idOrName)
- if err != nil {
- return nil, err
- }
-
- rc := remoteContainer{
- r,
- config,
- state,
- }
-
- c := Container{
- rc,
- }
- return &c, nil
-}
-
-func (r *LocalRuntime) GetLatestContainer() (*Container, error) {
- return nil, libpod.ErrNotImplemented
-}
-
-// ContainerState returns the "state" of the container.
-func (r *LocalRuntime) ContainerState(name string) (*libpod.ContainerState, error) { //no-lint
- reply, err := iopodman.ContainerStateData().Call(r.Conn, name)
- if err != nil {
- return nil, err
- }
- data := libpod.ContainerState{}
- if err := json.Unmarshal([]byte(reply), &data); err != nil {
- return nil, err
- }
- return &data, err
-
-}
-
-// Config returns a container config
-func (r *LocalRuntime) Config(name string) *libpod.ContainerConfig {
- // TODO the Spec being returned is not populated. Matt and I could not figure out why. Will defer
- // further looking into it for after devconf.
- // The libpod function for this has no errors so we are kind of in a tough
- // spot here. Logging the errors for now.
- reply, err := iopodman.ContainerConfig().Call(r.Conn, name)
- if err != nil {
- logrus.Error("call to container.config failed")
- }
- data := libpod.ContainerConfig{}
- if err := json.Unmarshal([]byte(reply), &data); err != nil {
- logrus.Error("failed to unmarshal container inspect data")
- }
- return &data
-
-}
-
// PruneImages is the wrapper call for a remote-client to prune images
func (r *LocalRuntime) PruneImages(all bool) ([]string, error) {
return iopodman.ImagesPrune().Call(r.Conn, all)
diff --git a/pkg/adapter/shortcuts/shortcuts.go b/pkg/adapter/shortcuts/shortcuts.go
index 0633399ae..677d88457 100644
--- a/pkg/adapter/shortcuts/shortcuts.go
+++ b/pkg/adapter/shortcuts/shortcuts.go
@@ -25,3 +25,30 @@ func GetPodsByContext(all, latest bool, pods []string, runtime *libpod.Runtime)
}
return outpods, nil
}
+
+// GetContainersByContext gets pods whether all, latest, or a slice of names/ids
+func GetContainersByContext(all, latest bool, names []string, runtime *libpod.Runtime) ([]*libpod.Container, error) {
+ var ctrs = []*libpod.Container{}
+
+ if all {
+ return runtime.GetAllContainers()
+ }
+
+ if latest {
+ c, err := runtime.GetLatestContainer()
+ if err != nil {
+ return nil, err
+ }
+ ctrs = append(ctrs, c)
+ return ctrs, nil
+ }
+
+ for _, c := range names {
+ ctr, err := runtime.LookupContainer(c)
+ if err != nil {
+ return nil, err
+ }
+ ctrs = append(ctrs, ctr)
+ }
+ return ctrs, nil
+}
diff --git a/pkg/logs/logs.go b/pkg/logs/logs.go
index b104c592b..7fb5c7ea8 100644
--- a/pkg/logs/logs.go
+++ b/pkg/logs/logs.go
@@ -35,7 +35,10 @@ import (
const (
// timeFormat is the time format used in the log.
- timeFormat = time.RFC3339Nano
+ // It is a modified version of RFC3339Nano that guarantees trailing
+ // zeroes are not trimmed, taken from
+ // https://github.com/golang/go/issues/19635
+ timeFormat = "2006-01-02T15:04:05.000000000Z07:00"
)
// LogStreamType is the type of the stream in CRI container log.
@@ -277,10 +280,11 @@ func readLog(reader *bufio.Reader, opts *LogOptions) []string {
// logWriter controls the writing into the stream based on the log options.
type logWriter struct {
- stdout io.Writer
- stderr io.Writer
- opts *LogOptions
- remain int64
+ stdout io.Writer
+ stderr io.Writer
+ opts *LogOptions
+ remain int64
+ doAppend bool
}
// errMaximumWrite is returned when all bytes have been written.
@@ -309,9 +313,15 @@ func (w *logWriter) write(msg *logMessage) error {
return nil
}
line := msg.log
- if w.opts.Timestamps {
+ if w.opts.Timestamps && !w.doAppend {
prefix := append([]byte(msg.timestamp.Format(timeFormat)), delimiter[0])
line = append(prefix, line...)
+ if len(line) > 0 && line[len(line)-1] != '\n' {
+ w.doAppend = true
+ }
+ }
+ if w.doAppend && len(line) > 0 && line[len(line)-1] == '\n' {
+ w.doAppend = false
}
// If the line is longer than the remaining bytes, cut it.
if int64(len(line)) > w.remain {
diff --git a/pkg/varlinkapi/containers.go b/pkg/varlinkapi/containers.go
index ad9f107a7..27b8d15d2 100644
--- a/pkg/varlinkapi/containers.go
+++ b/pkg/varlinkapi/containers.go
@@ -13,6 +13,7 @@ import (
"github.com/containers/libpod/cmd/podman/shared"
"github.com/containers/libpod/cmd/podman/varlink"
"github.com/containers/libpod/libpod"
+ "github.com/containers/libpod/pkg/adapter/shortcuts"
cc "github.com/containers/libpod/pkg/spec"
"github.com/containers/storage/pkg/archive"
"github.com/pkg/errors"
@@ -60,6 +61,21 @@ func (i *LibpodAPI) GetContainer(call iopodman.VarlinkCall, id string) error {
return call.ReplyGetContainer(makeListContainer(ctr.ID(), batchInfo))
}
+// GetContainersByContext returns a slice of container ids based on all, latest, or a list
+func (i *LibpodAPI) GetContainersByContext(call iopodman.VarlinkCall, all, latest bool, input []string) error {
+ var ids []string
+
+ ctrs, err := shortcuts.GetContainersByContext(all, latest, input, i.Runtime)
+ if err != nil {
+ return call.ReplyErrorOccurred(err.Error())
+ }
+
+ for _, c := range ctrs {
+ ids = append(ids, c.ID())
+ }
+ return call.ReplyGetContainersByContext(ids)
+}
+
// InspectContainer ...
func (i *LibpodAPI) InspectContainer(call iopodman.VarlinkCall, name string) error {
ctr, err := i.Runtime.LookupContainer(name)