podman-remote start

enable the ability to start containers from the remote-client.  also,
enable start integration tests for remote testing.

Signed-off-by: baude <bbaude@redhat.com>
This commit is contained in:
baude
2019-04-11 14:42:28 -05:00
parent a87cf6fef8
commit 3c0147e559
9 changed files with 166 additions and 123 deletions

View File

@ -22,7 +22,6 @@ func getMainCommands() []*cobra.Command {
_refreshCommand, _refreshCommand,
_restartCommand, _restartCommand,
_searchCommand, _searchCommand,
_startCommand,
_statsCommand, _statsCommand,
_topCommand, _topCommand,
_unpauseCommand, _unpauseCommand,
@ -57,7 +56,6 @@ func getContainerSubCommands() []*cobra.Command {
_restartCommand, _restartCommand,
_restoreCommand, _restoreCommand,
_runlabelCommand, _runlabelCommand,
_startCommand,
_statsCommand, _statsCommand,
_stopCommand, _stopCommand,
_topCommand, _topCommand,

View File

@ -61,6 +61,7 @@ var (
_logsCommand, _logsCommand,
_runCommand, _runCommand,
_rmCommand, _rmCommand,
_startCommand,
_waitCommand, _waitCommand,
} }
) )

View File

@ -59,6 +59,7 @@ var mainCommands = []*cobra.Command{
_versionCommand, _versionCommand,
_waitCommand, _waitCommand,
imageCommand.Command, imageCommand.Command,
_startCommand,
systemCommand.Command, systemCommand.Command,
} }

View File

@ -4,8 +4,10 @@ package shared
import ( import (
"fmt" "fmt"
"github.com/containers/libpod/cmd/podman/cliconfig" "github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/varlink" "github.com/containers/libpod/cmd/podman/varlink"
"github.com/containers/libpod/pkg/rootless"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -318,6 +320,12 @@ func VarlinkCreateToGeneric(opts iopodman.Create) GenericCLIResults {
// We do not get a default network over varlink. Unlike the other default values for some cli // We do not get a default network over varlink. Unlike the other default values for some cli
// elements, it seems it gets set to the default anyway. // elements, it seems it gets set to the default anyway.
var memSwapDefault int64 = -1
netModeDefault := "bridge"
if rootless.IsRootless() {
netModeDefault = "slirp4netns"
}
m := make(map[string]GenericCLIResult) m := make(map[string]GenericCLIResult)
m["add-host"] = stringSliceFromVarlink(opts.AddHost, "add-host", nil) m["add-host"] = stringSliceFromVarlink(opts.AddHost, "add-host", nil)
m["annotation"] = stringSliceFromVarlink(opts.Annotation, "annotation", nil) m["annotation"] = stringSliceFromVarlink(opts.Annotation, "annotation", nil)
@ -374,10 +382,10 @@ func VarlinkCreateToGeneric(opts iopodman.Create) GenericCLIResults {
m["memory"] = stringFromVarlink(opts.Memory, "memory", nil) m["memory"] = stringFromVarlink(opts.Memory, "memory", nil)
m["memory-reservation"] = stringFromVarlink(opts.MemoryReservation, "memory-reservation", nil) m["memory-reservation"] = stringFromVarlink(opts.MemoryReservation, "memory-reservation", nil)
m["memory-swap"] = stringFromVarlink(opts.MemorySwap, "memory-swap", nil) m["memory-swap"] = stringFromVarlink(opts.MemorySwap, "memory-swap", nil)
m["memory-swappiness"] = int64FromVarlink(opts.MemorySwappiness, "memory-swappiness", nil) m["memory-swappiness"] = int64FromVarlink(opts.MemorySwappiness, "memory-swappiness", &memSwapDefault)
m["name"] = stringFromVarlink(opts.Name, "name", nil) m["name"] = stringFromVarlink(opts.Name, "name", nil)
m["net"] = stringFromVarlink(opts.Net, "net", nil) m["net"] = stringFromVarlink(opts.Net, "net", &netModeDefault)
m["network"] = stringFromVarlink(opts.Network, "network", nil) m["network"] = stringFromVarlink(opts.Network, "network", &netModeDefault)
m["no-hosts"] = boolFromVarlink(opts.NoHosts, "no-hosts", false) m["no-hosts"] = boolFromVarlink(opts.NoHosts, "no-hosts", false)
m["oom-kill-disable"] = boolFromVarlink(opts.OomKillDisable, "oon-kill-disable", false) m["oom-kill-disable"] = boolFromVarlink(opts.OomKillDisable, "oon-kill-disable", false)
m["oom-score-adj"] = intFromVarlink(opts.OomScoreAdj, "oom-score-adj", nil) m["oom-score-adj"] = intFromVarlink(opts.OomScoreAdj, "oom-score-adj", nil)

View File

@ -1,16 +1,11 @@
package main package main
import ( import (
"fmt"
"os"
"github.com/containers/libpod/cmd/podman/cliconfig" "github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/libpod" "github.com/containers/libpod/libpod"
"github.com/containers/libpod/pkg/adapter" "github.com/containers/libpod/pkg/adapter"
opentracing "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -48,7 +43,7 @@ func init() {
} }
func startCmd(c *cliconfig.StartValues) error { func startCmd(c *cliconfig.StartValues) error {
if c.Bool("trace") { if !remoteclient && c.Bool("trace") {
span, _ := opentracing.StartSpanFromContext(Ctx, "startCmd") span, _ := opentracing.StartSpanFromContext(Ctx, "startCmd")
defer span.Finish() defer span.Finish()
} }
@ -70,100 +65,11 @@ func startCmd(c *cliconfig.StartValues) error {
return errors.Wrapf(libpod.ErrInvalidArg, "you cannot use sig-proxy without --attach") return errors.Wrapf(libpod.ErrInvalidArg, "you cannot use sig-proxy without --attach")
} }
runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand) runtime, err := adapter.GetRuntime(&c.PodmanCommand)
if err != nil { if err != nil {
return errors.Wrapf(err, "error creating libpod runtime") return errors.Wrapf(err, "error creating libpod runtime")
} }
defer runtime.Shutdown(false) defer runtime.Shutdown(false)
if c.Latest { exitCode, err = runtime.Start(getContext(), c, sigProxy)
lastCtr, err := runtime.GetLatestContainer() return err
if err != nil {
return errors.Wrapf(err, "unable to get latest container")
}
args = append(args, lastCtr.ID())
}
ctx := getContext()
var lastError error
for _, container := range args {
ctr, err := runtime.LookupContainer(container)
if err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "unable to find container %s", container)
continue
}
ctrState, err := ctr.State()
if err != nil {
return errors.Wrapf(err, "unable to get container state")
}
ctrRunning := ctrState == libpod.ContainerStateRunning
if attach {
inputStream := os.Stdin
if !c.Interactive {
inputStream = nil
}
// attach to the container and also start it not already running
// If the container is in a pod, also set to recursively start dependencies
err = adapter.StartAttachCtr(ctx, ctr, os.Stdout, os.Stderr, inputStream, c.DetachKeys, sigProxy, !ctrRunning, ctr.PodID() != "")
if errors.Cause(err) == libpod.ErrDetach {
// User manually detached
// Exit cleanly immediately
exitCode = 0
return nil
}
if ctrRunning {
return err
}
if err != nil {
return errors.Wrapf(err, "unable to start container %s", ctr.ID())
}
if ecode, err := ctr.Wait(); err != nil {
if errors.Cause(err) == libpod.ErrNoSuchCtr {
// The container may have been removed
// Go looking for an exit file
rtc, err := runtime.GetConfig()
if err != nil {
return err
}
ctrExitCode, err := adapter.ReadExitFile(rtc.TmpDir, ctr.ID())
if err != nil {
logrus.Errorf("Cannot get exit code: %v", err)
exitCode = 127
} else {
exitCode = ctrExitCode
}
}
} else {
exitCode = int(ecode)
}
return nil
}
if ctrRunning {
fmt.Println(ctr.ID())
continue
}
// Handle non-attach start
// If the container is in a pod, also set to recursively start dependencies
if err := ctr.Start(ctx, ctr.PodID() != ""); err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "unable to start container %q", container)
continue
}
fmt.Println(container)
}
return lastError
} }

View File

@ -510,3 +510,100 @@ func (r *LocalRuntime) Restore(c *cliconfig.RestoreValues, options libpod.Contai
} }
return lastError return lastError
} }
// Start will start a container
func (r *LocalRuntime) Start(ctx context.Context, c *cliconfig.StartValues, sigProxy bool) (int, error) {
var (
exitCode = 125
lastError error
)
args := c.InputArgs
if c.Latest {
lastCtr, err := r.GetLatestContainer()
if err != nil {
return 0, errors.Wrapf(err, "unable to get latest container")
}
args = append(args, lastCtr.ID())
}
for _, container := range args {
ctr, err := r.LookupContainer(container)
if err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "unable to find container %s", container)
continue
}
ctrState, err := ctr.State()
if err != nil {
return exitCode, errors.Wrapf(err, "unable to get container state")
}
ctrRunning := ctrState == libpod.ContainerStateRunning
if c.Attach {
inputStream := os.Stdin
if !c.Interactive {
inputStream = nil
}
// attach to the container and also start it not already running
// If the container is in a pod, also set to recursively start dependencies
err = StartAttachCtr(ctx, ctr.Container, os.Stdout, os.Stderr, inputStream, c.DetachKeys, sigProxy, !ctrRunning, ctr.PodID() != "")
if errors.Cause(err) == libpod.ErrDetach {
// User manually detached
// Exit cleanly immediately
exitCode = 0
return exitCode, nil
}
if ctrRunning {
return 0, err
}
if err != nil {
return exitCode, errors.Wrapf(err, "unable to start container %s", ctr.ID())
}
if ecode, err := ctr.Wait(); err != nil {
if errors.Cause(err) == libpod.ErrNoSuchCtr {
// The container may have been removed
// Go looking for an exit file
rtc, err := r.GetConfig()
if err != nil {
return 0, err
}
ctrExitCode, err := ReadExitFile(rtc.TmpDir, ctr.ID())
if err != nil {
logrus.Errorf("Cannot get exit code: %v", err)
exitCode = 127
} else {
exitCode = ctrExitCode
}
}
} else {
exitCode = int(ecode)
}
return exitCode, nil
}
if ctrRunning {
fmt.Println(ctr.ID())
continue
}
// Handle non-attach start
// If the container is in a pod, also set to recursively start dependencies
if err := ctr.Start(ctx, ctr.PodID() != ""); err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "unable to start container %q", container)
continue
}
fmt.Println(container)
}
return exitCode, lastError
}

View File

@ -327,22 +327,12 @@ func (r *LocalRuntime) Log(c *cliconfig.LogsValues, options *libpod.LogOptions)
// CreateContainer creates a container from the cli over varlink // CreateContainer creates a container from the cli over varlink
func (r *LocalRuntime) CreateContainer(ctx context.Context, c *cliconfig.CreateValues) (string, error) { func (r *LocalRuntime) CreateContainer(ctx context.Context, c *cliconfig.CreateValues) (string, error) {
if !c.Bool("detach") {
// TODO need to add attach when that function becomes available
return "", errors.New("the remote client only supports detached containers")
}
results := shared.NewIntermediateLayer(&c.PodmanCommand, true) results := shared.NewIntermediateLayer(&c.PodmanCommand, true)
return iopodman.CreateContainer().Call(r.Conn, results.MakeVarlink()) return iopodman.CreateContainer().Call(r.Conn, results.MakeVarlink())
} }
// Run creates a container overvarlink and then starts it // Run creates a container overvarlink and then starts it
func (r *LocalRuntime) Run(ctx context.Context, c *cliconfig.RunValues, exitCode int) (int, error) { func (r *LocalRuntime) Run(ctx context.Context, c *cliconfig.RunValues, exitCode int) (int, error) {
// FIXME
// podman-remote run -it alpine ls DOES NOT WORK YET
// podman-remote run -it alpine /bin/sh does, i suspect there is some sort of
// timing issue between the socket availability and terminal setup and the command
// being run.
// TODO the exit codes for run need to be figured out for remote connections // TODO the exit codes for run need to be figured out for remote connections
results := shared.NewIntermediateLayer(&c.PodmanCommand, true) results := shared.NewIntermediateLayer(&c.PodmanCommand, true)
cid, err := iopodman.CreateContainer().Call(r.Conn, results.MakeVarlink()) cid, err := iopodman.CreateContainer().Call(r.Conn, results.MakeVarlink())
@ -354,8 +344,7 @@ func (r *LocalRuntime) Run(ctx context.Context, c *cliconfig.RunValues, exitCode
fmt.Println(cid) fmt.Println(cid)
return 0, err return 0, err
} }
errChan, err := r.attach(ctx, os.Stdin, os.Stdout, cid, true, c.String("detach-keys"))
errChan, err := r.attach(ctx, os.Stdin, os.Stdout, cid, true)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -367,7 +356,7 @@ func ReadExitFile(runtimeTmp, ctrID string) (int, error) {
return 0, libpod.ErrNotImplemented return 0, libpod.ErrNotImplemented
} }
// Ps ... // Ps lists containers based on criteria from user
func (r *LocalRuntime) Ps(c *cliconfig.PsValues, opts shared.PsOptions) ([]shared.PsContainerOutput, error) { func (r *LocalRuntime) Ps(c *cliconfig.PsValues, opts shared.PsOptions) ([]shared.PsContainerOutput, error) {
var psContainers []shared.PsContainerOutput var psContainers []shared.PsContainerOutput
last := int64(c.Last) last := int64(c.Last)
@ -439,7 +428,7 @@ func (r *LocalRuntime) Ps(c *cliconfig.PsValues, opts shared.PsOptions) ([]share
return psContainers, nil return psContainers, nil
} }
func (r *LocalRuntime) attach(ctx context.Context, stdin, stdout *os.File, cid string, start bool) (chan error, error) { func (r *LocalRuntime) attach(ctx context.Context, stdin, stdout *os.File, cid string, start bool, detachKeys string) (chan error, error) {
var ( var (
oldTermState *term.State oldTermState *term.State
) )
@ -470,7 +459,7 @@ func (r *LocalRuntime) attach(ctx context.Context, stdin, stdout *os.File, cid s
} }
// TODO add detach keys support // TODO add detach keys support
_, err = iopodman.Attach().Send(r.Conn, varlink.Upgrade, cid, "", start) _, err = iopodman.Attach().Send(r.Conn, varlink.Upgrade, cid, detachKeys, start)
if err != nil { if err != nil {
restoreTerminal(oldTermState) restoreTerminal(oldTermState)
return nil, err return nil, err
@ -531,7 +520,7 @@ func (r *LocalRuntime) Attach(ctx context.Context, c *cliconfig.AttachValues) er
if c.NoStdin { if c.NoStdin {
inputStream = nil inputStream = nil
} }
errChan, err := r.attach(ctx, inputStream, os.Stdout, c.InputArgs[0], false) errChan, err := r.attach(ctx, inputStream, os.Stdout, c.InputArgs[0], false, c.DetachKeys)
if err != nil { if err != nil {
return err return err
} }
@ -609,3 +598,48 @@ func (r *LocalRuntime) Restore(c *cliconfig.RestoreValues, options libpod.Contai
} }
return lastError return lastError
} }
// Start starts an already created container
func (r *LocalRuntime) Start(ctx context.Context, c *cliconfig.StartValues, sigProxy bool) (int, error) {
var (
finalErr error
exitCode = 125
)
// TODO Figure out how to deal with exit codes
inputStream := os.Stdin
if !c.Interactive {
inputStream = nil
}
containerIDs, err := iopodman.GetContainersByContext().Call(r.Conn, false, c.Latest, c.InputArgs)
if err != nil {
return exitCode, err
}
if len(containerIDs) < 1 {
return exitCode, errors.New("failed to find containers to start")
}
// start.go makes sure that if attach, there can be only one ctr
if c.Attach {
errChan, err := r.attach(ctx, inputStream, os.Stdout, containerIDs[0], true, c.DetachKeys)
if err != nil {
return exitCode, nil
}
err = <-errChan
return 0, err
}
// TODO the notion of starting a pod container and its deps still needs to be worked through
// Everything else is detached
for _, cid := range containerIDs {
reply, err := iopodman.StartContainer().Call(r.Conn, cid)
if err != nil {
if finalErr != nil {
fmt.Println(err)
}
finalErr = err
} else {
fmt.Println(reply)
}
}
return exitCode, finalErr
}

View File

@ -344,7 +344,7 @@ registries = ['{{.Host}}:{{.Port}}']`
defer lock8.Unlock() defer lock8.Unlock()
podmanTest.RestoreArtifact(registry) podmanTest.RestoreArtifact(registry)
registryLocal := podmanTest.Podman([]string{"run", "-d", "-p", fmt.Sprintf("%s:5000", registryEndpoints[7].Port), registryLocal := podmanTest.Podman([]string{"run", "-d", "--net=host", "-p", fmt.Sprintf("%s:5000", registryEndpoints[7].Port),
"--name", "registry7", registry}) "--name", "registry7", registry})
registryLocal.WaitWithDefaultTimeout() registryLocal.WaitWithDefaultTimeout()
Expect(registryLocal.ExitCode()).To(Equal(0)) Expect(registryLocal.ExitCode()).To(Equal(0))

View File

@ -1,5 +1,3 @@
// +build !remoteclient
package integration package integration
import ( import (