2014-02-20 04:40:36 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-02-20 04:50:10 +04:00
|
|
|
"errors"
|
2014-02-20 04:40:36 +04:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
2014-02-20 08:35:04 +04:00
|
|
|
"io/ioutil"
|
2014-02-20 04:40:36 +04:00
|
|
|
"log"
|
|
|
|
"os"
|
2014-02-20 08:35:04 +04:00
|
|
|
"strconv"
|
2014-02-20 04:40:36 +04:00
|
|
|
)
|
|
|
|
|
2014-02-20 04:50:10 +04:00
|
|
|
var (
|
|
|
|
ErrUnsupported = errors.New("Unsupported method")
|
|
|
|
ErrWrongArguments = errors.New("Wrong argument count")
|
|
|
|
)
|
|
|
|
|
2014-02-20 04:40:36 +04:00
|
|
|
func main() {
|
|
|
|
container, err := loadContainer()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-02-20 04:50:10 +04:00
|
|
|
argc := len(os.Args)
|
|
|
|
if argc < 2 {
|
|
|
|
log.Fatal(ErrWrongArguments)
|
|
|
|
}
|
2014-02-20 04:40:36 +04:00
|
|
|
switch os.Args[1] {
|
2014-02-20 10:43:40 +04:00
|
|
|
case "exec": // this is executed outside of the namespace in the cwd
|
2014-02-20 08:35:04 +04:00
|
|
|
var exitCode int
|
|
|
|
nspid, err := readPid()
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nspid > 0 {
|
|
|
|
exitCode, err = execinCommand(container, nspid, os.Args[2:])
|
|
|
|
} else {
|
|
|
|
exitCode, err = execCommand(container, os.Args[2:])
|
|
|
|
}
|
2014-02-20 04:40:36 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
os.Exit(exitCode)
|
2014-02-20 10:43:40 +04:00
|
|
|
case "init": // this is executed inside of the namespace to setup the container
|
2014-02-20 08:35:04 +04:00
|
|
|
if argc < 3 {
|
2014-02-20 04:50:10 +04:00
|
|
|
log.Fatal(ErrWrongArguments)
|
|
|
|
}
|
2014-02-20 08:35:04 +04:00
|
|
|
if err := initCommand(container, os.Args[2], os.Args[3:]); err != nil {
|
2014-02-20 04:40:36 +04:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-02-20 07:53:25 +04:00
|
|
|
default:
|
|
|
|
log.Fatalf("command not supported for nsinit %s", os.Args[1])
|
2014-02-20 04:40:36 +04: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 08:35:04 +04: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
|
|
|
|
}
|