2014-02-20 00:40:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-02-20 00:50:10 +00:00
|
|
|
"errors"
|
2014-02-20 00:40:36 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2014-02-20 00:50:10 +00:00
|
|
|
var (
|
|
|
|
ErrUnsupported = errors.New("Unsupported method")
|
|
|
|
ErrWrongArguments = errors.New("Wrong argument count")
|
|
|
|
)
|
|
|
|
|
2014-02-20 00:40:36 +00:00
|
|
|
func main() {
|
|
|
|
container, err := loadContainer()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-02-20 00:50:10 +00:00
|
|
|
argc := len(os.Args)
|
|
|
|
if argc < 2 {
|
|
|
|
log.Fatal(ErrWrongArguments)
|
|
|
|
}
|
2014-02-20 00:40:36 +00:00
|
|
|
switch os.Args[1] {
|
|
|
|
case "exec":
|
|
|
|
exitCode, err := execCommand(container)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
os.Exit(exitCode)
|
|
|
|
case "init":
|
2014-02-20 00:50:10 +00:00
|
|
|
if argc != 3 {
|
|
|
|
log.Fatal(ErrWrongArguments)
|
|
|
|
}
|
2014-02-20 00:40:36 +00:00
|
|
|
if err := initCommand(container, os.Args[2]); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-02-20 03:53:25 +00:00
|
|
|
case "execin":
|
|
|
|
exitCode, err := execinCommand(container)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
os.Exit(exitCode)
|
|
|
|
default:
|
|
|
|
log.Fatalf("command not supported for nsinit %s", os.Args[1])
|
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
|
|
|
|
}
|