forked from hashicorp/nomad
/
vars.go
169 lines (136 loc) · 3.71 KB
/
vars.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package environment
import (
"fmt"
"strconv"
"strings"
)
// 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"
// The IP address for the task.
TaskIP = "NOMAD_IP"
// Prefix for passing both dynamic and static port allocations to
// tasks.
// E.g. $NOMAD_PORT_1 or $NOMAD_PORT_http
PortPrefix = "NOMAD_PORT_"
// Prefix for passing task meta data.
MetaPrefix = "NOMAD_META_"
)
var (
nomadVars = []string{AllocDir, TaskLocalDir, MemLimit, CpuLimit, TaskIP, PortPrefix, MetaPrefix}
)
type TaskEnvironment map[string]string
func NewTaskEnivornment() TaskEnvironment {
return make(map[string]string)
}
// ParseFromList parses a list of strings with NAME=value pairs and returns a
// TaskEnvironment.
func ParseFromList(envVars []string) (TaskEnvironment, error) {
t := NewTaskEnivornment()
for _, pair := range envVars {
// Start the search from the second byte to skip a possible leading
// "=". Cmd.exe on Windows creates some special environment variables
// that start with an "=" and they can be properly retrieved by OS
// functions so we should handle them properly here.
idx := strings.Index(pair[1:], "=")
if idx == -1 {
return nil, fmt.Errorf("Couldn't parse environment variable: %v", pair)
}
idx++ // adjust for slice offset above
t[pair[:idx]] = pair[idx+1:]
}
return t, nil
}
// Returns a list of strings with NAME=value pairs.
func (t TaskEnvironment) List() []string {
env := []string{}
for k, v := range t {
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
return env
}
func (t TaskEnvironment) Map() map[string]string {
return t
}
func (t TaskEnvironment) SetAllocDir(dir string) {
t[AllocDir] = dir
}
func (t TaskEnvironment) ClearAllocDir() {
delete(t, AllocDir)
}
func (t TaskEnvironment) SetTaskLocalDir(dir string) {
t[TaskLocalDir] = dir
}
func (t TaskEnvironment) ClearTaskLocalDir() {
delete(t, TaskLocalDir)
}
func (t TaskEnvironment) SetMemLimit(limit int) {
t[MemLimit] = strconv.Itoa(limit)
}
func (t TaskEnvironment) ClearMemLimit() {
delete(t, MemLimit)
}
func (t TaskEnvironment) SetCpuLimit(limit int) {
t[CpuLimit] = strconv.Itoa(limit)
}
func (t TaskEnvironment) ClearCpuLimit() {
delete(t, CpuLimit)
}
func (t TaskEnvironment) SetTaskIp(ip string) {
t[TaskIP] = ip
}
func (t TaskEnvironment) ClearTaskIp() {
delete(t, TaskIP)
}
// Takes a map of port labels to their port value.
func (t TaskEnvironment) SetPorts(ports map[string]int) {
for label, port := range ports {
t[fmt.Sprintf("%s%s", PortPrefix, label)] = strconv.Itoa(port)
}
}
func (t TaskEnvironment) ClearPorts() {
for k, _ := range t {
if strings.HasPrefix(k, PortPrefix) {
delete(t, k)
}
}
}
// 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) {
for k, v := range m {
t[fmt.Sprintf("%s%s", MetaPrefix, strings.ToUpper(k))] = v
}
}
func (t TaskEnvironment) ClearMeta() {
for k, _ := range t {
if strings.HasPrefix(k, MetaPrefix) {
delete(t, k)
}
}
}
func (t TaskEnvironment) SetEnvvars(m map[string]string) {
for k, v := range m {
t[k] = v
}
}
func (t TaskEnvironment) ClearEnvvars() {
OUTER:
for k, _ := range t {
for _, nomadPrefix := range nomadVars {
if strings.HasPrefix(k, nomadPrefix) {
continue OUTER
}
}
delete(t, k)
}
}