Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass environment variables through to drivers #206

Merged
merged 6 commits into from
Oct 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions client/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,9 @@ func TaskEnvironmentVariables(ctx *ExecContext, task *structs.Task) environment.
}
}

if task.Env != nil {
env.SetEnvvars(task.Env)
}

return env
}
6 changes: 6 additions & 0 deletions client/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions client/driver/environment/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
14 changes: 14 additions & 0 deletions jobspec/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions jobspec/test-fixtures/basic.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ job "binstore-storagelocker" {
config {
image = "hashicorp/binstore"
}
env {
HELLO = "world"
LOREM = "ipsum"
}
resources {
cpu = 500
memory = 128
Expand Down
3 changes: 3 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions website/source/docs/jobspec/index.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down