mirror of
https://github.com/containers/podman.git
synced 2025-05-22 17:46:52 +08:00
Changes to attach to enable per-stream attaching
This allows us to attach to attach to just stdout or stderr or stdin, or any combination of these. Signed-off-by: Matthew Heon <matthew.heon@gmail.com> Closes: #608 Approved by: baude
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/projectatomic/libpod/libpod"
|
"github.com/projectatomic/libpod/libpod"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
@ -71,7 +73,12 @@ func attachCmd(c *cli.Context) error {
|
|||||||
ProxySignals(ctr)
|
ProxySignals(ctr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ctr.Attach(c.Bool("no-stdin"), c.String("detach-keys")); err != nil {
|
inputStream := os.Stdin
|
||||||
|
if c.Bool("no-stdin") {
|
||||||
|
inputStream = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := attachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys")); err != nil {
|
||||||
return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
|
return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,9 +144,42 @@ func runCmd(c *cli.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: that "false" should probably be linked to -i
|
outputStream := os.Stdout
|
||||||
// Handle this when we split streams to allow attaching just stdin/out/err
|
errorStream := os.Stderr
|
||||||
attachChan, err := ctr.StartAndAttach(false, c.String("detach-keys"))
|
inputStream := os.Stdin
|
||||||
|
|
||||||
|
// If -i is not set, clear stdin
|
||||||
|
if !c.Bool("interactive") {
|
||||||
|
inputStream = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// If attach is set, clear stdin/stdout/stderr and only attach requested
|
||||||
|
if c.IsSet("attach") {
|
||||||
|
outputStream = nil
|
||||||
|
errorStream = nil
|
||||||
|
inputStream = nil
|
||||||
|
|
||||||
|
attachTo := c.StringSlice("attach")
|
||||||
|
for _, stream := range attachTo {
|
||||||
|
switch strings.ToLower(stream) {
|
||||||
|
case "stdout":
|
||||||
|
outputStream = os.Stdout
|
||||||
|
case "stderr":
|
||||||
|
errorStream = os.Stderr
|
||||||
|
case "stdin":
|
||||||
|
inputStream = os.Stdin
|
||||||
|
default:
|
||||||
|
return errors.Wrapf(libpod.ErrInvalidArg, "invalid stream %q for --attach - must be one of stdin, stdout, or stderr", stream)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If --interactive is set, restore stdin
|
||||||
|
if c.Bool("interactive") {
|
||||||
|
inputStream = os.Stdin
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
attachChan, err := startAttachCtr(ctr, outputStream, errorStream, inputStream, c.String("detach-keys"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// This means the command did not exist
|
// This means the command did not exist
|
||||||
exitCode = 127
|
exitCode = 127
|
||||||
|
@ -95,7 +95,7 @@ func startCmd(c *cli.Context) error {
|
|||||||
if c.Bool("interactive") && !ctr.Config().Stdin {
|
if c.Bool("interactive") && !ctr.Config().Stdin {
|
||||||
return errors.Errorf("the container was not created with the interactive option")
|
return errors.Errorf("the container was not created with the interactive option")
|
||||||
}
|
}
|
||||||
noStdIn := c.Bool("interactive")
|
|
||||||
tty, err := strconv.ParseBool(ctr.Spec().Annotations["io.kubernetes.cri-o.TTY"])
|
tty, err := strconv.ParseBool(ctr.Spec().Annotations["io.kubernetes.cri-o.TTY"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "unable to parse annotations in %s", ctr.ID())
|
return errors.Wrapf(err, "unable to parse annotations in %s", ctr.ID())
|
||||||
@ -105,7 +105,12 @@ func startCmd(c *cli.Context) error {
|
|||||||
// We only get a terminal session if both a tty was specified in the spec and
|
// We only get a terminal session if both a tty was specified in the spec and
|
||||||
// -a on the command-line was given.
|
// -a on the command-line was given.
|
||||||
if attach && tty {
|
if attach && tty {
|
||||||
attachChan, err := ctr.StartAndAttach(noStdIn, c.String("detach-keys"))
|
inputStream := os.Stdin
|
||||||
|
if !c.Bool("interactive") {
|
||||||
|
inputStream = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
attachChan, err := startAttachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"))
|
||||||
if 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())
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
gosignal "os/signal"
|
||||||
|
|
||||||
"github.com/containers/storage"
|
"github.com/containers/storage"
|
||||||
|
"github.com/docker/docker/pkg/signal"
|
||||||
|
"github.com/docker/docker/pkg/term"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/projectatomic/libpod/libpod"
|
"github.com/projectatomic/libpod/libpod"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
|
"k8s.io/client-go/tools/remotecommand"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Generate a new libpod runtime configured by command line options
|
// Generate a new libpod runtime configured by command line options
|
||||||
@ -54,3 +63,81 @@ func getRuntime(c *cli.Context) (*libpod.Runtime, error) {
|
|||||||
|
|
||||||
return libpod.NewRuntime(options...)
|
return libpod.NewRuntime(options...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attach to a container
|
||||||
|
func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string) error {
|
||||||
|
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 {
|
||||||
|
logrus.Debugf("Handling terminal attach")
|
||||||
|
|
||||||
|
resizeTty(resize)
|
||||||
|
|
||||||
|
oldTermState, err := term.SaveState(os.Stdin.Fd())
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "unable to save terminal state")
|
||||||
|
}
|
||||||
|
|
||||||
|
term.SetRawTerminal(os.Stdin.Fd())
|
||||||
|
|
||||||
|
defer term.RestoreTerminal(os.Stdin.Fd(), oldTermState)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctr.Attach(stdout, stderr, stdin, detachKeys, resize)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start and attach to a container
|
||||||
|
func startAttachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string) (<-chan error, error) {
|
||||||
|
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")
|
||||||
|
|
||||||
|
resizeTty(resize)
|
||||||
|
|
||||||
|
oldTermState, err := term.SaveState(os.Stdin.Fd())
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "unable to save terminal state")
|
||||||
|
}
|
||||||
|
|
||||||
|
term.SetRawTerminal(os.Stdin.Fd())
|
||||||
|
|
||||||
|
defer term.RestoreTerminal(os.Stdin.Fd(), oldTermState)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctr.StartAndAttach(stdout, stderr, stdin, detachKeys, resize)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper for prepareAttach - set up a goroutine to generate terminal resize events
|
||||||
|
func resizeTty(resize chan remotecommand.TerminalSize) {
|
||||||
|
sigchan := make(chan os.Signal, 1)
|
||||||
|
gosignal.Notify(sigchan, signal.SIGWINCH)
|
||||||
|
sendUpdate := func() {
|
||||||
|
winsize, err := term.GetWinsize(os.Stdin.Fd())
|
||||||
|
if err != nil {
|
||||||
|
logrus.Warnf("Could not get terminal size %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resize <- remotecommand.TerminalSize{
|
||||||
|
Width: winsize.Width,
|
||||||
|
Height: winsize.Height,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
defer close(resize)
|
||||||
|
// Update the terminal size immediately without waiting
|
||||||
|
// for a SIGWINCH to get the correct initial size.
|
||||||
|
sendUpdate()
|
||||||
|
for range sigchan {
|
||||||
|
sendUpdate()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package libpod
|
package libpod
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -14,6 +15,7 @@ import (
|
|||||||
"github.com/projectatomic/libpod/pkg/inspect"
|
"github.com/projectatomic/libpod/pkg/inspect"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
|
"k8s.io/client-go/tools/remotecommand"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Init creates a container in the OCI runtime
|
// Init creates a container in the OCI runtime
|
||||||
@ -123,7 +125,7 @@ func (c *Container) Start() (err error) {
|
|||||||
// attach call.
|
// attach call.
|
||||||
// The channel will be closed automatically after the result of attach has been
|
// The channel will be closed automatically after the result of attach has been
|
||||||
// sent
|
// sent
|
||||||
func (c *Container) StartAndAttach(noStdin bool, keys string) (attachResChan <-chan error, err error) {
|
func (c *Container) StartAndAttach(outputStream, errorStream io.WriteCloser, inputStream io.Reader, keys string, resize <-chan remotecommand.TerminalSize) (attachResChan <-chan error, err error) {
|
||||||
if !c.locked {
|
if !c.locked {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
@ -176,7 +178,7 @@ func (c *Container) StartAndAttach(noStdin bool, keys string) (attachResChan <-c
|
|||||||
|
|
||||||
// Attach to the container before starting it
|
// Attach to the container before starting it
|
||||||
go func() {
|
go func() {
|
||||||
if err := c.attach(noStdin, keys); err != nil {
|
if err := c.attach(outputStream, errorStream, inputStream, keys, resize); err != nil {
|
||||||
attachChan <- err
|
attachChan <- err
|
||||||
}
|
}
|
||||||
close(attachChan)
|
close(attachChan)
|
||||||
@ -404,8 +406,7 @@ func (c *Container) Exec(tty, privileged bool, env, cmd []string, user string) e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Attach attaches to a container
|
// Attach attaches to a container
|
||||||
// Returns fully qualified URL of streaming server for the container
|
func (c *Container) Attach(outputStream, errorStream io.WriteCloser, inputStream io.Reader, keys string, resize <-chan remotecommand.TerminalSize) error {
|
||||||
func (c *Container) Attach(noStdin bool, keys string) error {
|
|
||||||
if !c.locked {
|
if !c.locked {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
if err := c.syncContainer(); err != nil {
|
if err := c.syncContainer(); err != nil {
|
||||||
@ -420,7 +421,7 @@ func (c *Container) Attach(noStdin bool, keys string) error {
|
|||||||
return errors.Wrapf(ErrCtrStateInvalid, "can only attach to created or running containers")
|
return errors.Wrapf(ErrCtrStateInvalid, "can only attach to created or running containers")
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.attach(noStdin, keys)
|
return c.attach(outputStream, errorStream, inputStream, keys, resize)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount mounts a container's filesystem on the host
|
// Mount mounts a container's filesystem on the host
|
||||||
|
@ -5,16 +5,13 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
gosignal "os/signal"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/signal"
|
|
||||||
"github.com/docker/docker/pkg/term"
|
"github.com/docker/docker/pkg/term"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/projectatomic/libpod/pkg/kubeutils"
|
"github.com/projectatomic/libpod/pkg/kubeutils"
|
||||||
"github.com/projectatomic/libpod/utils"
|
"github.com/projectatomic/libpod/utils"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/crypto/ssh/terminal"
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
"k8s.io/client-go/tools/remotecommand"
|
"k8s.io/client-go/tools/remotecommand"
|
||||||
)
|
)
|
||||||
@ -26,33 +23,19 @@ const (
|
|||||||
AttachPipeStderr = 3
|
AttachPipeStderr = 3
|
||||||
)
|
)
|
||||||
|
|
||||||
// resizeTty handles TTY resizing for Attach()
|
// Attach to the given container
|
||||||
func resizeTty(resize chan remotecommand.TerminalSize) {
|
// Does not check if state is appropriate
|
||||||
sigchan := make(chan os.Signal, 1)
|
func (c *Container) attach(outputStream, errorStream io.WriteCloser, inputStream io.Reader, keys string, resize <-chan remotecommand.TerminalSize) error {
|
||||||
gosignal.Notify(sigchan, signal.SIGWINCH)
|
if outputStream == nil && errorStream == nil && inputStream == nil {
|
||||||
sendUpdate := func() {
|
return errors.Wrapf(ErrInvalidArg, "must provide at least one stream to attach to")
|
||||||
winsize, err := term.GetWinsize(os.Stdin.Fd())
|
}
|
||||||
if err != nil {
|
|
||||||
logrus.Warnf("Could not get terminal size %v", err)
|
// Do not allow stdin on containers without Stdin set
|
||||||
return
|
// Conmon was not configured properly
|
||||||
|
if inputStream != nil && !c.config.Stdin {
|
||||||
|
return errors.Wrapf(ErrInvalidArg, "this container was not created as interactive, cannot attach stdin")
|
||||||
}
|
}
|
||||||
resize <- remotecommand.TerminalSize{
|
|
||||||
Width: winsize.Width,
|
|
||||||
Height: winsize.Height,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
defer close(resize)
|
|
||||||
// Update the terminal size immediately without waiting
|
|
||||||
// for a SIGWINCH to get the correct initial size.
|
|
||||||
sendUpdate()
|
|
||||||
for range sigchan {
|
|
||||||
sendUpdate()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Container) attach(noStdin bool, keys string) error {
|
|
||||||
// Check the validity of the provided keys first
|
// Check the validity of the provided keys first
|
||||||
var err error
|
var err error
|
||||||
detachKeys := []byte{}
|
detachKeys := []byte{}
|
||||||
@ -63,40 +46,14 @@ func (c *Container) attach(noStdin bool, keys string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: allow resize channel to be passed in for CRI-O use
|
|
||||||
resize := make(chan remotecommand.TerminalSize)
|
|
||||||
if terminal.IsTerminal(int(os.Stdin.Fd())) {
|
|
||||||
resizeTty(resize)
|
|
||||||
} else {
|
|
||||||
defer close(resize)
|
|
||||||
}
|
|
||||||
|
|
||||||
logrus.Debugf("Attaching to container %s", c.ID())
|
logrus.Debugf("Attaching to container %s", c.ID())
|
||||||
|
|
||||||
return c.attachContainerSocket(resize, noStdin, detachKeys)
|
return c.attachContainerSocket(resize, detachKeys, outputStream, errorStream, inputStream)
|
||||||
}
|
}
|
||||||
|
|
||||||
// attachContainerSocket connects to the container's attach socket and deals with the IO
|
// attachContainerSocket connects to the container's attach socket and deals with the IO
|
||||||
// TODO add a channel to allow interruptiong
|
// TODO add a channel to allow interruptiong
|
||||||
func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSize, noStdIn bool, detachKeys []byte) error {
|
func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSize, detachKeys []byte, outputStream, errorStream io.WriteCloser, inputStream io.Reader) error {
|
||||||
inputStream := os.Stdin
|
|
||||||
outputStream := os.Stdout
|
|
||||||
errorStream := os.Stderr
|
|
||||||
defer inputStream.Close()
|
|
||||||
if terminal.IsTerminal(int(inputStream.Fd())) {
|
|
||||||
oldTermState, err := term.SaveState(inputStream.Fd())
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "unable to save terminal state")
|
|
||||||
}
|
|
||||||
|
|
||||||
defer term.RestoreTerminal(inputStream.Fd(), oldTermState)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put both input and output into raw when we have a terminal
|
|
||||||
if !noStdIn && c.config.Spec.Process.Terminal {
|
|
||||||
term.SetRawTerminal(inputStream.Fd())
|
|
||||||
}
|
|
||||||
|
|
||||||
kubeutils.HandleResizing(resize, func(size remotecommand.TerminalSize) {
|
kubeutils.HandleResizing(resize, func(size remotecommand.TerminalSize) {
|
||||||
controlPath := filepath.Join(c.bundlePath(), "ctl")
|
controlPath := filepath.Join(c.bundlePath(), "ctl")
|
||||||
controlFile, err := os.OpenFile(controlPath, unix.O_WRONLY, 0)
|
controlFile, err := os.OpenFile(controlPath, unix.O_WRONLY, 0)
|
||||||
@ -129,7 +86,7 @@ func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSi
|
|||||||
stdinDone := make(chan error)
|
stdinDone := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
var err error
|
var err error
|
||||||
if inputStream != nil && !noStdIn {
|
if inputStream != nil {
|
||||||
_, err = utils.CopyDetachable(conn, inputStream, detachKeys)
|
_, err = utils.CopyDetachable(conn, inputStream, detachKeys)
|
||||||
conn.CloseWrite()
|
conn.CloseWrite()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user