This commit is contained in:
Alex Dadgar 2016-09-26 15:23:26 -07:00
parent b4c3f015bc
commit fca2becaa5
4 changed files with 96 additions and 6 deletions

View File

@ -550,6 +550,7 @@ func parseTasks(jobName string, taskGroupName string, result *[]*structs.Task, l
"meta",
"resources",
"service",
"template",
"user",
"vault",
}
@ -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
@ -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
@ -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

View File

@ -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",
@ -453,7 +471,7 @@ func TestParse(t *testing.T) {
&structs.TaskGroup{
Name: "cache",
Count: 1,
LocalDisk: structs.DefaultLocalDisk(),
EphemeralDisk: structs.DefaultEphemeralDisk(),
Tasks: []*structs.Task{
&structs.Task{
Name: "redis",
@ -476,7 +494,7 @@ func TestParse(t *testing.T) {
&structs.TaskGroup{
Name: "cache2",
Count: 1,
LocalDisk: structs.DefaultLocalDisk(),
EphemeralDisk: structs.DefaultEphemeralDisk(),
Tasks: []*structs.Task{
&structs.Task{
Name: "redis",

View File

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

View File

@ -2242,6 +2242,15 @@ type Template struct {
Once bool `mapstructure:"once"`
}
// DefaultTemplate returns a default template.
func DefaultTemplate() *Template {
return &Template{
ChangeMode: TemplateChangeModeRestart,
Splay: 5 * time.Second,
Once: false,
}
}
func (t *Template) Copy() *Template {
if t == nil {
return nil