/
exec.go
95 lines (84 loc) · 2.23 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"os"
"code.cloudfoundry.org/winc/runtime"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/urfave/cli"
)
var execCommand = cli.Command{
Name: "exec",
Usage: "execute new process inside a container",
ArgsUsage: `<container-id> <command> [command options] || -p process.json <container-id>
Where "<container-id>" is the name for the instance of the container and
"<command>" is the command to be executed in the container.
"<command>" can't be empty unless a "-p" flag provided.
EXAMPLE:
For example, if the container is configured to run the Windows ps command the
following will output a list of processes running in the container:
# winc exec <container-id> ps`,
Flags: []cli.Flag{
cli.StringFlag{
Name: "pid-file",
Value: "",
Usage: "specify the file to write the process id to",
},
cli.BoolFlag{
Name: "detach,d",
Usage: "detach from the container's process",
},
cli.StringFlag{
Name: "process, p",
Value: "",
Usage: "path to the process.json",
},
cli.StringFlag{
Name: "cwd",
Value: "",
Usage: "current working directory in the container",
},
cli.StringFlag{
Name: "user, u",
Value: "",
Usage: "Username to execute process as",
},
cli.StringSliceFlag{
Name: "env, e",
Usage: "set environment variables",
},
// cli.BoolFlag{
// Name: "tty, t",
// Usage: "allocate a pseudo-TTY",
// },
},
Action: func(context *cli.Context) error {
if err := checkArgs(context, 1, minArgs); err != nil {
return err
}
containerId := context.Args().First()
processConfig := context.String("process")
args := context.Args()[1:]
cwd := context.String("cwd")
user := context.String("user")
env := context.StringSlice("env")
pidFile := context.String("pid-file")
detach := context.Bool("detach")
processOverrides := &specs.Process{
Args: args,
Cwd: cwd,
User: specs.User{
Username: user,
},
Env: env,
}
io := runtime.IO{Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr}
exitCode, err := run.Exec(containerId, processConfig, pidFile, processOverrides, io, detach)
if err != nil {
return err
}
if !detach {
os.Exit(exitCode)
}
return nil
},
SkipArgReorder: true,
}