2016-01-05 22:50:25 +00:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
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 (
|
|
|
|
// The path to the alloc directory that is shared across tasks within a task
|
|
|
|
// group.
|
|
|
|
AllocDir = "NOMAD_ALLOC_DIR"
|
|
|
|
|
|
|
|
// The path to the tasks local directory where it can store data that is
|
|
|
|
// persisted to the alloc is removed.
|
|
|
|
TaskLocalDir = "NOMAD_TASK_DIR"
|
|
|
|
|
|
|
|
// The tasks memory limit in MBs.
|
|
|
|
MemLimit = "NOMAD_MEMORY_LIMIT"
|
|
|
|
|
|
|
|
// The tasks limit in MHz.
|
|
|
|
CpuLimit = "NOMAD_CPU_LIMIT"
|
|
|
|
|
|
|
|
// Prefix for passing both dynamic and static port allocations to
|
|
|
|
// tasks.
|
2016-01-24 09:31:03 +00:00
|
|
|
// E.g. $NOMAD_IP_1=127.0.0.1:1 or $NOMAD_IP_http=127.0.0.1:80
|
2016-01-25 19:46:01 +00:00
|
|
|
AddrPrefix = "NOMAD_ADDR_"
|
|
|
|
|
|
|
|
// Prefix for passing the host port when a portmap is specified.
|
|
|
|
HostPortPrefix = "NOMAD_HOST_PORT_"
|
2016-01-05 22:50:25 +00:00
|
|
|
|
|
|
|
// Prefix for passing task meta data.
|
|
|
|
MetaPrefix = "NOMAD_META_"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-02-03 02:54:04 +00:00
|
|
|
Env map[string]string
|
|
|
|
Meta map[string]string
|
|
|
|
AllocDir string
|
|
|
|
TaskDir string
|
|
|
|
CpuLimit int
|
|
|
|
MemLimit int
|
|
|
|
Node *structs.Node
|
|
|
|
Networks []*structs.NetworkResource
|
|
|
|
PortMap map[string]int
|
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-02-03 02:54:04 +00:00
|
|
|
return &TaskEnvironment{Node: node}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReplaceEnv takes an arg and replaces all occurences of environment variables
|
|
|
|
// 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
|
|
|
|
|
|
|
// Build the task metadata
|
2016-02-03 02:54:04 +00:00
|
|
|
for k, v := range t.Meta {
|
|
|
|
t.TaskEnv[fmt.Sprintf("%s%s", MetaPrefix, strings.ToUpper(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 {
|
|
|
|
for label, value := range network.MapLabelToValues(t.PortMap) {
|
2016-01-24 09:31:03 +00:00
|
|
|
IPPort := fmt.Sprintf("%s:%d", network.IP, value)
|
2016-02-03 02:54:04 +00:00
|
|
|
t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort
|
2016-01-25 19:46:01 +00:00
|
|
|
|
|
|
|
// Pass an explicit port mapping to the environment
|
2016-02-03 02:54:04 +00:00
|
|
|
if port, ok := t.PortMap[label]; ok {
|
|
|
|
t.TaskEnv[fmt.Sprintf("%s%s", HostPortPrefix, label)] = strconv.Itoa(port)
|
2016-01-25 19:46:01 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
func (t *TaskEnvironment) SetMeta(m map[string]string) *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.Meta = m
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskEnvironment) ClearMeta() *TaskEnvironment {
|
2016-02-03 02:54:04 +00:00
|
|
|
t.Meta = nil
|
2016-01-05 22:50:25 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
|
}
|