2018-07-17 00:19:56 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
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"
|
|
|
|
"github.com/hashicorp/nomad/client/driver"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return "task_dir"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *taskDirHook) Prestart(ctx context.Context, req *interfaces.TaskPrestartRequest, resp *interfaces.TaskPrestartResponse) error {
|
|
|
|
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
|
2018-10-04 19:08:20 +00:00
|
|
|
fsi := h.runner.driverCapabilities.FSIsolation
|
2018-07-17 00:19:56 +00:00
|
|
|
err := h.runner.taskDir.Build(false, chroot, fsi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the environment variables based on the built task directory
|
|
|
|
driver.SetEnvvars(h.runner.envBuilder, fsi, h.runner.taskDir, h.runner.clientConfig)
|
|
|
|
resp.Done = true
|
|
|
|
return nil
|
|
|
|
}
|