From acd9c668647d273488772bfcb06a0f1a44dfb411 Mon Sep 17 00:00:00 2001 From: baude Date: Wed, 8 Nov 2017 15:14:33 -0600 Subject: Fix terminal attach Re-order the startup of a new container via run from initialize > start > attach to initialize > attach > start. This fixes output when running: kpod run -i -t IMAGE command and kpod run IMAGE command Signed-off-by: baude --- cmd/kpod/create.go | 10 +++++++++- cmd/kpod/run.go | 35 +++++++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 9 deletions(-) (limited to 'cmd') diff --git a/cmd/kpod/create.go b/cmd/kpod/create.go index bbfeaebae..0a0e31c40 100644 --- a/cmd/kpod/create.go +++ b/cmd/kpod/create.go @@ -319,6 +319,14 @@ func parseCreateOpts(c *cli.Context, runtime *libpod.Runtime) (*createConfig, er blkioWeight = uint16(u) } + // Because we cannot do a non-terminal attach, we need to set tty to true + // if detach is not false + // TODO Allow non-terminal attach + tty := c.Bool("tty") + if !c.Bool("detach") && !tty { + tty = true + } + config := &createConfig{ capAdd: c.StringSlice("cap-add"), capDrop: c.StringSlice("cap-drop"), @@ -387,7 +395,7 @@ func parseCreateOpts(c *cli.Context, runtime *libpod.Runtime) (*createConfig, er storageOpts: c.StringSlice("storage-opt"), sysctl: sysctl, tmpfs: c.StringSlice("tmpfs"), - tty: c.Bool("tty"), + tty: tty, user: uid, group: gid, volumes: c.StringSlice("volume"), diff --git a/cmd/kpod/run.go b/cmd/kpod/run.go index 84da12e6a..87ff2d035 100644 --- a/cmd/kpod/run.go +++ b/cmd/kpod/run.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "sync" "github.com/pkg/errors" "github.com/projectatomic/libpod/libpod" @@ -91,20 +92,38 @@ func runCmd(c *cli.Context) error { libpod.WriteFile(ctr.ID(), c.String("cidfile")) return nil } + + // 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.Debug("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()) } logrus.Debug("started container ", ctr.ID()) - if createConfig.tty { - // Attach to the running container - logrus.Debug("trying to attach to the container %s", ctr.ID()) - if err := ctr.Attach(false, c.String("detach-keys")); err != nil { - return errors.Wrapf(err, "unable to attach to container %s", ctr.ID()) - } - } else { + + if createConfig.detach { fmt.Printf("%s\n", ctr.ID()) } - + wg.Wait() return nil } -- cgit v1.2.3-54-g00ecf