2019-03-07 13:42:32 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-03-07 17:01:34 +00:00
|
|
|
"regexp"
|
2019-03-07 13:42:32 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sorintlab/agola/internal/common"
|
2019-03-07 17:01:34 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
2019-03-07 13:42:32 +00:00
|
|
|
"github.com/sorintlab/agola/internal/util"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-04-15 21:42:10 +00:00
|
|
|
maxRunNameLength = 100
|
|
|
|
maxTaskNameLength = 100
|
|
|
|
maxStepNameLength = 100
|
2019-03-07 13:42:32 +00:00
|
|
|
)
|
|
|
|
|
2019-03-07 17:01:34 +00:00
|
|
|
var (
|
|
|
|
regExpDelimiters = []string{"/", "#"}
|
|
|
|
)
|
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
type Config struct {
|
2019-04-15 21:42:10 +00:00
|
|
|
Runtimes map[string]*Runtime `yaml:"runtimes"`
|
|
|
|
Tasks map[string]*Task `yaml:"tasks"`
|
|
|
|
Runs map[string]*Run `yaml:"runs"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Task struct {
|
2019-04-10 15:12:39 +00:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Runtime string `yaml:"runtime"`
|
|
|
|
Environment map[string]Value `yaml:"environment,omitempty"`
|
|
|
|
WorkingDir string `yaml:"working_dir"`
|
|
|
|
Shell string `yaml:"shell"`
|
|
|
|
User string `yaml:"user"`
|
|
|
|
Steps []interface{} `yaml:"steps"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RuntimeType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
RuntimeTypePod RuntimeType = "pod"
|
|
|
|
)
|
|
|
|
|
2019-04-10 15:13:51 +00:00
|
|
|
type RegistryAuthType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
RegistryAuthTypeDefault RegistryAuthType = "default"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RegistryAuth struct {
|
|
|
|
Type RegistryAuthType `yaml:"type"`
|
|
|
|
|
|
|
|
// default auth
|
|
|
|
Username Value `yaml:"username"`
|
|
|
|
Password Value `yaml:"password"`
|
|
|
|
}
|
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
type Runtime struct {
|
2019-04-10 15:13:51 +00:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Type RuntimeType `yaml:"type,omitempty"`
|
|
|
|
Auth *RegistryAuth `yaml:"auth"`
|
|
|
|
Arch common.Arch `yaml:"arch,omitempty"`
|
|
|
|
Containers []*Container `yaml:"containers,omitempty"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Container struct {
|
2019-04-10 15:12:39 +00:00
|
|
|
Image string `yaml:"image,omitempty"`
|
2019-04-10 15:13:51 +00:00
|
|
|
Auth *RegistryAuth `yaml:"auth"`
|
2019-04-10 15:12:39 +00:00
|
|
|
Environment map[string]Value `yaml:"environment,omitempty"`
|
|
|
|
User string `yaml:"user"`
|
|
|
|
Privileged bool `yaml:"privileged"`
|
|
|
|
Entrypoint string `yaml:"entrypoint"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:42:10 +00:00
|
|
|
type Run struct {
|
2019-03-07 13:42:32 +00:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Elements map[string]*Element `yaml:"elements"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Element struct {
|
2019-03-07 17:01:34 +00:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Task string `yaml:"task"`
|
|
|
|
Depends []*Depend `yaml:"depends"`
|
|
|
|
IgnoreFailure bool `yaml:"ignore_failure"`
|
|
|
|
Approval bool `yaml:"approval"`
|
|
|
|
When *types.When `yaml:"when"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DependCondition string
|
|
|
|
|
|
|
|
const (
|
|
|
|
DependConditionOnSuccess DependCondition = "on_success"
|
|
|
|
DependConditionOnFailure DependCondition = "on_failure"
|
2019-04-12 14:46:04 +00:00
|
|
|
DependConditionOnSkipped DependCondition = "on_skipped"
|
2019-03-07 13:42:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Depend struct {
|
|
|
|
ElementName string `yaml:"name"`
|
2019-03-07 17:01:34 +00:00
|
|
|
Conditions []DependCondition `yaml:"conditions"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Step struct {
|
|
|
|
Type string `yaml:"type"`
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CloneStep struct {
|
|
|
|
Step `yaml:",inline"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunStep struct {
|
|
|
|
Step `yaml:",inline"`
|
2019-04-10 15:12:39 +00:00
|
|
|
Command string `yaml:"command"`
|
|
|
|
Environment map[string]Value `yaml:"environment,omitempty"`
|
|
|
|
WorkingDir string `yaml:"working_dir"`
|
|
|
|
Shell string `yaml:"shell"`
|
|
|
|
User string `yaml:"user"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 15:12:39 +00:00
|
|
|
type ValueType int
|
2019-03-18 14:02:32 +00:00
|
|
|
|
|
|
|
const (
|
2019-04-10 15:12:39 +00:00
|
|
|
ValueTypeString ValueType = iota
|
|
|
|
ValueTypeFromVariable
|
2019-03-18 14:02:32 +00:00
|
|
|
)
|
|
|
|
|
2019-04-10 15:12:39 +00:00
|
|
|
type Value struct {
|
|
|
|
Type ValueType
|
2019-03-18 14:02:32 +00:00
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2019-04-13 12:58:56 +00:00
|
|
|
type SaveContent struct {
|
2019-03-07 13:42:32 +00:00
|
|
|
SourceDir string `yaml:"source_dir"`
|
|
|
|
DestDir string `yaml:"dest_dir"`
|
|
|
|
Paths []string `yaml:"paths"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SaveToWorkspaceStep struct {
|
|
|
|
Step `yaml:",inline"`
|
2019-04-13 12:58:56 +00:00
|
|
|
Contents []*SaveContent `yaml:"contents"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RestoreWorkspaceStep struct {
|
|
|
|
Step `yaml:",inline"`
|
|
|
|
DestDir string `yaml:"dest_dir"`
|
|
|
|
}
|
|
|
|
|
2019-04-13 12:58:56 +00:00
|
|
|
type SaveCacheStep struct {
|
|
|
|
Step `yaml:",inline"`
|
|
|
|
Key string `yaml:"key"`
|
|
|
|
Contents []*SaveContent `yaml:"contents"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RestoreCacheStep struct {
|
|
|
|
Step `yaml:",inline"`
|
|
|
|
Keys []string `yaml:"keys"`
|
|
|
|
DestDir string `yaml:"dest_dir"`
|
|
|
|
}
|
|
|
|
|
2019-03-18 14:02:32 +00:00
|
|
|
func (t *Task) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
type task struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Runtime string `yaml:"runtime"`
|
2019-04-10 15:12:39 +00:00
|
|
|
Environment map[string]Value `yaml:"environment,omitempty"`
|
2019-03-18 14:02:32 +00:00
|
|
|
WorkingDir string `yaml:"working_dir"`
|
|
|
|
Shell string `yaml:"shell"`
|
|
|
|
User string `yaml:"user"`
|
|
|
|
Steps []map[string]interface{} `yaml:"steps"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var tt *task
|
|
|
|
if err := unmarshal(&tt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Name = tt.Name
|
|
|
|
t.Runtime = tt.Runtime
|
2019-04-10 15:12:39 +00:00
|
|
|
t.Environment = tt.Environment
|
2019-03-18 14:02:32 +00:00
|
|
|
t.WorkingDir = tt.WorkingDir
|
|
|
|
t.Shell = tt.Shell
|
|
|
|
t.User = tt.User
|
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
steps := make([]interface{}, len(tt.Steps))
|
2019-03-18 14:02:32 +00:00
|
|
|
for i, stepEntry := range tt.Steps {
|
2019-03-07 13:42:32 +00:00
|
|
|
if len(stepEntry) > 1 {
|
|
|
|
return errors.Errorf("wrong steps description at index %d: more than one step name per list entry", i)
|
|
|
|
}
|
|
|
|
for stepType, stepSpec := range stepEntry {
|
|
|
|
o, err := yaml.Marshal(stepSpec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch stepType {
|
|
|
|
case "clone":
|
2019-04-13 12:58:56 +00:00
|
|
|
var s CloneStep
|
|
|
|
s.Type = stepType
|
|
|
|
steps[i] = &s
|
2019-03-07 13:42:32 +00:00
|
|
|
|
|
|
|
case "run":
|
2019-04-13 12:58:56 +00:00
|
|
|
var s RunStep
|
2019-03-07 13:42:32 +00:00
|
|
|
switch stepSpec.(type) {
|
|
|
|
case string:
|
2019-04-13 12:58:56 +00:00
|
|
|
s.Command = stepSpec.(string)
|
2019-03-07 13:42:32 +00:00
|
|
|
default:
|
2019-04-13 12:58:56 +00:00
|
|
|
if err := yaml.Unmarshal(o, &s); err != nil {
|
2019-03-07 13:42:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-04-13 12:58:56 +00:00
|
|
|
s.Type = stepType
|
|
|
|
steps[i] = &s
|
2019-03-07 13:42:32 +00:00
|
|
|
|
|
|
|
case "save_to_workspace":
|
2019-04-13 12:58:56 +00:00
|
|
|
var s SaveToWorkspaceStep
|
|
|
|
if err := yaml.Unmarshal(o, &s); err != nil {
|
2019-03-07 13:42:32 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-04-13 12:58:56 +00:00
|
|
|
s.Type = stepType
|
|
|
|
steps[i] = &s
|
2019-03-07 13:42:32 +00:00
|
|
|
|
|
|
|
case "restore_workspace":
|
2019-04-13 12:58:56 +00:00
|
|
|
var s RestoreWorkspaceStep
|
|
|
|
if err := yaml.Unmarshal(o, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Type = stepType
|
|
|
|
steps[i] = &s
|
|
|
|
|
|
|
|
case "save_cache":
|
|
|
|
var s SaveCacheStep
|
|
|
|
if err := yaml.Unmarshal(o, &s); err != nil {
|
2019-03-07 13:42:32 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-04-13 12:58:56 +00:00
|
|
|
s.Type = stepType
|
|
|
|
steps[i] = &s
|
|
|
|
|
|
|
|
case "restore_cache":
|
|
|
|
var s RestoreCacheStep
|
|
|
|
if err := yaml.Unmarshal(o, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Type = stepType
|
|
|
|
steps[i] = &s
|
2019-03-07 13:42:32 +00:00
|
|
|
default:
|
|
|
|
return errors.Errorf("unknown step type: %s", stepType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Steps = steps
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Element) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
2019-03-07 17:01:34 +00:00
|
|
|
type when struct {
|
|
|
|
Branch interface{} `yaml:"branch"`
|
|
|
|
Tag interface{} `yaml:"tag"`
|
|
|
|
Ref interface{} `yaml:"ref"`
|
|
|
|
}
|
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
type element struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Task string `yaml:"task"`
|
|
|
|
Depends []interface{} `yaml:"depends"`
|
|
|
|
IgnoreFailure bool `yaml:"ignore_failure"`
|
2019-04-08 15:29:57 +00:00
|
|
|
Approval bool `yaml:"approval"`
|
2019-03-07 17:01:34 +00:00
|
|
|
When *when `yaml:"when"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
2019-03-07 17:01:34 +00:00
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
var te *element
|
|
|
|
|
|
|
|
if err := unmarshal(&te); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Name = te.Name
|
|
|
|
e.Task = te.Task
|
|
|
|
e.IgnoreFailure = te.IgnoreFailure
|
2019-04-08 15:29:57 +00:00
|
|
|
e.Approval = te.Approval
|
2019-03-07 13:42:32 +00:00
|
|
|
|
|
|
|
depends := make([]*Depend, len(te.Depends))
|
|
|
|
for i, dependEntry := range te.Depends {
|
|
|
|
var depend *Depend
|
|
|
|
switch dependEntry.(type) {
|
|
|
|
case string:
|
|
|
|
depend = &Depend{
|
|
|
|
ElementName: dependEntry.(string),
|
|
|
|
}
|
|
|
|
case map[interface{}]interface{}:
|
|
|
|
type deplist map[string][]DependCondition
|
|
|
|
var dl deplist
|
|
|
|
o, err := yaml.Marshal(dependEntry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := yaml.Unmarshal(o, &dl); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(dl) != 1 {
|
|
|
|
return errors.Errorf("unsupported depend format. Must be a string or a list")
|
|
|
|
}
|
|
|
|
for k, v := range dl {
|
|
|
|
depend = &Depend{
|
|
|
|
ElementName: k,
|
|
|
|
Conditions: v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return errors.Errorf("unsupported depend format. Must be a string or a list")
|
|
|
|
}
|
|
|
|
depends[i] = depend
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Depends = depends
|
|
|
|
|
2019-03-07 17:01:34 +00:00
|
|
|
if te.When != nil {
|
|
|
|
w := &types.When{}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if te.When.Branch != nil {
|
|
|
|
w.Branch, err = parseWhenConditions(te.When.Branch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if te.When.Tag != nil {
|
|
|
|
w.Tag, err = parseWhenConditions(te.When.Tag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if te.When.Ref != nil {
|
|
|
|
w.Ref, err = parseWhenConditions(te.When.Ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e.When = w
|
|
|
|
}
|
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:12:39 +00:00
|
|
|
func (val *Value) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var ival interface{}
|
|
|
|
if err := unmarshal(&ival); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch valValue := ival.(type) {
|
|
|
|
case string:
|
|
|
|
val.Type = ValueTypeString
|
|
|
|
val.Value = valValue
|
|
|
|
case map[interface{}]interface{}:
|
|
|
|
for k, v := range valValue {
|
|
|
|
if k == "from_variable" {
|
|
|
|
switch v.(type) {
|
|
|
|
case string:
|
|
|
|
default:
|
|
|
|
return errors.Errorf("unknown value format: %v", v)
|
2019-03-18 14:02:32 +00:00
|
|
|
}
|
2019-04-10 15:12:39 +00:00
|
|
|
val.Type = ValueTypeFromVariable
|
|
|
|
val.Value = v.(string)
|
2019-03-18 14:02:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-10 15:12:39 +00:00
|
|
|
default:
|
|
|
|
return errors.Errorf("unknown value format: %v", ival)
|
2019-03-18 14:02:32 +00:00
|
|
|
}
|
2019-04-10 15:12:39 +00:00
|
|
|
return nil
|
2019-03-18 14:02:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 17:01:34 +00:00
|
|
|
func parseWhenConditions(wi interface{}) (*types.WhenConditions, error) {
|
|
|
|
w := &types.WhenConditions{}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
include := []string{}
|
|
|
|
exclude := []string{}
|
|
|
|
|
|
|
|
switch c := wi.(type) {
|
|
|
|
case string:
|
|
|
|
include = []string{c}
|
|
|
|
case []interface{}:
|
|
|
|
ss, err := parseSliceString(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
include = ss
|
|
|
|
case map[interface{}]interface{}:
|
|
|
|
for k, v := range c {
|
|
|
|
ks, ok := k.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf(`expected one of "include" or "exclude", got %s`, ks)
|
|
|
|
}
|
|
|
|
switch ks {
|
|
|
|
case "include":
|
|
|
|
include, err = parseStringOrSlice(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case "exclude":
|
|
|
|
exclude, err = parseStringOrSlice(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf(`expected one of "include" or "exclude", got %s`, ks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("wrong when format")
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Include, err = parseWhenConditionSlice(include)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
w.Exclude, err = parseWhenConditionSlice(exclude)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseWhenConditionSlice(conds []string) ([]types.WhenCondition, error) {
|
|
|
|
if len(conds) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
wcs := []types.WhenCondition{}
|
|
|
|
for _, cond := range conds {
|
|
|
|
wc, err := parseWhenCondition(cond)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wcs = append(wcs, *wc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return wcs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseWhenCondition(s string) (*types.WhenCondition, error) {
|
|
|
|
isRegExp := false
|
|
|
|
if len(s) > 2 {
|
|
|
|
for _, d := range regExpDelimiters {
|
|
|
|
if strings.HasPrefix(s, d) && strings.HasSuffix(s, d) {
|
|
|
|
isRegExp = true
|
|
|
|
s = s[1 : len(s)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wc := &types.WhenCondition{Match: s}
|
|
|
|
|
|
|
|
if isRegExp {
|
|
|
|
if _, err := regexp.Compile(s); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "wrong regular expression")
|
|
|
|
}
|
|
|
|
wc.Type = types.WhenConditionTypeRegExp
|
|
|
|
} else {
|
|
|
|
wc.Type = types.WhenConditionTypeSimple
|
|
|
|
}
|
|
|
|
return wc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseStringOrSlice(si interface{}) ([]string, error) {
|
|
|
|
ss := []string{}
|
|
|
|
switch c := si.(type) {
|
|
|
|
case string:
|
|
|
|
ss = []string{c}
|
|
|
|
case []interface{}:
|
|
|
|
var err error
|
|
|
|
ss, err = parseSliceString(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSliceString(si []interface{}) ([]string, error) {
|
|
|
|
ss := []string{}
|
|
|
|
for _, v := range si {
|
|
|
|
switch s := v.(type) {
|
|
|
|
case string:
|
|
|
|
ss = append(ss, s)
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("expected string")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
func (c *Config) Runtime(runtimeName string) *Runtime {
|
|
|
|
for n, r := range c.Runtimes {
|
|
|
|
if n == runtimeName {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("runtime %q doesn't exists", runtimeName))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) Task(taskName string) *Task {
|
|
|
|
for n, t := range c.Tasks {
|
|
|
|
if n == taskName {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("task %q doesn't exists", taskName))
|
|
|
|
}
|
|
|
|
|
2019-04-15 21:42:10 +00:00
|
|
|
func (c *Config) Run(runName string) *Run {
|
|
|
|
for n, p := range c.Runs {
|
|
|
|
if n == runName {
|
2019-03-07 13:42:32 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 21:42:10 +00:00
|
|
|
panic(fmt.Sprintf("run %q doesn't exists", runName))
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultConfig = Config{}
|
|
|
|
|
|
|
|
func ParseConfig(configData []byte) (*Config, error) {
|
|
|
|
config := DefaultConfig
|
|
|
|
if err := yaml.Unmarshal(configData, &config); err != nil {
|
2019-03-14 13:36:18 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to unmarshal config")
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:42:10 +00:00
|
|
|
if len(config.Runs) == 0 {
|
|
|
|
return nil, errors.Errorf("no runs defined")
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set names from maps keys
|
|
|
|
for n, runtime := range config.Runtimes {
|
|
|
|
if runtime == nil {
|
|
|
|
return nil, errors.Errorf("runtime %q is empty", n)
|
|
|
|
}
|
|
|
|
runtime.Name = n
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, task := range config.Tasks {
|
|
|
|
if task == nil {
|
|
|
|
return nil, errors.Errorf("task %q is empty", n)
|
|
|
|
}
|
|
|
|
task.Name = n
|
|
|
|
}
|
|
|
|
|
2019-04-15 21:42:10 +00:00
|
|
|
for n, run := range config.Runs {
|
|
|
|
if run == nil {
|
|
|
|
return nil, errors.Errorf("run %q is empty", n)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
2019-04-15 21:42:10 +00:00
|
|
|
run.Name = n
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:42:10 +00:00
|
|
|
for _, run := range config.Runs {
|
|
|
|
for n, element := range run.Elements {
|
2019-03-07 13:42:32 +00:00
|
|
|
if element == nil {
|
2019-04-15 21:42:10 +00:00
|
|
|
return nil, errors.Errorf("run %q: element %q is empty", run.Name, n)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
element.Name = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:13:51 +00:00
|
|
|
// Set auth type to default if not specified
|
|
|
|
for _, runtime := range config.Runtimes {
|
|
|
|
if runtime.Auth != nil {
|
|
|
|
if runtime.Auth.Type == "" {
|
|
|
|
runtime.Auth.Type = RegistryAuthTypeDefault
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, container := range runtime.Containers {
|
|
|
|
if container.Auth != nil {
|
|
|
|
if container.Auth.Type == "" {
|
|
|
|
container.Auth.Type = RegistryAuthTypeDefault
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 12:58:56 +00:00
|
|
|
// set steps defaults
|
|
|
|
for _, t := range config.Tasks {
|
|
|
|
for _, s := range t.Steps {
|
|
|
|
switch step := s.(type) {
|
|
|
|
// TODO(sgotti) we could use the run step command as step name but when the
|
|
|
|
// command is very long or multi line it doesn't makes sense and will
|
|
|
|
// probably be quite unuseful/confusing from an UI point of view
|
|
|
|
case *SaveCacheStep:
|
|
|
|
for _, content := range step.Contents {
|
|
|
|
if len(content.Paths) == 0 {
|
|
|
|
// default to all files inside the sourceDir
|
|
|
|
content.Paths = []string{"**"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Infof("s: %s", util.Dump(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
return &config, checkConfig(&config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkConfig(config *Config) error {
|
|
|
|
// check broken dependencies
|
2019-04-15 21:42:10 +00:00
|
|
|
for _, run := range config.Runs {
|
2019-03-07 13:42:32 +00:00
|
|
|
// collect all task names
|
|
|
|
allElements := map[string]struct{}{}
|
2019-04-15 21:42:10 +00:00
|
|
|
for _, element := range run.Elements {
|
2019-03-07 13:42:32 +00:00
|
|
|
allElements[element.Name] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2019-04-15 21:42:10 +00:00
|
|
|
for _, element := range run.Elements {
|
2019-03-07 13:42:32 +00:00
|
|
|
for _, dep := range element.Depends {
|
|
|
|
if _, ok := allElements[dep.ElementName]; !ok {
|
2019-04-15 21:42:10 +00:00
|
|
|
return errors.Errorf("run element %q needed by element %q doesn't exist", dep.ElementName, element.Name)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check circular dependencies
|
2019-04-15 21:42:10 +00:00
|
|
|
for _, run := range config.Runs {
|
2019-03-07 13:42:32 +00:00
|
|
|
cerrs := &util.Errors{}
|
2019-04-15 21:42:10 +00:00
|
|
|
for _, element := range run.Elements {
|
|
|
|
allParents := getAllElementParents(run, element)
|
2019-03-07 13:42:32 +00:00
|
|
|
for _, parent := range allParents {
|
|
|
|
if parent.Name == element.Name {
|
|
|
|
// TODO(sgotti) get the parent that depends on task to report it
|
|
|
|
dep := []string{}
|
|
|
|
for _, parent := range allParents {
|
2019-04-15 21:42:10 +00:00
|
|
|
pparents := getElementParents(run, parent)
|
2019-03-07 13:42:32 +00:00
|
|
|
for _, pparent := range pparents {
|
|
|
|
if pparent.Name == element.Name {
|
|
|
|
dep = append(dep, fmt.Sprintf("%q", parent.Name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cerrs.Append(errors.Errorf("circular dependency between element %q and elements %s", element.Name, strings.Join(dep, " ")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cerrs.IsErr() {
|
|
|
|
return cerrs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that the task and its parent don't have a common dependency
|
2019-04-15 21:42:10 +00:00
|
|
|
for _, run := range config.Runs {
|
|
|
|
for _, element := range run.Elements {
|
|
|
|
parents := getElementParents(run, element)
|
2019-03-07 13:42:32 +00:00
|
|
|
for _, parent := range parents {
|
2019-04-15 21:42:10 +00:00
|
|
|
allParents := getAllElementParents(run, element)
|
|
|
|
allParentParents := getAllElementParents(run, parent)
|
2019-03-07 13:42:32 +00:00
|
|
|
for _, p := range allParents {
|
|
|
|
for _, pp := range allParentParents {
|
|
|
|
if p.Name == pp.Name {
|
|
|
|
return errors.Errorf("element %s and its dependency %s have both a dependency on element %s", element.Name, parent.Name, p.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range config.Runtimes {
|
|
|
|
if r.Type != RuntimeTypePod {
|
|
|
|
return errors.Errorf("runtime %q: wrong type %q", r.Name, r.Type)
|
|
|
|
}
|
|
|
|
if len(r.Containers) == 0 {
|
|
|
|
return errors.Errorf("runtime %q: at least one container must be defined", r.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range config.Tasks {
|
|
|
|
if len(t.Name) > maxTaskNameLength {
|
|
|
|
return errors.Errorf("task name %q too long", t.Name)
|
|
|
|
}
|
|
|
|
if t.Runtime == "" {
|
|
|
|
return errors.Errorf("task %q: undefined runtime", t.Name)
|
|
|
|
}
|
|
|
|
if _, ok := config.Runtimes[t.Runtime]; !ok {
|
|
|
|
return errors.Errorf("runtime %q needed by task %q doesn't exist", t.Runtime, t.Name)
|
|
|
|
}
|
|
|
|
for i, s := range t.Steps {
|
|
|
|
switch step := s.(type) {
|
|
|
|
// TODO(sgotti) we could use the run step command as step name but when the
|
|
|
|
// command is very long or multi line it doesn't makes sense and will
|
|
|
|
// probably be quite unuseful/confusing from an UI point of view
|
|
|
|
case *RunStep:
|
|
|
|
if step.Name == "" {
|
|
|
|
lines, err := util.CountLines(step.Command)
|
|
|
|
// if we failed to count the lines (shouldn't happen) or the number of lines is > 1 then a name is requred
|
|
|
|
if err != nil || lines > 1 {
|
|
|
|
return errors.Errorf("missing step name for step %d in task %q, required since command is more than one line", i, t.Name)
|
|
|
|
}
|
|
|
|
len := len(step.Command)
|
|
|
|
if len > maxStepNameLength {
|
|
|
|
len = maxStepNameLength
|
|
|
|
}
|
|
|
|
step.Name = step.Command[:len]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 21:42:10 +00:00
|
|
|
for _, run := range config.Runs {
|
|
|
|
if len(run.Name) > maxRunNameLength {
|
|
|
|
return errors.Errorf("run name %q too long", run.Name)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
2019-04-15 21:42:10 +00:00
|
|
|
for _, element := range run.Elements {
|
2019-03-07 13:42:32 +00:00
|
|
|
// check missing tasks reference
|
|
|
|
if element.Task == "" {
|
2019-04-15 21:42:10 +00:00
|
|
|
return errors.Errorf("no task defined for run element %q", element.Name)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
if _, ok := config.Tasks[element.Task]; !ok {
|
2019-04-15 21:42:10 +00:00
|
|
|
return errors.Errorf("task %q needed by run element %q doesn't exist", element.Task, element.Name)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
// check duplicate dependencies in task
|
|
|
|
seenDependencies := map[string]struct{}{}
|
|
|
|
for _, dep := range element.Depends {
|
|
|
|
if _, ok := seenDependencies[dep.ElementName]; ok {
|
|
|
|
return errors.Errorf("duplicate task dependency: %s", element.Name)
|
|
|
|
}
|
|
|
|
seenDependencies[dep.ElementName] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getElementParents returns direct parents of element.
|
2019-04-15 21:42:10 +00:00
|
|
|
func getElementParents(run *Run, element *Element) []*Element {
|
2019-03-07 13:42:32 +00:00
|
|
|
parents := []*Element{}
|
2019-04-15 21:42:10 +00:00
|
|
|
for _, el := range run.Elements {
|
2019-03-07 13:42:32 +00:00
|
|
|
isParent := false
|
|
|
|
for _, d := range element.Depends {
|
|
|
|
if d.ElementName == el.Name {
|
|
|
|
isParent = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isParent {
|
|
|
|
parents = append(parents, el)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parents
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAllElementParents returns all the parents (both direct and ancestors) of an element.
|
|
|
|
// In case of circular dependency it won't loop forever but will also return
|
|
|
|
// the element as parent of itself
|
2019-04-15 21:42:10 +00:00
|
|
|
func getAllElementParents(run *Run, element *Element) []*Element {
|
2019-03-07 13:42:32 +00:00
|
|
|
pMap := map[string]*Element{}
|
2019-04-15 21:42:10 +00:00
|
|
|
nextParents := getElementParents(run, element)
|
2019-03-07 13:42:32 +00:00
|
|
|
|
|
|
|
for len(nextParents) > 0 {
|
|
|
|
parents := nextParents
|
|
|
|
nextParents = []*Element{}
|
|
|
|
for _, parent := range parents {
|
|
|
|
if _, ok := pMap[parent.Name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pMap[parent.Name] = parent
|
2019-04-15 21:42:10 +00:00
|
|
|
nextParents = append(nextParents, getElementParents(run, parent)...)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parents := make([]*Element, 0, len(pMap))
|
|
|
|
for _, v := range pMap {
|
|
|
|
parents = append(parents, v)
|
|
|
|
}
|
|
|
|
return parents
|
|
|
|
}
|