github.com/jmitchell/nomad@v0.1.3-0.20151007230021-7ab84c2862d8/client/driver/environment/vars.go (about) 1 package environment 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 ) 8 9 // A set of environment variables that are exported by each driver. 10 const ( 11 // The path to the alloc directory that is shared across tasks within a task 12 // group. 13 AllocDir = "NOMAD_ALLOC_DIR" 14 15 // The tasks memory limit in MBs. 16 MemLimit = "NOMAD_MEMORY_LIMIT" 17 18 // The tasks limit in MHz. 19 CpuLimit = "NOMAD_CPU_LIMIT" 20 21 // The IP address for the task. 22 TaskIP = "NOMAD_IP" 23 24 // Prefix for passing both dynamic and static port allocations to 25 // tasks. 26 // E.g. $NOMAD_PORT_1 or $NOMAD_PORT_http 27 PortPrefix = "NOMAD_PORT_" 28 29 // Prefix for passing task meta data. 30 MetaPrefix = "NOMAD_META_" 31 ) 32 33 type TaskEnvironment map[string]string 34 35 func NewTaskEnivornment() TaskEnvironment { 36 return make(map[string]string) 37 } 38 39 // Parses a list of strings with NAME=value pairs and returns a TaskEnvironment. 40 func ParseFromList(envVars []string) (TaskEnvironment, error) { 41 t := NewTaskEnivornment() 42 43 for _, pair := range envVars { 44 parts := strings.Split(pair, "=") 45 if len(parts) != 2 { 46 return nil, fmt.Errorf("Couldn't parse environment variable: %v", pair) 47 } 48 49 t[parts[0]] = parts[1] 50 } 51 52 return t, nil 53 } 54 55 // Returns a list of strings with NAME=value pairs. 56 func (t TaskEnvironment) List() []string { 57 env := []string{} 58 for k, v := range t { 59 env = append(env, fmt.Sprintf("%s=%s", k, v)) 60 } 61 62 return env 63 } 64 65 func (t TaskEnvironment) Map() map[string]string { 66 return t 67 } 68 69 func (t TaskEnvironment) SetAllocDir(dir string) { 70 t[AllocDir] = dir 71 } 72 73 func (t TaskEnvironment) SetMemLimit(limit int) { 74 t[MemLimit] = strconv.Itoa(limit) 75 } 76 77 func (t TaskEnvironment) SetCpuLimit(limit int) { 78 t[CpuLimit] = strconv.Itoa(limit) 79 } 80 81 func (t TaskEnvironment) SetTaskIp(ip string) { 82 t[TaskIP] = ip 83 } 84 85 // Takes a map of port labels to their port value. 86 func (t TaskEnvironment) SetPorts(ports map[string]int) { 87 for label, port := range ports { 88 t[fmt.Sprintf("%s%s", PortPrefix, label)] = strconv.Itoa(port) 89 } 90 } 91 92 // Takes a map of meta values to be passed to the task. The keys are capatilized 93 // when the environent variable is set. 94 func (t TaskEnvironment) SetMeta(m map[string]string) { 95 for k, v := range m { 96 t[fmt.Sprintf("%s%s", MetaPrefix, strings.ToUpper(k))] = v 97 } 98 } 99 100 func (t TaskEnvironment) SetEnvvars(m map[string]string) { 101 for k, v := range m { 102 t[k] = v 103 } 104 }