summaryrefslogtreecommitdiff
path: root/cmd/kpod
diff options
context:
space:
mode:
authorbaude <bbaude@redhat.com>2017-12-01 11:26:30 -0600
committerAtomic Bot <atomic-devel@projectatomic.io>2017-12-04 17:00:48 +0000
commit5c9694a0c1fc1d717c61597f659590dcb7b6f25b (patch)
tree4c2ff5ee08fbcd7d26faa2fc1e8ff736122a94e1 /cmd/kpod
parentadf8809521733283c364ec7de27c783e324185e8 (diff)
downloadpodman-5c9694a0c1fc1d717c61597f659590dcb7b6f25b.tar.gz
podman-5c9694a0c1fc1d717c61597f659590dcb7b6f25b.tar.bz2
podman-5c9694a0c1fc1d717c61597f659590dcb7b6f25b.zip
kpod attach
Attach to a running container Signed-off-by: baude <bbaude@redhat.com> Closes: #95 Approved by: rhatdan
Diffstat (limited to 'cmd/kpod')
-rw-r--r--cmd/kpod/attach.go86
-rw-r--r--cmd/kpod/main.go1
2 files changed, 87 insertions, 0 deletions
diff --git a/cmd/kpod/attach.go b/cmd/kpod/attach.go
new file mode 100644
index 000000000..d319ff70d
--- /dev/null
+++ b/cmd/kpod/attach.go
@@ -0,0 +1,86 @@
+package main
+
+import (
+ "sync"
+
+ "github.com/pkg/errors"
+ "github.com/projectatomic/libpod/libpod"
+ "github.com/sirupsen/logrus"
+ "github.com/urfave/cli"
+)
+
+var (
+ attachFlags = []cli.Flag{
+ cli.StringFlag{
+ Name: "detach-keys",
+ Usage: "Override the key sequence for detaching a container. Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _.",
+ },
+ cli.BoolFlag{
+ Name: "no-stdin",
+ Usage: "Do not attach STDIN. The default is false.",
+ },
+ }
+ attachDescription = "The kpod attach command allows you to attach to a running container using the container's ID or name, either to view its ongoing output or to control it interactively."
+ attachCommand = cli.Command{
+ Name: "attach",
+ Usage: "Attach to a running container",
+ Description: attachDescription,
+ Flags: attachFlags,
+ Action: attachCmd,
+ ArgsUsage: "",
+ }
+)
+
+func attachCmd(c *cli.Context) error {
+ args := c.Args()
+ if err := validateFlags(c, attachFlags); err != nil {
+ return err
+ }
+
+ if len(c.Args()) < 1 || len(c.Args()) > 1 {
+ return errors.Errorf("attach requires the name or id of one running container")
+ }
+
+ runtime, err := getRuntime(c)
+ if err != nil {
+ return errors.Wrapf(err, "error creating libpod runtime")
+ }
+ defer runtime.Shutdown(false)
+
+ ctr, err := runtime.LookupContainer(args[0])
+
+ if err != nil {
+ return errors.Wrapf(err, "unable to exec into %s", args[0])
+ }
+
+ conState, err := ctr.State()
+ if err != nil {
+ return errors.Wrapf(err, "unable to determine state of %s", args[0])
+ }
+ if conState != libpod.ContainerStateRunning {
+ return errors.Errorf("you can only attach to running containers")
+ }
+ // 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
+
+ // 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(c.Bool("no-stdin"), 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())
+ }
+ wg.Wait()
+
+ return nil
+}
diff --git a/cmd/kpod/main.go b/cmd/kpod/main.go
index a4bb12475..dfa57d689 100644
--- a/cmd/kpod/main.go
+++ b/cmd/kpod/main.go
@@ -34,6 +34,7 @@ func main() {
app.Version = v
app.Commands = []cli.Command{
+ attachCommand,
createCommand,
diffCommand,
exportCommand,