2014-02-20 00:40:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-02-20 00:50:10 +00:00
|
|
|
"errors"
|
2014-02-21 01:53:50 +00:00
|
|
|
"flag"
|
2014-02-20 00:40:36 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
2014-02-21 02:27:42 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/nsinit"
|
2014-02-20 04:35:04 +00:00
|
|
|
"io/ioutil"
|
2014-02-20 00:40:36 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2014-02-20 04:35:04 +00:00
|
|
|
"strconv"
|
2014-02-20 00:40:36 +00:00
|
|
|
)
|
|
|
|
|
2014-02-21 02:38:28 +00:00
|
|
|
var (
|
|
|
|
console string
|
|
|
|
pipeFd int
|
|
|
|
)
|
|
|
|
|
2014-02-20 00:50:10 +00:00
|
|
|
var (
|
|
|
|
ErrUnsupported = errors.New("Unsupported method")
|
|
|
|
ErrWrongArguments = errors.New("Wrong argument count")
|
|
|
|
)
|
|
|
|
|
2014-02-24 21:40:17 +00:00
|
|
|
func registerFlags() {
|
2014-02-21 02:38:28 +00:00
|
|
|
flag.StringVar(&console, "console", "", "console (pty slave) path")
|
|
|
|
flag.IntVar(&pipeFd, "pipe", 0, "sync pipe fd")
|
|
|
|
|
2014-02-21 01:53:50 +00:00
|
|
|
flag.Parse()
|
2014-02-21 02:38:28 +00:00
|
|
|
}
|
2014-02-21 01:53:50 +00:00
|
|
|
|
2014-02-21 02:38:28 +00:00
|
|
|
func main() {
|
2014-02-24 21:40:17 +00:00
|
|
|
registerFlags()
|
|
|
|
|
2014-02-21 22:49:55 +00:00
|
|
|
if flag.NArg() < 1 {
|
|
|
|
log.Fatal(ErrWrongArguments)
|
|
|
|
}
|
2014-02-20 00:40:36 +00:00
|
|
|
container, err := loadContainer()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-02-25 02:38:24 +00:00
|
|
|
ns, err := newNsInit()
|
|
|
|
if err != nil {
|
2014-02-21 22:49:55 +00:00
|
|
|
log.Fatal(err)
|
2014-02-20 00:50:10 +00:00
|
|
|
}
|
2014-02-25 02:38:24 +00:00
|
|
|
|
2014-02-21 01:53:50 +00:00
|
|
|
switch flag.Arg(0) {
|
2014-02-20 06:43:40 +00:00
|
|
|
case "exec": // this is executed outside of the namespace in the cwd
|
2014-02-20 04:35:04 +00:00
|
|
|
var exitCode int
|
|
|
|
nspid, err := readPid()
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nspid > 0 {
|
2014-02-25 02:38:24 +00:00
|
|
|
exitCode, err = ns.ExecIn(container, nspid, flag.Args()[1:])
|
2014-02-20 04:35:04 +00:00
|
|
|
} else {
|
2014-02-22 08:29:21 +00:00
|
|
|
term := nsinit.NewTerminal(os.Stdin, os.Stdout, os.Stderr, container.Tty)
|
2014-02-25 02:38:24 +00:00
|
|
|
exitCode, err = ns.Exec(container, term, flag.Args()[1:])
|
2014-02-20 04:35:04 +00:00
|
|
|
}
|
2014-02-20 00:40:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
os.Exit(exitCode)
|
2014-02-20 06:43:40 +00:00
|
|
|
case "init": // this is executed inside of the namespace to setup the container
|
2014-02-21 22:49:55 +00:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-02-21 01:53:50 +00:00
|
|
|
if flag.NArg() < 2 {
|
2014-02-20 00:50:10 +00:00
|
|
|
log.Fatal(ErrWrongArguments)
|
|
|
|
}
|
2014-02-22 06:58:30 +00:00
|
|
|
syncPipe, err := nsinit.NewSyncPipeFromFd(0, uintptr(pipeFd))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-02-25 02:38:24 +00:00
|
|
|
if err := ns.Init(container, cwd, console, syncPipe, flag.Args()[1:]); err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-02-20 03:53:25 +00:00
|
|
|
default:
|
2014-02-21 01:53:50 +00:00
|
|
|
log.Fatalf("command not supported for nsinit %s", flag.Arg(0))
|
2014-02-20 00:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadContainer() (*libcontainer.Container, error) {
|
|
|
|
f, err := os.Open("container.json")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var container *libcontainer.Container
|
|
|
|
if err := json.NewDecoder(f).Decode(&container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return container, nil
|
|
|
|
}
|
2014-02-20 04:35:04 +00:00
|
|
|
|
|
|
|
func readPid() (int, error) {
|
|
|
|
data, err := ioutil.ReadFile(".nspid")
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
pid, err := strconv.Atoi(string(data))
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return pid, nil
|
|
|
|
}
|
2014-02-21 22:49:55 +00:00
|
|
|
|
2014-02-25 02:38:24 +00:00
|
|
|
func newNsInit() (nsinit.NsInit, error) {
|
2014-02-25 05:11:52 +00:00
|
|
|
return nsinit.NewNsInit(&nsinit.DefaultCommandFactory{}, &nsinit.DefaultStateWriter{}), nil
|
2014-02-21 22:49:55 +00:00
|
|
|
}
|