Reuse exec cli function and strip nsenter- from funcs

Signed-off-by: Michael Crosby <michael@docker.com>
This commit is contained in:
Michael Crosby 2014-08-12 17:33:20 -07:00
parent 7d1ba0698f
commit bdafa085ae
3 changed files with 24 additions and 64 deletions

View File

@ -8,6 +8,7 @@ import (
"os/exec"
"os/signal"
"syscall"
"text/tabwriter"
"github.com/codegangsta/cli"
"github.com/docker/docker/pkg/term"
@ -20,9 +21,26 @@ var execCommand = cli.Command{
Name: "exec",
Usage: "execute a new command inside a container",
Action: execAction,
Flags: []cli.Flag{
cli.BoolFlag{Name: "list", Usage: "list all registered exec functions"},
cli.StringFlag{Name: "func", Value: "exec", Usage: "function name to exec inside a container"},
},
}
func execAction(context *cli.Context) {
if context.Bool("list") {
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
fmt.Fprint(w, "NAME\tUSAGE\n")
for k, f := range argvs {
fmt.Fprintf(w, "%s\t%s\n", k, f.Usage)
}
w.Flush()
return
}
var exitCode int
container, err := loadConfig()
@ -36,7 +54,7 @@ func execAction(context *cli.Context) {
}
if state != nil {
exitCode, err = startInExistingContainer(container, state, "exec", context)
exitCode, err = startInExistingContainer(container, state, context.String("func"), context)
} else {
exitCode, err = startContainer(container, dataPath, []string(context.Args()))
}

View File

@ -1,58 +0,0 @@
package main
import (
"fmt"
"log"
"os"
"text/tabwriter"
"github.com/codegangsta/cli"
"github.com/docker/libcontainer"
)
var execFuncCommand = cli.Command{
Name: "func",
Usage: "execute a registered function inside an existing container",
Action: execFuncAction,
Flags: []cli.Flag{
cli.BoolFlag{Name: "list", Usage: "list all registered functions"},
cli.StringFlag{Name: "func", Usage: "function name to exec inside a container"},
},
}
func execFuncAction(context *cli.Context) {
if context.Bool("list") {
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
fmt.Fprint(w, "NAME\tUSAGE\n")
for k, f := range argvs {
fmt.Fprintf(w, "%s\t%s\n", k, f.Usage)
}
w.Flush()
return
}
var exitCode int
config, err := loadConfig()
if err != nil {
log.Fatal(err)
}
// FIXME: remove tty from container config, this should be per process
config.Tty = false
state, err := libcontainer.GetState(dataPath)
if err != nil {
log.Fatalf("unable to read state.json: %s", err)
}
exitCode, err = startInExistingContainer(config, state, context.String("func"), context)
if err != nil {
log.Fatal(err)
}
os.Exit(exitCode)
}

View File

@ -3,6 +3,7 @@ package main
import (
"log"
"os"
"strings"
"github.com/codegangsta/cli"
)
@ -13,17 +14,17 @@ var (
)
func init() {
argvs["nsenter-exec"] = &rFunc{
argvs["exec"] = &rFunc{
Usage: "execute a process inside an existing container",
Action: nsenterExec,
}
argvs["nsenter-mknod"] = &rFunc{
argvs["mknod"] = &rFunc{
Usage: "mknod a device inside an existing container",
Action: nsenterMknod,
}
argvs["nsenter-ip"] = &rFunc{
argvs["ip"] = &rFunc{
Usage: "display the container's network interfaces",
Action: nsenterIp,
}
@ -32,7 +33,7 @@ func init() {
func main() {
// we need to check our argv 0 for any registred functions to run instead of the
// normal cli code path
f, exists := argvs[os.Args[0]]
f, exists := argvs[strings.TrimPrefix(os.Args[0], "nsenter-")]
if exists {
runFunc(f)
@ -58,7 +59,6 @@ func main() {
configCommand,
pauseCommand,
unpauseCommand,
execFuncCommand,
}
if err := app.Run(os.Args); err != nil {