mirror of
https://github.com/containers/podman.git
synced 2025-06-20 00:51:16 +08:00
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:

committed by
Atomic Bot

parent
cf2be66f52
commit
9eef9eb212
@ -75,7 +75,7 @@ func attachCmd(c *cli.Context) error {
|
|||||||
inputStream = nil
|
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())
|
return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
// This means the command did not exist
|
||||||
exitCode = 127
|
exitCode = 127
|
||||||
if strings.Index(err.Error(), "permission denied") > -1 {
|
if strings.Index(err.Error(), "permission denied") > -1 {
|
||||||
|
@ -96,16 +96,21 @@ func startCmd(c *cli.Context) error {
|
|||||||
return errors.Wrapf(err, "unable to get container state")
|
return errors.Wrapf(err, "unable to get container state")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctrRunning := ctrState == libpod.ContainerStateRunning
|
||||||
|
|
||||||
if attach {
|
if attach {
|
||||||
inputStream := os.Stdin
|
inputStream := os.Stdin
|
||||||
if !c.Bool("interactive") {
|
if !c.Bool("interactive") {
|
||||||
inputStream = nil
|
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())
|
return errors.Wrapf(err, "unable to start container %s", ctr.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,7 +122,7 @@ func startCmd(c *cli.Context) error {
|
|||||||
|
|
||||||
return ctr.Cleanup()
|
return ctr.Cleanup()
|
||||||
}
|
}
|
||||||
if ctrState == libpod.ContainerStateRunning {
|
if ctrRunning {
|
||||||
fmt.Println(ctr.ID())
|
fmt.Println(ctr.ID())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,8 @@ import (
|
|||||||
type RawTtyFormatter struct {
|
type RawTtyFormatter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attach to a container
|
// Start (if required) and attach to a container
|
||||||
func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string, sigProxy bool) error {
|
func startAttachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string, sigProxy bool, startContainer bool) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
resize := make(chan remotecommand.TerminalSize)
|
resize := make(chan remotecommand.TerminalSize)
|
||||||
|
|
||||||
@ -67,6 +67,7 @@ func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys
|
|||||||
streams.AttachInput = false
|
streams.AttachInput = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !startContainer {
|
||||||
if sigProxy {
|
if sigProxy {
|
||||||
ProxySignals(ctr)
|
ProxySignals(ctr)
|
||||||
}
|
}
|
||||||
@ -74,55 +75,6 @@ func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys
|
|||||||
return ctr.Attach(streams, detachKeys, resize)
|
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)
|
attachChan, err := ctr.StartAndAttach(getContext(), streams, detachKeys, resize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Reference in New Issue
Block a user