Check for nils before accessing data

This commit is contained in:
Chris Bednarski
2015-09-22 23:11:55 -07:00
parent 2c352e3ff8
commit 3cf1d33626

View File

@@ -111,22 +111,26 @@ func PopulateEnvironment(ctx *ExecContext, task *structs.Task) []string {
env := []string{}
env = append(env, fmt.Sprintf("NOMAD_ALLOC_DIR=%s", ctx.AllocDir))
env = append(env, fmt.Sprintf("NOMAD_MEMORY_LIMIT=%d", task.Resources.MemoryMB))
env = append(env, fmt.Sprintf("NOMAD_CPU_LIMIT=%d", task.Resources.CPU))
if task.Resources != nil {
env = append(env, fmt.Sprintf("NOMAD_MEMORY_LIMIT=%d", task.Resources.MemoryMB))
env = append(env, fmt.Sprintf("NOMAD_CPU_LIMIT=%d", task.Resources.CPU))
// Named Ports
if len(task.Resources.Networks) > 0 {
network := task.Resources.Networks[0]
env = append(env, fmt.Sprintf("NOMAD_HOST_IP=%d", network.IP))
for idx, port := range network.ListDynamicPorts() {
env = append(env, fmt.Sprintf("NOMAD_PORT_%s=%d", strings.ToUpper(network.DynamicPorts[idx]), port))
}
}
}
// Meta values
for key, value := range task.Meta {
env = append(env, fmt.Sprintf("NOMAD_META_%s=%s", strings.ToUpper(key), value))
}
// Named Ports
// TODO make this work with multiple network interfaces.
network := task.Resources.Networks[0]
env = append(env, fmt.Sprintf("NOMAD_HOST_IP=%d", network.IP))
for idx, port := range network.ListDynamicPorts() {
env = append(env, fmt.Sprintf("NOMAD_PORT_%s=%d", strings.ToUpper(network.DynamicPorts[idx]), port))
}
return env
}