diff options
author | Matthew Heon <matthew.heon@gmail.com> | 2018-03-14 15:14:49 -0400 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2018-03-15 17:45:11 +0000 |
commit | 55f2f58145e9871c299456cff8285a6d2595da86 (patch) | |
tree | 78f6aa90a9fa25aa7db83ceb6cd88ac01918728d /cmd/podman/run.go | |
parent | 4739fc2d98baf0ccfc46ae3ef770243bbdcea47a (diff) | |
download | podman-55f2f58145e9871c299456cff8285a6d2595da86.tar.gz podman-55f2f58145e9871c299456cff8285a6d2595da86.tar.bz2 podman-55f2f58145e9871c299456cff8285a6d2595da86.zip |
Add StartAndAttach() API endpoint for containers
This solves our prior problems with attach races by ensuring the
order is correct.
Also contains substantial cleanups to the attach code.
Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
Closes: #482
Approved by: baude
Diffstat (limited to 'cmd/podman/run.go')
-rw-r--r-- | cmd/podman/run.go | 47 |
1 files changed, 19 insertions, 28 deletions
diff --git a/cmd/podman/run.go b/cmd/podman/run.go index 3d6175cef..f68db9036 100644 --- a/cmd/podman/run.go +++ b/cmd/podman/run.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "strings" - "sync" "github.com/pkg/errors" "github.com/projectatomic/libpod/libpod" @@ -116,38 +115,30 @@ func runCmd(c *cli.Context) error { } } - // Create a bool channel to track that the console socket attach - // is successful. - attached := make(chan bool) - // Create a waitgroup so we can sync and wait for all goroutines - // to finish before exiting main - var wg sync.WaitGroup - - if !createConfig.Detach { - // We increment the wg counter because we need to do the attach - wg.Add(1) - // Attach to the running container - go func() { - logrus.Debugf("trying to attach to the container %s", ctr.ID()) - defer wg.Done() - if err := ctr.Attach(false, c.String("detach-keys"), attached); err != nil { - logrus.Errorf("unable to attach to container %s: %q", ctr.ID(), err) - } - }() - if !<-attached { - return errors.Errorf("unable to attach to container %s", ctr.ID()) - } - } - // Start the container - if err := ctr.Start(); err != nil { - return errors.Wrapf(err, "unable to start container %q", ctr.ID()) - } + // Handle detached start if createConfig.Detach { + if err := ctr.Start(); err != nil { + return errors.Wrapf(err, "unable to start container %q", ctr.ID()) + } + fmt.Printf("%s\n", ctr.ID()) exitCode = 0 return nil } - wg.Wait() + + // TODO: that "false" should probably be linked to -i + // Handle this when we split streams to allow attaching just stdin/out/err + attachChan, err := ctr.StartAndAttach(false, c.String("detach-keys")) + if err != nil { + return errors.Wrapf(err, "unable to start container %q", ctr.ID()) + } + + // Wait for attach to complete + err = <-attachChan + if err != nil { + return errors.Wrapf(err, "error attaching to container %s", ctr.ID()) + } + if ecode, err := ctr.ExitCode(); err != nil { logrus.Errorf("unable to get exit code of container %s: %q", ctr.ID(), err) } else { |