Skip to content

Commit

Permalink
Merge pull request #2877 from kmalec/add-group-name-env
Browse files Browse the repository at this point in the history
Pass task group name as NOMAD_GROUP_NAME environment variable
  • Loading branch information
schmichael committed Jul 20, 2017
2 parents 8ae8f61 + bafe927 commit c98104d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions client/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func setupTaskEnv(t *testing.T, driver string) (*allocdir.TaskDir, map[string]st
"NOMAD_ALLOC_INDEX": "0",
"NOMAD_ALLOC_NAME": alloc.Name,
"NOMAD_TASK_NAME": task.Name,
"NOMAD_GROUP_NAME": alloc.TaskGroup,
"NOMAD_JOB_NAME": alloc.Job.Name,
"NOMAD_DC": "dc1",
"NOMAD_REGION": "global",
Expand Down
8 changes: 8 additions & 0 deletions client/driver/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const (
// TaskName is the environment variable for passing the task name.
TaskName = "NOMAD_TASK_NAME"

// GroupName is the environment variable for passing the task group name.
GroupName = "NOMAD_GROUP_NAME"

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

Expand Down Expand Up @@ -208,6 +211,7 @@ type Builder struct {
region string
allocId string
allocName string
groupName string
vaultToken string
injectVaultToken bool
jobName string
Expand Down Expand Up @@ -277,6 +281,9 @@ func (b *Builder) Build() *TaskEnv {
if b.allocName != "" {
envMap[AllocName] = b.allocName
}
if b.groupName != "" {
envMap[GroupName] = b.groupName
}
if b.allocIndex != -1 {
envMap[AllocIndex] = strconv.Itoa(b.allocIndex)
}
Expand Down Expand Up @@ -380,6 +387,7 @@ func (b *Builder) setTask(task *structs.Task) *Builder {
func (b *Builder) setAlloc(alloc *structs.Allocation) *Builder {
b.allocId = alloc.ID
b.allocName = alloc.Name
b.groupName = alloc.TaskGroup
b.allocIndex = int(alloc.Index())
b.jobName = alloc.Job.Name

Expand Down
1 change: 1 addition & 0 deletions client/driver/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func TestEnvironment_AsList(t *testing.T) {
"NOMAD_HOST_PORT_http=80",
"NOMAD_HOST_PORT_https=8080",
"NOMAD_TASK_NAME=web",
"NOMAD_GROUP_NAME=web",
"NOMAD_ADDR_ssh_other=192.168.0.100:1234",
"NOMAD_ADDR_ssh_ssh=192.168.0.100:22",
"NOMAD_IP_ssh_other=192.168.0.100",
Expand Down
4 changes: 4 additions & 0 deletions website/source/docs/runtime/_envvars.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
<td><tt>NOMAD_TASK_NAME</tt></td>
<td>Task's name</td>
</tr>
<tr>
<td><tt>NOMAD_GROUP_NAME</tt></td>
<td>Group's name</td>
</tr>
<tr>
<td><tt>NOMAD_JOB_NAME</tt></td>
<td>Job's name</td>
Expand Down
10 changes: 5 additions & 5 deletions website/source/docs/runtime/environment.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ environment variable names such as `NOMAD_ADDR_<task>_<label>`.

## Task Identifiers

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.
Nomad will pass both the allocation ID and name as well as the task, group and
job's names. These are given as `NOMAD_ALLOC_ID`, `NOMAD_ALLOC_NAME`,
`NOMAD_ALLOC_INDEX`, `NOMAD_JOB_NAME`, `NOMAD_GROUP_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

0 comments on commit c98104d

Please sign in to comment.