2018-09-24 18:37:45 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-01-15 17:31:57 +00:00
|
|
|
"encoding/json"
|
2018-09-24 18:37:45 +00:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2019-04-25 19:07:08 +00:00
|
|
|
"time"
|
2018-09-24 18:37:45 +00:00
|
|
|
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2018-10-04 23:22:01 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
2018-09-24 18:37:45 +00:00
|
|
|
"github.com/hashicorp/nomad/client/logmon"
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2019-02-25 23:42:45 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2019-04-25 19:07:08 +00:00
|
|
|
bstructs "github.com/hashicorp/nomad/plugins/base/structs"
|
2019-12-07 03:11:41 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2019-01-15 18:54:37 +00:00
|
|
|
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
2019-04-25 22:39:17 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
2018-09-24 18:37:45 +00:00
|
|
|
)
|
|
|
|
|
2019-02-21 20:02:41 +00:00
|
|
|
const (
|
|
|
|
// logmonReattachKey is the HookData key where logmon's reattach config
|
|
|
|
// is stored.
|
|
|
|
logmonReattachKey = "reattach_config"
|
|
|
|
)
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// logmonHook launches logmon and manages task logging
|
|
|
|
type logmonHook struct {
|
2019-12-07 03:11:41 +00:00
|
|
|
runner *TaskRunner
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
// logmon is the handle to the log monitor process for the task.
|
|
|
|
logmon logmon.LogMon
|
|
|
|
logmonPluginClient *plugin.Client
|
|
|
|
|
|
|
|
config *logmonHookConfig
|
|
|
|
|
|
|
|
logger hclog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type logmonHookConfig struct {
|
|
|
|
logDir string
|
|
|
|
stdoutFifo string
|
|
|
|
stderrFifo string
|
|
|
|
}
|
|
|
|
|
2019-12-07 03:11:41 +00:00
|
|
|
func newLogMonHook(tr *TaskRunner, logger hclog.Logger) *logmonHook {
|
2018-09-24 18:37:45 +00:00
|
|
|
hook := &logmonHook{
|
2019-12-07 03:11:41 +00:00
|
|
|
runner: tr,
|
|
|
|
config: tr.logmonHookConfig,
|
2018-09-24 18:37:45 +00:00
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
return hook
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLogMonHookConfig(taskName, logDir string) *logmonHookConfig {
|
|
|
|
cfg := &logmonHookConfig{
|
|
|
|
logDir: logDir,
|
|
|
|
}
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
id := uuid.Generate()[:8]
|
|
|
|
cfg.stdoutFifo = fmt.Sprintf("//./pipe/%s-%s.stdout", taskName, id)
|
|
|
|
cfg.stderrFifo = fmt.Sprintf("//./pipe/%s-%s.stderr", taskName, id)
|
|
|
|
} else {
|
|
|
|
cfg.stdoutFifo = filepath.Join(logDir, fmt.Sprintf(".%s.stdout.fifo", taskName))
|
|
|
|
cfg.stderrFifo = filepath.Join(logDir, fmt.Sprintf(".%s.stderr.fifo", taskName))
|
|
|
|
}
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*logmonHook) Name() string {
|
|
|
|
return "logmon"
|
|
|
|
}
|
|
|
|
|
2019-01-15 17:31:57 +00:00
|
|
|
func (h *logmonHook) launchLogMon(reattachConfig *plugin.ReattachConfig) error {
|
|
|
|
l, c, err := logmon.LaunchLogMon(h.logger, reattachConfig)
|
2018-09-24 18:37:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.logmon = l
|
|
|
|
h.logmonPluginClient = c
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-15 17:31:57 +00:00
|
|
|
func reattachConfigFromHookData(data map[string]string) (*plugin.ReattachConfig, error) {
|
2019-02-21 20:02:41 +00:00
|
|
|
if data == nil || data[logmonReattachKey] == "" {
|
2019-01-15 17:31:57 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 20:02:41 +00:00
|
|
|
var cfg pstructs.ReattachConfig
|
|
|
|
err := json.Unmarshal([]byte(data[logmonReattachKey]), &cfg)
|
2019-01-15 17:31:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-21 20:02:41 +00:00
|
|
|
return pstructs.ReattachConfigToGoPlugin(&cfg)
|
2019-01-15 17:31:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
func (h *logmonHook) Prestart(ctx context.Context,
|
|
|
|
req *interfaces.TaskPrestartRequest, resp *interfaces.TaskPrestartResponse) error {
|
|
|
|
|
2019-12-07 03:11:41 +00:00
|
|
|
if h.isLoggingDisabled() {
|
2019-12-13 16:08:12 +00:00
|
|
|
h.logger.Debug("logging is disabled by driver")
|
2019-12-07 03:11:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:09:36 +00:00
|
|
|
attempts := 0
|
2019-04-25 19:07:08 +00:00
|
|
|
for {
|
|
|
|
err := h.prestartOneLoop(ctx, req)
|
2019-04-25 22:39:17 +00:00
|
|
|
if err == bstructs.ErrPluginShutdown || grpc.Code(err) == codes.Unavailable {
|
2019-04-25 19:07:08 +00:00
|
|
|
h.logger.Warn("logmon shutdown while making request", "error", err)
|
|
|
|
|
2019-04-25 22:09:36 +00:00
|
|
|
if attempts > 3 {
|
|
|
|
h.logger.Warn("logmon shutdown while making request; giving up", "attempts", attempts, "error", err)
|
2019-04-25 19:07:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// retry after killing process and ensure we start a new logmon process
|
2019-04-25 22:09:36 +00:00
|
|
|
attempts++
|
|
|
|
h.logger.Warn("logmon shutdown while making request; retrying", "attempts", attempts, "error", err)
|
2019-04-25 19:07:08 +00:00
|
|
|
h.logmonPluginClient.Kill()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rCfg := pstructs.ReattachConfigFromGoPlugin(h.logmonPluginClient.ReattachConfig())
|
|
|
|
jsonCfg, err := json.Marshal(rCfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.State = map[string]string{logmonReattachKey: string(jsonCfg)}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 03:11:41 +00:00
|
|
|
func (h *logmonHook) isLoggingDisabled() bool {
|
|
|
|
ic, ok := h.runner.driver.(drivers.InternalCapabilitiesDriver)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
caps := ic.InternalCapabilities()
|
|
|
|
return caps.DisableLogCollection
|
|
|
|
}
|
|
|
|
|
2019-04-25 19:07:08 +00:00
|
|
|
func (h *logmonHook) prestartOneLoop(ctx context.Context, req *interfaces.TaskPrestartRequest) error {
|
|
|
|
// attach to a running logmon if state indicates one
|
2019-04-18 10:50:49 +00:00
|
|
|
if h.logmonPluginClient == nil {
|
2019-03-19 18:02:36 +00:00
|
|
|
reattachConfig, err := reattachConfigFromHookData(req.PreviousState)
|
2019-03-16 03:59:18 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logger.Error("failed to load reattach config", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
2019-04-18 10:50:49 +00:00
|
|
|
if reattachConfig != nil {
|
|
|
|
if err := h.launchLogMon(reattachConfig); err != nil {
|
2019-04-22 18:40:17 +00:00
|
|
|
h.logger.Warn("failed to reattach to logmon process", "error", err)
|
2019-04-25 19:07:08 +00:00
|
|
|
// if we failed to launch logmon, try again below
|
2019-04-18 10:50:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-03-16 03:59:18 +00:00
|
|
|
|
2019-04-25 19:07:08 +00:00
|
|
|
// create a new client in initial starts, failed reattachment, or if we detect exits
|
2019-04-18 10:50:49 +00:00
|
|
|
if h.logmonPluginClient == nil || h.logmonPluginClient.Exited() {
|
|
|
|
if err := h.launchLogMon(nil); err != nil {
|
|
|
|
// Retry errors launching logmon as logmon may have crashed on start and
|
2019-03-16 03:59:18 +00:00
|
|
|
// subsequent attempts will start a new one.
|
|
|
|
h.logger.Error("failed to launch logmon process", "error", err)
|
|
|
|
return structs.NewRecoverableError(err, true)
|
|
|
|
}
|
2018-09-24 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 18:02:36 +00:00
|
|
|
err := h.logmon.Start(&logmon.LogConfig{
|
|
|
|
LogDir: h.config.logDir,
|
|
|
|
StdoutLogFile: fmt.Sprintf("%s.stdout", req.Task.Name),
|
|
|
|
StderrLogFile: fmt.Sprintf("%s.stderr", req.Task.Name),
|
|
|
|
StdoutFifo: h.config.stdoutFifo,
|
|
|
|
StderrFifo: h.config.stderrFifo,
|
|
|
|
MaxFiles: req.Task.LogConfig.MaxFiles,
|
|
|
|
MaxFileSizeMB: req.Task.LogConfig.MaxFileSizeMB,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
h.logger.Error("failed to start logmon", "error", err)
|
|
|
|
return err
|
2019-01-15 17:31:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 23:37:22 +00:00
|
|
|
func (h *logmonHook) Stop(_ context.Context, req *interfaces.TaskStopRequest, _ *interfaces.TaskStopResponse) error {
|
|
|
|
|
|
|
|
// It's possible that Stop was called without calling Prestart on agent
|
|
|
|
// restarts. Attempt to reattach to an existing logmon.
|
|
|
|
if h.logmon == nil || h.logmonPluginClient == nil {
|
|
|
|
if err := h.reattach(req); err != nil {
|
2019-03-04 21:26:01 +00:00
|
|
|
h.logger.Trace("error reattaching to logmon when stopping", "error", err)
|
2019-02-21 23:37:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
if h.logmon != nil {
|
|
|
|
h.logmon.Stop()
|
|
|
|
}
|
|
|
|
if h.logmonPluginClient != nil {
|
|
|
|
h.logmonPluginClient.Kill()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-21 23:37:22 +00:00
|
|
|
|
|
|
|
// reattach to a running logmon if possible. Will not start a new logmon.
|
|
|
|
func (h *logmonHook) reattach(req *interfaces.TaskStopRequest) error {
|
|
|
|
reattachConfig, err := reattachConfigFromHookData(req.ExistingState)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give up if there's no reattach config
|
|
|
|
if reattachConfig == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.launchLogMon(reattachConfig)
|
|
|
|
}
|