Switch from string to artifact struct

This commit is contained in:
Alex Dadgar 2016-03-14 11:13:43 -07:00
parent 315fd954ea
commit 277a91336d
4 changed files with 57 additions and 21 deletions

View File

@ -611,7 +611,7 @@ func parseTasks(jobName string, taskGroupName string, result *[]*structs.Task, l
return nil
}
func parseArtifacts(result *[]string, list *ast.ObjectList) error {
func parseArtifacts(result *[]*structs.TaskArtifact, list *ast.ObjectList) error {
for _, o := range list.Elem().Items {
// Check for invalid keys
valid := []string{
@ -627,18 +627,11 @@ func parseArtifacts(result *[]string, list *ast.ObjectList) error {
return err
}
baseI, ok := m["source"]
if !ok {
return fmt.Errorf("artifact source must be supplied")
}
base, ok := baseI.(string)
if !ok {
return fmt.Errorf("artifact source must be a string. Got %v", baseI)
}
delete(m, "options")
base = strings.TrimSpace(base)
if base == "" {
return fmt.Errorf("artifact source can not be empty")
var ta structs.TaskArtifact
if err := mapstructure.WeakDecode(m, &ta); err != nil {
return err
}
var optionList *ast.ObjectList
@ -648,24 +641,19 @@ func parseArtifacts(result *[]string, list *ast.ObjectList) error {
return fmt.Errorf("artifact should be an object")
}
options := make(map[string]string)
if oo := optionList.Filter("options"); len(oo.Items) > 0 {
if err := parseArtifactOption(options, oo); err != nil {
if err := parseArtifactOption(&ta.GetterOptions, oo); err != nil {
return multierror.Prefix(err, "options: ")
}
}
for k, v := range options {
base = fmt.Sprintf("%s?%s=%s", base, k, v)
}
*result = append(*result, base)
*result = append(*result, &ta)
}
return nil
}
func parseArtifactOption(options map[string]string, list *ast.ObjectList) error {
func parseArtifactOption(result **structs.GetterOptions, list *ast.ObjectList) error {
list = list.Elem()
if len(list.Items) > 1 {
return fmt.Errorf("only one 'options' block allowed per artifact")
@ -687,10 +675,12 @@ func parseArtifactOption(options map[string]string, list *ast.ObjectList) error
return err
}
var options structs.GetterOptions
if err := mapstructure.WeakDecode(m, &options); err != nil {
return err
}
*result = &options
return nil
}

View File

@ -128,6 +128,20 @@ func TestParse(t *testing.T) {
MaxFiles: 10,
MaxFileSizeMB: 100,
},
Artifacts: []*structs.TaskArtifact{
{
GetterSource: "http://foo.com/artifact",
GetterOptions: &structs.GetterOptions{
Checksum: "md5:b8a4f3f72ecab0510a6a31e997461c5f",
},
},
{
GetterSource: "http://bar.com/artifact",
GetterOptions: &structs.GetterOptions{
Checksum: "md5:ff1cc0d3432dad54d607c1505fb7245c",
},
},
},
},
&structs.Task{
Name: "storagelocker",

View File

@ -82,6 +82,20 @@ job "binstore-storagelocker" {
}
kill_timeout = "22s"
artifact {
source = "http://foo.com/artifact"
options {
checksum = "md5:b8a4f3f72ecab0510a6a31e997461c5f"
}
}
artifact {
source = "http://bar.com/artifact"
options {
checksum = "md5:ff1cc0d3432dad54d607c1505fb7245c"
}
}
}
task "storagelocker" {

View File

@ -1606,7 +1606,25 @@ type Task struct {
// Artifacts is a list of artifacts to download and extract before running
// the task.
Artifacts []string
Artifacts []*TaskArtifact
}
// TaskArtifact is an artifact to download before running the task.
type TaskArtifact struct {
// GetterSource is the source to download an artifact using go-getter
GetterSource string `mapstructure:"source"`
// GetterOptions are options to use when downloading the artifact using
// go-getter.
GetterOptions *GetterOptions `mapstructure:"options"`
}
// GetterOptions are options to apply when downloading an artifact using
// go-getter
type GetterOptions struct {
// Checksum is the checksum to use to validate the downloaded artifact. It
// is given as 'type:value' such as 'md5:1a2b...'
Checksum string `mapstructure:"checksum"`
}
func (t *Task) Copy() *Task {