2014-08-13 01:46:43 +08:00
|
|
|
package main
|
2014-06-05 07:46:30 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer"
|
2015-02-10 06:07:18 +08:00
|
|
|
"github.com/docker/libcontainer/utils"
|
2014-06-05 07:46:30 +08:00
|
|
|
)
|
|
|
|
|
2015-02-10 06:07:18 +08:00
|
|
|
var standardEnvironment = &cli.StringSlice{
|
|
|
|
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
|
|
|
"HOSTNAME=nsinit",
|
|
|
|
"TERM=xterm",
|
2015-02-01 13:21:06 +08:00
|
|
|
}
|
|
|
|
|
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"},
|
2015-02-10 06:07:18 +08:00
|
|
|
cli.StringFlag{Name: "user,u", Value: "root", Usage: "set the user, uid, and/or gid for the process"},
|
|
|
|
cli.StringSliceFlag{Name: "env", Value: standardEnvironment, Usage: "set environment variables for the process"},
|
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-10 06:07:18 +08:00
|
|
|
config.Console = tty.console.Path()
|
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-10 06:07:18 +08:00
|
|
|
Env: context.StringSlice("env"),
|
|
|
|
User: context.String("user"),
|
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-04 02:50:18 +08:00
|
|
|
pid, err := container.Start(process)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
proc, err := os.FindProcess(pid)
|
|
|
|
if err != nil {
|
2015-02-01 12:51:12 +08:00
|
|
|
fatal(err)
|
2014-12-15 23:08:56 +08:00
|
|
|
}
|
2015-02-04 02:50:18 +08:00
|
|
|
status, err := proc.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-10 06:07:18 +08:00
|
|
|
os.Exit(utils.ExitStatus(status.Sys().(syscall.WaitStatus)))
|
2014-07-22 07:25:46 +08:00
|
|
|
}
|
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
|
|
|
}
|