Skip to content

Commit

Permalink
cli: Use ExecOptions for detached exec.
Browse files Browse the repository at this point in the history
Enable ExecOptions for detached exec.
Improve logging for executed command.

Signed-off-by: Ruben Jenster <[email protected]>
  • Loading branch information
r10r committed May 6, 2021
1 parent 374d37a commit a2b8f0c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
61 changes: 33 additions & 28 deletions cmd/lxcri/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,42 +634,47 @@ func doExec(ctxcli *cli.Context) error {
}
defer clxc.releaseContainer(c)

opts := lxcri.ExecOptions{}

if ctxcli.Bool("cgroup") {
opts.Namespaces = append(opts.Namespaces, specs.CgroupNamespace)
}
if ctxcli.Bool("ipc") {
opts.Namespaces = append(opts.Namespaces, specs.IPCNamespace)
}
if ctxcli.Bool("mnt") {
opts.Namespaces = append(opts.Namespaces, specs.MountNamespace)
}
if ctxcli.Bool("net") {
opts.Namespaces = append(opts.Namespaces, specs.NetworkNamespace)
}
if ctxcli.Bool("pid") {
opts.Namespaces = append(opts.Namespaces, specs.PIDNamespace)
}
//if ctxcli.Bool("time") {
// opts.Namespaces = append(opts.Namespaces, specs.TimeNamespace)
//}
if ctxcli.Bool("user") {
opts.Namespaces = append(opts.Namespaces, specs.UserNamespace)
}
if ctxcli.Bool("uts") {
opts.Namespaces = append(opts.Namespaces, specs.UTSNamespace)
}

c.Log.Info().Str("cmd", procSpec.Args[0]).
Uint32("uid", procSpec.User.UID).Uint32("gid", procSpec.User.GID).
Uints32("groups", procSpec.User.AdditionalGids).
Str("namespaces", fmt.Sprintf("%s", opts.Namespaces)).Msg("execute cmd")

if detach {
pid, err := c.ExecDetached(procSpec, nil)
pid, err := c.ExecDetached(procSpec, &opts)
if err != nil {
return err
}
if pidFile != "" {
return createPidFile(pidFile, pid)
}
} else {
opts := lxcri.ExecOptions{}

if ctxcli.Bool("cgroup") {
opts.Namespaces = append(opts.Namespaces, specs.CgroupNamespace)
}
if ctxcli.Bool("ipc") {
opts.Namespaces = append(opts.Namespaces, specs.IPCNamespace)
}
if ctxcli.Bool("mnt") {
opts.Namespaces = append(opts.Namespaces, specs.MountNamespace)
}
if ctxcli.Bool("net") {
opts.Namespaces = append(opts.Namespaces, specs.NetworkNamespace)
}
if ctxcli.Bool("pid") {
opts.Namespaces = append(opts.Namespaces, specs.PIDNamespace)
}
//if ctxcli.Bool("time") {
// opts.Namespaces = append(opts.Namespaces, specs.TimeNamespace)
//}
if ctxcli.Bool("user") {
opts.Namespaces = append(opts.Namespaces, specs.UserNamespace)
}
if ctxcli.Bool("uts") {
opts.Namespaces = append(opts.Namespaces, specs.UTSNamespace)
}

status, err := c.Exec(procSpec, &opts)
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,6 @@ func (c *Container) ExecDetached(proc *specs.Process, execOpts *ExecOptions) (pi
return 0, errorf("failed to create attach options: %w", err)
}

c.Log.Info().Strs("args", proc.Args).
Int("uid", opts.UID).Int("gid", opts.GID).
Ints("groups", opts.Groups).Msg("execute cmd")

pid, err = c.LinuxContainer.RunCommandNoWait(proc.Args, opts)
if err != nil {
return pid, errorf("failed to run exec cmd detached: %w", err)
Expand Down

0 comments on commit a2b8f0c

Please sign in to comment.