github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/client/allocrunner/taskrunner/task_dir_hook.go (about) 1 package taskrunner 2 3 import ( 4 "context" 5 "strings" 6 7 log "github.com/hashicorp/go-hclog" 8 "github.com/hashicorp/nomad/client/allocdir" 9 "github.com/hashicorp/nomad/client/allocrunner/interfaces" 10 cconfig "github.com/hashicorp/nomad/client/config" 11 "github.com/hashicorp/nomad/client/taskenv" 12 "github.com/hashicorp/nomad/nomad/structs" 13 "github.com/hashicorp/nomad/plugins/drivers" 14 ) 15 16 const ( 17 // TaskDirHookIsDoneDataKey is used to mark whether the hook is done. We 18 // do not use the Done response value because we still need to set the 19 // environment variables every time a task starts. 20 // TODO(0.9.1): Use the resp.Env map and switch to resp.Done. We need to 21 // remove usage of the envBuilder 22 TaskDirHookIsDoneDataKey = "is_done" 23 ) 24 25 type taskDirHook struct { 26 runner *TaskRunner 27 logger log.Logger 28 } 29 30 func newTaskDirHook(runner *TaskRunner, logger log.Logger) *taskDirHook { 31 td := &taskDirHook{ 32 runner: runner, 33 } 34 td.logger = logger.Named(td.Name()) 35 return td 36 } 37 38 func (h *taskDirHook) Name() string { 39 // Copied in client/state when upgrading from <0.9 schemas, so if you 40 // change it here you also must change it there. 41 return "task_dir" 42 } 43 44 func (h *taskDirHook) Prestart(ctx context.Context, req *interfaces.TaskPrestartRequest, resp *interfaces.TaskPrestartResponse) error { 45 fsi := h.runner.driverCapabilities.FSIsolation 46 if v, ok := req.PreviousState[TaskDirHookIsDoneDataKey]; ok && v == "true" { 47 setEnvvars(h.runner.envBuilder, fsi, h.runner.taskDir, h.runner.clientConfig) 48 resp.State = map[string]string{ 49 TaskDirHookIsDoneDataKey: "true", 50 } 51 return nil 52 } 53 54 cc := h.runner.clientConfig 55 chroot := cconfig.DefaultChrootEnv 56 if len(cc.ChrootEnv) > 0 { 57 chroot = cc.ChrootEnv 58 } 59 60 // Emit the event that we are going to be building the task directory 61 h.runner.EmitEvent(structs.NewTaskEvent(structs.TaskSetup).SetMessage(structs.TaskBuildingTaskDir)) 62 63 // Build the task directory structure 64 err := h.runner.taskDir.Build(fsi == drivers.FSIsolationChroot, chroot) 65 if err != nil { 66 return err 67 } 68 69 // Update the environment variables based on the built task directory 70 setEnvvars(h.runner.envBuilder, fsi, h.runner.taskDir, h.runner.clientConfig) 71 resp.State = map[string]string{ 72 TaskDirHookIsDoneDataKey: "true", 73 } 74 return nil 75 } 76 77 // setEnvvars sets path and host env vars depending on the FS isolation used. 78 func setEnvvars(envBuilder *taskenv.Builder, fsi drivers.FSIsolation, taskDir *allocdir.TaskDir, conf *cconfig.Config) { 79 // Set driver-specific environment variables 80 switch fsi { 81 case drivers.FSIsolationNone: 82 // Use host paths 83 envBuilder.SetAllocDir(taskDir.SharedAllocDir) 84 envBuilder.SetTaskLocalDir(taskDir.LocalDir) 85 envBuilder.SetSecretsDir(taskDir.SecretsDir) 86 default: 87 // filesystem isolation; use container paths 88 envBuilder.SetAllocDir(allocdir.SharedAllocContainerPath) 89 envBuilder.SetTaskLocalDir(allocdir.TaskLocalContainerPath) 90 envBuilder.SetSecretsDir(allocdir.TaskSecretsContainerPath) 91 } 92 93 // Set the host environment variables for non-image based drivers 94 if fsi != drivers.FSIsolationImage { 95 filter := strings.Split(conf.ReadDefault("env.blacklist", cconfig.DefaultEnvBlacklist), ",") 96 envBuilder.SetHostEnvvars(filter) 97 } 98 }