Skip to content
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

User/maksiman/add binary logging support #1

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.exe
.idea
52 changes: 52 additions & 0 deletions cmd/containerd-shim-runhcs-v1/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Example logger, which writes to 2 different files
*/

package main

import (
"context"
"io"
"os"
"sync"

"github.com/Microsoft/hcsshim/cmd/logging"
)

func _main() {
logging.Run(logger)
}

func logger(_ context.Context, config *logging.Config, ready func() error) error {
var wg sync.WaitGroup
wg.Add(2)

fileOut, err := os.Create("C:/Users/Administrator/LCOW/container-stdout.txt")
defer fileOut.Close()
if err != nil {
return err
}

fileErr, err := os.Create("C:/Users/Administrator/LCOW/container-stderr.txt")
defer fileErr.Close()
if err != nil {
return err
}

go func() {
defer wg.Done()
io.Copy(fileOut, config.Stdout)
}()

go func() {
defer wg.Done()
io.Copy(fileErr, config.Stderr)
}()

if err := ready(); err != nil {
return err
}

wg.Wait()
return nil
}
45 changes: 41 additions & 4 deletions cmd/containerd-shim-runhcs-v1/task_hcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -124,7 +125,18 @@ func newHcsTask(

owner := filepath.Base(os.Args[0])

io, err := cmd.NewNpipeIO(ctx, req.Stdin, req.Stdout, req.Stderr, req.Terminal)
var (
io cmd.UpstreamIO
)

u, err := url.Parse(req.Stdout)

if u.Scheme != "binary" || err != nil {
io, err = cmd.NewNpipeIO(ctx, req.Stdin, req.Stdout, req.Stderr, req.Terminal)
} else {
io, err = cmd.NewBinaryIO(ctx, req.ID, u)
}

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -177,7 +189,8 @@ func newHcsTask(
req.Bundle,
ht.isWCOW,
s.Process,
io)
io,
)

if parent != nil {
// We have a parent UVM. Listen for its exit and forcibly close this
Expand Down Expand Up @@ -284,11 +297,35 @@ func (ht *hcsTask) CreateExec(ctx context.Context, req *task.ExecProcessRequest,
return errors.Wrapf(errdefs.ErrFailedPrecondition, "exec: '' in task: '%s' must be running to create additional execs", ht.id)
}

io, err := cmd.NewNpipeIO(ctx, req.Stdin, req.Stdout, req.Stderr, req.Terminal)
var (
io cmd.UpstreamIO
err error
)

u, err := url.Parse(req.Stdout)

if err != nil || u.Scheme != "binary" {
io, err = cmd.NewNpipeIO(ctx, req.Stdin, req.Stdout, req.Stderr, req.Terminal)
} else {
io, err = cmd.NewBinaryIO(ctx, req.ID, u)
}

if err != nil {
return err
}
he := newHcsExec(ctx, ht.events, ht.id, ht.host, ht.c, req.ExecID, ht.init.Status().Bundle, ht.isWCOW, spec, io)
he := newHcsExec(
ctx,
ht.events,
ht.id,
ht.host,
ht.c,
req.ExecID,
ht.init.Status().Bundle,
ht.isWCOW,
spec,
io,
)

ht.execs.Store(req.ExecID, he)

// Publish the created event
Expand Down
87 changes: 87 additions & 0 deletions cmd/logging/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package logging

import (
"bufio"
"context"
"fmt"
"io"
"os"
"time"

"github.com/Microsoft/go-winio"
)

// Config is passed to the binary logging function
type Config struct {
ID string
Namespace string
Stdout io.Reader
Stderr io.Reader
}

// LoggerFunc is a binary logging function signature
type LoggerFunc func(context.Context, *Config, func() error) error

// Run runs LoggerFunc
func Run(fn LoggerFunc) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var errCh = make(chan error, 0)

sout, _ := winio.DialPipeContext(ctx, os.Getenv("CONTAINER_STDOUT"))
serr, _ := winio.DialPipeContext(ctx, os.Getenv("CONTAINER_STDERR"))
wait, _ := winio.DialPipeContext(ctx, os.Getenv("CONTAINER_WAIT"))

config := &Config{
ID: os.Getenv("CONTAINER_ID"),
Namespace: os.Getenv("CONTAINER_NAMESPACE"),
Stdout: sout,
Stderr: serr,
}

// Write to wait pipe
ready := func() error {
wait.Write([]byte("#"))
return wait.Close()
}

f, _ := os.Create("C:/Users/Administrator/LCOW/binary-results.txt")
defer f.Close()

w := bufio.NewWriter(f)
w.WriteString("Starting logging goroutine\n")
w.Flush()

go func() {
if err := fn(ctx, config, ready); err != nil {
w.WriteString("Binary exited with error. sending error via channel\n")
w.Flush()
errCh <- err
return
}
w.WriteString("Binary exited normally.\n")
w.Flush()
errCh <- nil
}()

w.WriteString("Started logging goroutine\n")
w.Flush()

for {
select {
case err := <-errCh:
w.WriteString(fmt.Sprintf("Received from error channel: %s\n", err))
w.Flush()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
default:
w.WriteString("Nothing received, sleeping for 500ms\n")
w.Flush()
time.Sleep(500 * time.Millisecond)
}
}
}
Loading