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

Allow specification of template delimiters #2315

Merged
merged 1 commit into from
Feb 21, 2017
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
8 changes: 8 additions & 0 deletions api/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ type Template struct {
ChangeSignal *string
Splay *time.Duration
Perms *string
LeftDelim *string
RightDelim *string
}

func (tmpl *Template) Canonicalize() {
Expand All @@ -326,6 +328,12 @@ func (tmpl *Template) Canonicalize() {
sig := *tmpl.ChangeSignal
tmpl.ChangeSignal = helper.StringToPtr(strings.ToUpper(sig))
}
if tmpl.LeftDelim == nil {
tmpl.LeftDelim = helper.StringToPtr("{{")
}
if tmpl.RightDelim == nil {
tmpl.RightDelim = helper.StringToPtr("}}")
}
}

type Vault struct {
Expand Down
2 changes: 2 additions & 0 deletions client/consul_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ func parseTemplateConfigs(tmpls []*structs.Template, taskDir string,
ct.Source = &src
ct.Destination = &dest
ct.Contents = &tmpl.EmbeddedTmpl
ct.LeftDelim = &tmpl.LeftDelim
ct.RightDelim = &tmpl.RightDelim

// Set the permissions
if tmpl.Perms != "" {
Expand Down
2 changes: 2 additions & 0 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@ func apiTaskToStructsTask(apiTask *api.Task, structsTask *structs.Task) {
ChangeSignal: *template.ChangeSignal,
Splay: *template.Splay,
Perms: *template.Perms,
LeftDelim: *template.LeftDelim,
RightDelim: *template.RightDelim,
}
}
if apiTask.DispatchPayload != nil {
Expand Down
8 changes: 8 additions & 0 deletions command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
Spec: helper.StringToPtr("spec"),
SpecType: helper.StringToPtr("cron"),
ProhibitOverlap: helper.BoolToPtr(true),
TimeZone: helper.StringToPtr("test zone"),
},
ParameterizedJob: &api.ParameterizedJobConfig{
Payload: "payload",
Expand Down Expand Up @@ -747,6 +748,7 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
Tasks: []*api.Task{
{
Name: "task1",
Leader: true,
Driver: "docker",
User: "mary",
Config: map[string]interface{}{
Expand Down Expand Up @@ -840,6 +842,8 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
ChangeSignal: helper.StringToPtr("signal"),
Splay: helper.TimeToPtr(1 * time.Minute),
Perms: helper.StringToPtr("666"),
LeftDelim: helper.StringToPtr("abc"),
RightDelim: helper.StringToPtr("def"),
},
},
DispatchPayload: &api.DispatchPayloadConfig{
Expand Down Expand Up @@ -882,6 +886,7 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
Spec: "spec",
SpecType: "cron",
ProhibitOverlap: true,
TimeZone: "test zone",
},
ParameterizedJob: &structs.ParameterizedJobConfig{
Payload: "payload",
Expand Down Expand Up @@ -921,6 +926,7 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
{
Name: "task1",
Driver: "docker",
Leader: true,
User: "mary",
Config: map[string]interface{}{
"lol": "code",
Expand Down Expand Up @@ -1010,6 +1016,8 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
ChangeSignal: "SIGNAL",
Splay: 1 * time.Minute,
Perms: "666",
LeftDelim: "abc",
RightDelim: "def",
},
},
DispatchPayload: &structs.DispatchPayloadConfig{
Expand Down
2 changes: 2 additions & 0 deletions jobspec/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,9 @@ func parseTemplates(result *[]*api.Template, list *ast.ObjectList) error {
"change_signal",
"data",
"destination",
"left_delimiter",
"perms",
"right_delimiter",
"source",
"splay",
}
Expand Down
2 changes: 2 additions & 0 deletions jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ func TestParse(t *testing.T) {
ChangeSignal: "",
Splay: 5 * time.Second,
Perms: "777",
LeftDelim: "--",
RightDelim: "__",
},
},
Leader: true,
Expand Down
2 changes: 2 additions & 0 deletions jobspec/test-fixtures/basic.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ job "binstore-storagelocker" {
source = "bar"
destination = "bar"
perms = "777"
left_delimiter = "--"
right_delimiter = "__"
}
}

Expand Down
5 changes: 5 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2671,6 +2671,11 @@ type Template struct {

// Perms is the permission the file should be written out with.
Perms string `mapstructure:"perms"`

// LeftDelim and RightDelim are optional configurations to control what
// delimiter is utilized when parsing the template.
LeftDelim string `mapstructure:"left_delimiter"`
RightDelim string `mapstructure:"right_delimiter"`
}

// DefaultTemplate returns a default template.
Expand Down
36 changes: 22 additions & 14 deletions website/source/docs/http/json-jobs.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -667,19 +667,6 @@ README][ct].

`Template` object supports following attributes:

* `SourcePath` - Specifies the path to the template to be rendered. `SourcePath`
is mutually exclusive with `EmbeddedTmpl` attribute. The source can be fetched
using an [`Artifact`](#artifact) resource. The template must exist on the
machine prior to starting the task; it is not possible to reference a template
inside of a Docker container, for example.

* `EmbeddedTmpl` - Specifies the raw template to execute. One of `SourcePath`
or `EmbeddedTmpl` must be specified, but not both. This is useful for smaller
templates, but we recommend using `SourcePath` for larger templates.

* `DestPath` - Specifies the location where the resulting template should be
rendered, relative to the task directory.

- `ChangeMode` - Specifies the behavior Nomad should take if the rendered
template changes. The default value is `"restart"`. The possible values are:

Expand All @@ -691,9 +678,30 @@ README][ct].
"SIGUSR1" or "SIGINT". This option is required if the `ChangeMode` is
`signal`.

* `perms` - Specifies the rendered template's permissions. File permissions are
* `DestPath` - Specifies the location where the resulting template should be
rendered, relative to the task directory.

* `EmbeddedTmpl` - Specifies the raw template to execute. One of `SourcePath`
or `EmbeddedTmpl` must be specified, but not both. This is useful for smaller
templates, but we recommend using `SourcePath` for larger templates.

* `LeftDelim` - Specifies the left delimiter to use in the template. The default
is "{{" for some templates, it may be easier to use a different delimiter that
does not conflict with the output file itself.

* `Perms` - Specifies the rendered template's permissions. File permissions are
given as octal of the unix file permissions rwxrwxrwx.

* `RightDelim` - Specifies the right delimiter to use in the template. The default
is "}}" for some templates, it may be easier to use a different delimiter that
does not conflict with the output file itself.

* `SourcePath` - Specifies the path to the template to be rendered. `SourcePath`
is mutually exclusive with `EmbeddedTmpl` attribute. The source can be fetched
using an [`Artifact`](#artifact) resource. The template must exist on the
machine prior to starting the task; it is not possible to reference a template
inside of a Docker container, for example.

* `Splay` - Specifies a random amount of time to wait between 0ms and the given
splay value before invoking the change mode. Should be specified in
nanoseconds.
Expand Down
8 changes: 8 additions & 0 deletions website/source/docs/job-specification/template.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,17 @@ README][ct].
- `destination` `(string: <required>)` - Specifies the location where the
resulting template should be rendered, relative to the task directory.

* `left_delimiter` `(string: "{{")` - Specifies the left delimiter to use in the
template. The default is "{{" for some templates, it may be easier to use a
different delimiter that does not conflict with the output file itself.

- `perms` `(string: "666")` - Specifies the rendered template's permissions.
File permissions are given as octal of the unix file permissions rwxrwxrwx.

* `right_delimiter` `(string: "}}")` - Specifies the right delimiter to use in the
template. The default is "}}" for some templates, it may be easier to use a
different delimiter that does not conflict with the output file itself.

- `source` `(string: "")` - Specifies the path to the template to be rendered.
One of `source` or `data` must be specified, but not both. This source can
optionally be fetched using an [`artifact`][artifact] resource. This template
Expand Down