Refactor podman/utils with a single container start and attach function

Use a single function startAttachCtr() to handle both container start
with attach and attach to running containers, as the code handling the
attach is common for the 2 use cases.

Signed-off-by: Marco Vedovati <mvedovati@suse.com>

Closes: #1025
Approved by: rhatdan
This commit is contained in:
Marco Vedovati
2018-06-28 19:08:49 +02:00
committed by Atomic Bot
parent cf2be66f52
commit 9eef9eb212
4 changed files with 17 additions and 60 deletions

View File

@ -75,7 +75,7 @@ func attachCmd(c *cli.Context) error {
inputStream = nil
}
if err := attachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy")); err != nil {
if err := startAttachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy"), false); err != nil {
return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
}

View File

@ -194,7 +194,7 @@ func runCmd(c *cli.Context) error {
}
}
if err := startAttachCtr(ctr, outputStream, errorStream, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy")); err != nil {
if err := startAttachCtr(ctr, outputStream, errorStream, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy"), true); err != nil {
// This means the command did not exist
exitCode = 127
if strings.Index(err.Error(), "permission denied") > -1 {

View File

@ -96,16 +96,21 @@ func startCmd(c *cli.Context) error {
return errors.Wrapf(err, "unable to get container state")
}
ctrRunning := ctrState == libpod.ContainerStateRunning
if attach {
inputStream := os.Stdin
if !c.Bool("interactive") {
inputStream = nil
}
if ctrState == libpod.ContainerStateRunning {
return attachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy"))
// attach to the container and also start it not already running
err = startAttachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"), c.Bool("sig-proxy"), !ctrRunning)
if ctrRunning {
return err
}
if err := startAttachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"), c.Bool("sig-proxy")); err != nil {
if err != nil {
return errors.Wrapf(err, "unable to start container %s", ctr.ID())
}
@ -117,7 +122,7 @@ func startCmd(c *cli.Context) error {
return ctr.Cleanup()
}
if ctrState == libpod.ContainerStateRunning {
if ctrRunning {
fmt.Println(ctr.ID())
continue
}

View File

@ -18,8 +18,8 @@ import (
type RawTtyFormatter struct {
}
// Attach to a container
func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string, sigProxy bool) error {
// Start (if required) and attach to a container
func startAttachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string, sigProxy bool, startContainer bool) error {
ctx := context.Background()
resize := make(chan remotecommand.TerminalSize)
@ -67,6 +67,7 @@ func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys
streams.AttachInput = false
}
if !startContainer {
if sigProxy {
ProxySignals(ctr)
}
@ -74,55 +75,6 @@ func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys
return ctr.Attach(streams, detachKeys, resize)
}
// Start and attach to a container
func startAttachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string, sigProxy bool) error {
ctx := context.Background()
resize := make(chan remotecommand.TerminalSize)
haveTerminal := terminal.IsTerminal(int(os.Stdin.Fd()))
// Check if we are attached to a terminal. If we are, generate resize
// events, and set the terminal to raw mode
if haveTerminal && ctr.Spec().Process.Terminal {
logrus.Debugf("Handling terminal attach")
subCtx, cancel := context.WithCancel(ctx)
defer cancel()
resizeTty(subCtx, resize)
oldTermState, err := term.SaveState(os.Stdin.Fd())
if err != nil {
return errors.Wrapf(err, "unable to save terminal state")
}
logrus.SetFormatter(&RawTtyFormatter{})
term.SetRawTerminal(os.Stdin.Fd())
defer restoreTerminal(oldTermState)
}
streams := new(libpod.AttachStreams)
streams.OutputStream = stdout
streams.ErrorStream = stderr
streams.InputStream = stdin
streams.AttachOutput = true
streams.AttachError = true
streams.AttachInput = true
if stdout == nil {
logrus.Debugf("Not attaching to stdout")
streams.AttachOutput = false
}
if stderr == nil {
logrus.Debugf("Not attaching to stderr")
streams.AttachError = false
}
if stdin == nil {
logrus.Debugf("Not attaching to stdin")
streams.AttachInput = false
}
attachChan, err := ctr.StartAndAttach(getContext(), streams, detachKeys, resize)
if err != nil {
return err