summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--cmd/kpod/attach.go86
-rw-r--r--cmd/kpod/main.go1
-rw-r--r--completions/bash/kpod12
-rw-r--r--docs/kpod-attach.1.md57
-rw-r--r--docs/kpod.1.md3
-rw-r--r--test/kpod_attach.bats32
7 files changed, 168 insertions, 25 deletions
diff --git a/README.md b/README.md
index e2aa9e724..049dbf00a 100644
--- a/README.md
+++ b/README.md
@@ -34,7 +34,7 @@ libpod is currently in active development.
| Command | Description | Demo|
| ---------------------------------------------------- | --------------------------------------------------------------------------|-----|
| [kpod(1)](/docs/kpod.1.md) | Simple management tool for pods and images ||
-| [kpod-attach(1)](/docs/kpod-attach.1.md) | Instead of providing a `kpod attach` command, the man page `kpod-attach` describes how to use the `kpod logs` and `kpod exec` commands to achieve the same goals as `kpod attach`.||
+| [kpod-attach(1)](/docs/kpod-attach.1.md) | Attach to a running container.
| [kpod-cp(1)](/docs/kpod-cp.1.md) | Instead of providing a `kpod cp` command, the man page `kpod-cp` describes how to use the `kpod mount` command to have even more flexibility and functionality.||
| [kpod-diff(1)](/docs/kpod-diff.1.md) | Inspect changes on a container or image's filesystem |[![...](/docs/play.png)](https://asciinema.org/a/FXfWB9CKYFwYM4EfqW3NSZy1G)|
| [kpod-export(1)](/docs/kpod-export.1.md) | Export container's filesystem contents as a tar archive |[![...](/docs/play.png)](https://asciinema.org/a/913lBIRAg5hK8asyIhhkQVLtV)|
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,
diff --git a/completions/bash/kpod b/completions/bash/kpod
index 68001c36e..0901a231b 100644
--- a/completions/bash/kpod
+++ b/completions/bash/kpod
@@ -653,6 +653,17 @@ __kpod_images() {
__kpod_q images $images_args | awk "$awk_script" | grep -v '<none>$'
}
+_kpod_attach() {
+ local options_with_args="
+ --detach-keys
+ "
+ local boolean_options="
+ --help
+ -h
+ --no-stdin"
+ _complete_ "$options_with_args" "$boolean_options"
+}
+
_kpod_diff() {
local options_with_args="
--format
@@ -1443,6 +1454,7 @@ _kpod_kpod() {
--version -v
"
commands="
+ attach
create
diff
export
diff --git a/docs/kpod-attach.1.md b/docs/kpod-attach.1.md
index 4c2a02186..0710cd06f 100644
--- a/docs/kpod-attach.1.md
+++ b/docs/kpod-attach.1.md
@@ -1,31 +1,40 @@
% kpod(1) kpod-attach - See the output of pid 1 of a container or enter the container
% Dan Walsh
-# kpod-attach "1" "September 2017" "kpod"
+# kpod-attach "1" "December 2017" "kpod"
## NAME
kpod-attach - Attach to a running container
-## Description
-
-We chose not to implement the `attach` feature in `kpod` even though the upstream Docker
-project has it. The upstream project has had lots of issues with attaching to running
-processes that we did not want to replicate. The `kpod exec` and `kpod log` commands
-offer you the same functionality far more dependably.
-
-**Reasons to attach to the primary PID of a container:**
-
-
-1) Executing commands inside of the container
-
- We recommend that you use `kpod exec` to execute a command within a container
-
- `kpod exec CONTAINERID /bin/sh`
-
-2) Viewing the output stream of the primary process in the container
-
- We recommend that you use `kpod logs` to see the output from the container
-
- `kpod logs CONTAINERID`
-
+## SYNOPSIS
+**kpod attach [OPTIONS] CONTAINER**
+
+## DESCRIPTION
+The 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.
+
+You can detach from the container (and leave it running) using a configurable key sequence. The default
+sequence is CTRL-p CTRL-q. You configure the key sequence using the --detach-keys option
+
+## OPTIONS
+**--detach-keys**
+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 _.
+
+**--no-stdin**
+Do not attach STDIN. The default is false.
+
+## EXAMPLES ##
+
+```
+kpod attach foobar
+[root@localhost /]#
+```
+```
+kpod attach 1234
+[root@localhost /]#
+```
+```
+kpod attach --no-stdin foobar
+```
## SEE ALSO
-kpod(1), kpod-exec(1), kpod-logs(1)
+kpod(1), kpod-exec(1), kpod-run(1)
diff --git a/docs/kpod.1.md b/docs/kpod.1.md
index 0e5204bec..7009289c7 100644
--- a/docs/kpod.1.md
+++ b/docs/kpod.1.md
@@ -52,6 +52,9 @@ has the capability to debug pods/images created by crio.
## COMMANDS
+### attach
+Attach to a running container
+
### create
create a new container
diff --git a/test/kpod_attach.bats b/test/kpod_attach.bats
new file mode 100644
index 000000000..968795935
--- /dev/null
+++ b/test/kpod_attach.bats
@@ -0,0 +1,32 @@
+#!/usr/bin/env bats
+
+load helpers
+
+function teardown() {
+ cleanup_test
+}
+
+function setup() {
+ copy_images
+}
+
+@test "attach to a bogus container" {
+ run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} attach foobar"
+ echo "$output"
+ [ "$status" -eq 1 ]
+}
+
+@test "attach to non-running container" {
+ ${KPOD_BINARY} ${KPOD_OPTIONS} create --name foobar -d -i ${ALPINE} ls
+ run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} attach foobar"
+ echo "$output"
+ [ "$status" -eq 1 ]
+}
+
+@test "attach to multiple containers" {
+ ${KPOD_BINARY} ${KPOD_OPTIONS} run --name foobar1 -d -i ${ALPINE} /bin/sh
+ ${KPOD_BINARY} ${KPOD_OPTIONS} run --name foobar2 -d -i ${ALPINE} /bin/sh
+ run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} attach foobar1 foobar2"
+ echo "$output"
+ [ "$status" -eq 1 ]
+}