Skip to content

Commit

Permalink
Merge pull request #1752 from hashicorp/f-consul-template
Browse files Browse the repository at this point in the history
Consul template structs
  • Loading branch information
dadgar authored Sep 26, 2016
2 parents b097191 + 757ba24 commit ec9e395
Show file tree
Hide file tree
Showing 8 changed files with 501 additions and 21 deletions.
11 changes: 11 additions & 0 deletions api/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ type Task struct {
LogConfig *LogConfig
Artifacts []*TaskArtifact
Vault *Vault
Templates []*Template
}

// TaskArtifact is used to download artifacts before running a task.
Expand All @@ -164,6 +165,16 @@ type TaskArtifact struct {
RelativeDest string
}

type Template struct {
SourcePath string
DestPath string
EmbededTmpl string
ChangeMode string
RestartSignal string
Splay time.Duration
Once bool
}

type Vault struct {
Policies []string
Env bool
Expand Down
49 changes: 49 additions & 0 deletions jobspec/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ func parseTasks(jobName string, taskGroupName string, result *[]*structs.Task, l
"meta",
"resources",
"service",
"template",
"user",
"vault",
}
Expand All @@ -569,6 +570,7 @@ func parseTasks(jobName string, taskGroupName string, result *[]*structs.Task, l
delete(m, "meta")
delete(m, "resources")
delete(m, "service")
delete(m, "template")
delete(m, "vault")

// Build the task
Expand Down Expand Up @@ -705,6 +707,13 @@ func parseTasks(jobName string, taskGroupName string, result *[]*structs.Task, l
}
}

// Parse templates
if o := listVal.Filter("template"); len(o.Items) > 0 {
if err := parseTemplates(&t.Templates, o); err != nil {
return multierror.Prefix(err, fmt.Sprintf("'%s', template ->", n))
}
}

// If we have a vault block, then parse that
if o := listVal.Filter("vault"); len(o.Items) > 0 {
var v structs.Vault
Expand Down Expand Up @@ -792,6 +801,46 @@ func parseArtifactOption(result map[string]string, list *ast.ObjectList) error {
return nil
}

func parseTemplates(result *[]*structs.Template, list *ast.ObjectList) error {
for _, o := range list.Elem().Items {
// Check for invalid keys
valid := []string{
"source",
"destination",
"data",
"change_mode",
"restart_signal",
"splay",
"once",
}
if err := checkHCLKeys(o.Val, valid); err != nil {
return err
}

var m map[string]interface{}
if err := hcl.DecodeObject(&m, o.Val); err != nil {
return err
}

templ := structs.DefaultTemplate()
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
WeaklyTypedInput: true,
Result: templ,
})
if err != nil {
return err
}
if err := dec.Decode(m); err != nil {
return err
}

*result = append(*result, templ)
}

return nil
}

func parseServices(jobName string, taskGroupName string, task *structs.Task, serviceObjs *ast.ObjectList) error {
task.Services = make([]*structs.Service, len(serviceObjs.Items))
var defaultServiceName bool
Expand Down
30 changes: 24 additions & 6 deletions jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ func TestParse(t *testing.T) {
Policies: []string{"foo", "bar"},
Env: true,
},
Templates: []*structs.Template{
{
SourcePath: "foo",
DestPath: "foo",
ChangeMode: "foo",
RestartSignal: "foo",
Splay: 10 * time.Second,
Once: true,
},
{
SourcePath: "bar",
DestPath: "bar",
ChangeMode: structs.TemplateChangeModeRestart,
RestartSignal: "",
Splay: 5 * time.Second,
Once: false,
},
},
},
&structs.Task{
Name: "storagelocker",
Expand Down Expand Up @@ -451,9 +469,9 @@ func TestParse(t *testing.T) {
Region: "global",
TaskGroups: []*structs.TaskGroup{
&structs.TaskGroup{
Name: "cache",
Count: 1,
LocalDisk: structs.DefaultLocalDisk(),
Name: "cache",
Count: 1,
EphemeralDisk: structs.DefaultEphemeralDisk(),
Tasks: []*structs.Task{
&structs.Task{
Name: "redis",
Expand All @@ -474,9 +492,9 @@ func TestParse(t *testing.T) {
},
},
&structs.TaskGroup{
Name: "cache2",
Count: 1,
LocalDisk: structs.DefaultLocalDisk(),
Name: "cache2",
Count: 1,
EphemeralDisk: structs.DefaultEphemeralDisk(),
Tasks: []*structs.Task{
&structs.Task{
Name: "redis",
Expand Down
14 changes: 14 additions & 0 deletions jobspec/test-fixtures/basic.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ job "binstore-storagelocker" {
vault {
policies = ["foo", "bar"]
}

template {
source = "foo"
destination = "foo"
change_mode = "foo"
restart_signal = "foo"
splay = "10s"
once = true
}

template {
source = "bar"
destination = "bar"
}
}

task "storagelocker" {
Expand Down
11 changes: 11 additions & 0 deletions nomad/structs/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,17 @@ func (t *Task) Diff(other *Task, contextual bool) (*TaskDiff, error) {
diff.Objects = append(diff.Objects, vDiff)
}

// Artifacts diff
tmplDiffs := primitiveObjectSetDiff(
interfaceSlice(t.Templates),
interfaceSlice(other.Templates),
nil,
"Template",
contextual)
if tmplDiffs != nil {
diff.Objects = append(diff.Objects, tmplDiffs...)
}

return diff, nil
}

Expand Down
148 changes: 148 additions & 0 deletions nomad/structs/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3203,6 +3203,154 @@ func TestTaskDiff(t *testing.T) {
},
},
},
{
// Template edited
Old: &Task{
Templates: []*Template{
{
SourcePath: "foo",
DestPath: "bar",
EmbededTmpl: "baz",
ChangeMode: "bam",
RestartSignal: "SIGHUP",
Splay: 1,
Once: true,
},
{
SourcePath: "foo2",
DestPath: "bar2",
EmbededTmpl: "baz2",
ChangeMode: "bam2",
RestartSignal: "SIGHUP2",
Splay: 2,
Once: false,
},
},
},
New: &Task{
Templates: []*Template{
{
SourcePath: "foo",
DestPath: "bar",
EmbededTmpl: "baz",
ChangeMode: "bam",
RestartSignal: "SIGHUP",
Splay: 1,
Once: true,
},
{
SourcePath: "foo3",
DestPath: "bar3",
EmbededTmpl: "baz3",
ChangeMode: "bam3",
RestartSignal: "SIGHUP3",
Splay: 3,
Once: true,
},
},
},
Expected: &TaskDiff{
Type: DiffTypeEdited,
Objects: []*ObjectDiff{
{
Type: DiffTypeAdded,
Name: "Template",
Fields: []*FieldDiff{
{
Type: DiffTypeAdded,
Name: "ChangeMode",
Old: "",
New: "bam3",
},
{
Type: DiffTypeAdded,
Name: "DestPath",
Old: "",
New: "bar3",
},
{
Type: DiffTypeAdded,
Name: "EmbededTmpl",
Old: "",
New: "baz3",
},
{
Type: DiffTypeAdded,
Name: "Once",
Old: "",
New: "true",
},
{
Type: DiffTypeAdded,
Name: "RestartSignal",
Old: "",
New: "SIGHUP3",
},
{
Type: DiffTypeAdded,
Name: "SourcePath",
Old: "",
New: "foo3",
},
{
Type: DiffTypeAdded,
Name: "Splay",
Old: "",
New: "3",
},
},
},
{
Type: DiffTypeDeleted,
Name: "Template",
Fields: []*FieldDiff{
{
Type: DiffTypeDeleted,
Name: "ChangeMode",
Old: "bam2",
New: "",
},
{
Type: DiffTypeDeleted,
Name: "DestPath",
Old: "bar2",
New: "",
},
{
Type: DiffTypeDeleted,
Name: "EmbededTmpl",
Old: "baz2",
New: "",
},
{
Type: DiffTypeDeleted,
Name: "Once",
Old: "false",
New: "",
},
{
Type: DiffTypeDeleted,
Name: "RestartSignal",
Old: "SIGHUP2",
New: "",
},
{
Type: DiffTypeDeleted,
Name: "SourcePath",
Old: "foo2",
New: "",
},
{
Type: DiffTypeDeleted,
Name: "Splay",
Old: "2",
New: "",
},
},
},
},
},
},
}

for i, c := range cases {
Expand Down
Loading

0 comments on commit ec9e395

Please sign in to comment.