2014-02-20 00:40:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-02-21 01:53:50 +00:00
|
|
|
"flag"
|
2014-03-13 17:35:16 +00:00
|
|
|
"io"
|
2014-02-20 04:35:04 +00:00
|
|
|
"io/ioutil"
|
2014-02-20 00:40:36 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2014-02-25 20:41:31 +00:00
|
|
|
"path/filepath"
|
2014-02-20 04:35:04 +00:00
|
|
|
"strconv"
|
2014-03-04 05:47:03 +00:00
|
|
|
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/nsinit"
|
2014-02-20 00:40:36 +00:00
|
|
|
)
|
|
|
|
|
2014-02-21 02:38:28 +00:00
|
|
|
var (
|
2014-03-13 17:35:16 +00:00
|
|
|
root, console, logs string
|
|
|
|
pipeFd int
|
2014-02-21 02:38:28 +00:00
|
|
|
)
|
|
|
|
|
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-25 20:41:31 +00:00
|
|
|
flag.StringVar(&root, "root", ".", "root for storing configuration data")
|
2014-03-13 17:35:16 +00:00
|
|
|
flag.StringVar(&logs, "log", "none", "set stderr or a filepath to enable logging")
|
2014-02-21 02:38:28 +00:00
|
|
|
|
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 {
|
2014-04-25 00:20:14 +00:00
|
|
|
log.Fatalf("wrong number of arguments %d", flag.NArg())
|
2014-02-21 22:49:55 +00:00
|
|
|
}
|
2014-02-20 00:40:36 +00:00
|
|
|
container, err := loadContainer()
|
|
|
|
if err != nil {
|
2014-03-10 23:50:29 +00:00
|
|
|
log.Fatalf("Unable to load container: %s", err)
|
2014-02-20 00:40:36 +00:00
|
|
|
}
|
2014-03-13 17:35:16 +00:00
|
|
|
l, err := getLogger("[exec] ")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ns, err := newNsInit(l)
|
2014-02-25 02:38:24 +00:00
|
|
|
if err != nil {
|
2014-03-10 23:50:29 +00:00
|
|
|
log.Fatalf("Unable to initialize nsinit: %s", 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) {
|
2014-03-13 17:35:16 +00:00
|
|
|
l.Fatalf("Unable to read pid: %s", err)
|
2014-02-20 04:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2014-03-13 17:35:16 +00:00
|
|
|
l.Fatalf("Failed to exec: %s", err)
|
2014-02-20 00:40:36 +00:00
|
|
|
}
|
|
|
|
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 {
|
2014-03-13 17:35:16 +00:00
|
|
|
l.Fatal(err)
|
2014-02-21 22:49:55 +00:00
|
|
|
}
|
2014-02-21 01:53:50 +00:00
|
|
|
if flag.NArg() < 2 {
|
2014-04-25 00:20:14 +00:00
|
|
|
l.Fatalf("wrong number of arguments %d", flag.NArg())
|
2014-02-20 00:50:10 +00:00
|
|
|
}
|
2014-02-22 06:58:30 +00:00
|
|
|
syncPipe, err := nsinit.NewSyncPipeFromFd(0, uintptr(pipeFd))
|
|
|
|
if err != nil {
|
2014-03-13 17:35:16 +00:00
|
|
|
l.Fatalf("Unable to create sync pipe: %s", err)
|
2014-02-22 06:58:30 +00:00
|
|
|
}
|
2014-02-25 02:38:24 +00:00
|
|
|
if err := ns.Init(container, cwd, console, syncPipe, flag.Args()[1:]); err != nil {
|
2014-03-13 17:35:16 +00:00
|
|
|
l.Fatalf("Unable to initialize for container: %s", err)
|
2014-02-20 00:40:36 +00:00
|
|
|
}
|
2014-02-20 03:53:25 +00:00
|
|
|
default:
|
2014-03-13 17:35:16 +00:00
|
|
|
l.Fatalf("command not supported for nsinit %s", flag.Arg(0))
|
2014-02-20 00:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadContainer() (*libcontainer.Container, error) {
|
2014-02-25 20:41:31 +00:00
|
|
|
f, err := os.Open(filepath.Join(root, "container.json"))
|
2014-02-20 00:40:36 +00:00
|
|
|
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) {
|
2014-02-25 20:41:31 +00:00
|
|
|
data, err := ioutil.ReadFile(filepath.Join(root, "pid"))
|
2014-02-20 04:35:04 +00:00
|
|
|
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-03-13 17:35:16 +00:00
|
|
|
func newNsInit(l *log.Logger) (nsinit.NsInit, error) {
|
|
|
|
return nsinit.NewNsInit(&nsinit.DefaultCommandFactory{root}, &nsinit.DefaultStateWriter{root}, l), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLogger(prefix string) (*log.Logger, error) {
|
|
|
|
var w io.Writer
|
|
|
|
switch logs {
|
|
|
|
case "", "none":
|
|
|
|
w = ioutil.Discard
|
|
|
|
case "stderr":
|
|
|
|
w = os.Stderr
|
|
|
|
default: // we have a filepath
|
|
|
|
f, err := os.OpenFile(logs, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
w = f
|
|
|
|
}
|
|
|
|
return log.New(w, prefix, log.LstdFlags), nil
|
2014-02-21 22:49:55 +00:00
|
|
|
}
|