Implemented log rotator for exec based drivers

This commit is contained in:
Diptanu Choudhury
2016-02-04 23:54:15 -08:00
parent e4f4e35c80
commit 7bf254c9af
8 changed files with 30 additions and 12 deletions

View File

@@ -74,6 +74,12 @@ func (g *TaskGroup) AddTask(t *Task) *TaskGroup {
return g
}
// LogConfig provides configuration for log rotation
type LogConfig struct {
MaxFiles int `mapstructure:"max_files"`
MaxFileSizeMB int `mapstructure:"max_file_size"`
}
// Task is a single process in a task group.
type Task struct {
Name string
@@ -85,6 +91,7 @@ type Task struct {
Resources *Resources
Meta map[string]string
KillTimeout time.Duration
LogConfig *LogConfig
}
// NewTask creates and initializes a new Task.

View File

@@ -116,6 +116,7 @@ func (d *ExecDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle,
AllocDir: ctx.AllocDir,
TaskName: task.Name,
TaskResources: task.Resources,
LogConfig: task.LogConfig,
ResourceLimits: true,
FSIsolation: true,
UnprivilegedUser: true,

View File

@@ -2,6 +2,7 @@ package executor
import (
"fmt"
"io"
"log"
"os"
"os/exec"
@@ -15,6 +16,7 @@ import (
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/driver/env"
"github.com/hashicorp/nomad/client/driver/logrotator"
"github.com/hashicorp/nomad/nomad/structs"
)
@@ -25,6 +27,7 @@ type ExecutorContext struct {
AllocDir *allocdir.AllocDir
TaskName string
TaskResources *structs.Resources
LogConfig *structs.LogConfig
FSIsolation bool
ResourceLimits bool
UnprivilegedUser bool
@@ -94,19 +97,23 @@ func (e *UniversalExecutor) LaunchCmd(command *ExecCommand, ctx *ExecutorContext
}
}
stdoPath := filepath.Join(e.taskDir, allocdir.TaskLocal, fmt.Sprintf("%v.stdout", ctx.TaskName))
stdo, err := os.OpenFile(stdoPath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
return nil, err
}
e.cmd.Stdout = stdo
logFileSize := int64(ctx.LogConfig.MaxFileSizeMB * 1024 * 1024)
stdePath := filepath.Join(e.taskDir, allocdir.TaskLocal, fmt.Sprintf("%v.stderr", ctx.TaskName))
stde, err := os.OpenFile(stdePath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
stdor, stdow := io.Pipe()
lro, err := logrotator.NewLogRotator(filepath.Join(e.taskDir, allocdir.TaskLocal), fmt.Sprintf("%v.stdout", ctx.TaskName), ctx.LogConfig.MaxFiles, logFileSize, e.logger)
if err != nil {
return nil, err
return nil, fmt.Errorf("error creating log rotator for stdout of task %v", err)
}
e.cmd.Stderr = stde
e.cmd.Stdout = stdow
go lro.Start(stdor)
stder, stdew := io.Pipe()
lre, err := logrotator.NewLogRotator(filepath.Join(e.taskDir, allocdir.TaskLocal), 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)
}
e.cmd.Stderr = stdew
go lre.Start(stder)
e.cmd.Env = ctx.TaskEnv.EnvList()

View File

@@ -162,6 +162,7 @@ func (d *JavaDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle,
AllocDir: ctx.AllocDir,
TaskName: task.Name,
TaskResources: task.Resources,
LogConfig: task.LogConfig,
}
ps, err := exec.LaunchCmd(&executor.ExecCommand{Cmd: "java", Args: args}, executorCtx)
if err != nil {

View File

@@ -1,4 +1,4 @@
package driver
package logrotator
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package driver
package logrotator
import (
"io"

View File

@@ -206,6 +206,7 @@ func (d *QemuDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle,
AllocDir: ctx.AllocDir,
TaskName: task.Name,
TaskResources: task.Resources,
LogConfig: task.LogConfig,
}
ps, err := exec.LaunchCmd(&executor.ExecCommand{Cmd: args[0], Args: args[1:]}, executorCtx)
if err != nil {

View File

@@ -112,6 +112,7 @@ func (d *RawExecDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandl
AllocDir: ctx.AllocDir,
TaskName: task.Name,
TaskResources: task.Resources,
LogConfig: task.LogConfig,
}
ps, err := exec.LaunchCmd(&executor.ExecCommand{Cmd: command, Args: driverConfig.Args}, executorCtx)
if err != nil {