2015-09-15 00:43:42 +00:00
|
|
|
package jobspec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-09-23 20:44:08 +00:00
|
|
|
"regexp"
|
2015-10-22 23:37:20 +00:00
|
|
|
"strconv"
|
2015-09-26 01:59:17 +00:00
|
|
|
"strings"
|
2015-09-15 00:43:42 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/hcl"
|
2015-11-09 06:57:39 +00:00
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
2015-09-15 00:43:42 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
var reDynamicPorts *regexp.Regexp = regexp.MustCompile("^[a-zA-Z0-9_]+$")
|
|
|
|
var errDynamicPorts = fmt.Errorf("DynamicPort label does not conform to naming requirements %s", reDynamicPorts.String())
|
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
// Parse parses the job spec from the given io.Reader.
|
|
|
|
//
|
|
|
|
// Due to current internal limitations, the entire contents of the
|
|
|
|
// io.Reader will be copied into memory first before parsing.
|
|
|
|
func Parse(r io.Reader) (*structs.Job, error) {
|
|
|
|
// Copy the reader into an in-memory buffer first since HCL requires it.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if _, err := io.Copy(&buf, r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the buffer
|
2015-11-09 06:57:39 +00:00
|
|
|
root, err := hcl.Parse(buf.String())
|
2015-09-15 00:43:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing: %s", err)
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
// Top-level item should be a list
|
|
|
|
list, ok := root.Node.(*ast.ObjectList)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error parsing: root should be an object")
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
var job structs.Job
|
|
|
|
|
|
|
|
// Parse the job out
|
2015-11-09 06:57:39 +00:00
|
|
|
matches := list.Filter("job")
|
|
|
|
if len(matches.Items) == 0 {
|
2015-09-15 00:43:42 +00:00
|
|
|
return nil, fmt.Errorf("'job' stanza not found")
|
|
|
|
}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := parseJob(&job, matches); err != nil {
|
2015-09-15 00:43:42 +00:00
|
|
|
return nil, fmt.Errorf("error parsing 'job': %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &job, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseFile parses the given path as a job spec.
|
|
|
|
func ParseFile(path string) (*structs.Job, error) {
|
|
|
|
path, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return Parse(f)
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
func parseJob(result *structs.Job, list *ast.ObjectList) error {
|
|
|
|
list = list.Children()
|
|
|
|
if len(list.Items) != 1 {
|
2015-09-15 00:43:42 +00:00
|
|
|
return fmt.Errorf("only one 'job' block allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get our job object
|
2015-11-09 06:57:39 +00:00
|
|
|
obj := list.Items[0]
|
2015-09-15 00:43:42 +00:00
|
|
|
|
|
|
|
// Decode the full thing into a map[string]interface for ease
|
|
|
|
var m map[string]interface{}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := hcl.DecodeObject(&m, obj.Val); err != nil {
|
2015-09-15 00:43:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-09-15 00:48:11 +00:00
|
|
|
delete(m, "constraint")
|
2015-09-15 00:46:52 +00:00
|
|
|
delete(m, "meta")
|
2015-09-20 21:18:10 +00:00
|
|
|
delete(m, "update")
|
2015-09-15 00:43:42 +00:00
|
|
|
|
2015-09-17 05:06:55 +00:00
|
|
|
// Set the ID and name to the object key
|
2015-11-09 06:57:39 +00:00
|
|
|
result.ID = obj.Keys[0].Token.Value().(string)
|
|
|
|
result.Name = result.ID
|
2015-09-15 00:43:42 +00:00
|
|
|
|
2015-09-15 01:34:26 +00:00
|
|
|
// Defaults
|
|
|
|
result.Priority = 50
|
|
|
|
result.Region = "global"
|
|
|
|
result.Type = "service"
|
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
// Decode the rest
|
|
|
|
if err := mapstructure.WeakDecode(m, result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
// Value should be an object
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := obj.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("job '%s' value: should be an object", result.ID)
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:48:11 +00:00
|
|
|
// Parse constraints
|
2015-11-09 06:57:39 +00:00
|
|
|
if o := listVal.Filter("constraint"); len(o.Items) > 0 {
|
2015-09-15 00:48:11 +00:00
|
|
|
if err := parseConstraints(&result.Constraints, o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 21:18:10 +00:00
|
|
|
// If we have an update strategy, then parse that
|
2015-11-09 06:57:39 +00:00
|
|
|
if o := listVal.Filter("update"); len(o.Items) > 0 {
|
2015-09-20 21:18:10 +00:00
|
|
|
if err := parseUpdate(&result.Update, o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:46:52 +00:00
|
|
|
// Parse out meta fields. These are in HCL as a list so we need
|
|
|
|
// to iterate over them and merge them.
|
2015-11-09 06:57:39 +00:00
|
|
|
if metaO := listVal.Filter("meta"); len(metaO.Items) > 0 {
|
|
|
|
for _, o := range metaO.Elem().Items {
|
2015-09-15 00:46:52 +00:00
|
|
|
var m map[string]interface{}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
2015-09-15 00:46:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := mapstructure.WeakDecode(m, &result.Meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 22:51:39 +00:00
|
|
|
// If we have tasks outside, create TaskGroups for them
|
2015-11-09 06:57:39 +00:00
|
|
|
if o := listVal.Filter("task"); len(o.Items) > 0 {
|
2015-09-15 00:43:42 +00:00
|
|
|
var tasks []*structs.Task
|
|
|
|
if err := parseTasks(&tasks, o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
result.TaskGroups = make([]*structs.TaskGroup, len(tasks), len(tasks)*2)
|
|
|
|
for i, t := range tasks {
|
|
|
|
result.TaskGroups[i] = &structs.TaskGroup{
|
2015-10-30 22:51:39 +00:00
|
|
|
Name: t.Name,
|
|
|
|
Count: 1,
|
|
|
|
Tasks: []*structs.Task{t},
|
|
|
|
RestartPolicy: structs.NewRestartPolicy(result.Type),
|
2015-09-15 00:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the task groups
|
2015-11-09 06:57:39 +00:00
|
|
|
if o := listVal.Filter("group"); len(o.Items) > 0 {
|
2015-09-15 00:43:42 +00:00
|
|
|
if err := parseGroups(result, o); err != nil {
|
|
|
|
return fmt.Errorf("error parsing 'group': %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
func parseGroups(result *structs.Job, list *ast.ObjectList) error {
|
|
|
|
list = list.Children()
|
|
|
|
if len(list.Items) == 0 {
|
2015-09-15 00:43:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through each object and turn it into an actual result.
|
2015-11-09 06:57:39 +00:00
|
|
|
collection := make([]*structs.TaskGroup, 0, len(list.Items))
|
|
|
|
seen := make(map[string]struct{})
|
|
|
|
for _, item := range list.Items {
|
|
|
|
n := item.Keys[0].Token.Value().(string)
|
|
|
|
|
|
|
|
// Make sure we haven't already found this
|
|
|
|
if _, ok := seen[n]; ok {
|
|
|
|
return fmt.Errorf("group '%s' defined more than once", n)
|
|
|
|
}
|
|
|
|
seen[n] = struct{}{}
|
|
|
|
|
|
|
|
// We need this later
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := item.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("group '%s': should be an object", n)
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
var m map[string]interface{}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := hcl.DecodeObject(&m, item.Val); err != nil {
|
2015-09-15 00:43:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(m, "constraint")
|
|
|
|
delete(m, "meta")
|
|
|
|
delete(m, "task")
|
2015-10-30 22:51:39 +00:00
|
|
|
delete(m, "restart")
|
2015-09-15 00:43:42 +00:00
|
|
|
|
2015-09-20 21:36:37 +00:00
|
|
|
// Default count to 1 if not specified
|
|
|
|
if _, ok := m["count"]; !ok {
|
|
|
|
m["count"] = 1
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
// Build the group with the basic decode
|
|
|
|
var g structs.TaskGroup
|
|
|
|
g.Name = n
|
|
|
|
if err := mapstructure.WeakDecode(m, &g); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse constraints
|
2015-11-09 06:57:39 +00:00
|
|
|
if o := listVal.Filter("constraint"); len(o.Items) > 0 {
|
2015-09-15 00:43:42 +00:00
|
|
|
if err := parseConstraints(&g.Constraints, o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-10-30 23:32:05 +00:00
|
|
|
g.RestartPolicy = structs.NewRestartPolicy(result.Type)
|
2015-09-15 00:43:42 +00:00
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
// Parse restart policy
|
|
|
|
if o := listVal.Filter("restart"); len(o.Items) > 0 {
|
|
|
|
if err := parseRestartPolicy(g.RestartPolicy, o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-30 22:51:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
// Parse out meta fields. These are in HCL as a list so we need
|
|
|
|
// to iterate over them and merge them.
|
2015-11-09 06:57:39 +00:00
|
|
|
if metaO := listVal.Filter("meta"); len(metaO.Items) > 0 {
|
|
|
|
for _, o := range metaO.Elem().Items {
|
2015-09-15 00:43:42 +00:00
|
|
|
var m map[string]interface{}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
2015-09-15 00:43:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := mapstructure.WeakDecode(m, &g.Meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse tasks
|
2015-11-09 06:57:39 +00:00
|
|
|
if o := listVal.Filter("task"); len(o.Items) > 0 {
|
2015-09-15 00:43:42 +00:00
|
|
|
if err := parseTasks(&g.Tasks, o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
collection = append(collection, &g)
|
|
|
|
}
|
|
|
|
|
|
|
|
result.TaskGroups = append(result.TaskGroups, collection...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
func parseRestartPolicy(final *structs.RestartPolicy, list *ast.ObjectList) error {
|
|
|
|
list = list.Elem()
|
|
|
|
if len(list.Items) == 0 {
|
2015-10-30 22:51:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-11-09 06:57:39 +00:00
|
|
|
if len(list.Items) != 1 {
|
|
|
|
return fmt.Errorf("only one 'restart' block allowed")
|
2015-10-30 22:51:39 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
// Get our job object
|
|
|
|
obj := list.Items[0]
|
2015-10-30 22:51:39 +00:00
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, obj.Val); err != nil {
|
|
|
|
return err
|
2015-10-30 22:51:39 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
var result structs.RestartPolicy
|
|
|
|
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: &result,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-30 22:51:39 +00:00
|
|
|
}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := dec.Decode(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*final = result
|
2015-10-30 22:51:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
func parseConstraints(result *[]*structs.Constraint, list *ast.ObjectList) error {
|
|
|
|
for _, o := range list.Elem().Items {
|
2015-09-15 00:43:42 +00:00
|
|
|
var m map[string]interface{}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
2015-09-15 00:43:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
m["LTarget"] = m["attribute"]
|
|
|
|
m["RTarget"] = m["value"]
|
|
|
|
m["Operand"] = m["operator"]
|
|
|
|
|
2015-10-11 19:20:58 +00:00
|
|
|
// If "version" is provided, set the operand
|
|
|
|
// to "version" and the value to the "RTarget"
|
2015-10-26 20:47:56 +00:00
|
|
|
if constraint, ok := m[structs.ConstraintVersion]; ok {
|
|
|
|
m["Operand"] = structs.ConstraintVersion
|
2015-10-11 19:20:58 +00:00
|
|
|
m["RTarget"] = constraint
|
2015-10-11 19:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If "regexp" is provided, set the operand
|
|
|
|
// to "regexp" and the value to the "RTarget"
|
2015-10-26 20:47:56 +00:00
|
|
|
if constraint, ok := m[structs.ConstraintRegex]; ok {
|
|
|
|
m["Operand"] = structs.ConstraintRegex
|
2015-10-11 19:37:50 +00:00
|
|
|
m["RTarget"] = constraint
|
2015-10-11 19:20:58 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 20:47:56 +00:00
|
|
|
if value, ok := m[structs.ConstraintDistinctHosts]; ok {
|
2015-10-22 23:37:20 +00:00
|
|
|
enabled, err := strconv.ParseBool(value.(string))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is not enabled, skip the constraint.
|
|
|
|
if !enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-26 20:47:56 +00:00
|
|
|
m["Operand"] = structs.ConstraintDistinctHosts
|
2015-10-22 23:37:20 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
// Build the constraint
|
|
|
|
var c structs.Constraint
|
|
|
|
if err := mapstructure.WeakDecode(m, &c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.Operand == "" {
|
|
|
|
c.Operand = "="
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = append(*result, &c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
func parseTasks(result *[]*structs.Task, list *ast.ObjectList) error {
|
|
|
|
list = list.Children()
|
|
|
|
if len(list.Items) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-15 00:43:42 +00:00
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
// Go through each object and turn it into an actual result.
|
|
|
|
seen := make(map[string]struct{})
|
|
|
|
for _, item := range list.Items {
|
|
|
|
n := item.Keys[0].Token.Value().(string)
|
|
|
|
|
|
|
|
// Make sure we haven't already found this
|
|
|
|
if _, ok := seen[n]; ok {
|
|
|
|
return fmt.Errorf("task '%s' defined more than once", n)
|
2015-09-15 00:43:42 +00:00
|
|
|
}
|
2015-11-09 06:57:39 +00:00
|
|
|
seen[n] = struct{}{}
|
2015-09-15 00:43:42 +00:00
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
// We need this later
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := item.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("group '%s': should be an object", n)
|
|
|
|
}
|
2015-09-15 00:43:42 +00:00
|
|
|
|
|
|
|
var m map[string]interface{}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := hcl.DecodeObject(&m, item.Val); err != nil {
|
2015-09-15 00:43:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(m, "config")
|
2015-09-30 16:18:43 +00:00
|
|
|
delete(m, "env")
|
2015-09-15 00:50:34 +00:00
|
|
|
delete(m, "constraint")
|
2015-09-15 00:43:42 +00:00
|
|
|
delete(m, "meta")
|
|
|
|
delete(m, "resources")
|
|
|
|
|
|
|
|
// Build the task
|
|
|
|
var t structs.Task
|
2015-11-09 06:57:39 +00:00
|
|
|
t.Name = n
|
2015-09-15 00:43:42 +00:00
|
|
|
if err := mapstructure.WeakDecode(m, &t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-30 16:18:43 +00:00
|
|
|
// If we have env, then parse them
|
2015-11-09 06:57:39 +00:00
|
|
|
if o := listVal.Filter("env"); len(o.Items) > 0 {
|
|
|
|
for _, o := range o.Elem().Items {
|
2015-09-30 16:18:43 +00:00
|
|
|
var m map[string]interface{}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
2015-09-30 16:18:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := mapstructure.WeakDecode(m, &t.Env); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
// If we have config, then parse that
|
2015-11-09 06:57:39 +00:00
|
|
|
if o := listVal.Filter("config"); len(o.Items) > 0 {
|
|
|
|
for _, o := range o.Elem().Items {
|
2015-09-15 00:43:42 +00:00
|
|
|
var m map[string]interface{}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
2015-09-15 00:43:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := mapstructure.WeakDecode(m, &t.Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:50:34 +00:00
|
|
|
// Parse constraints
|
2015-11-09 06:57:39 +00:00
|
|
|
if o := listVal.Filter("constraint"); len(o.Items) > 0 {
|
2015-09-15 00:50:34 +00:00
|
|
|
if err := parseConstraints(&t.Constraints, o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
// Parse out meta fields. These are in HCL as a list so we need
|
|
|
|
// to iterate over them and merge them.
|
2015-11-09 06:57:39 +00:00
|
|
|
if metaO := listVal.Filter("meta"); len(metaO.Items) > 0 {
|
|
|
|
for _, o := range metaO.Elem().Items {
|
2015-09-15 00:43:42 +00:00
|
|
|
var m map[string]interface{}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
2015-09-15 00:43:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := mapstructure.WeakDecode(m, &t.Meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have resources, then parse that
|
2015-11-09 06:57:39 +00:00
|
|
|
if o := listVal.Filter("resources"); len(o.Items) > 0 {
|
2015-09-15 00:43:42 +00:00
|
|
|
var r structs.Resources
|
2015-09-15 01:27:37 +00:00
|
|
|
if err := parseResources(&r, o); err != nil {
|
2015-09-15 01:30:26 +00:00
|
|
|
return fmt.Errorf("task '%s': %s", t.Name, err)
|
2015-09-15 00:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Resources = &r
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = append(*result, &t)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-15 01:27:37 +00:00
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
func parseResources(result *structs.Resources, list *ast.ObjectList) error {
|
|
|
|
list = list.Elem()
|
|
|
|
if len(list.Items) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(list.Items) > 1 {
|
2015-09-15 01:30:26 +00:00
|
|
|
return fmt.Errorf("only one 'resource' block allowed per task")
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
// Get our resource object
|
|
|
|
o := list.Items[0]
|
|
|
|
|
|
|
|
// We need this later
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := o.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("resource: should be an object")
|
|
|
|
}
|
|
|
|
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(m, "network")
|
|
|
|
|
|
|
|
if err := mapstructure.WeakDecode(m, result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the network resources
|
|
|
|
if o := listVal.Filter("network"); len(o.Items) > 0 {
|
|
|
|
if len(o.Items) > 1 {
|
|
|
|
return fmt.Errorf("only one 'network' resource allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
var r structs.NetworkResource
|
2015-09-15 01:27:37 +00:00
|
|
|
var m map[string]interface{}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := hcl.DecodeObject(&m, o.Items[0].Val); err != nil {
|
2015-09-15 01:27:37 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-11-09 06:57:39 +00:00
|
|
|
if err := mapstructure.WeakDecode(m, &r); err != nil {
|
2015-09-15 01:27:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
// Keep track of labels we've already seen so we can ensure there
|
|
|
|
// are no collisions when we turn them into environment variables.
|
|
|
|
// lowercase:NomalCase so we can get the first for the error message
|
|
|
|
seenLabel := map[string]string{}
|
|
|
|
for _, label := range r.DynamicPorts {
|
|
|
|
if !reDynamicPorts.MatchString(label) {
|
|
|
|
return errDynamicPorts
|
2015-09-15 01:30:26 +00:00
|
|
|
}
|
2015-11-09 06:57:39 +00:00
|
|
|
first, seen := seenLabel[strings.ToLower(label)]
|
|
|
|
if seen {
|
|
|
|
return fmt.Errorf("Found a port label collision: `%s` overlaps with previous `%s`", label, first)
|
|
|
|
} else {
|
|
|
|
seenLabel[strings.ToLower(label)] = label
|
2015-09-15 01:27:37 +00:00
|
|
|
}
|
2015-09-26 01:59:17 +00:00
|
|
|
|
2015-09-15 01:27:37 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
result.Networks = []*structs.NetworkResource{&r}
|
2015-09-15 01:27:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-20 21:18:10 +00:00
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
func parseUpdate(result *structs.UpdateStrategy, list *ast.ObjectList) error {
|
|
|
|
list = list.Elem()
|
|
|
|
if len(list.Items) > 1 {
|
2015-09-20 21:18:10 +00:00
|
|
|
return fmt.Errorf("only one 'update' block allowed per job")
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
// Get our resource object
|
|
|
|
o := list.Items[0]
|
2015-09-20 21:18:10 +00:00
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
|
|
|
return err
|
2015-09-20 21:18:10 +00:00
|
|
|
}
|
2015-10-30 22:51:39 +00:00
|
|
|
|
2015-11-09 06:57:39 +00:00
|
|
|
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: result,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-30 22:51:39 +00:00
|
|
|
}
|
2015-11-09 06:57:39 +00:00
|
|
|
return dec.Decode(m)
|
2015-10-30 22:51:39 +00:00
|
|
|
}
|