Added a jobspec directive to specify envvars. Updated Docker driver to use them accordingly.

This commit is contained in:
Antoine POPINEAU
2015-09-30 18:18:43 +02:00
committed by Alex Dadgar
parent ccb36b3647
commit 9c17c0a1d8
6 changed files with 33 additions and 1 deletions

View File

@@ -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
}

View File

@@ -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"],
}

View File

@@ -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) {

View File

@@ -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,

View File

@@ -36,6 +36,10 @@ job "binstore-storagelocker" {
config {
image = "hashicorp/binstore"
}
env {
HELLO = "world"
PLOP = "coucou"
}
resources {
cpu = 500
memory = 128

View File

@@ -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