Put a logger in AllocDir/TaskDir

This commit is contained in:
Michael Schurter
2017-01-05 15:57:58 -08:00
parent 957adf2df3
commit e25274b775
9 changed files with 30 additions and 18 deletions

View File

@@ -132,7 +132,7 @@ func (r *AllocRunner) RestoreState() error {
// Context struct to new AllocDir struct
if snap.AllocDir == nil && snap.Context != nil {
r.logger.Printf("[DEBUG] client: migrating state snapshot for alloc %q", r.alloc.ID)
snap.AllocDir = allocdir.NewAllocDir(snap.Context.AllocDir.AllocDir)
snap.AllocDir = allocdir.NewAllocDir(r.logger, snap.Context.AllocDir.AllocDir)
for taskName := range snap.Context.AllocDir.TaskDirs {
snap.AllocDir.NewTaskDir(taskName)
}
@@ -437,7 +437,7 @@ func (r *AllocRunner) Run() {
r.allocDirLock.Lock()
if r.allocDir == nil {
// Build allocation directory
r.allocDir = allocdir.NewAllocDir(filepath.Join(r.config.AllocDir, r.alloc.ID))
r.allocDir = allocdir.NewAllocDir(r.logger, filepath.Join(r.config.AllocDir, r.alloc.ID))
if err := r.allocDir.Build(); err != nil {
r.logger.Printf("[WARN] client: failed to build task directories: %v", err)
r.setStatus(structs.AllocClientStatusFailed, fmt.Sprintf("failed to build task dirs for '%s'", alloc.TaskGroup))

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
@@ -53,6 +54,8 @@ type AllocDir struct {
// TaskDirs is a mapping of task names to their non-shared directory.
TaskDirs map[string]*TaskDir
logger *log.Logger
}
// AllocFileInfo holds information about a file inside the AllocDir
@@ -76,17 +79,18 @@ type AllocDirFS interface {
// NewAllocDir initializes the AllocDir struct with allocDir as base path for
// the allocation directory.
func NewAllocDir(allocDir string) *AllocDir {
func NewAllocDir(logger *log.Logger, allocDir string) *AllocDir {
return &AllocDir{
AllocDir: allocDir,
SharedDir: filepath.Join(allocDir, SharedAllocName),
TaskDirs: make(map[string]*TaskDir),
logger: logger,
}
}
// NewTaskDir creates a new TaskDir and adds it to the AllocDirs TaskDirs map.
func (d *AllocDir) NewTaskDir(name string) *TaskDir {
td := newTaskDir(d.AllocDir, name)
td := newTaskDir(d.logger, d.AllocDir, name)
d.TaskDirs[name] = td
return td
}

View File

@@ -49,6 +49,10 @@ var (
}
)
func testLogger() *log.Logger {
return log.New(os.Stderr, "", log.LstdFlags)
}
// Test that AllocDir.Build builds just the alloc directory.
func TestAllocDir_BuildAlloc(t *testing.T) {
tmp, err := ioutil.TempDir("", "AllocDir")
@@ -57,7 +61,7 @@ func TestAllocDir_BuildAlloc(t *testing.T) {
}
defer os.RemoveAll(tmp)
d := NewAllocDir(tmp)
d := NewAllocDir(testLogger(), tmp)
defer d.Destroy()
d.NewTaskDir(t1.Name)
d.NewTaskDir(t2.Name)
@@ -94,7 +98,7 @@ func TestAllocDir_MountSharedAlloc(t *testing.T) {
}
defer os.RemoveAll(tmp)
d := NewAllocDir(tmp)
d := NewAllocDir(testLogger(), tmp)
defer d.Destroy()
if err := d.Build(); err != nil {
t.Fatalf("Build() failed: %v", err)
@@ -138,7 +142,7 @@ func TestAllocDir_Snapshot(t *testing.T) {
}
defer os.RemoveAll(tmp)
d := NewAllocDir(tmp)
d := NewAllocDir(testLogger(), tmp)
defer d.Destroy()
if err := d.Build(); err != nil {
t.Fatalf("Build() failed: %v", err)
@@ -207,13 +211,13 @@ func TestAllocDir_Move(t *testing.T) {
defer os.RemoveAll(tmp2)
// Create two alloc dirs
d1 := NewAllocDir(tmp1)
d1 := NewAllocDir(testLogger(), tmp1)
if err := d1.Build(); err != nil {
t.Fatalf("Build() failed: %v", err)
}
defer d1.Destroy()
d2 := NewAllocDir(tmp2)
d2 := NewAllocDir(testLogger(), tmp2)
if err := d2.Build(); err != nil {
t.Fatalf("Build() failed: %v", err)
}
@@ -269,7 +273,7 @@ func TestAllocDir_EscapeChecking(t *testing.T) {
}
defer os.RemoveAll(tmp)
d := NewAllocDir(tmp)
d := NewAllocDir(testLogger(), tmp)
if err := d.Build(); err != nil {
t.Fatalf("Build() failed: %v", err)
}
@@ -311,7 +315,7 @@ func TestAllocDir_ReadAt_SecretDir(t *testing.T) {
}
defer os.RemoveAll(tmp)
d := NewAllocDir(tmp)
d := NewAllocDir(testLogger(), tmp)
if err := d.Build(); err != nil {
t.Fatalf("Build() failed: %v", err)
}

View File

@@ -3,6 +3,7 @@ package allocdir
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
@@ -35,13 +36,15 @@ type TaskDir struct {
// SecretsDir is the path to secrets/ directory on the host
// <task_dir>/secrets/
SecretsDir string
logger *log.Logger
}
// newTaskDir creates a TaskDir struct with paths set. Call Build() to
// create paths on disk.
//
// Call AllocDir.NewTaskDir to create new TaskDirs
func newTaskDir(allocDir, taskName string) *TaskDir {
func newTaskDir(logger *log.Logger, allocDir, taskName string) *TaskDir {
taskDir := filepath.Join(allocDir, taskName)
return &TaskDir{
Dir: taskDir,
@@ -50,6 +53,7 @@ func newTaskDir(allocDir, taskName string) *TaskDir {
SharedTaskDir: filepath.Join(taskDir, SharedAllocName),
LocalDir: filepath.Join(taskDir, TaskLocal),
SecretsDir: filepath.Join(taskDir, TaskSecrets),
logger: logger,
}
}

View File

@@ -15,7 +15,7 @@ func TestTaskDir_EmbedNonExistent(t *testing.T) {
}
defer os.RemoveAll(tmp)
d := NewAllocDir(tmp)
d := NewAllocDir(testLogger(), tmp)
defer d.Destroy()
td := d.NewTaskDir(t1.Name)
if err := d.Build(); err != nil {
@@ -37,7 +37,7 @@ func TestTaskDir_EmbedDirs(t *testing.T) {
}
defer os.RemoveAll(tmp)
d := NewAllocDir(tmp)
d := NewAllocDir(testLogger(), tmp)
defer d.Destroy()
td := d.NewTaskDir(t1.Name)
if err := d.Build(); err != nil {

View File

@@ -1616,7 +1616,7 @@ func (c *Client) migrateRemoteAllocDir(alloc *structs.Allocation, allocID string
}
// If there were no errors then we create the allocdir
prevAllocDir := allocdir.NewAllocDir(pathToAllocDir)
prevAllocDir := allocdir.NewAllocDir(c.logger, pathToAllocDir)
return prevAllocDir, nil
}

View File

@@ -1112,7 +1112,7 @@ func setupDockerVolumes(t *testing.T, cfg *config.Config, hostpath string) (*str
}
// Build alloc and task directory structure
allocDir := allocdir.NewAllocDir(filepath.Join(cfg.AllocDir, structs.GenerateUUID()))
allocDir := allocdir.NewAllocDir(testLogger(), filepath.Join(cfg.AllocDir, structs.GenerateUUID()))
if err := allocDir.Build(); err != nil {
t.Fatalf("failed to build alloc dir: %v", err)
}

View File

@@ -87,7 +87,7 @@ type testContext struct {
// It is up to the caller to call AllocDir.Destroy to cleanup.
func testDriverContexts(t *testing.T, task *structs.Task) *testContext {
cfg := testConfig()
allocDir := allocdir.NewAllocDir(filepath.Join(cfg.AllocDir, structs.GenerateUUID()))
allocDir := allocdir.NewAllocDir(testLogger(), filepath.Join(cfg.AllocDir, structs.GenerateUUID()))
if err := allocDir.Build(); err != nil {
t.Fatalf("AllocDir.Build() failed: %v", err)
}

View File

@@ -82,7 +82,7 @@ func testTaskRunnerFromAlloc(t *testing.T, restarts bool, alloc *structs.Allocat
// we have a mock so that doesn't happen.
task.Resources.Networks[0].ReservedPorts = []structs.Port{{"", 80}}
allocDir := allocdir.NewAllocDir(filepath.Join(conf.AllocDir, alloc.ID))
allocDir := allocdir.NewAllocDir(testLogger(), filepath.Join(conf.AllocDir, alloc.ID))
if err := allocDir.Build(); err != nil {
t.Fatalf("error building alloc dir: %v", err)
return nil