2014-08-13 01:46:43 +08:00
|
|
|
package main
|
2014-06-05 07:46:30 +08:00
|
|
|
|
|
|
|
import (
|
2014-12-26 21:13:10 +08:00
|
|
|
"io"
|
2014-06-05 07:46:30 +08:00
|
|
|
"os"
|
2015-02-01 13:21:06 +08:00
|
|
|
"os/signal"
|
2014-07-16 07:26:28 +08:00
|
|
|
"syscall"
|
2014-06-05 07:46:30 +08:00
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
2014-12-26 21:13:10 +08:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer"
|
2015-02-01 13:21:06 +08:00
|
|
|
"github.com/docker/libcontainer/configs"
|
2014-12-26 21:13:10 +08:00
|
|
|
consolepkg "github.com/docker/libcontainer/console"
|
2014-06-05 07:46:30 +08:00
|
|
|
)
|
|
|
|
|
2015-02-01 13:21:06 +08:00
|
|
|
type tty struct {
|
|
|
|
master *os.File
|
|
|
|
console string
|
|
|
|
state *term.State
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tty) Close() error {
|
|
|
|
if t.master != nil {
|
|
|
|
t.master.Close()
|
|
|
|
}
|
|
|
|
if t.state != nil {
|
|
|
|
term.RestoreTerminal(os.Stdin.Fd(), t.state)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tty) set(config *configs.Config) {
|
|
|
|
config.Console = t.console
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tty) attach(process *libcontainer.Process) {
|
|
|
|
if t.master != nil {
|
|
|
|
process.Stderr = nil
|
|
|
|
process.Stdout = nil
|
|
|
|
process.Stdin = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tty) resize() error {
|
|
|
|
if t.master == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ws, err := term.GetWinsize(os.Stdin.Fd())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return term.SetWinsize(t.master.Fd(), ws)
|
|
|
|
}
|
|
|
|
|
2014-06-05 07:46:30 +08:00
|
|
|
var execCommand = cli.Command{
|
|
|
|
Name: "exec",
|
|
|
|
Usage: "execute a new command inside a container",
|
|
|
|
Action: execAction,
|
2014-08-13 08:33:20 +08:00
|
|
|
Flags: []cli.Flag{
|
2015-02-01 11:56:27 +08:00
|
|
|
cli.BoolFlag{Name: "tty", Usage: "allocate a TTY to the container"},
|
2015-02-01 12:51:12 +08:00
|
|
|
cli.StringFlag{Name: "id", Value: "nsinit", Usage: "specify the ID for a container"},
|
|
|
|
cli.StringFlag{Name: "config", Value: "container.json", Usage: "path to the configuration file"},
|
2014-08-13 08:33:20 +08:00
|
|
|
},
|
2014-06-05 07:46:30 +08:00
|
|
|
}
|
|
|
|
|
2015-01-13 19:49:46 +08:00
|
|
|
func execAction(context *cli.Context) {
|
2015-02-01 12:51:12 +08:00
|
|
|
factory, err := loadFactory(context)
|
2014-12-15 23:08:56 +08:00
|
|
|
if err != nil {
|
2015-02-01 12:51:12 +08:00
|
|
|
fatal(err)
|
2014-07-22 07:25:46 +08:00
|
|
|
}
|
2015-02-01 13:21:06 +08:00
|
|
|
tty, err := newTty(context)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
2015-02-01 12:51:12 +08:00
|
|
|
container, err := factory.Load(context.String("id"))
|
|
|
|
if err != nil {
|
|
|
|
if lerr, ok := err.(libcontainer.Error); !ok || lerr.Code() != libcontainer.ContainerNotExists {
|
|
|
|
fatal(err)
|
2014-12-26 21:13:10 +08:00
|
|
|
}
|
2015-02-01 12:51:12 +08:00
|
|
|
config, err := loadConfig(context)
|
2014-12-26 21:13:10 +08:00
|
|
|
if err != nil {
|
2015-02-01 12:51:12 +08:00
|
|
|
fatal(err)
|
|
|
|
}
|
2015-02-01 13:21:06 +08:00
|
|
|
tty.set(config)
|
2015-02-01 12:51:12 +08:00
|
|
|
if container, err = factory.Create(context.String("id"), config); err != nil {
|
|
|
|
fatal(err)
|
2014-12-26 21:13:10 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-01 13:21:06 +08:00
|
|
|
go handleSignals(container, tty)
|
2015-02-01 12:51:12 +08:00
|
|
|
process := &libcontainer.Process{
|
|
|
|
Args: context.Args(),
|
2015-02-01 13:21:06 +08:00
|
|
|
Stdin: os.Stdin,
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stderr,
|
2014-07-22 07:25:46 +08:00
|
|
|
}
|
2015-02-01 13:21:06 +08:00
|
|
|
tty.attach(process)
|
2015-02-01 12:51:12 +08:00
|
|
|
if _, err := container.Start(process); err != nil {
|
|
|
|
fatal(err)
|
2014-12-15 23:08:56 +08:00
|
|
|
}
|
2015-02-01 12:51:12 +08:00
|
|
|
status, err := container.Wait()
|
2014-12-15 23:08:56 +08:00
|
|
|
if err != nil {
|
2015-02-01 12:51:12 +08:00
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
if err := container.Destroy(); err != nil {
|
|
|
|
fatal(err)
|
2014-12-15 23:08:56 +08:00
|
|
|
}
|
2015-02-01 13:21:06 +08:00
|
|
|
exit(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func exit(status syscall.WaitStatus) {
|
|
|
|
var exitCode int
|
2014-12-15 23:08:56 +08:00
|
|
|
if status.Exited() {
|
|
|
|
exitCode = status.ExitStatus()
|
|
|
|
} else if status.Signaled() {
|
|
|
|
exitCode = -int(status.Signal())
|
|
|
|
} else {
|
2015-02-01 12:51:12 +08:00
|
|
|
fatalf("Unexpected status")
|
2014-12-15 23:08:56 +08:00
|
|
|
}
|
2014-07-22 07:25:46 +08:00
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
2014-12-26 21:13:10 +08:00
|
|
|
|
2015-02-01 13:21:06 +08:00
|
|
|
func handleSignals(container libcontainer.Container, tty *tty) {
|
|
|
|
sigc := make(chan os.Signal, 10)
|
|
|
|
signal.Notify(sigc)
|
|
|
|
tty.resize()
|
|
|
|
for sig := range sigc {
|
|
|
|
switch sig {
|
|
|
|
case syscall.SIGWINCH:
|
|
|
|
tty.resize()
|
|
|
|
default:
|
|
|
|
container.Signal(sig)
|
|
|
|
}
|
2014-12-26 21:13:10 +08:00
|
|
|
}
|
2015-02-01 13:21:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTty(context *cli.Context) (*tty, error) {
|
|
|
|
if context.Bool("tty") {
|
|
|
|
master, console, err := consolepkg.CreateMasterAndConsole()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
go io.Copy(master, os.Stdin)
|
|
|
|
go io.Copy(os.Stdout, master)
|
|
|
|
state, err := term.SetRawTerminal(os.Stdin.Fd())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &tty{
|
|
|
|
master: master,
|
|
|
|
console: console,
|
|
|
|
state: state,
|
|
|
|
}, nil
|
2014-12-26 21:13:10 +08:00
|
|
|
}
|
2015-02-01 13:21:06 +08:00
|
|
|
return &tty{}, nil
|
2014-12-26 21:13:10 +08:00
|
|
|
}
|