2014-02-20 08:40:36 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-05-30 08:23:18 +08:00
|
|
|
"fmt"
|
2014-02-20 12:35:04 +08:00
|
|
|
"io/ioutil"
|
2014-02-20 08:40:36 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
2014-05-14 16:56:55 +08:00
|
|
|
"os/exec"
|
|
|
|
"os/signal"
|
2014-02-26 04:41:31 +08:00
|
|
|
"path/filepath"
|
2014-02-20 12:35:04 +08:00
|
|
|
"strconv"
|
2014-03-04 13:47:03 +08:00
|
|
|
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
2014-05-30 08:23:18 +08:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/cgroups/fs"
|
2014-06-05 06:47:57 +08:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/namespaces"
|
2014-02-20 08:40:36 +08:00
|
|
|
)
|
|
|
|
|
2014-02-21 10:38:28 +08:00
|
|
|
var (
|
2014-05-01 08:55:15 +08:00
|
|
|
dataPath = os.Getenv("data_path")
|
|
|
|
console = os.Getenv("console")
|
|
|
|
rawPipeFd = os.Getenv("pipe")
|
2014-02-21 10:38:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2014-05-01 08:55:15 +08:00
|
|
|
if len(os.Args) < 2 {
|
|
|
|
log.Fatalf("invalid number of arguments %d", len(os.Args))
|
2014-02-20 08:40:36 +08:00
|
|
|
}
|
2014-03-14 01:35:16 +08:00
|
|
|
|
2014-05-01 08:55:15 +08:00
|
|
|
container, err := loadContainer()
|
2014-02-25 10:38:24 +08:00
|
|
|
if err != nil {
|
2014-05-01 08:55:15 +08:00
|
|
|
log.Fatalf("unable to load container: %s", err)
|
2014-02-20 08:50:10 +08:00
|
|
|
}
|
2014-02-25 10:38:24 +08:00
|
|
|
|
2014-05-01 08:55:15 +08:00
|
|
|
switch os.Args[1] {
|
2014-02-20 14:43:40 +08:00
|
|
|
case "exec": // this is executed outside of the namespace in the cwd
|
2014-05-01 08:55:15 +08:00
|
|
|
var nspid, exitCode int
|
|
|
|
if nspid, err = readPid(); err != nil && !os.IsNotExist(err) {
|
|
|
|
log.Fatalf("unable to read pid: %s", err)
|
2014-02-20 12:35:04 +08:00
|
|
|
}
|
2014-05-01 08:55:15 +08:00
|
|
|
|
2014-02-20 12:35:04 +08:00
|
|
|
if nspid > 0 {
|
2014-06-05 06:47:57 +08:00
|
|
|
exitCode, err = namespaces.ExecIn(container, nspid, os.Args[2:])
|
2014-02-20 12:35:04 +08:00
|
|
|
} else {
|
2014-06-05 06:47:57 +08:00
|
|
|
term := namespaces.NewTerminal(os.Stdin, os.Stdout, os.Stderr, container.Tty)
|
2014-05-14 16:56:55 +08:00
|
|
|
exitCode, err = startContainer(container, term, dataPath, os.Args[2:])
|
2014-02-20 12:35:04 +08:00
|
|
|
}
|
2014-05-01 08:55:15 +08:00
|
|
|
|
2014-02-20 08:40:36 +08:00
|
|
|
if err != nil {
|
2014-05-01 08:55:15 +08:00
|
|
|
log.Fatalf("failed to exec: %s", err)
|
2014-02-20 08:40:36 +08:00
|
|
|
}
|
|
|
|
os.Exit(exitCode)
|
2014-02-20 14:43:40 +08:00
|
|
|
case "init": // this is executed inside of the namespace to setup the container
|
2014-05-01 08:55:15 +08:00
|
|
|
// by default our current dir is always our rootfs
|
|
|
|
rootfs, err := os.Getwd()
|
2014-02-22 06:49:55 +08:00
|
|
|
if err != nil {
|
2014-05-01 06:24:18 +08:00
|
|
|
log.Fatal(err)
|
2014-02-22 06:49:55 +08:00
|
|
|
}
|
2014-05-01 08:55:15 +08:00
|
|
|
|
|
|
|
pipeFd, err := strconv.Atoi(rawPipeFd)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2014-02-20 08:50:10 +08:00
|
|
|
}
|
2014-06-05 06:47:57 +08:00
|
|
|
syncPipe, err := namespaces.NewSyncPipeFromFd(0, uintptr(pipeFd))
|
2014-02-22 14:58:30 +08:00
|
|
|
if err != nil {
|
2014-05-01 08:55:15 +08:00
|
|
|
log.Fatalf("unable to create sync pipe: %s", err)
|
2014-02-22 14:58:30 +08:00
|
|
|
}
|
2014-05-01 08:55:15 +08:00
|
|
|
|
2014-06-05 06:47:57 +08:00
|
|
|
if err := namespaces.Init(container, rootfs, console, syncPipe, os.Args[2:]); err != nil {
|
2014-05-01 08:55:15 +08:00
|
|
|
log.Fatalf("unable to initialize for container: %s", err)
|
2014-02-20 08:40:36 +08:00
|
|
|
}
|
2014-05-30 08:23:18 +08:00
|
|
|
case "stats":
|
|
|
|
// returns the stats of the current container.
|
|
|
|
stats, err := getContainerStats(container)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to get stats - %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Printf("Stats:\n%v\n", stats)
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
|
|
case "spec":
|
|
|
|
// returns the spec of the current container.
|
|
|
|
spec, err := getContainerSpec(container)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to get spec - %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Printf("Spec:\n%v\n", spec)
|
|
|
|
os.Exit(0)
|
|
|
|
|
2014-02-20 11:53:25 +08:00
|
|
|
default:
|
2014-05-01 08:55:15 +08:00
|
|
|
log.Fatalf("command not supported for nsinit %s", os.Args[0])
|
2014-02-20 08:40:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadContainer() (*libcontainer.Container, error) {
|
2014-05-01 08:55:15 +08:00
|
|
|
f, err := os.Open(filepath.Join(dataPath, "container.json"))
|
2014-02-20 08:40:36 +08: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 12:35:04 +08:00
|
|
|
|
|
|
|
func readPid() (int, error) {
|
2014-05-01 08:55:15 +08:00
|
|
|
data, err := ioutil.ReadFile(filepath.Join(dataPath, "pid"))
|
2014-02-20 12:35:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
pid, err := strconv.Atoi(string(data))
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return pid, nil
|
|
|
|
}
|
2014-05-14 16:56:55 +08:00
|
|
|
|
|
|
|
// startContainer starts the container. Returns the exit status or -1 and an
|
|
|
|
// error.
|
|
|
|
//
|
|
|
|
// Signals sent to the current process will be forwarded to container.
|
2014-06-05 06:47:57 +08:00
|
|
|
func startContainer(container *libcontainer.Container, term namespaces.Terminal, dataPath string, args []string) (int, error) {
|
2014-05-14 16:56:55 +08:00
|
|
|
var (
|
|
|
|
cmd *exec.Cmd
|
|
|
|
sigc = make(chan os.Signal, 10)
|
|
|
|
)
|
|
|
|
|
|
|
|
signal.Notify(sigc)
|
|
|
|
|
|
|
|
createCommand := func(container *libcontainer.Container, console, rootfs, dataPath, init string, pipe *os.File, args []string) *exec.Cmd {
|
2014-06-05 06:47:57 +08:00
|
|
|
cmd = namespaces.DefaultCreateCommand(container, console, rootfs, dataPath, init, pipe, args)
|
2014-05-14 16:56:55 +08:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
startCallback := func() {
|
|
|
|
go func() {
|
|
|
|
for sig := range sigc {
|
|
|
|
cmd.Process.Signal(sig)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-06-05 06:47:57 +08:00
|
|
|
return namespaces.Exec(container, term, "", dataPath, args, createCommand, startCallback)
|
2014-05-14 16:56:55 +08:00
|
|
|
}
|
2014-05-30 08:23:18 +08:00
|
|
|
|
|
|
|
// returns the container stats in json format.
|
|
|
|
func getContainerStats(container *libcontainer.Container) (string, error) {
|
|
|
|
stats, err := fs.GetStats(container.Cgroups)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
out, err := json.MarshalIndent(stats, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(out), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the container spec in json format.
|
|
|
|
func getContainerSpec(container *libcontainer.Container) (string, error) {
|
|
|
|
spec, err := json.MarshalIndent(container, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(spec), nil
|
|
|
|
}
|