2018-07-17 00:19:56 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-28 23:49:37 +00:00
|
|
|
"strings"
|
2018-07-17 00:19:56 +00:00
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-11-28 23:49:37 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2018-10-04 23:22:01 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
2018-07-17 00:19:56 +00:00
|
|
|
cconfig "github.com/hashicorp/nomad/client/config"
|
2018-11-30 11:18:39 +00:00
|
|
|
"github.com/hashicorp/nomad/client/taskenv"
|
2018-07-17 00:19:56 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2019-01-04 21:11:25 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-07-17 00:19:56 +00:00
|
|
|
)
|
|
|
|
|
2019-01-29 21:17:10 +00:00
|
|
|
const (
|
|
|
|
// TaskDirHookIsDoneDataKey is used to mark whether the hook is done. We
|
|
|
|
// do not use the Done response value because we still need to set the
|
|
|
|
// environment variables every time a task starts.
|
|
|
|
// TODO(0.9.1): Use the resp.Env map and switch to resp.Done. We need to
|
|
|
|
// remove usage of the envBuilder
|
|
|
|
TaskDirHookIsDoneDataKey = "is_done"
|
|
|
|
)
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
type taskDirHook struct {
|
|
|
|
runner *TaskRunner
|
|
|
|
logger log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTaskDirHook(runner *TaskRunner, logger log.Logger) *taskDirHook {
|
|
|
|
td := &taskDirHook{
|
|
|
|
runner: runner,
|
|
|
|
}
|
|
|
|
td.logger = logger.Named(td.Name())
|
|
|
|
return td
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *taskDirHook) Name() string {
|
2018-12-07 01:24:43 +00:00
|
|
|
// Copied in client/state when upgrading from <0.9 schemas, so if you
|
|
|
|
// change it here you also must change it there.
|
2018-07-17 00:19:56 +00:00
|
|
|
return "task_dir"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *taskDirHook) Prestart(ctx context.Context, req *interfaces.TaskPrestartRequest, resp *interfaces.TaskPrestartResponse) error {
|
2019-01-29 22:07:55 +00:00
|
|
|
fsi := h.runner.driverCapabilities.FSIsolation
|
2019-02-21 23:37:22 +00:00
|
|
|
if v, ok := req.PreviousState[TaskDirHookIsDoneDataKey]; ok && v == "true" {
|
2019-01-29 21:17:10 +00:00
|
|
|
setEnvvars(h.runner.envBuilder, fsi, h.runner.taskDir, h.runner.clientConfig)
|
2019-02-21 23:37:22 +00:00
|
|
|
resp.State = map[string]string{
|
2019-01-29 21:17:10 +00:00
|
|
|
TaskDirHookIsDoneDataKey: "true",
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
cc := h.runner.clientConfig
|
|
|
|
chroot := cconfig.DefaultChrootEnv
|
|
|
|
if len(cc.ChrootEnv) > 0 {
|
|
|
|
chroot = cc.ChrootEnv
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the event that we are going to be building the task directory
|
2018-07-17 20:48:53 +00:00
|
|
|
h.runner.EmitEvent(structs.NewTaskEvent(structs.TaskSetup).SetMessage(structs.TaskBuildingTaskDir))
|
2018-07-17 00:19:56 +00:00
|
|
|
|
|
|
|
// Build the task directory structure
|
2019-01-04 21:11:25 +00:00
|
|
|
err := h.runner.taskDir.Build(fsi == drivers.FSIsolationChroot, chroot)
|
2018-07-17 00:19:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the environment variables based on the built task directory
|
2018-11-28 23:49:37 +00:00
|
|
|
setEnvvars(h.runner.envBuilder, fsi, h.runner.taskDir, h.runner.clientConfig)
|
2019-02-21 23:37:22 +00:00
|
|
|
resp.State = map[string]string{
|
2019-01-29 21:17:10 +00:00
|
|
|
TaskDirHookIsDoneDataKey: "true",
|
|
|
|
}
|
2018-07-17 00:19:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-28 23:49:37 +00:00
|
|
|
|
|
|
|
// setEnvvars sets path and host env vars depending on the FS isolation used.
|
2019-01-04 21:11:25 +00:00
|
|
|
func setEnvvars(envBuilder *taskenv.Builder, fsi drivers.FSIsolation, taskDir *allocdir.TaskDir, conf *cconfig.Config) {
|
2018-11-28 23:49:37 +00:00
|
|
|
// Set driver-specific environment variables
|
|
|
|
switch fsi {
|
2019-01-04 21:11:25 +00:00
|
|
|
case drivers.FSIsolationNone:
|
2018-11-28 23:49:37 +00:00
|
|
|
// Use host paths
|
|
|
|
envBuilder.SetAllocDir(taskDir.SharedAllocDir)
|
|
|
|
envBuilder.SetTaskLocalDir(taskDir.LocalDir)
|
|
|
|
envBuilder.SetSecretsDir(taskDir.SecretsDir)
|
|
|
|
default:
|
|
|
|
// filesystem isolation; use container paths
|
|
|
|
envBuilder.SetAllocDir(allocdir.SharedAllocContainerPath)
|
|
|
|
envBuilder.SetTaskLocalDir(allocdir.TaskLocalContainerPath)
|
|
|
|
envBuilder.SetSecretsDir(allocdir.TaskSecretsContainerPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the host environment variables for non-image based drivers
|
2019-01-04 21:11:25 +00:00
|
|
|
if fsi != drivers.FSIsolationImage {
|
2020-10-14 22:17:47 +00:00
|
|
|
// COMPAT(1.0) using inclusive language, blacklist is kept for backward compatibility.
|
2020-10-12 12:47:05 +00:00
|
|
|
denylist := conf.ReadAlternativeDefault(
|
|
|
|
[]string{"env.denylist", "env.blacklist"},
|
|
|
|
cconfig.DefaultEnvDenylist,
|
|
|
|
)
|
|
|
|
filter := strings.Split(denylist, ",")
|
2018-11-28 23:49:37 +00:00
|
|
|
envBuilder.SetHostEnvvars(filter)
|
|
|
|
}
|
|
|
|
}
|