Skip to content

Commit

Permalink
Merge pull request #1804 from hashicorp/f-job-env-var
Browse files Browse the repository at this point in the history
Add NOMAD_JOB_NAME to task environment
  • Loading branch information
schmichael committed Oct 11, 2016
2 parents d398c04 + 0f11b2d commit 5773e1b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ IMPROVEMENTS:
* client: Enforce shared allocation directory disk usage [GH-1580]
* client: Introduce a `secrets/` directory to tasks where sensitive data can
be written [GH-1681]
* driver: Export `NOMAD_JOB_NAME` environment variable [GH-1804]
* driver/docker: Support Docker volumes [GH-1767]
* driver/docker: Allow Docker logging to be configured [GH-1767]

Expand Down
1 change: 1 addition & 0 deletions client/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func GetTaskEnv(allocDir *allocdir.AllocDir, node *structs.Node,
SetTaskMeta(task.Meta).
SetTaskGroupMeta(tg.Meta).
SetJobMeta(alloc.Job.Meta).
SetJobName(alloc.Job.Name).
SetEnvvars(task.Env).
SetTaskName(task.Name)

Expand Down
1 change: 1 addition & 0 deletions client/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func TestDriver_GetTaskEnv(t *testing.T) {
"NOMAD_ALLOC_ID": alloc.ID,
"NOMAD_ALLOC_NAME": alloc.Name,
"NOMAD_TASK_NAME": task.Name,
"NOMAD_JOB_NAME": alloc.Job.Name,
}

act := env.EnvMap()
Expand Down
17 changes: 17 additions & 0 deletions client/driver/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const (
// TaskName is the environment variable for passing the task name.
TaskName = "NOMAD_TASK_NAME"

// JobName is the environment variable for passing the job name.
JobName = "NOMAD_JOB_NAME"

// AllocIndex is the environment variable for passing the allocation index.
AllocIndex = "NOMAD_ALLOC_INDEX"

Expand Down Expand Up @@ -98,6 +101,7 @@ type TaskEnvironment struct {
PortMap map[string]int
VaultToken string
InjectVaultToken bool
JobName string

// taskEnv is the variables that will be set in the tasks environment
TaskEnv map[string]string
Expand Down Expand Up @@ -188,6 +192,9 @@ func (t *TaskEnvironment) Build() *TaskEnvironment {
if t.TaskName != "" {
t.TaskEnv[TaskName] = t.TaskName
}
if t.JobName != "" {
t.TaskEnv[JobName] = t.JobName
}

// Build the node
if t.Node != nil {
Expand Down Expand Up @@ -452,11 +459,21 @@ func (t *TaskEnvironment) SetTaskName(name string) *TaskEnvironment {
return t
}

func (t *TaskEnvironment) SetJobName(name string) *TaskEnvironment {
t.JobName = name
return t
}

func (t *TaskEnvironment) ClearTaskName() *TaskEnvironment {
t.TaskName = ""
return t
}

func (t *TaskEnvironment) ClearJobName() *TaskEnvironment {
t.JobName = ""
return t
}

func (t *TaskEnvironment) SetVaultToken(token string, inject bool) *TaskEnvironment {
t.VaultToken = token
t.InjectVaultToken = inject
Expand Down
13 changes: 9 additions & 4 deletions website/source/docs/jobspec/environment.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ environment variables.
<td>`NOMAD_TASK_NAME`</td>
<td>The task's name</td>
</tr>
<tr>
<td>`NOMAD_JOB_NAME`</td>
<td>The job's name</td>
</tr>
<tr>
<td>`NOMAD_IP_<label>`</td>
<td>The IP of the port with the given label</td>
Expand All @@ -76,10 +80,11 @@ environment variables.

## Task Identifiers

Nomad will pass both the allocation ID and name as well as the task's name.
These are given as `NOMAD_ALLOC_ID`, `NOMAD_ALLOC_NAME`, `NOMAD_ALLOC_INDEX` and
`NOMAD_TASK_NAME`. The allocation ID and index can be useful when the task being
run needs a unique identifier or to know its instance count.
Nomad will pass both the allocation ID and name as well as the task and job's
names. These are given as `NOMAD_ALLOC_ID`, `NOMAD_ALLOC_NAME`,
`NOMAD_ALLOC_INDEX`, `NOMAD_JOB_NAME`, and `NOMAD_TASK_NAME`. The allocation ID
and index can be useful when the task being run needs a unique identifier or to
know its instance count.

## Resources

Expand Down
4 changes: 4 additions & 0 deletions website/source/docs/jobspec/interpreted.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ a particular node and as such can not be used in constraints.
<td><tt>${NOMAD_TASK_NAME}</tt></td>
<td>The task's name</td>
</tr>
<tr>
<td><tt>${NOMAD_JOB_NAME}</tt></td>
<td>The job's name</td>
</tr>
<tr>
<td><tt>${NOMAD_IP_&lt;label&gt;}</tt></td>
<td>The IP for the given port <tt>label</tt>. See
Expand Down

0 comments on commit 5773e1b

Please sign in to comment.