Update for nsenter

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-06-05 14:28:09 -07:00
parent 0001699b5f
commit 303bb7ca66
7 changed files with 85 additions and 27 deletions

View file

@ -19,17 +19,19 @@ var execCommand = cli.Command{
} }
func execAction(context *cli.Context) { func execAction(context *cli.Context) {
var ( var nspid, exitCode int
err error
nspid, exitCode int container, err := loadContainer()
) if err != nil {
log.Fatal(err)
}
if nspid, err = readPid(); err != nil && !os.IsNotExist(err) { if nspid, err = readPid(); err != nil && !os.IsNotExist(err) {
log.Fatalf("unable to read pid: %s", err) log.Fatalf("unable to read pid: %s", err)
} }
if nspid > 0 { if nspid > 0 {
exitCode, err = namespaces.ExecIn(container, nspid, []string(context.Args())) err = namespaces.ExecIn(container, nspid, []string(context.Args()))
} else { } else {
term := namespaces.NewTerminal(os.Stdin, os.Stdout, os.Stderr, container.Tty) term := namespaces.NewTerminal(os.Stdin, os.Stdout, os.Stderr, container.Tty)
exitCode, err = startContainer(container, term, dataPath, []string(context.Args())) exitCode, err = startContainer(container, term, dataPath, []string(context.Args()))

View file

@ -22,6 +22,11 @@ var (
) )
func initAction(context *cli.Context) { func initAction(context *cli.Context) {
container, err := loadContainer()
if err != nil {
log.Fatal(err)
}
rootfs, err := os.Getwd() rootfs, err := os.Getwd()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View file

@ -5,21 +5,15 @@ import (
"os" "os"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/dotcloud/docker/pkg/libcontainer"
) )
var ( var logPath = os.Getenv("log")
container *libcontainer.Container
logPath = os.Getenv("log")
)
func preload(context *cli.Context) (err error) { func preload(context *cli.Context) error {
container, err = loadContainer() if logPath != "" {
if err != nil { if err := openLog(logPath); err != nil {
return err return err
} }
if logPath != "" {
} }
return nil return nil
@ -37,6 +31,7 @@ func main() {
initCommand, initCommand,
statsCommand, statsCommand,
specCommand, specCommand,
nsenterCommand,
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {

View file

@ -0,0 +1,40 @@
package main
import (
"log"
"strconv"
"github.com/codegangsta/cli"
"github.com/dotcloud/docker/pkg/libcontainer/namespaces"
)
var nsenterCommand = cli.Command{
Name: "nsenter",
Usage: "init process for entering an existing namespace",
Action: nsenterAction,
}
func nsenterAction(context *cli.Context) {
args := context.Args()
if len(args) < 4 {
log.Fatalf("incorrect usage: <pid> <process label> <container JSON> <cmd>...")
}
container, err := loadContainerFromJson(args.Get(2))
if err != nil {
log.Fatalf("unable to load container: %s", err)
}
nspid, err := strconv.Atoi(args.Get(0))
if err != nil {
log.Fatalf("unable to read pid: %s from %q", err, args.Get(0))
}
if nspid <= 0 {
log.Fatalf("cannot enter into namespaces without valid pid: %q", nspid)
}
if err := namespaces.NsEnter(container, args.Get(1), nspid, args[3:]); err != nil {
log.Fatalf("failed to nsenter: %s", err)
}
}

View file

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"os"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/dotcloud/docker/pkg/libcontainer" "github.com/dotcloud/docker/pkg/libcontainer"
@ -17,15 +16,17 @@ var specCommand = cli.Command{
} }
func specAction(context *cli.Context) { func specAction(context *cli.Context) {
// returns the spec of the current container. container, err := loadContainer()
if err != nil {
log.Fatal(err)
}
spec, err := getContainerSpec(container) spec, err := getContainerSpec(container)
if err != nil { if err != nil {
log.Printf("Failed to get spec - %v\n", err) log.Fatalf("Failed to get spec - %v\n", err)
os.Exit(1)
} }
fmt.Printf("Spec:\n%v\n", spec)
os.Exit(0)
fmt.Printf("Spec:\n%v\n", spec)
} }
// returns the container spec in json format. // returns the container spec in json format.
@ -34,5 +35,6 @@ func getContainerSpec(container *libcontainer.Container) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
return string(spec), nil return string(spec), nil
} }

View file

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"os"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/dotcloud/docker/pkg/libcontainer" "github.com/dotcloud/docker/pkg/libcontainer"
@ -18,14 +17,17 @@ var statsCommand = cli.Command{
} }
func statsAction(context *cli.Context) { func statsAction(context *cli.Context) {
// returns the stats of the current container. container, err := loadContainer()
if err != nil {
log.Fatal(err)
}
stats, err := getContainerStats(container) stats, err := getContainerStats(container)
if err != nil { if err != nil {
log.Printf("Failed to get stats - %v\n", err) log.Fatalf("Failed to get stats - %v\n", err)
os.Exit(1)
} }
fmt.Printf("Stats:\n%v\n", stats) fmt.Printf("Stats:\n%v\n", stats)
os.Exit(0)
} }
// returns the container stats in json format. // returns the container stats in json format.
@ -34,9 +36,11 @@ func getContainerStats(container *libcontainer.Container) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
out, err := json.MarshalIndent(stats, "", "\t") out, err := json.MarshalIndent(stats, "", "\t")
if err != nil { if err != nil {
return "", err return "", err
} }
return string(out), nil return string(out), nil
} }

View file

@ -50,3 +50,13 @@ func openLog(name string) error {
return nil return nil
} }
func loadContainerFromJson(rawData string) (*libcontainer.Container, error) {
var container *libcontainer.Container
if err := json.Unmarshal([]byte(rawData), &container); err != nil {
return nil, err
}
return container, nil
}