Skip to content

exec:support to set custom log path #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
path: src/github.com/containerd/go-runc
fetch-depth: 25

- uses: containerd/project-checks@v1.1.0
- uses: containerd/project-checks@d7751f3c375b8fe4a84c02a068184ee4c1f59bc4 # v1.2.2
with:
working-directory: src/github.com/containerd/go-runc

Expand Down
6 changes: 5 additions & 1 deletion command_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ import (
)

func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
return r.commandWithCustomLogPath(context, "", args...)
}

func (r *Runc) commandWithCustomLogPath(context context.Context, logPath string, args ...string) *exec.Cmd {
command := r.Command
if command == "" {
command = DefaultCommand
}
cmd := exec.CommandContext(context, command, append(r.args(), args...)...)
cmd := exec.CommandContext(context, command, append(r.args(logPath), args...)...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: r.Setpgid,
}
Expand Down
6 changes: 5 additions & 1 deletion command_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ import (
)

func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
return r.commandWithCustomLogPath(context, "", args...)
}

func (r *Runc) commandWithCustomLogPath(context context.Context, logPath string, args ...string) *exec.Cmd {
command := r.Command
if command == "" {
command = DefaultCommand
}
cmd := exec.CommandContext(context, command, append(r.args(), args...)...)
cmd := exec.CommandContext(context, command, append(r.args(logPath), args...)...)
cmd.Env = os.Environ()
return cmd
}
12 changes: 9 additions & 3 deletions runc.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func (r *Runc) Start(context context.Context, id string) error {
// ExecOpts holds optional settings when starting an exec process with runc
type ExecOpts struct {
IO
LogPath string
PidFile string
ConsoleSocket ConsoleSocket
Detach bool
Expand Down Expand Up @@ -280,7 +281,7 @@ func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts
return err
}
args = append(args, oargs...)
cmd := r.command(context, append(args, id)...)
cmd := r.commandWithCustomLogPath(context, opts.LogPath, append(args, id)...)
if opts.IO != nil {
opts.Set(cmd)
}
Expand Down Expand Up @@ -765,15 +766,20 @@ func (r *Runc) Features(context context.Context) (*features.Features, error) {
return &feat, nil
}

func (r *Runc) args() (out []string) {
func (r *Runc) args(logPath string) (out []string) {
if r.Root != "" {
out = append(out, "--root", r.Root)
}
if r.Debug {
out = append(out, "--debug")
}
if r.Log != "" {
out = append(out, "--log", r.Log)
if logPath == "" {
out = append(out, "--log", r.Log)
}
}
if logPath != "" {
out = append(out, "--log", logPath)
}
if r.LogFormat != none {
out = append(out, "--log-format", string(r.LogFormat))
Expand Down
27 changes: 27 additions & 0 deletions runc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"testing"
Expand Down Expand Up @@ -336,6 +337,32 @@ func TestCreateArgs(t *testing.T) {
}
}

func TestExecWithLogPath(t *testing.T) {
o := &Runc{}
out := o.args("/tmp/exec.log")
if strings.Join(out, " ") != "--log /tmp/exec.log" {
t.Fatalf("should be :--log /tmp/exec.log but got :%s", strings.Join(out, " "))
}

o = &Runc{Log: "/tmp/runc.log"}
out = o.args("/tmp/exec.log")
if strings.Join(out, " ") != "--log /tmp/exec.log" {
t.Fatalf("should be :--log /tmp/exec.log but got :%s", strings.Join(out, " "))
}

o = &Runc{Log: "/tmp/runc.log"}
out = o.args("")
if strings.Join(out, " ") != "--log /tmp/runc.log" {
t.Fatalf("should be :--log /tmp/runc.log but got :%s", strings.Join(out, " "))
}

o = &Runc{}
out = o.args("")
if len(out) != 0 {
t.Fatalf("len(out) should be 0 but got :%d", len(out))
}
}

func TestRuncFeatures(t *testing.T) {
ctx := context.Background()
if _, err := exec.LookPath(DefaultCommand); err != nil {
Expand Down
Loading