mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
Reuse exec cli function and strip nsenter- from funcs
Signed-off-by: Michael Crosby <michael@docker.com>
This commit is contained in:
+19
-1
@@ -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()))
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
+5
-5
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user