Skip to content

Commit

Permalink
Merge pull request #851 from hashicorp/logdir
Browse files Browse the repository at this point in the history
Writing logs to alloc dir
  • Loading branch information
diptanu committed Feb 25, 2016
2 parents 608ab79 + 40b57b9 commit d7c3479
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 43 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 0.3.0 (UNRELEASED)

__BACKWARDS INCOMPATIBILITIES:__
* Stdout and Stderr log files of tasks have moved from task/local to
alloc/logs [GH-851]
* Any users of the runtime environment variable `$NOMAD_PORT_` will need to
update to the new `${NOMAD_ADDR_}` varriable [GH-704]
* Service names that include periods will fail validation. To fix, remove any
Expand Down
10 changes: 9 additions & 1 deletion client/allocdir/alloc_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ var (
// The name of the directory that is shared across tasks in a task group.
SharedAllocName = "alloc"

// Name of the directory where logs of Tasks are written
LogDirName = "logs"

// The set of directories that exist inside eache shared alloc directory.
SharedAllocDirs = []string{"logs", "tmp", "data"}
SharedAllocDirs = []string{LogDirName, "tmp", "data"}

// The name of the directory that exists inside each task directory
// regardless of driver.
Expand Down Expand Up @@ -275,6 +278,11 @@ func (d *AllocDir) MountSharedDir(task string) error {
return nil
}

// LogDir returns the log dir in the current allocation directory
func (d *AllocDir) LogDir() string {
return filepath.Join(d.AllocDir, SharedAllocName, LogDirName)
}

// List returns the list of files at a path relative to the alloc dir
func (d *AllocDir) List(path string) ([]*AllocFileInfo, error) {
p := filepath.Join(d.AllocDir, path)
Expand Down
16 changes: 16 additions & 0 deletions client/allocdir/alloc_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ func TestAllocDir_BuildAlloc(t *testing.T) {
}
}

func TestAllocDir_LogDir(t *testing.T) {
tmp, err := ioutil.TempDir("", "AllocDir")
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(tmp)

d := NewAllocDir(tmp)
defer d.Destroy()

expected := filepath.Join(d.AllocDir, SharedAllocName, LogDirName)
if d.LogDir() != expected {
t.Fatalf("expected: %v, got: %v", expected, d.LogDir())
}
}

func TestAllocDir_EmbedNonExistent(t *testing.T) {
tmp, err := ioutil.TempDir("", "AllocDir")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion client/driver/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ func TestExecDriver_KillUserPid_OnPluginReconnectFailure(t *testing.T) {
d := NewExecDriver(driverCtx)

handle, err := d.Start(execCtx, task)
defer handle.Kill()
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
defer handle.Kill()

id := &execId{}
if err := json.Unmarshal([]byte(handle.ID()), id); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions client/driver/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ func (e *UniversalExecutor) LaunchCmd(command *ExecCommand, ctx *ExecutorContext
}

logFileSize := int64(ctx.LogConfig.MaxFileSizeMB * 1024 * 1024)
path := filepath.Join(e.taskDir, allocdir.TaskLocal)
lro, err := logging.NewFileRotator(path, fmt.Sprintf("%v.stdout", ctx.TaskName),
lro, err := logging.NewFileRotator(ctx.AllocDir.LogDir(), fmt.Sprintf("%v.stdout", ctx.TaskName),
ctx.LogConfig.MaxFiles, logFileSize, e.logger)

if err != nil {
Expand All @@ -139,7 +138,7 @@ func (e *UniversalExecutor) LaunchCmd(command *ExecCommand, ctx *ExecutorContext
e.cmd.Stdout = lro
e.lro = lro

lre, err := logging.NewFileRotator(path, fmt.Sprintf("%v.stderr", ctx.TaskName),
lre, err := logging.NewFileRotator(ctx.AllocDir.LogDir(), fmt.Sprintf("%v.stderr", ctx.TaskName),
ctx.LogConfig.MaxFiles, logFileSize, e.logger)
if err != nil {
return nil, fmt.Errorf("error creating log rotator for stderr of task %v", err)
Expand Down
40 changes: 9 additions & 31 deletions client/driver/executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,11 @@ func TestExecutor_Start_Wait(t *testing.T) {
t.Fatalf("error in waiting for command: %v", err)
}

task := "web"
taskDir, ok := ctx.AllocDir.TaskDirs[task]
if !ok {
log.Panicf("No task directory found for task %v", task)
}

expected := "hello world"
file := filepath.Join(allocdir.TaskLocal, "web.stdout.0")
absFilePath := filepath.Join(taskDir, file)
output, err := ioutil.ReadFile(absFilePath)
file := filepath.Join(ctx.AllocDir.LogDir(), "web.stdout.0")
output, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("Couldn't read file %v", absFilePath)
t.Fatalf("Couldn't read file %v", file)
}

act := strings.TrimSpace(string(output))
Expand Down Expand Up @@ -143,18 +136,11 @@ func TestExecutor_IsolationAndConstraints(t *testing.T) {
t.Fatalf("error in waiting for command: %v", err)
}

task := "web"
taskDir, ok := ctx.AllocDir.TaskDirs[task]
if !ok {
log.Panicf("No task directory found for task %v", task)
}

expected := "hello world"
file := filepath.Join(allocdir.TaskLocal, "web.stdout.0")
absFilePath := filepath.Join(taskDir, file)
output, err := ioutil.ReadFile(absFilePath)
file := filepath.Join(ctx.AllocDir.LogDir(), "web.stdout.0")
output, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("Couldn't read file %v", absFilePath)
t.Fatalf("Couldn't read file %v", file)
}

act := strings.TrimSpace(string(output))
Expand All @@ -180,20 +166,12 @@ func TestExecutor_Start_Kill(t *testing.T) {
t.Fatalf("error in waiting for command: %v", err)
}

task := "web"
taskDir, ok := ctx.AllocDir.TaskDirs[task]
if !ok {
t.Fatalf("No task directory found for task %v", task)
}

file := filepath.Join(allocdir.TaskLocal, "web.stdout.0")
absFilePath := filepath.Join(taskDir, file)

file := filepath.Join(ctx.AllocDir.LogDir(), "web.stdout.0")
time.Sleep(time.Duration(tu.TestMultiplier()*2) * time.Second)

output, err := ioutil.ReadFile(absFilePath)
output, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("Couldn't read file %v", absFilePath)
t.Fatalf("Couldn't read file %v", file)
}

expected := ""
Expand Down
4 changes: 1 addition & 3 deletions client/driver/java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"
"time"

"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
Expand Down Expand Up @@ -143,8 +142,7 @@ func TestJavaDriver_Start_Wait(t *testing.T) {
}

// Get the stdout of the process and assrt that it's not empty
taskDir := execCtx.AllocDir.TaskDirs["demo-app"]
stdout := filepath.Join(taskDir, allocdir.TaskLocal, "demo-app.stdout.0")
stdout := filepath.Join(execCtx.AllocDir.LogDir(), "demo-app.stdout.0")
fInfo, err := os.Stat(stdout)
if err != nil {
t.Fatalf("failed to get stdout of process: %v", err)
Expand Down
6 changes: 2 additions & 4 deletions client/driver/logging/universal_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"log"
"log/syslog"
"net"
"path/filepath"

"github.com/hashicorp/nomad/client/allocdir"
cstructs "github.com/hashicorp/nomad/client/driver/structs"
Expand Down Expand Up @@ -90,16 +89,15 @@ func (s *SyslogCollector) LaunchCollector(ctx *LogCollectorContext) (*SyslogColl
go s.server.Start()
logFileSize := int64(ctx.LogConfig.MaxFileSizeMB * 1024 * 1024)

path := filepath.Join(s.taskDir, allocdir.TaskLocal)
lro, err := NewFileRotator(path, fmt.Sprintf("%v.stdout", ctx.TaskName),
lro, err := NewFileRotator(ctx.AllocDir.LogDir(), fmt.Sprintf("%v.stdout", ctx.TaskName),
ctx.LogConfig.MaxFiles, logFileSize, s.logger)

if err != nil {
return nil, err
}
s.lro = lro

lre, err := NewFileRotator(path, fmt.Sprintf("%v.stderr", ctx.TaskName),
lre, err := NewFileRotator(ctx.AllocDir.LogDir(), fmt.Sprintf("%v.stderr", ctx.TaskName),
ctx.LogConfig.MaxFiles, logFileSize, s.logger)
if err != nil {
return nil, err
Expand Down

0 comments on commit d7c3479

Please sign in to comment.