Skip to content

Commit

Permalink
[ws-daemon] Collect logs from runc command
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Aug 11, 2021
1 parent 5e232e8 commit cb54263
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions components/ws-daemon/pkg/content/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"syscall"

"github.com/opencontainers/runtime-spec/specs-go"
Expand Down Expand Up @@ -229,12 +231,36 @@ func RunInitializer(ctx context.Context, destination string, initializer *csapi.
withDebug = "--debug"
}

logEntry := log.WithFields(opts.OWI)

rd, rw := io.Pipe()
defer rw.Close()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
b := make([]byte, 4096)
for {
n, err := rd.Read(b)
if err == io.EOF {
break
}
if err != nil {
log.WithError(err).Error("cannot read from content initializer output")
return
}

logEntry.Debug(string(b[:n]))
}
}()

cmd = exec.Command("runc", "--root", "state", withDebug, "--log-format", "json", "run", "gogogo")
cmd.Dir = tmpdir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdout = rw
cmd.Stderr = rw
cmd.Stdin = os.Stdin
err = cmd.Run()
wg.Wait()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0. If it's 42, it was deliberate.
Expand All @@ -249,6 +275,8 @@ func RunInitializer(ctx context.Context, destination string, initializer *csapi.
return nil
}

// CombinedOutput

// RunInitializerChild is the function that's expected to run when we call `/proc/self/exe content-initializer`
func RunInitializerChild() (err error) {
fc, err := os.ReadFile("/content.json")
Expand Down

0 comments on commit cb54263

Please sign in to comment.