From c699faf654ccee9d4977c850ea04c2561eda6844 Mon Sep 17 00:00:00 2001 From: Shishir Mahajan Date: Thu, 20 Aug 2020 17:38:33 -0700 Subject: [PATCH] Add support for {nomad alloc logs} command. --- containerd/containerd.go | 22 ++++++++++++++++++++-- containerd/driver.go | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/containerd/containerd.go b/containerd/containerd.go index 885529d..da7b5a7 100644 --- a/containerd/containerd.go +++ b/containerd/containerd.go @@ -19,6 +19,8 @@ package containerd import ( "fmt" + "os" + "syscall" "github.com/containerd/containerd" "github.com/containerd/containerd/cio" @@ -120,8 +122,24 @@ func (d *Driver) loadContainer(id string) (containerd.Container, error) { return d.client.LoadContainer(d.ctxContainerd, id) } -func (d *Driver) createTask(container containerd.Container) (containerd.Task, error) { - return container.NewTask(d.ctxContainerd, cio.NewCreator(cio.WithStdio)) +func (d *Driver) createTask(container containerd.Container, stdoutPath, stderrPath string) (containerd.Task, error) { + stdout, err := openFIFO(stdoutPath) + if err != nil { + return nil, err + } + + stderr, err := openFIFO(stderrPath) + if err != nil { + return nil, err + } + + return container.NewTask(d.ctxContainerd, cio.NewCreator(cio.WithStreams(nil, stdout, stderr))) +} + +// FIFO's are named pipes in linux. +// openFIFO() opens the nomad task stdout/stderr pipes and returns the fd. +func openFIFO(path string) (*os.File, error) { + return os.OpenFile(path, os.O_RDWR|syscall.O_NONBLOCK, 0600) } func (d *Driver) getTask(container containerd.Container) (containerd.Task, error) { diff --git a/containerd/driver.go b/containerd/driver.go index e36353c..d09a634 100644 --- a/containerd/driver.go +++ b/containerd/driver.go @@ -359,7 +359,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive d.logger.Info(fmt.Sprintf("Successfully created container with name: %s", containerName)) - task, err := d.createTask(container) + task, err := d.createTask(container, cfg.StdoutPath, cfg.StderrPath) if err != nil { return nil, nil, fmt.Errorf("Error in creating task: %v", err) }