diff --git a/api/tasks.go b/api/tasks.go index 23e9ca638c8..c1d5bf2ff63 100644 --- a/api/tasks.go +++ b/api/tasks.go @@ -44,6 +44,7 @@ type Task struct { Driver string Config map[string]string Constraints []*Constraint + Env map[string]string Resources *Resources Meta map[string]string } diff --git a/client/driver/driver.go b/client/driver/driver.go index a8cc9829a27..5310dca51ef 100644 --- a/client/driver/driver.go +++ b/client/driver/driver.go @@ -125,5 +125,9 @@ func TaskEnvironmentVariables(ctx *ExecContext, task *structs.Task) environment. } } + if task.Env != nil { + env.SetEnvvars(task.Env) + } + return env } diff --git a/client/driver/driver_test.go b/client/driver/driver_test.go index 47385649a25..a6f62145519 100644 --- a/client/driver/driver_test.go +++ b/client/driver/driver_test.go @@ -50,6 +50,10 @@ func testDriverExecContext(task *structs.Task, driverCtx *DriverContext) *ExecCo func TestDriver_TaskEnvironmentVariables(t *testing.T) { ctx := &ExecContext{} task := &structs.Task{ + Env: map[string]string{ + "HELLO": "world", + "lorem": "ipsum", + }, Resources: &structs.Resources{ CPU: 1000, MemoryMB: 500, @@ -76,6 +80,8 @@ func TestDriver_TaskEnvironmentVariables(t *testing.T) { "NOMAD_PORT_5000": "12345", "NOMAD_META_CHOCOLATE": "cake", "NOMAD_META_STRAWBERRY": "icecream", + "HELLO": "world", + "lorem": "ipsum", } act := env.Map() diff --git a/client/driver/environment/vars.go b/client/driver/environment/vars.go index 7da8afa0dbd..8e2d698ed2a 100644 --- a/client/driver/environment/vars.go +++ b/client/driver/environment/vars.go @@ -96,3 +96,9 @@ func (t TaskEnvironment) SetMeta(m map[string]string) { t[fmt.Sprintf("%s%s", MetaPrefix, strings.ToUpper(k))] = v } } + +func (t TaskEnvironment) SetEnvvars(m map[string]string) { + for k, v := range m { + t[k] = v + } +} diff --git a/jobspec/parse.go b/jobspec/parse.go index f1bed065dd0..1231b5ec059 100644 --- a/jobspec/parse.go +++ b/jobspec/parse.go @@ -284,6 +284,7 @@ func parseTasks(result *[]*structs.Task, obj *hclobj.Object) error { return err } delete(m, "config") + delete(m, "env") delete(m, "constraint") delete(m, "meta") delete(m, "resources") @@ -295,6 +296,19 @@ func parseTasks(result *[]*structs.Task, obj *hclobj.Object) error { return err } + // If we have env, then parse them + if o := o.Get("env", false); o != nil { + for _, o := range o.Elem(false) { + var m map[string]interface{} + if err := hcl.DecodeObject(&m, o); err != nil { + return err + } + if err := mapstructure.WeakDecode(m, &t.Env); err != nil { + return err + } + } + } + // If we have config, then parse that if o := o.Get("config", false); o != nil { for _, o := range o.Elem(false) { diff --git a/jobspec/parse_test.go b/jobspec/parse_test.go index d26907a6061..dea59ec266e 100644 --- a/jobspec/parse_test.go +++ b/jobspec/parse_test.go @@ -86,6 +86,10 @@ func TestParse(t *testing.T) { Config: map[string]string{ "image": "hashicorp/binstore", }, + Env: map[string]string{ + "HELLO": "world", + "LOREM": "ipsum", + }, Resources: &structs.Resources{ CPU: 500, MemoryMB: 128, diff --git a/jobspec/test-fixtures/basic.hcl b/jobspec/test-fixtures/basic.hcl index f5716293840..941272b2dd7 100644 --- a/jobspec/test-fixtures/basic.hcl +++ b/jobspec/test-fixtures/basic.hcl @@ -36,6 +36,10 @@ job "binstore-storagelocker" { config { image = "hashicorp/binstore" } + env { + HELLO = "world" + LOREM = "ipsum" + } resources { cpu = 500 memory = 128 diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 02e735dd52b..098c57b32c7 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -966,6 +966,9 @@ type Task struct { // Config is provided to the driver to initialize Config map[string]string + // Map of environment variables to be used by the driver + Env map[string]string + // Constraints can be specified at a task level and apply only to // the particular task. Constraints []*Constraint diff --git a/website/source/docs/jobspec/index.html.md b/website/source/docs/jobspec/index.html.md index aa1aba33bfb..173cd041b48 100644 --- a/website/source/docs/jobspec/index.html.md +++ b/website/source/docs/jobspec/index.html.md @@ -45,6 +45,11 @@ job "my-service" { config { image = "hashicorp/web-frontend" } + env { + DB_HOST = "db01.example.com" + DB_USER = "web" + DB_PASSWORD = "loremipsum" + } resources { cpu = 500 memory = 128 @@ -166,6 +171,9 @@ The `task` object supports the following keys: to start the task. The details of configurations are specific to each driver. +* `env` - A map of key/value representing environment variables that + will be passed along to the running process. + * `resources` - Provides the resource requirements of the task. See the resources reference for more details.