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-05-17 00:33:50 +00:00
|
|
|
"sync"
|
2016-01-05 22:50:25 +00:00
|
|
|
|
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 (
|
2017-03-29 23:31:23 +00:00
|
|
|
nodeIdKey = "node.unique.id"
|
|
|
|
nodeDcKey = "node.datacenter"
|
|
|
|
nodeRegionKey = "node.region"
|
|
|
|
nodeNameKey = "node.unique.name"
|
|
|
|
nodeClassKey = "node.class"
|
2016-01-05 22:50:25 +00:00
|
|
|
|
|
|
|
// Prefixes used for lookups.
|
|
|
|
nodeAttributePrefix = "attr."
|
|
|
|
nodeMetaPrefix = "meta."
|
|
|
|
)
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// TaskEnv is a task's environment as well as node attribute's for
|
|
|
|
// interpolation.
|
|
|
|
type TaskEnv struct {
|
|
|
|
// NodeAttrs is the map of node attributes for interpolation
|
|
|
|
NodeAttrs map[string]string
|
|
|
|
|
|
|
|
// EnvMap is the map of environment variables
|
|
|
|
EnvMap map[string]string
|
|
|
|
|
|
|
|
// envList is a memoized list created by List()
|
|
|
|
envList []string
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// NewTaskEnv creates a new task environment with the given environment and
|
|
|
|
// node attribute maps.
|
|
|
|
func NewTaskEnv(env, node map[string]string) *TaskEnv {
|
|
|
|
return &TaskEnv{
|
|
|
|
NodeAttrs: node,
|
|
|
|
EnvMap: env,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// List returns the task's environment as a slice of NAME=value pair strings.
|
|
|
|
func (t *TaskEnv) List() []string {
|
|
|
|
if t.envList != nil {
|
|
|
|
return t.envList
|
|
|
|
}
|
|
|
|
|
|
|
|
env := []string{}
|
|
|
|
for k, v := range t.EnvMap {
|
|
|
|
env = append(env, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map of the task's environment variables.
|
|
|
|
func (t *TaskEnv) Map() map[string]string {
|
|
|
|
m := make(map[string]string, len(t.EnvMap))
|
|
|
|
for k, v := range t.EnvMap {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// All of the task's environment variables and the node's attributes in a
|
|
|
|
// single map.
|
|
|
|
func (t *TaskEnv) All() map[string]string {
|
|
|
|
m := make(map[string]string, len(t.EnvMap)+len(t.NodeAttrs))
|
|
|
|
for k, v := range t.EnvMap {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range t.NodeAttrs {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseAndReplace takes the user supplied args replaces any instance of an
|
2017-05-17 00:33:50 +00:00
|
|
|
// environment variable or Nomad variable in the args with the actual value.
|
|
|
|
func (t *TaskEnv) ParseAndReplace(args []string) []string {
|
2016-01-05 22:50:25 +00:00
|
|
|
replaced := make([]string, len(args))
|
|
|
|
for i, arg := range args {
|
2017-05-17 00:33:50 +00:00
|
|
|
replaced[i] = hargs.ReplaceEnv(arg, t.EnvMap, t.NodeAttrs)
|
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
|
2017-05-17 00:33:50 +00:00
|
|
|
// and Nomad variables. If the variable is found in the passed map it is
|
2016-01-05 22:50:25 +00:00
|
|
|
// replaced, otherwise the original string is returned.
|
2017-05-17 00:33:50 +00:00
|
|
|
func (t *TaskEnv) ReplaceEnv(arg string) string {
|
|
|
|
return hargs.ReplaceEnv(arg, t.EnvMap, t.NodeAttrs)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Builder is used to build task environment's and is safe for concurrent use.
|
|
|
|
type Builder struct {
|
|
|
|
// envvars are custom set environment variables
|
|
|
|
envvars map[string]string
|
2016-07-09 00:42:34 +00:00
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// templateEnv are env vars set from templates
|
|
|
|
templateEnv map[string]string
|
|
|
|
|
|
|
|
// hostEnv are environment variables filtered from the host
|
|
|
|
hostEnv map[string]string
|
|
|
|
|
|
|
|
// nodeAttrs are Node attributes and metadata
|
|
|
|
nodeAttrs map[string]string
|
|
|
|
|
|
|
|
// taskMeta are the meta attributes on the task
|
|
|
|
taskMeta map[string]string
|
|
|
|
|
|
|
|
// allocDir from task's perspective; eg /alloc
|
|
|
|
allocDir string
|
|
|
|
|
|
|
|
// localDir from task's perspective; eg /local
|
|
|
|
localDir string
|
|
|
|
|
|
|
|
// secrestsDir from task's perspective; eg /secrets
|
|
|
|
secretsDir string
|
|
|
|
|
|
|
|
cpuLimit int
|
|
|
|
memLimit int
|
|
|
|
taskName string
|
|
|
|
allocIndex int
|
|
|
|
datacenter string
|
|
|
|
region string
|
|
|
|
allocId string
|
|
|
|
allocName string
|
|
|
|
portMap map[string]string
|
|
|
|
vaultToken string
|
|
|
|
injectVaultToken bool
|
|
|
|
jobName string
|
|
|
|
|
|
|
|
// otherPorts for tasks in the same alloc
|
|
|
|
otherPorts map[string]string
|
|
|
|
|
2017-05-18 18:34:40 +00:00
|
|
|
// network resources from the task; must be lazily turned into env vars
|
|
|
|
// because portMaps can change after builder creation and affect
|
|
|
|
// network env vars.
|
|
|
|
networks []*structs.NetworkResource
|
2017-05-17 00:33:50 +00:00
|
|
|
|
|
|
|
mu *sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBuilder creates a new task environment builder.
|
2017-05-18 18:34:40 +00:00
|
|
|
func NewBuilder(node *structs.Node, alloc *structs.Allocation, task *structs.Task, region string) *Builder {
|
2017-05-17 00:33:50 +00:00
|
|
|
b := &Builder{
|
2017-05-18 18:34:40 +00:00
|
|
|
region: region,
|
2017-05-17 00:33:50 +00:00
|
|
|
envvars: make(map[string]string),
|
|
|
|
nodeAttrs: make(map[string]string),
|
|
|
|
mu: &sync.RWMutex{},
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
2017-05-18 18:34:40 +00:00
|
|
|
return b.setTask(task).setAlloc(alloc).setNode(node)
|
2017-05-17 00:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build must be called after all the tasks environment values have been set.
|
|
|
|
func (b *Builder) Build() *TaskEnv {
|
|
|
|
nodeAttrs := make(map[string]string)
|
|
|
|
envMap := make(map[string]string)
|
|
|
|
|
|
|
|
b.mu.RLock()
|
|
|
|
defer b.mu.RUnlock()
|
2016-01-05 22:50:25 +00:00
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Add the directories
|
|
|
|
if b.allocDir != "" {
|
|
|
|
envMap[AllocDir] = b.allocDir
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.localDir != "" {
|
|
|
|
envMap[TaskLocalDir] = b.localDir
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.secretsDir != "" {
|
|
|
|
envMap[SecretsDir] = b.secretsDir
|
2016-09-02 19:44:05 +00:00
|
|
|
}
|
2016-01-05 22:50:25 +00:00
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Add the resource limits
|
|
|
|
if b.memLimit != 0 {
|
|
|
|
envMap[MemLimit] = strconv.Itoa(b.memLimit)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.cpuLimit != 0 {
|
|
|
|
envMap[CpuLimit] = strconv.Itoa(b.cpuLimit)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Add the task metadata
|
|
|
|
if b.allocId != "" {
|
|
|
|
envMap[AllocID] = b.allocId
|
2016-03-02 00:08:21 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.allocName != "" {
|
|
|
|
envMap[AllocName] = b.allocName
|
2016-03-02 00:08:21 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.allocIndex != -1 {
|
|
|
|
envMap[AllocIndex] = strconv.Itoa(b.allocIndex)
|
2016-03-10 02:09:51 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.taskName != "" {
|
|
|
|
envMap[TaskName] = b.taskName
|
2016-03-02 00:08:21 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.jobName != "" {
|
|
|
|
envMap[JobName] = b.jobName
|
2016-10-11 17:57:12 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.datacenter != "" {
|
|
|
|
envMap[Datacenter] = b.datacenter
|
2017-03-29 22:21:21 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
if b.region != "" {
|
|
|
|
envMap[Region] = b.region
|
|
|
|
|
|
|
|
// Copy region over to node attrs
|
|
|
|
nodeAttrs[nodeRegionKey] = b.region
|
2017-03-29 22:21:21 +00:00
|
|
|
}
|
2016-03-02 00:08:21 +00:00
|
|
|
|
2017-05-18 18:34:40 +00:00
|
|
|
// Build the addrs for this task
|
|
|
|
for _, network := range b.networks {
|
|
|
|
for label, intVal := range network.MapLabelToValues(nil) {
|
|
|
|
value := strconv.Itoa(intVal)
|
|
|
|
envMap[fmt.Sprintf("%s%s", IpPrefix, label)] = network.IP
|
|
|
|
envMap[fmt.Sprintf("%s%s", HostPortPrefix, label)] = value
|
|
|
|
if forwardedPort, ok := b.portMap[label]; ok {
|
|
|
|
value = forwardedPort
|
|
|
|
}
|
|
|
|
envMap[fmt.Sprintf("%s%s", PortPrefix, label)] = value
|
|
|
|
ipPort := net.JoinHostPort(network.IP, value)
|
|
|
|
envMap[fmt.Sprintf("%s%s", AddrPrefix, label)] = ipPort
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 21:43:22 +00:00
|
|
|
// Build the addr of the other tasks
|
2017-05-17 00:33:50 +00:00
|
|
|
for k, v := range b.otherPorts {
|
|
|
|
envMap[k] = v
|
2017-01-20 21:43:22 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Build the Vault Token
|
|
|
|
if b.injectVaultToken && b.vaultToken != "" {
|
|
|
|
envMap[VaultToken] = b.vaultToken
|
|
|
|
}
|
2016-01-05 22:50:25 +00:00
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Copy node attributes
|
|
|
|
for k, v := range b.nodeAttrs {
|
|
|
|
nodeAttrs[k] = v
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Interpolate and add environment variables
|
|
|
|
for k, v := range b.hostEnv {
|
|
|
|
envMap[k] = hargs.ReplaceEnv(v, nodeAttrs, envMap)
|
2016-09-14 20:30:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Copy task env vars second as they override host env vars
|
|
|
|
for k, v := range b.envvars {
|
|
|
|
envMap[k] = v
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Copy template env vars third as they override task env vars
|
|
|
|
for k, v := range b.templateEnv {
|
|
|
|
envMap[k] = v
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 01:09:44 +00:00
|
|
|
// Clean keys (see #2405)
|
2017-05-17 00:33:50 +00:00
|
|
|
cleanedEnv := make(map[string]string, len(envMap))
|
|
|
|
for k, v := range envMap {
|
2017-03-09 00:44:46 +00:00
|
|
|
cleanedK := helper.CleanEnvVar(k, '_')
|
2017-03-07 01:09:44 +00:00
|
|
|
cleanedEnv[cleanedK] = v
|
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
return NewTaskEnv(cleanedEnv, nodeAttrs)
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 18:34:40 +00:00
|
|
|
// setTask is called from NewBuilder to populate task related environment
|
|
|
|
// variables.
|
|
|
|
func (b *Builder) setTask(task *structs.Task) *Builder {
|
|
|
|
b.taskName = task.Name
|
|
|
|
for k, v := range task.Env {
|
|
|
|
b.envvars[k] = v
|
|
|
|
}
|
|
|
|
if task.Resources != nil {
|
|
|
|
b.memLimit = task.Resources.MemoryMB
|
|
|
|
b.cpuLimit = task.Resources.CPU
|
|
|
|
// Copy networks to prevent sharing
|
|
|
|
b.networks = make([]*structs.NetworkResource, len(task.Resources.Networks))
|
|
|
|
for i, n := range task.Resources.Networks {
|
|
|
|
b.networks[i] = n.Copy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// setAlloc is called from NewBuilder to populate alloc related environment
|
|
|
|
// variables.
|
|
|
|
func (b *Builder) setAlloc(alloc *structs.Allocation) *Builder {
|
|
|
|
b.allocId = alloc.ID
|
|
|
|
b.allocName = alloc.Name
|
|
|
|
b.allocIndex = alloc.Index()
|
|
|
|
b.jobName = alloc.Job.Name
|
|
|
|
|
|
|
|
// Set meta
|
|
|
|
combined := alloc.Job.CombinedTaskMeta(alloc.TaskGroup, b.taskName)
|
|
|
|
b.taskMeta = make(map[string]string, len(combined)*2)
|
|
|
|
for k, v := range combined {
|
|
|
|
b.taskMeta[fmt.Sprintf("%s%s", MetaPrefix, strings.ToUpper(k))] = v
|
|
|
|
b.taskMeta[fmt.Sprintf("%s%s", MetaPrefix, k)] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add ports from other tasks
|
|
|
|
for taskName, resources := range alloc.TaskResources {
|
|
|
|
if taskName == b.taskName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, nw := range resources.Networks {
|
|
|
|
for _, p := range nw.ReservedPorts {
|
|
|
|
addPort(b.otherPorts, b.taskName, nw.IP, p.Label, p.Value)
|
|
|
|
}
|
|
|
|
for _, p := range nw.DynamicPorts {
|
|
|
|
addPort(b.otherPorts, b.taskName, nw.IP, p.Label, p.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// setNode is called from NewBuilder to populate node attributes.
|
|
|
|
func (b *Builder) setNode(n *structs.Node) *Builder {
|
|
|
|
b.nodeAttrs[nodeIdKey] = n.ID
|
|
|
|
b.nodeAttrs[nodeDcKey] = n.Datacenter
|
|
|
|
b.nodeAttrs[nodeNameKey] = n.Name
|
|
|
|
b.nodeAttrs[nodeClassKey] = n.NodeClass
|
2016-01-05 22:50:25 +00:00
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Set up the attributes.
|
|
|
|
for k, v := range n.Attributes {
|
|
|
|
b.nodeAttrs[fmt.Sprintf("%s%s", nodeAttributePrefix, k)] = v
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// Set up the meta.
|
|
|
|
for k, v := range n.Meta {
|
|
|
|
b.nodeAttrs[fmt.Sprintf("%s%s", nodeMetaPrefix, k)] = v
|
2017-03-27 21:58:04 +00:00
|
|
|
}
|
2017-05-17 00:33:50 +00:00
|
|
|
return b
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
func (b *Builder) SetAllocDir(dir string) *Builder {
|
|
|
|
b.mu.Lock()
|
|
|
|
b.allocDir = dir
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2016-09-02 19:44:05 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
func (b *Builder) SetTaskLocalDir(dir string) *Builder {
|
|
|
|
b.mu.Lock()
|
|
|
|
b.localDir = dir
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2016-09-02 19:44:05 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
func (b *Builder) SetSecretsDir(dir string) *Builder {
|
|
|
|
b.mu.Lock()
|
|
|
|
b.secretsDir = dir
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
func (b *Builder) SetPortMap(portMap map[string]int) *Builder {
|
|
|
|
newPortMap := make(map[string]string, len(portMap))
|
|
|
|
for k, v := range portMap {
|
|
|
|
newPortMap[k] = strconv.Itoa(v)
|
|
|
|
}
|
|
|
|
b.mu.Lock()
|
|
|
|
b.portMap = newPortMap
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// SetHostEnvvars adds the host environment variables to the tasks. The filter
|
|
|
|
// parameter can be use to filter host environment from entering the tasks.
|
|
|
|
func (b *Builder) SetHostEnvvars(filter []string) *Builder {
|
|
|
|
filterMap := make(map[string]struct{}, len(filter))
|
2016-03-23 18:45:03 +00:00
|
|
|
for _, f := range filter {
|
2017-05-17 00:33:50 +00:00
|
|
|
filterMap[f] = struct{}{}
|
2016-03-23 18:45:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
fullHostEnv := os.Environ()
|
|
|
|
filteredHostEnv := make(map[string]string, len(fullHostEnv))
|
|
|
|
for _, e := range fullHostEnv {
|
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
|
2017-05-17 00:33:50 +00:00
|
|
|
if _, filtered := filterMap[key]; filtered {
|
2016-03-23 21:07:12 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
filteredHostEnv[key] = value
|
2016-03-23 18:45:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
b.hostEnv = filteredHostEnv
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
2016-03-02 00:08:21 +00:00
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
func (b *Builder) SetTemplateEnv(m map[string]string) *Builder {
|
|
|
|
b.mu.Lock()
|
|
|
|
b.templateEnv = m
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2017-03-29 23:20:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
func (b *Builder) SetVaultToken(token string, inject bool) *Builder {
|
|
|
|
b.mu.Lock()
|
|
|
|
b.vaultToken = token
|
|
|
|
b.injectVaultToken = inject
|
|
|
|
b.mu.Unlock()
|
|
|
|
return b
|
2016-09-14 20:30:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:33:50 +00:00
|
|
|
// addPort keys and values for other tasks to an env var map
|
|
|
|
func addPort(m map[string]string, taskName, ip, portLabel string, port int) {
|
|
|
|
key := fmt.Sprintf("%s%s_%s", AddrPrefix, taskName, portLabel)
|
|
|
|
m[key] = fmt.Sprintf("%s:%d", ip, port)
|
|
|
|
key = fmt.Sprintf("%s%s_%s", IpPrefix, taskName, portLabel)
|
|
|
|
m[key] = ip
|
|
|
|
key = fmt.Sprintf("%s%s_%s", PortPrefix, taskName, portLabel)
|
|
|
|
m[key] = strconv.Itoa(port)
|
2016-09-14 20:30:01 +00:00
|
|
|
}
|