From 9c17c0a1d84f7c6bd8e923c7da9fedae33e63856 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Wed, 30 Sep 2015 18:18:43 +0200 Subject: [PATCH 1/6] Added a jobspec directive to specify envvars. Updated Docker driver to use them accordingly. --- api/tasks.go | 1 + client/driver/docker.go | 8 +++++++- jobspec/parse.go | 14 ++++++++++++++ jobspec/parse_test.go | 4 ++++ jobspec/test-fixtures/basic.hcl | 4 ++++ nomad/structs/structs.go | 3 +++ 6 files changed, 33 insertions(+), 1 deletion(-) diff --git a/api/tasks.go b/api/tasks.go index 23e9ca638c8c..c1d5bf2ff632 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/docker.go b/client/driver/docker.go index 7f59e7ce38c7..feb842cc54fc 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -170,8 +170,14 @@ func createContainer(ctx *ExecContext, task *structs.Task, logger *log.Logger) d hostConfig.PortBindings = dockerPorts } + // Merge Nomad-native with user-specified envvars + env := TaskEnvironmentVariables(ctx, task).List() + for k, v := range task.Env { + env = append(env, fmt.Sprintf("%s=%s", k, v)) + } + config := &docker.Config{ - Env: TaskEnvironmentVariables(ctx, task).List(), + Env: env, Image: task.Config["image"], } diff --git a/jobspec/parse.go b/jobspec/parse.go index f1bed065dd0f..1231b5ec059e 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 d26907a6061c..dea59ec266ed 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 f5716293840a..9378327f8505 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" + PLOP = "coucou" + } resources { cpu = 500 memory = 128 diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 02e735dd52b3..098c57b32c74 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 From 7b0b492d33ae553885304ed433b930beb5773181 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Wed, 30 Sep 2015 18:23:53 +0200 Subject: [PATCH 2/6] Forgot to fix my test after testing it. --- jobspec/test-fixtures/basic.hcl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jobspec/test-fixtures/basic.hcl b/jobspec/test-fixtures/basic.hcl index 9378327f8505..941272b2dd7e 100644 --- a/jobspec/test-fixtures/basic.hcl +++ b/jobspec/test-fixtures/basic.hcl @@ -38,7 +38,7 @@ job "binstore-storagelocker" { } env { HELLO = "world" - PLOP = "coucou" + LOREM = "ipsum" } resources { cpu = 500 From 962c509586114d86fc9c5d19d9dbca6ab7e0a922 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Thu, 1 Oct 2015 13:22:26 +0200 Subject: [PATCH 3/6] Moved the envvars logic to TaskEnvironment and TaskEnvironmentVariables(). Added tests there. --- client/driver/docker.go | 8 +------- client/driver/driver.go | 4 ++++ client/driver/driver_test.go | 6 ++++++ client/driver/environment/vars.go | 6 ++++++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/client/driver/docker.go b/client/driver/docker.go index feb842cc54fc..7f59e7ce38c7 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -170,14 +170,8 @@ func createContainer(ctx *ExecContext, task *structs.Task, logger *log.Logger) d hostConfig.PortBindings = dockerPorts } - // Merge Nomad-native with user-specified envvars - env := TaskEnvironmentVariables(ctx, task).List() - for k, v := range task.Env { - env = append(env, fmt.Sprintf("%s=%s", k, v)) - } - config := &docker.Config{ - Env: env, + Env: TaskEnvironmentVariables(ctx, task).List(), Image: task.Config["image"], } diff --git a/client/driver/driver.go b/client/driver/driver.go index a8cc9829a277..5310dca51ef6 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 47385649a25b..0c8e9bdbf619 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 7da8afa0dbd4..aa9b9357eda9 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[strings.ToUpper(k)] = v + } +} From b42d987e19d4b8b7bb200a5dbb2e6f748e75bec6 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Thu, 1 Oct 2015 13:59:23 +0200 Subject: [PATCH 4/6] Updated website documentation. --- website/source/docs/jobspec/index.html.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/website/source/docs/jobspec/index.html.md b/website/source/docs/jobspec/index.html.md index aa1aba33bfbb..173cd041b483 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. From 78f74c57aebe2a61969cd37c159a37c5390eb116 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Thu, 1 Oct 2015 20:25:48 +0200 Subject: [PATCH 5/6] Removed capitalization of user-defined envvars. --- client/driver/environment/vars.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/driver/environment/vars.go b/client/driver/environment/vars.go index aa9b9357eda9..8e2d698ed2ad 100644 --- a/client/driver/environment/vars.go +++ b/client/driver/environment/vars.go @@ -99,6 +99,6 @@ func (t TaskEnvironment) SetMeta(m map[string]string) { func (t TaskEnvironment) SetEnvvars(m map[string]string) { for k, v := range m { - t[strings.ToUpper(k)] = v + t[k] = v } } From 7f359cfe99110a94ac88972feed65ad5f0749df9 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Fri, 2 Oct 2015 17:49:18 -0700 Subject: [PATCH 6/6] update test to reflect not uppercasing passed env vars --- client/driver/driver_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/driver/driver_test.go b/client/driver/driver_test.go index 0c8e9bdbf619..a6f621455199 100644 --- a/client/driver/driver_test.go +++ b/client/driver/driver_test.go @@ -81,7 +81,7 @@ func TestDriver_TaskEnvironmentVariables(t *testing.T) { "NOMAD_META_CHOCOLATE": "cake", "NOMAD_META_STRAWBERRY": "icecream", "HELLO": "world", - "LOREM": "ipsum", + "lorem": "ipsum", } act := env.Map()