2014-08-13 01:46:43 +08:00
|
|
|
package main
|
2014-06-05 07:46:30 +08:00
|
|
|
|
|
|
|
import (
|
2014-12-15 23:08:56 +08:00
|
|
|
"crypto/md5"
|
2014-06-05 07:46:30 +08:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
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"
|
2014-12-17 17:12:23 +08:00
|
|
|
"github.com/docker/libcontainer/configs"
|
2014-06-05 07:46:30 +08:00
|
|
|
)
|
|
|
|
|
2014-12-15 23:08:56 +08:00
|
|
|
var (
|
|
|
|
dataPath = os.Getenv("data_path")
|
|
|
|
console = os.Getenv("console")
|
|
|
|
rawPipeFd = os.Getenv("pipe")
|
|
|
|
)
|
|
|
|
|
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{
|
|
|
|
cli.BoolFlag{Name: "list", Usage: "list all registered exec functions"},
|
|
|
|
cli.StringFlag{Name: "func", Value: "exec", Usage: "function name to exec inside a container"},
|
|
|
|
},
|
2014-06-05 07:46:30 +08:00
|
|
|
}
|
|
|
|
|
2015-01-13 19:49:46 +08:00
|
|
|
func getContainer(context *cli.Context) (libcontainer.Container, error) {
|
2014-12-15 23:08:56 +08:00
|
|
|
factory, err := libcontainer.New(context.GlobalString("root"), []string{os.Args[0], "init", "--fd", "3", "--"})
|
2014-07-22 07:25:46 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-15 23:08:56 +08:00
|
|
|
id := fmt.Sprintf("%x", md5.Sum([]byte(dataPath)))
|
|
|
|
container, err := factory.Load(id)
|
2014-07-22 07:25:46 +08:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2014-12-17 17:12:23 +08:00
|
|
|
var config *configs.Config
|
2014-07-22 07:25:46 +08:00
|
|
|
|
2014-12-15 23:08:56 +08:00
|
|
|
config, err = loadConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
container, err = factory.Create(id, config)
|
|
|
|
}
|
2015-01-13 19:49:46 +08:00
|
|
|
|
|
|
|
return container, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func execAction(context *cli.Context) {
|
|
|
|
var exitCode int
|
|
|
|
|
|
|
|
process := &libcontainer.ProcessConfig{
|
|
|
|
Args: context.Args(),
|
|
|
|
Env: context.StringSlice("env"),
|
|
|
|
Stdin: os.Stdin,
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stderr,
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err := getContainer(context)
|
2014-12-15 23:08:56 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2014-07-22 07:25:46 +08:00
|
|
|
}
|
|
|
|
|
2014-12-15 23:08:56 +08:00
|
|
|
pid, err := container.StartProcess(process)
|
2014-07-22 07:25:46 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to exec: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-12-15 23:08:56 +08:00
|
|
|
p, err := os.FindProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to find the %d process: %s", pid, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ps, err := p.Wait()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to wait the %d process: %s", pid, err)
|
|
|
|
}
|
|
|
|
container.Destroy()
|
|
|
|
|
|
|
|
status := ps.Sys().(syscall.WaitStatus)
|
|
|
|
if status.Exited() {
|
|
|
|
exitCode = status.ExitStatus()
|
|
|
|
} else if status.Signaled() {
|
|
|
|
exitCode = -int(status.Signal())
|
|
|
|
} else {
|
|
|
|
log.Fatalf("Unexpected status")
|
|
|
|
}
|
|
|
|
|
2014-07-22 07:25:46 +08:00
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|