2016-01-05 22:50:25 +00:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-28 18:38:54 +00:00
|
|
|
"net"
|
2016-03-23 18:45:03 +00:00
|
|
|
"os"
|
2016-01-05 22:50:25 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2017-03-09 00:44:46 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2016-01-05 22:50:25 +00:00
|
|
|
hargs "github.com/hashicorp/nomad/helper/args"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A set of environment variables that are exported by each driver.
|
|
|
|
const (
|
2016-03-02 00:08:21 +00:00
|
|
|
// AllocDir is the environment variable with the path to the alloc directory
|
|
|
|
// that is shared across tasks within a task group.
|
2016-01-05 22:50:25 +00:00
|
|
|
AllocDir = "NOMAD_ALLOC_DIR"
|
|
|
|
|
2016-03-02 00:08:21 +00:00
|
|
|
// TaskLocalDir is the environment variable with the path to the tasks local
|
|
|
|
// directory where it can store data that is persisted to the alloc is
|
|
|
|
// removed.
|
2016-01-05 22:50:25 +00:00
|
|
|
TaskLocalDir = "NOMAD_TASK_DIR"
|
|
|
|
|
2016-11-01 17:36:59 +00:00
|
|
|
// SecretsDir is the environment variable with the path to the tasks secret
|
2016-09-02 19:44:05 +00:00
|
|
|
// directory where it can store sensitive data.
|
2016-11-01 17:36:59 +00:00
|
|
|
SecretsDir = "NOMAD_SECRETS_DIR"
|
2016-09-02 19:44:05 +00:00
|
|
|
|
2016-03-02 00:08:21 +00:00
|
|
|
// MemLimit is the environment variable with the tasks memory limit in MBs.
|
2016-01-05 22:50:25 +00:00
|
|
|
MemLimit = "NOMAD_MEMORY_LIMIT"
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// CpuLimit is the environment variable with the tasks CPU limit in MHz.
|
2016-01-05 22:50:25 +00:00
|
|
|
CpuLimit = "NOMAD_CPU_LIMIT"
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// AllocID is the environment variable for passing the allocation ID.
|
2016-03-02 00:08:21 +00:00
|
|
|
AllocID = "NOMAD_ALLOC_ID"
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// AllocName is the environment variable for passing the allocation name.
|
2016-03-02 00:08:21 +00:00
|
|
|
AllocName = "NOMAD_ALLOC_NAME"
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// TaskName is the environment variable for passing the task name.
|
2016-03-02 00:08:21 +00:00
|
|
|
TaskName = "NOMAD_TASK_NAME"
|
|
|
|
|
2016-10-11 17:57:12 +00:00
|
|
|
// JobName is the environment variable for passing the job name.
|
|
|
|
JobName = "NOMAD_JOB_NAME"
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// AllocIndex is the environment variable for passing the allocation index.
|
2016-03-10 02:09:51 +00:00
|
|
|
AllocIndex = "NOMAD_ALLOC_INDEX"
|
|
|
|
|
2017-03-09 01:57:38 +00:00
|
|
|
// Datacenter is the environment variable for passing the datacenter in which the alloc is running.
|
|
|
|
Datacenter = "NOMAD_DC"
|
|
|
|
|
|
|
|
// Region is the environment variable for passing the region in which the alloc is running.
|
|
|
|
Region = "NOMAD_REGION"
|
|
|
|
|
2016-03-02 00:08:21 +00:00
|
|
|
// AddrPrefix is the prefix for passing both dynamic and static port
|
|
|
|
// allocations to tasks.
|
2017-03-10 20:17:43 +00:00
|
|
|
// E.g $NOMAD_ADDR_http=127.0.0.1:80
|
2016-01-25 19:46:01 +00:00
|
|
|
AddrPrefix = "NOMAD_ADDR_"
|
|
|
|
|
2016-04-15 17:27:51 +00:00
|
|
|
// IpPrefix is the prefix for passing the IP of a port allocation to a task.
|
|
|
|
IpPrefix = "NOMAD_IP_"
|
|
|
|
|
|
|
|
// PortPrefix is the prefix for passing the port allocation to a task.
|
|
|
|
PortPrefix = "NOMAD_PORT_"
|
|
|
|
|
2016-03-02 00:08:21 +00:00
|
|
|
// HostPortPrefix is the prefix for passing the host port when a portmap is
|
|
|
|
// specified.
|
2016-01-25 19:46:01 +00:00
|
|
|
HostPortPrefix = "NOMAD_HOST_PORT_"
|
2016-01-05 22:50:25 +00:00
|
|
|
|
2016-03-02 00:08:21 +00:00
|
|
|
// MetaPrefix is the prefix for passing task meta data.
|
2016-01-05 22:50:25 +00:00
|
|
|
MetaPrefix = "NOMAD_META_"
|
2016-09-14 20:30:01 +00:00
|
|
|
|
|
|
|
// VaultToken is the environment variable for passing the Vault token
|
|
|
|
VaultToken = "VAULT_TOKEN"
|
2016-01-05 22:50:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// The node values that can be interpreted.
|
|
|
|
const (
|
2016-01-27 01:38:36 +00:00
|
|
|
nodeIdKey = "node.unique.id"
|
2016-01-05 22:50:25 +00:00
|
|
|
nodeDcKey = "node.datacenter"
|
2016-01-27 01:38:36 +00:00
|
|
|
nodeNameKey = "node.unique.name"
|
2016-01-05 22:50:25 +00:00
|
|
|
nodeClassKey = "node.class"
|
|
|
|
|
|
|
|
// Prefixes used for lookups.
|
|
|
|
nodeAttributePrefix = "attr."
|
|
|
|
nodeMetaPrefix = "meta."
|
|
|
|
)
|
|
|
|
|
|
|
|
// TaskEnvironment is used to expose information to a task via environment
|
|
|
|
// variables and provide interpolation of Nomad variables.
|
|
|
|
type TaskEnvironment struct {
|
2016-09-14 20:30:01 +00:00
|
|
|
Env map[string]string
|
|
|
|
TaskMeta map[string]string
|
|
|
|
AllocDir string
|
|
|
|
TaskDir string
|
2016-11-01 17:36:59 +00:00
|
|
|
SecretsDir string
|
2016-09-14 20:30:01 +00:00
|
|
|
CpuLimit int
|
|
|
|
MemLimit int
|
|
|
|
TaskName string
|
|
|
|
AllocIndex int
|
2017-03-09 01:57:38 +00:00
|
|
|
Datacenter string
|
|
|
|
Region string
|
2016-09-14 20:30:01 +00:00
|
|
|
AllocId string
|
|
|
|
AllocName string
|
|
|
|
Node *structs.Node
|
|
|
|
Networks []*structs.NetworkResource
|
|
|
|
PortMap map[string]int
|
|
|
|
VaultToken string
|
|
|
|
InjectVaultToken bool
|
2016-10-11 17:57:12 +00:00
|
|
|
JobName string
|
2017-01-20 21:43:22 +00:00
|
|
|
Alloc *structs.Allocation
|
2016-01-05 22:50:25 +00:00
|
|
|
|
|
|
|
// taskEnv is the variables that will be set in the tasks environment
|
2016-02-03 02:54:04 +00:00
|
|
|
TaskEnv map[string]string
|
2016-01-05 22:50:25 +00:00
|
|
|
|
|
|
|
// nodeValues is the values that are allowed for interprolation from the
|
|
|
|
// node.
|
2016-02-03 02:54:04 +00:00
|
|
|
NodeValues map[string]string
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTaskEnvironment(node *structs.Node) *TaskEnvironment {
|
2016-03-10 02:09:51 +00:00
|
|
|
return &TaskEnvironment{Node: node, AllocIndex: -1}
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseAndReplace takes the user supplied args replaces any instance of an
|
|
|
|
// environment variable or nomad variable in the args with the actual value.
|
|
|
|
func (t *TaskEnvironment) ParseAndReplace(args []string) []string {
|
|
|
|
replaced := make([]string, len(args))
|
|
|
|
for i, arg := range args {
|
2016-02-03 02:54:04 +00:00
|
|
|
replaced[i] = hargs.ReplaceEnv(arg, t.TaskEnv, t.NodeValues)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return replaced
|
|
|
|
}
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// ReplaceEnv takes an arg and replaces all occurrences of environment variables
|
2016-01-05 22:50:25 +00:00
|
|
|
// and nomad variables. If the variable is found in the passed map it is
|
|
|
|
// replaced, otherwise the original string is returned.
|
|
|
|
func (t *TaskEnvironment) ReplaceEnv(arg string) string {
|
2016-02-03 02:54:04 +00:00
|
|
|
return hargs.ReplaceEnv(arg, t.TaskEnv, t.NodeValues)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build must be called after all the tasks environment values have been set.
|
|
|
|
func (t *TaskEnvironment) Build() *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.NodeValues = make(map[string]string)
|
|
|
|
t.TaskEnv = make(map[string]string)
|
2016-01-05 22:50:25 +00:00
|
|
|
|
2016-12-16 18:45:09 +00:00
|
|
|
// Build the meta
|
|
|
|
for k, v := range t.TaskMeta {
|
|
|
|
t.TaskEnv[fmt.Sprintf("%s%s", MetaPrefix, strings.ToUpper(k))] = v
|
2017-02-21 03:51:24 +00:00
|
|
|
t.TaskEnv[fmt.Sprintf("%s%s", MetaPrefix, k)] = v
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the ports
|
2016-02-03 02:54:04 +00:00
|
|
|
for _, network := range t.Networks {
|
2016-07-08 22:37:44 +00:00
|
|
|
for label, value := range network.MapLabelToValues(nil) {
|
2016-04-15 17:27:51 +00:00
|
|
|
t.TaskEnv[fmt.Sprintf("%s%s", IpPrefix, label)] = network.IP
|
2016-07-08 22:37:44 +00:00
|
|
|
t.TaskEnv[fmt.Sprintf("%s%s", HostPortPrefix, label)] = strconv.Itoa(value)
|
2016-07-09 00:42:34 +00:00
|
|
|
if forwardedPort, ok := t.PortMap[label]; ok {
|
|
|
|
value = forwardedPort
|
2016-01-25 19:46:01 +00:00
|
|
|
}
|
2016-11-28 18:38:54 +00:00
|
|
|
t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = strconv.Itoa(value)
|
|
|
|
IPPort := net.JoinHostPort(network.IP, strconv.Itoa(value))
|
2016-07-09 00:42:34 +00:00
|
|
|
t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort
|
|
|
|
|
2016-01-24 09:31:03 +00:00
|
|
|
}
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the directories
|
2016-02-03 02:54:04 +00:00
|
|
|
if t.AllocDir != "" {
|
|
|
|
t.TaskEnv[AllocDir] = t.AllocDir
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
2016-02-03 02:54:04 +00:00
|
|
|
if t.TaskDir != "" {
|
|
|
|
t.TaskEnv[TaskLocalDir] = t.TaskDir
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
2016-11-01 17:36:59 +00:00
|
|
|
if t.SecretsDir != "" {
|
|
|
|
t.TaskEnv[SecretsDir] = t.SecretsDir
|
2016-09-02 19:44:05 +00:00
|
|
|
}
|
2016-01-05 22:50:25 +00:00
|
|
|
|
|
|
|
// Build the resource limits
|
2016-02-03 02:54:04 +00:00
|
|
|
if t.MemLimit != 0 {
|
|
|
|
t.TaskEnv[MemLimit] = strconv.Itoa(t.MemLimit)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
2016-02-03 02:54:04 +00:00
|
|
|
if t.CpuLimit != 0 {
|
|
|
|
t.TaskEnv[CpuLimit] = strconv.Itoa(t.CpuLimit)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 00:08:21 +00:00
|
|
|
// Build the tasks ids
|
|
|
|
if t.AllocId != "" {
|
|
|
|
t.TaskEnv[AllocID] = t.AllocId
|
|
|
|
}
|
|
|
|
if t.AllocName != "" {
|
|
|
|
t.TaskEnv[AllocName] = t.AllocName
|
|
|
|
}
|
2016-03-10 02:09:51 +00:00
|
|
|
if t.AllocIndex != -1 {
|
|
|
|
t.TaskEnv[AllocIndex] = strconv.Itoa(t.AllocIndex)
|
|
|
|
}
|
2016-03-02 00:08:21 +00:00
|
|
|
if t.TaskName != "" {
|
|
|
|
t.TaskEnv[TaskName] = t.TaskName
|
|
|
|
}
|
2016-10-11 17:57:12 +00:00
|
|
|
if t.JobName != "" {
|
|
|
|
t.TaskEnv[JobName] = t.JobName
|
|
|
|
}
|
2017-03-29 22:21:21 +00:00
|
|
|
if t.Datacenter != "" {
|
|
|
|
t.TaskEnv[Datacenter] = t.Datacenter
|
|
|
|
}
|
|
|
|
if t.Region != "" {
|
|
|
|
t.TaskEnv[Region] = t.Region
|
|
|
|
}
|
2016-03-02 00:08:21 +00:00
|
|
|
|
2017-01-20 21:43:22 +00:00
|
|
|
// Build the addr of the other tasks
|
|
|
|
if t.Alloc != nil {
|
|
|
|
for taskName, resources := range t.Alloc.TaskResources {
|
|
|
|
if taskName == t.TaskName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, nw := range resources.Networks {
|
2017-03-28 17:16:27 +00:00
|
|
|
ports := make([]structs.Port, 0, len(nw.ReservedPorts)+len(nw.DynamicPorts))
|
2017-01-20 21:43:22 +00:00
|
|
|
for _, port := range nw.ReservedPorts {
|
2017-03-28 17:16:27 +00:00
|
|
|
ports = append(ports, port)
|
2017-01-20 21:43:22 +00:00
|
|
|
}
|
|
|
|
for _, port := range nw.DynamicPorts {
|
2017-03-28 17:16:27 +00:00
|
|
|
ports = append(ports, port)
|
2017-01-20 21:43:22 +00:00
|
|
|
}
|
|
|
|
for _, p := range ports {
|
|
|
|
key := fmt.Sprintf("%s%s_%s", AddrPrefix, taskName, p.Label)
|
|
|
|
t.TaskEnv[key] = fmt.Sprintf("%s:%d", nw.IP, p.Value)
|
2017-03-10 20:17:43 +00:00
|
|
|
key = fmt.Sprintf("%s%s_%s", IpPrefix, taskName, p.Label)
|
|
|
|
t.TaskEnv[key] = nw.IP
|
|
|
|
key = fmt.Sprintf("%s%s_%s", PortPrefix, taskName, p.Label)
|
|
|
|
t.TaskEnv[key] = strconv.Itoa(p.Value)
|
2017-01-20 21:43:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 22:50:25 +00:00
|
|
|
// Build the node
|
2016-02-03 02:54:04 +00:00
|
|
|
if t.Node != nil {
|
2016-01-05 22:50:25 +00:00
|
|
|
// Set up the node values.
|
2016-02-03 02:54:04 +00:00
|
|
|
t.NodeValues[nodeIdKey] = t.Node.ID
|
|
|
|
t.NodeValues[nodeDcKey] = t.Node.Datacenter
|
|
|
|
t.NodeValues[nodeNameKey] = t.Node.Name
|
|
|
|
t.NodeValues[nodeClassKey] = t.Node.NodeClass
|
2016-01-05 22:50:25 +00:00
|
|
|
|
|
|
|
// Set up the attributes.
|
2016-02-03 02:54:04 +00:00
|
|
|
for k, v := range t.Node.Attributes {
|
|
|
|
t.NodeValues[fmt.Sprintf("%s%s", nodeAttributePrefix, k)] = v
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the meta.
|
2016-02-03 02:54:04 +00:00
|
|
|
for k, v := range t.Node.Meta {
|
|
|
|
t.NodeValues[fmt.Sprintf("%s%s", nodeMetaPrefix, k)] = v
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 20:30:01 +00:00
|
|
|
// Build the Vault Token
|
|
|
|
if t.InjectVaultToken && t.VaultToken != "" {
|
|
|
|
t.TaskEnv[VaultToken] = t.VaultToken
|
|
|
|
}
|
|
|
|
|
2016-01-05 22:50:25 +00:00
|
|
|
// Interpret the environment variables
|
2016-02-03 02:54:04 +00:00
|
|
|
interpreted := make(map[string]string, len(t.Env))
|
|
|
|
for k, v := range t.Env {
|
|
|
|
interpreted[k] = hargs.ReplaceEnv(v, t.NodeValues, t.TaskEnv)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range interpreted {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.TaskEnv[k] = v
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 01:09:44 +00:00
|
|
|
// Clean keys (see #2405)
|
|
|
|
cleanedEnv := make(map[string]string, len(t.TaskEnv))
|
|
|
|
for k, v := range t.TaskEnv {
|
2017-03-09 00:44:46 +00:00
|
|
|
cleanedK := helper.CleanEnvVar(k, '_')
|
2017-03-07 01:09:44 +00:00
|
|
|
cleanedEnv[cleanedK] = v
|
|
|
|
}
|
|
|
|
t.TaskEnv = cleanedEnv
|
|
|
|
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnvList returns a list of strings with NAME=value pairs.
|
|
|
|
func (t *TaskEnvironment) EnvList() []string {
|
|
|
|
env := []string{}
|
2016-02-03 02:54:04 +00:00
|
|
|
for k, v := range t.TaskEnv {
|
2016-01-05 22:50:25 +00:00
|
|
|
env = append(env, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnvMap returns a copy of the tasks environment variables.
|
|
|
|
func (t *TaskEnvironment) EnvMap() map[string]string {
|
2016-02-03 02:54:04 +00:00
|
|
|
m := make(map[string]string, len(t.TaskEnv))
|
|
|
|
for k, v := range t.TaskEnv {
|
2016-01-05 22:50:25 +00:00
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2017-03-28 18:10:11 +00:00
|
|
|
// EnvMapAll returns the environment variables that will be set as well as node
|
2017-03-27 21:58:04 +00:00
|
|
|
// meta/attrs in the map. This is appropriate for interpolation.
|
|
|
|
func (t *TaskEnvironment) EnvMapAll() map[string]string {
|
|
|
|
m := make(map[string]string, len(t.TaskEnv))
|
|
|
|
for k, v := range t.TaskEnv {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range t.NodeValues {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2016-01-05 22:50:25 +00:00
|
|
|
// Builder methods to build the TaskEnvironment
|
|
|
|
func (t *TaskEnvironment) SetAllocDir(dir string) *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.AllocDir = dir
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) ClearAllocDir() *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.AllocDir = ""
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) SetTaskLocalDir(dir string) *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.TaskDir = dir
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) ClearTaskLocalDir() *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.TaskDir = ""
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-11-01 17:36:59 +00:00
|
|
|
func (t *TaskEnvironment) SetSecretsDir(dir string) *TaskEnvironment {
|
|
|
|
t.SecretsDir = dir
|
2016-09-02 19:44:05 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-11-01 17:36:59 +00:00
|
|
|
func (t *TaskEnvironment) ClearSecretsDir() *TaskEnvironment {
|
|
|
|
t.SecretsDir = ""
|
2016-09-02 19:44:05 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-01-05 22:50:25 +00:00
|
|
|
func (t *TaskEnvironment) SetMemLimit(limit int) *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.MemLimit = limit
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) ClearMemLimit() *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.MemLimit = 0
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) SetCpuLimit(limit int) *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.CpuLimit = limit
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) ClearCpuLimit() *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.CpuLimit = 0
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-01-24 09:31:03 +00:00
|
|
|
func (t *TaskEnvironment) SetNetworks(networks []*structs.NetworkResource) *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.Networks = networks
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-01-24 09:31:03 +00:00
|
|
|
func (t *TaskEnvironment) clearNetworks() *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.Networks = nil
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-01-24 09:31:03 +00:00
|
|
|
func (t *TaskEnvironment) SetPortMap(portMap map[string]int) *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.PortMap = portMap
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-01-24 09:31:03 +00:00
|
|
|
func (t *TaskEnvironment) clearPortMap() *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.PortMap = nil
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes a map of meta values to be passed to the task. The keys are capatilized
|
|
|
|
// when the environent variable is set.
|
2016-03-25 00:39:09 +00:00
|
|
|
func (t *TaskEnvironment) SetTaskMeta(m map[string]string) *TaskEnvironment {
|
|
|
|
t.TaskMeta = m
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) ClearTaskMeta() *TaskEnvironment {
|
|
|
|
t.TaskMeta = nil
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-01-05 22:50:25 +00:00
|
|
|
func (t *TaskEnvironment) SetEnvvars(m map[string]string) *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.Env = m
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-01-20 20:00:20 +00:00
|
|
|
// Appends the given environment variables.
|
|
|
|
func (t *TaskEnvironment) AppendEnvvars(m map[string]string) *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
if t.Env == nil {
|
|
|
|
t.Env = make(map[string]string, len(m))
|
2016-01-20 20:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range m {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.Env[k] = v
|
2016-01-20 20:00:20 +00:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-03-23 21:07:12 +00:00
|
|
|
// AppendHostEnvvars adds the host environment variables to the tasks. The
|
|
|
|
// filter parameter can be use to filter host environment from entering the
|
|
|
|
// tasks.
|
2016-03-23 18:45:03 +00:00
|
|
|
func (t *TaskEnvironment) AppendHostEnvvars(filter []string) *TaskEnvironment {
|
|
|
|
hostEnv := os.Environ()
|
|
|
|
if t.Env == nil {
|
|
|
|
t.Env = make(map[string]string, len(hostEnv))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index the filtered environment variables.
|
|
|
|
index := make(map[string]struct{}, len(filter))
|
|
|
|
for _, f := range filter {
|
|
|
|
index[f] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range hostEnv {
|
2016-04-22 00:08:47 +00:00
|
|
|
parts := strings.SplitN(e, "=", 2)
|
2016-03-23 18:45:03 +00:00
|
|
|
key, value := parts[0], parts[1]
|
2016-03-23 21:07:12 +00:00
|
|
|
|
|
|
|
// Skip filtered environment variables
|
|
|
|
if _, filtered := index[key]; filtered {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't override the tasks environment variables.
|
|
|
|
if _, existing := t.Env[key]; !existing {
|
2016-03-23 18:45:03 +00:00
|
|
|
t.Env[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-01-05 22:50:25 +00:00
|
|
|
func (t *TaskEnvironment) ClearEnvvars() *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.Env = nil
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
2016-03-02 00:08:21 +00:00
|
|
|
|
2016-03-10 02:09:51 +00:00
|
|
|
// Helper method for setting all fields from an allocation.
|
|
|
|
func (t *TaskEnvironment) SetAlloc(alloc *structs.Allocation) *TaskEnvironment {
|
|
|
|
t.AllocId = alloc.ID
|
|
|
|
t.AllocName = alloc.Name
|
|
|
|
t.AllocIndex = alloc.Index()
|
2017-01-20 21:43:22 +00:00
|
|
|
t.Alloc = alloc
|
2016-03-10 02:09:51 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method for clearing all fields from an allocation.
|
|
|
|
func (t *TaskEnvironment) ClearAlloc(alloc *structs.Allocation) *TaskEnvironment {
|
|
|
|
return t.ClearAllocId().ClearAllocName().ClearAllocIndex()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) SetAllocIndex(index int) *TaskEnvironment {
|
|
|
|
t.AllocIndex = index
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) ClearAllocIndex() *TaskEnvironment {
|
|
|
|
t.AllocIndex = -1
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-03-02 00:08:21 +00:00
|
|
|
func (t *TaskEnvironment) SetAllocId(id string) *TaskEnvironment {
|
|
|
|
t.AllocId = id
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) ClearAllocId() *TaskEnvironment {
|
|
|
|
t.AllocId = ""
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) SetAllocName(name string) *TaskEnvironment {
|
|
|
|
t.AllocName = name
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) ClearAllocName() *TaskEnvironment {
|
|
|
|
t.AllocName = ""
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) SetTaskName(name string) *TaskEnvironment {
|
|
|
|
t.TaskName = name
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2017-03-29 22:21:21 +00:00
|
|
|
func (t *TaskEnvironment) ClearTaskName() *TaskEnvironment {
|
|
|
|
t.TaskName = ""
|
2016-10-11 17:57:12 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2017-03-29 22:21:21 +00:00
|
|
|
func (t *TaskEnvironment) SetJobName(name string) *TaskEnvironment {
|
|
|
|
t.JobName = name
|
2016-03-02 00:08:21 +00:00
|
|
|
return t
|
|
|
|
}
|
2016-09-14 20:30:01 +00:00
|
|
|
|
2016-10-11 17:57:12 +00:00
|
|
|
func (t *TaskEnvironment) ClearJobName() *TaskEnvironment {
|
|
|
|
t.JobName = ""
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-09-14 20:30:01 +00:00
|
|
|
func (t *TaskEnvironment) SetVaultToken(token string, inject bool) *TaskEnvironment {
|
|
|
|
t.VaultToken = token
|
|
|
|
t.InjectVaultToken = inject
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) ClearVaultToken() *TaskEnvironment {
|
|
|
|
t.VaultToken = ""
|
|
|
|
t.InjectVaultToken = false
|
|
|
|
return t
|
|
|
|
}
|