Example #1
0
// GetTaskEnv converts the alloc dir, the node, task and alloc into a
// TaskEnvironment.
func GetTaskEnv(allocDir *allocdir.AllocDir, node *structs.Node,
	task *structs.Task, alloc *structs.Allocation) (*env.TaskEnvironment, error) {

	tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)
	env := env.NewTaskEnvironment(node).
		SetTaskMeta(task.Meta).
		SetTaskGroupMeta(tg.Meta).
		SetJobMeta(alloc.Job.Meta).
		SetEnvvars(task.Env).
		SetTaskName(task.Name)

	if allocDir != nil {
		env.SetAllocDir(allocDir.SharedDir)
		taskdir, ok := allocDir.TaskDirs[task.Name]
		if !ok {
			return nil, fmt.Errorf("failed to get task directory for task %q", task.Name)
		}

		env.SetTaskLocalDir(filepath.Join(taskdir, allocdir.TaskLocal))
	}

	if task.Resources != nil {
		env.SetMemLimit(task.Resources.MemoryMB).
			SetCpuLimit(task.Resources.CPU).
			SetNetworks(task.Resources.Networks)
	}

	if alloc != nil {
		env.SetAlloc(alloc)
	}

	return env.Build(), nil
}
Example #2
0
// GetTaskEnv converts the alloc dir, the node and task configuration into a
// TaskEnvironment.
func GetTaskEnv(alloc *allocdir.AllocDir, node *structs.Node, task *structs.Task) (*env.TaskEnvironment, error) {
	env := env.NewTaskEnvironment(node).
		SetMeta(task.Meta).
		SetEnvvars(task.Env)

	if alloc != nil {
		env.SetAllocDir(alloc.SharedDir)
		taskdir, ok := alloc.TaskDirs[task.Name]
		if !ok {
			return nil, fmt.Errorf("failed to get task directory for task %q", task.Name)
		}

		env.SetTaskLocalDir(filepath.Join(taskdir, allocdir.TaskLocal))
	}

	if task.Resources != nil {
		env.SetMemLimit(task.Resources.MemoryMB)
		env.SetCpuLimit(task.Resources.CPU)

		if len(task.Resources.Networks) > 0 {
			network := task.Resources.Networks[0]
			env.SetTaskIp(network.IP)
			env.SetPorts(network.MapLabelToValues(nil))
		}
	}

	return env.Build(), nil
}