mirror of
https://github.com/containers/podman.git
synced 2025-06-22 18:08:11 +08:00
@ -21,7 +21,6 @@ func getMainCommands() []*cobra.Command {
|
||||
_refreshCommand,
|
||||
_searchCommand,
|
||||
_statsCommand,
|
||||
_topCommand,
|
||||
}
|
||||
|
||||
if len(_varlinkCommand.Use) > 0 {
|
||||
@ -53,7 +52,6 @@ func getContainerSubCommands() []*cobra.Command {
|
||||
_runlabelCommand,
|
||||
_statsCommand,
|
||||
_stopCommand,
|
||||
_topCommand,
|
||||
_umountCommand,
|
||||
}
|
||||
}
|
||||
|
@ -64,6 +64,7 @@ var (
|
||||
_runCommand,
|
||||
_rmCommand,
|
||||
_startCommand,
|
||||
_topCommand,
|
||||
_unpauseCommand,
|
||||
_waitCommand,
|
||||
}
|
||||
|
@ -57,6 +57,7 @@ var mainCommands = []*cobra.Command{
|
||||
_saveCommand,
|
||||
_stopCommand,
|
||||
_tagCommand,
|
||||
_topCommand,
|
||||
_umountCommand,
|
||||
_unpauseCommand,
|
||||
_versionCommand,
|
||||
|
@ -7,8 +7,8 @@ import (
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/containers/libpod/cmd/podman/cliconfig"
|
||||
"github.com/containers/libpod/cmd/podman/libpodruntime"
|
||||
"github.com/containers/libpod/libpod"
|
||||
"github.com/containers/libpod/pkg/adapter"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@ -60,7 +60,6 @@ func init() {
|
||||
}
|
||||
|
||||
func topCmd(c *cliconfig.TopValues) error {
|
||||
var container *libpod.Container
|
||||
var err error
|
||||
args := c.InputArgs
|
||||
|
||||
@ -77,37 +76,16 @@ func topCmd(c *cliconfig.TopValues) error {
|
||||
return errors.Errorf("you must provide the name or id of a running container")
|
||||
}
|
||||
|
||||
runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
|
||||
runtime, err := adapter.GetRuntime(&c.PodmanCommand)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "error creating libpod runtime")
|
||||
}
|
||||
defer runtime.Shutdown(false)
|
||||
|
||||
var descriptors []string
|
||||
if c.Latest {
|
||||
descriptors = args
|
||||
container, err = runtime.GetLatestContainer()
|
||||
} else {
|
||||
descriptors = args[1:]
|
||||
container, err = runtime.LookupContainer(args[0])
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to lookup requested container")
|
||||
}
|
||||
|
||||
conStat, err := container.State()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to look up state for %s", args[0])
|
||||
}
|
||||
if conStat != libpod.ContainerStateRunning {
|
||||
return errors.Errorf("top can only be used on running containers")
|
||||
}
|
||||
psOutput, err := container.GetContainerPidInformation(descriptors)
|
||||
psOutput, err := runtime.Top(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
|
||||
for _, proc := range psOutput {
|
||||
fmt.Fprintln(w, proc)
|
||||
|
@ -524,6 +524,8 @@ method Ps(opts: PsOpts) -> (containers: []PsContainer)
|
||||
|
||||
method GetContainersByStatus(status: []string) -> (containerS: []Container)
|
||||
|
||||
method Top (nameOrID: string, descriptors: []string) -> (top: []string)
|
||||
|
||||
# GetContainer returns information about a single container. If a container
|
||||
# with the given id doesn't exist, a [ContainerNotFound](#ContainerNotFound)
|
||||
# error will be returned. See also [ListContainers](ListContainers) and
|
||||
|
@ -7,8 +7,22 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/containers/psgo"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Top gathers statistics about the running processes in a container. It returns a
|
||||
// []string for output
|
||||
func (c *Container) Top(descriptors []string) ([]string, error) {
|
||||
conStat, err := c.State()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "unable to look up state for %s", c.ID())
|
||||
}
|
||||
if conStat != ContainerStateRunning {
|
||||
return nil, errors.Errorf("top can only be used on running containers")
|
||||
}
|
||||
return c.GetContainerPidInformation(descriptors)
|
||||
}
|
||||
|
||||
// GetContainerPidInformation returns process-related data of all processes in
|
||||
// the container. The output data can be controlled via the `descriptors`
|
||||
// argument which expects format descriptors and supports all AIXformat
|
||||
|
@ -766,3 +766,23 @@ func (r *LocalRuntime) Restart(ctx context.Context, c *cliconfig.RestartValues)
|
||||
}
|
||||
return pool.Run()
|
||||
}
|
||||
|
||||
// Top display the running processes of a container
|
||||
func (r *LocalRuntime) Top(cli *cliconfig.TopValues) ([]string, error) {
|
||||
var (
|
||||
descriptors []string
|
||||
container *libpod.Container
|
||||
err error
|
||||
)
|
||||
if cli.Latest {
|
||||
descriptors = cli.InputArgs
|
||||
container, err = r.Runtime.GetLatestContainer()
|
||||
} else {
|
||||
descriptors = cli.InputArgs[1:]
|
||||
container, err = r.Runtime.LookupContainer(cli.InputArgs[0])
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "unable to lookup requested container")
|
||||
}
|
||||
return container.Top(descriptors)
|
||||
}
|
||||
|
@ -832,3 +832,23 @@ func (r *LocalRuntime) Restart(ctx context.Context, c *cliconfig.RestartValues)
|
||||
}
|
||||
return ok, failures, nil
|
||||
}
|
||||
|
||||
// Top display the running processes of a container
|
||||
func (r *LocalRuntime) Top(cli *cliconfig.TopValues) ([]string, error) {
|
||||
var (
|
||||
ctr *Container
|
||||
err error
|
||||
descriptors []string
|
||||
)
|
||||
if cli.Latest {
|
||||
ctr, err = r.GetLatestContainer()
|
||||
descriptors = cli.InputArgs
|
||||
} else {
|
||||
ctr, err = r.LookupContainer(cli.InputArgs[0])
|
||||
descriptors = cli.InputArgs[1:]
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return iopodman.Top().Call(r.Conn, ctr.ID(), descriptors)
|
||||
}
|
||||
|
@ -733,3 +733,16 @@ func newPodmanLogLine(line *libpod.LogLine) iopodman.LogLine {
|
||||
Cid: line.CID,
|
||||
}
|
||||
}
|
||||
|
||||
// Top displays information about a container's running processes
|
||||
func (i *LibpodAPI) Top(call iopodman.VarlinkCall, nameOrID string, descriptors []string) error {
|
||||
ctr, err := i.Runtime.LookupContainer(nameOrID)
|
||||
if err != nil {
|
||||
return call.ReplyContainerNotFound(ctr.ID(), err.Error())
|
||||
}
|
||||
topInfo, err := ctr.Top(descriptors)
|
||||
if err != nil {
|
||||
return call.ReplyErrorOccurred(err.Error())
|
||||
}
|
||||
return call.ReplyTop(topInfo)
|
||||
}
|
||||
|
Reference in New Issue
Block a user