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 (
|
2019-04-16 09:01:02 +00:00
|
|
|
"encoding/json"
|
2019-03-07 13:42:32 +00:00
|
|
|
"fmt"
|
2019-03-07 17:01:34 +00:00
|
|
|
"regexp"
|
2019-03-07 13:42:32 +00:00
|
|
|
"strings"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
"agola.io/agola/services/types"
|
2019-03-07 13:42:32 +00:00
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
"github.com/ghodss/yaml"
|
2019-04-16 13:00:37 +00:00
|
|
|
"github.com/google/go-jsonnet"
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-03-07 13:42:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-04-15 21:42:10 +00:00
|
|
|
maxRunNameLength = 100
|
|
|
|
maxTaskNameLength = 100
|
|
|
|
maxStepNameLength = 100
|
2019-04-16 13:20:45 +00:00
|
|
|
|
|
|
|
defaultWorkingDir = "~/project"
|
2019-03-07 13:42:32 +00:00
|
|
|
)
|
|
|
|
|
2019-04-16 13:00:37 +00:00
|
|
|
type ConfigFormat int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ConfigFormatJSON handles both json or yaml format (since json is a subset of yaml)
|
|
|
|
ConfigFormatJSON ConfigFormat = iota
|
|
|
|
ConfigFormatJsonnet
|
|
|
|
)
|
|
|
|
|
2019-03-07 17:01:34 +00:00
|
|
|
var (
|
|
|
|
regExpDelimiters = []string{"/", "#"}
|
|
|
|
)
|
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
type Config struct {
|
2019-04-16 09:01:02 +00:00
|
|
|
Runs []*Run `json:"runs"`
|
2019-05-10 09:45:00 +00:00
|
|
|
|
|
|
|
DockerRegistriesAuth map[string]*DockerRegistryAuth `json:"docker_registries_auth"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RuntimeType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
RuntimeTypePod RuntimeType = "pod"
|
|
|
|
)
|
|
|
|
|
2019-04-22 12:38:25 +00:00
|
|
|
type DockerRegistryAuthType string
|
2019-04-10 15:13:51 +00:00
|
|
|
|
|
|
|
const (
|
2019-04-22 12:38:25 +00:00
|
|
|
DockerRegistryAuthTypeBasic DockerRegistryAuthType = "basic"
|
|
|
|
DockerRegistryAuthTypeEncodedAuth DockerRegistryAuthType = "encodedauth"
|
2019-04-10 15:13:51 +00:00
|
|
|
)
|
|
|
|
|
2019-04-22 12:38:25 +00:00
|
|
|
type DockerRegistryAuth struct {
|
|
|
|
Type DockerRegistryAuthType `json:"type"`
|
2019-04-10 15:13:51 +00:00
|
|
|
|
2019-04-22 12:38:25 +00:00
|
|
|
// basic auth
|
2019-04-16 09:01:02 +00:00
|
|
|
Username Value `json:"username"`
|
|
|
|
Password Value `json:"password"`
|
2019-04-22 12:38:25 +00:00
|
|
|
|
|
|
|
// encoded auth string
|
|
|
|
Auth string `json:"auth"`
|
|
|
|
|
|
|
|
// future auths like aws ecr auth
|
2019-04-10 15:13:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
type Runtime struct {
|
2019-04-22 12:38:25 +00:00
|
|
|
Type RuntimeType `json:"type,omitempty"`
|
2019-07-31 13:39:07 +00:00
|
|
|
Arch types.Arch `json:"arch,omitempty"`
|
2019-04-22 12:38:25 +00:00
|
|
|
Containers []*Container `json:"containers,omitempty"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Container struct {
|
2019-04-16 09:01:02 +00:00
|
|
|
Image string `json:"image,omitempty"`
|
|
|
|
Environment map[string]Value `json:"environment,omitempty"`
|
|
|
|
User string `json:"user"`
|
|
|
|
Privileged bool `json:"privileged"`
|
|
|
|
Entrypoint string `json:"entrypoint"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:42:10 +00:00
|
|
|
type Run struct {
|
2019-04-22 12:38:25 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Tasks []*Task `json:"tasks"`
|
2019-08-01 16:16:34 +00:00
|
|
|
When *When `json:"when"`
|
2019-04-22 12:38:25 +00:00
|
|
|
DockerRegistriesAuth map[string]*DockerRegistryAuth `json:"docker_registries_auth"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
type Task struct {
|
2019-04-22 12:38:25 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Runtime *Runtime `json:"runtime"`
|
|
|
|
Environment map[string]Value `json:"environment,omitempty"`
|
|
|
|
WorkingDir string `json:"working_dir"`
|
|
|
|
Shell string `json:"shell"`
|
|
|
|
User string `json:"user"`
|
2019-06-08 14:07:15 +00:00
|
|
|
Steps Steps `json:"steps"`
|
|
|
|
Depends Depends `json:"depends"`
|
2019-04-22 12:38:25 +00:00
|
|
|
IgnoreFailure bool `json:"ignore_failure"`
|
|
|
|
Approval bool `json:"approval"`
|
2019-06-08 14:07:15 +00:00
|
|
|
When *When `json:"when"`
|
2019-04-22 12:38:25 +00:00
|
|
|
DockerRegistriesAuth map[string]*DockerRegistryAuth `json:"docker_registries_auth"`
|
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
|
|
|
)
|
|
|
|
|
2019-06-08 14:07:15 +00:00
|
|
|
type Depends []*Depend
|
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
type Depend struct {
|
2019-04-16 09:01:02 +00:00
|
|
|
TaskName string `json:"task"`
|
|
|
|
Conditions []DependCondition `json:"conditions"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:07:15 +00:00
|
|
|
type Step interface{}
|
|
|
|
|
|
|
|
type Steps []Step
|
|
|
|
|
|
|
|
type BaseStep struct {
|
2019-04-16 09:01:02 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
2019-06-08 14:07:15 +00:00
|
|
|
When *When `json:"when"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CloneStep struct {
|
2019-06-08 14:07:15 +00:00
|
|
|
BaseStep `json:",inline"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RunStep struct {
|
2019-06-08 14:07:15 +00:00
|
|
|
BaseStep `json:",inline"`
|
2019-04-16 09:01:02 +00:00
|
|
|
Command string `json:"command"`
|
|
|
|
Environment map[string]Value `json:"environment,omitempty"`
|
|
|
|
WorkingDir string `json:"working_dir"`
|
|
|
|
Shell string `json:"shell"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SaveToWorkspaceStep struct {
|
2019-06-08 14:07:15 +00:00
|
|
|
BaseStep `json:",inline"`
|
2019-04-16 09:01:02 +00:00
|
|
|
Contents []*SaveContent `json:"contents"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RestoreWorkspaceStep struct {
|
2019-06-08 14:07:15 +00:00
|
|
|
BaseStep `json:",inline"`
|
|
|
|
DestDir string `json:"dest_dir"`
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 12:58:56 +00:00
|
|
|
type SaveCacheStep struct {
|
2019-06-08 14:07:15 +00:00
|
|
|
BaseStep `json:",inline"`
|
2019-04-16 09:01:02 +00:00
|
|
|
Key string `json:"key"`
|
|
|
|
Contents []*SaveContent `json:"contents"`
|
2019-04-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RestoreCacheStep struct {
|
2019-06-08 14:07:15 +00:00
|
|
|
BaseStep `json:",inline"`
|
|
|
|
Keys []string `json:"keys"`
|
|
|
|
DestDir string `json:"dest_dir"`
|
2019-04-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:07:15 +00:00
|
|
|
type SaveContent struct {
|
|
|
|
SourceDir string `json:"source_dir"`
|
|
|
|
DestDir string `json:"dest_dir"`
|
|
|
|
Paths []string `json:"paths"`
|
|
|
|
}
|
2019-04-16 09:01:02 +00:00
|
|
|
|
2019-06-08 14:07:15 +00:00
|
|
|
func (s *Steps) UnmarshalJSON(b []byte) error {
|
|
|
|
var stepsRaw []json.RawMessage
|
|
|
|
if err := json.Unmarshal(b, &stepsRaw); err != nil {
|
2019-03-18 14:02:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:07:15 +00:00
|
|
|
steps := make(Steps, len(stepsRaw))
|
|
|
|
for i, stepRaw := range stepsRaw {
|
|
|
|
var step interface{}
|
|
|
|
|
|
|
|
var stepMap map[string]json.RawMessage
|
|
|
|
if err := json.Unmarshal(stepRaw, &stepMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// handle default step definition using format { type: "steptype", other steps fields }
|
|
|
|
if _, ok := stepMap["type"]; ok {
|
|
|
|
var stepTypeI interface{}
|
|
|
|
if err := json.Unmarshal(stepMap["type"], &stepTypeI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stepType, ok := stepTypeI.(string)
|
2019-04-16 09:01:02 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("step type at index %d must be a string", i)
|
|
|
|
}
|
2019-06-08 14:07:15 +00:00
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
switch stepType {
|
|
|
|
case "clone":
|
2019-04-13 12:58:56 +00:00
|
|
|
var s CloneStep
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(stepRaw, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-13 12:58:56 +00:00
|
|
|
s.Type = stepType
|
2019-06-08 14:07:15 +00:00
|
|
|
step = &s
|
2019-03-07 13:42:32 +00:00
|
|
|
|
|
|
|
case "run":
|
2019-04-13 12:58:56 +00:00
|
|
|
var s RunStep
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(stepRaw, &s); err != nil {
|
2019-04-16 09:01:02 +00:00
|
|
|
return err
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
2019-04-13 12:58:56 +00:00
|
|
|
s.Type = stepType
|
2019-06-08 14:07:15 +00:00
|
|
|
step = &s
|
2019-03-07 13:42:32 +00:00
|
|
|
|
|
|
|
case "save_to_workspace":
|
2019-04-13 12:58:56 +00:00
|
|
|
var s SaveToWorkspaceStep
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(stepRaw, &s); err != nil {
|
2019-03-07 13:42:32 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-04-13 12:58:56 +00:00
|
|
|
s.Type = stepType
|
2019-06-08 14:07:15 +00:00
|
|
|
step = &s
|
2019-03-07 13:42:32 +00:00
|
|
|
|
|
|
|
case "restore_workspace":
|
2019-04-13 12:58:56 +00:00
|
|
|
var s RestoreWorkspaceStep
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(stepRaw, &s); err != nil {
|
2019-04-13 12:58:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Type = stepType
|
2019-06-08 14:07:15 +00:00
|
|
|
step = &s
|
2019-04-13 12:58:56 +00:00
|
|
|
|
|
|
|
case "save_cache":
|
|
|
|
var s SaveCacheStep
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(stepRaw, &s); err != nil {
|
2019-03-07 13:42:32 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-04-13 12:58:56 +00:00
|
|
|
s.Type = stepType
|
2019-06-08 14:07:15 +00:00
|
|
|
step = &s
|
2019-04-13 12:58:56 +00:00
|
|
|
|
|
|
|
case "restore_cache":
|
|
|
|
var s RestoreCacheStep
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(stepRaw, &s); err != nil {
|
2019-04-13 12:58:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Type = stepType
|
2019-06-08 14:07:15 +00:00
|
|
|
step = &s
|
2019-03-07 13:42:32 +00:00
|
|
|
default:
|
|
|
|
return errors.Errorf("unknown step type: %s", stepType)
|
|
|
|
}
|
2019-04-16 09:01:02 +00:00
|
|
|
} else {
|
|
|
|
// handle simpler (for yaml not for json) steps definition using format "steptype": { stepSpecification }
|
2019-06-08 14:07:15 +00:00
|
|
|
if len(stepMap) > 1 {
|
2019-04-16 09:01:02 +00:00
|
|
|
return errors.Errorf("wrong steps description at index %d: more than one step name per list entry", i)
|
|
|
|
}
|
2019-06-08 14:07:15 +00:00
|
|
|
for stepType, stepSpecRaw := range stepMap {
|
|
|
|
var stepSpec interface{}
|
|
|
|
if err := json.Unmarshal(stepSpecRaw, &stepSpec); err != nil {
|
2019-04-16 09:01:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-06-08 14:07:15 +00:00
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
switch stepType {
|
|
|
|
case "clone":
|
|
|
|
var s CloneStep
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(stepSpecRaw, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-16 09:01:02 +00:00
|
|
|
s.Type = stepType
|
2019-06-08 14:07:15 +00:00
|
|
|
step = &s
|
2019-04-16 09:01:02 +00:00
|
|
|
|
|
|
|
case "run":
|
|
|
|
var s RunStep
|
2019-07-02 14:20:53 +00:00
|
|
|
switch stepSpec := stepSpec.(type) {
|
2019-04-16 09:01:02 +00:00
|
|
|
case string:
|
2019-07-02 14:20:53 +00:00
|
|
|
s.Command = stepSpec
|
2019-04-16 09:01:02 +00:00
|
|
|
default:
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(stepSpecRaw, &s); err != nil {
|
2019-04-16 09:01:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Type = stepType
|
2019-06-08 14:07:15 +00:00
|
|
|
step = &s
|
2019-03-07 13:42:32 +00:00
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
case "save_to_workspace":
|
|
|
|
var s SaveToWorkspaceStep
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(stepSpecRaw, &s); err != nil {
|
2019-04-16 09:01:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Type = stepType
|
2019-06-08 14:07:15 +00:00
|
|
|
step = &s
|
2019-03-07 17:01:34 +00:00
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
case "restore_workspace":
|
|
|
|
var s RestoreWorkspaceStep
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(stepSpecRaw, &s); err != nil {
|
2019-04-16 09:01:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Type = stepType
|
2019-06-08 14:07:15 +00:00
|
|
|
step = &s
|
2019-03-07 17:01:34 +00:00
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
case "save_cache":
|
|
|
|
var s SaveCacheStep
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(stepSpecRaw, &s); err != nil {
|
2019-04-16 09:01:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Type = stepType
|
2019-06-08 14:07:15 +00:00
|
|
|
step = &s
|
2019-03-07 13:42:32 +00:00
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
case "restore_cache":
|
|
|
|
var s RestoreCacheStep
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(stepSpecRaw, &s); err != nil {
|
2019-04-16 09:01:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Type = stepType
|
2019-06-08 14:07:15 +00:00
|
|
|
step = &s
|
2019-04-16 09:01:02 +00:00
|
|
|
default:
|
|
|
|
return errors.Errorf("unknown step type: %s", stepType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-08 14:07:15 +00:00
|
|
|
|
|
|
|
steps[i] = step
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:07:15 +00:00
|
|
|
*s = steps
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-07 13:42:32 +00:00
|
|
|
|
2019-06-08 14:07:15 +00:00
|
|
|
func (d *Depends) UnmarshalJSON(b []byte) error {
|
|
|
|
var dependsRaw []json.RawMessage
|
|
|
|
|
|
|
|
if err := json.Unmarshal(b, &dependsRaw); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
depends := make([]*Depend, len(dependsRaw))
|
|
|
|
for i, dependRaw := range dependsRaw {
|
|
|
|
var dependi interface{}
|
|
|
|
if err := json.Unmarshal(dependRaw, &dependi); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-07 13:42:32 +00:00
|
|
|
var depend *Depend
|
2019-04-16 09:01:02 +00:00
|
|
|
isSimpler := false
|
2019-06-08 14:07:15 +00:00
|
|
|
switch de := dependi.(type) {
|
2019-04-16 09:01:02 +00:00
|
|
|
// handle simpler (for yaml) depends definition using format "taskname":
|
2019-03-07 13:42:32 +00:00
|
|
|
case string:
|
|
|
|
depend = &Depend{
|
2019-06-08 14:07:15 +00:00
|
|
|
TaskName: dependi.(string),
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
2019-04-16 09:01:02 +00:00
|
|
|
case map[string]interface{}:
|
|
|
|
if len(de) == 1 {
|
|
|
|
for _, v := range de {
|
|
|
|
switch v.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
isSimpler = true
|
|
|
|
case string:
|
|
|
|
default:
|
|
|
|
return errors.Errorf("unsupported depend entry format")
|
|
|
|
}
|
|
|
|
}
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
2019-04-16 09:01:02 +00:00
|
|
|
if !isSimpler {
|
|
|
|
// handle default depends definition using format "task": "taskname", conditions: [ list of conditions ]
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(dependRaw, &depend); err != nil {
|
2019-04-16 09:01:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// handle simpler (for yaml) depends definition using format "taskname": [ list of conditions ]
|
|
|
|
if len(de) != 1 {
|
|
|
|
return errors.Errorf("unsupported depend entry format")
|
|
|
|
}
|
|
|
|
type deplist map[string][]DependCondition
|
|
|
|
var dl deplist
|
2019-06-08 14:07:15 +00:00
|
|
|
if err := json.Unmarshal(dependRaw, &dl); err != nil {
|
2019-04-16 09:01:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(dl) != 1 {
|
|
|
|
return errors.Errorf("unsupported depend entry format")
|
|
|
|
}
|
|
|
|
for k, v := range dl {
|
|
|
|
depend = &Depend{
|
|
|
|
TaskName: k,
|
|
|
|
Conditions: v,
|
|
|
|
}
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2019-04-16 09:01:02 +00:00
|
|
|
return errors.Errorf("unsupported depend entry format")
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
depends[i] = depend
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:07:15 +00:00
|
|
|
*d = depends
|
2019-03-07 17:01:34 +00:00
|
|
|
|
2019-06-08 14:07:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-07 17:01:34 +00:00
|
|
|
|
2019-06-08 14:07:15 +00:00
|
|
|
type ValueType int
|
2019-03-07 17:01:34 +00:00
|
|
|
|
2019-06-08 14:07:15 +00:00
|
|
|
const (
|
|
|
|
ValueTypeString ValueType = iota
|
|
|
|
ValueTypeFromVariable
|
|
|
|
)
|
2019-03-07 17:01:34 +00:00
|
|
|
|
2019-06-08 14:07:15 +00:00
|
|
|
type Value struct {
|
|
|
|
Type ValueType
|
|
|
|
Value string
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
func (val *Value) UnmarshalJSON(b []byte) error {
|
2019-04-10 15:12:39 +00:00
|
|
|
var ival interface{}
|
2019-04-16 09:01:02 +00:00
|
|
|
if err := json.Unmarshal(b, &ival); err != nil {
|
2019-04-10 15:12:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch valValue := ival.(type) {
|
|
|
|
case string:
|
|
|
|
val.Type = ValueTypeString
|
|
|
|
val.Value = valValue
|
2019-04-16 09:01:02 +00:00
|
|
|
case map[string]interface{}:
|
2019-04-10 15:12:39 +00:00
|
|
|
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-09-05 07:29:20 +00:00
|
|
|
type When types.When
|
2019-06-08 14:07:15 +00:00
|
|
|
|
|
|
|
type when struct {
|
|
|
|
Branch interface{} `json:"branch"`
|
|
|
|
Tag interface{} `json:"tag"`
|
|
|
|
Ref interface{} `json:"ref"`
|
|
|
|
}
|
|
|
|
|
2019-09-05 07:29:20 +00:00
|
|
|
func (w *When) ToWhen() *types.When {
|
|
|
|
return (*types.When)(w)
|
2019-08-01 16:16:34 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:07:15 +00:00
|
|
|
func (w *When) UnmarshalJSON(b []byte) error {
|
|
|
|
var wi *when
|
|
|
|
if err := json.Unmarshal(b, &wi); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if wi.Branch != nil {
|
|
|
|
w.Branch, err = parseWhenConditions(wi.Branch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if wi.Tag != nil {
|
|
|
|
w.Tag, err = parseWhenConditions(wi.Tag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if wi.Ref != nil {
|
|
|
|
w.Ref, err = parseWhenConditions(wi.Ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-05 07:29:20 +00:00
|
|
|
func parseWhenConditions(wi interface{}) (*types.WhenConditions, error) {
|
|
|
|
w := &types.WhenConditions{}
|
2019-03-07 17:01:34 +00:00
|
|
|
|
|
|
|
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
|
2019-04-16 09:01:02 +00:00
|
|
|
case map[string]interface{}:
|
2019-03-07 17:01:34 +00:00
|
|
|
for k, v := range c {
|
2019-04-16 09:01:02 +00:00
|
|
|
switch k {
|
2019-03-07 17:01:34 +00:00
|
|
|
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:
|
2019-04-16 09:01:02 +00:00
|
|
|
return nil, errors.Errorf(`expected one of "include" or "exclude", got %s`, k)
|
2019-03-07 17:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-09-05 07:29:20 +00:00
|
|
|
func parseWhenConditionSlice(conds []string) ([]types.WhenCondition, error) {
|
2019-03-07 17:01:34 +00:00
|
|
|
if len(conds) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-09-05 07:29:20 +00:00
|
|
|
wcs := []types.WhenCondition{}
|
2019-03-07 17:01:34 +00:00
|
|
|
for _, cond := range conds {
|
|
|
|
wc, err := parseWhenCondition(cond)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wcs = append(wcs, *wc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return wcs, nil
|
|
|
|
}
|
|
|
|
|
2019-09-05 07:29:20 +00:00
|
|
|
func parseWhenCondition(s string) (*types.WhenCondition, error) {
|
2019-03-07 17:01:34 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 07:29:20 +00:00
|
|
|
wc := &types.WhenCondition{Match: s}
|
2019-03-07 17:01:34 +00:00
|
|
|
|
|
|
|
if isRegExp {
|
|
|
|
if _, err := regexp.Compile(s); err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, errors.Errorf("wrong regular expression: %w", err)
|
2019-03-07 17:01:34 +00:00
|
|
|
}
|
2019-09-05 07:29:20 +00:00
|
|
|
wc.Type = types.WhenConditionTypeRegExp
|
2019-03-07 17:01:34 +00:00
|
|
|
} else {
|
2019-09-05 07:29:20 +00:00
|
|
|
wc.Type = types.WhenConditionTypeSimple
|
2019-03-07 17:01:34 +00:00
|
|
|
}
|
|
|
|
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-04-16 09:01:02 +00:00
|
|
|
func (c *Config) Run(runName string) *Run {
|
|
|
|
for _, r := range c.Runs {
|
|
|
|
if r.Name == runName {
|
2019-03-07 13:42:32 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 09:01:02 +00:00
|
|
|
panic(fmt.Sprintf("run %q doesn't exists", runName))
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
func (r *Run) Task(taskName string) *Task {
|
|
|
|
for _, t := range r.Tasks {
|
|
|
|
if t.Name == taskName {
|
2019-03-07 13:42:32 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 09:01:02 +00:00
|
|
|
panic(fmt.Sprintf("task %q for run %q doesn't exists", taskName, r.Name))
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultConfig = Config{}
|
|
|
|
|
2019-04-16 13:00:37 +00:00
|
|
|
func ParseConfig(configData []byte, format ConfigFormat) (*Config, error) {
|
|
|
|
// Generate json from jsonnet
|
|
|
|
if format == ConfigFormatJsonnet {
|
|
|
|
// TODO(sgotti) support custom import files inside the configdir ???
|
|
|
|
vm := jsonnet.MakeVM()
|
|
|
|
out, err := vm.EvaluateSnippet("", string(configData))
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, errors.Errorf("failed to evaluate jsonnet config: %w", err)
|
2019-04-16 13:00:37 +00:00
|
|
|
}
|
|
|
|
configData = []byte(out)
|
|
|
|
}
|
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
config := DefaultConfig
|
|
|
|
if err := yaml.Unmarshal(configData, &config); err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, errors.Errorf("failed to unmarshal config: %w", err)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
return &config, checkConfig(&config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkConfig(config *Config) error {
|
2019-04-15 21:42:10 +00:00
|
|
|
if len(config.Runs) == 0 {
|
2019-04-16 09:01:02 +00:00
|
|
|
return errors.Errorf("no runs defined")
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
seenRuns := map[string]struct{}{}
|
|
|
|
for ri, run := range config.Runs {
|
|
|
|
if run == nil {
|
|
|
|
return errors.Errorf("run at index %d is empty", ri)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
if run.Name == "" {
|
|
|
|
return errors.Errorf("run at index %d has empty name", ri)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
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-16 09:01:02 +00:00
|
|
|
if _, ok := seenRuns[run.Name]; ok {
|
|
|
|
return errors.Errorf("duplicate run name: %s", run.Name)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
2019-04-16 09:01:02 +00:00
|
|
|
seenRuns[run.Name] = struct{}{}
|
2019-03-07 13:42:32 +00:00
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
seenTasks := map[string]struct{}{}
|
|
|
|
for ti, task := range run.Tasks {
|
|
|
|
if task == nil {
|
|
|
|
return errors.Errorf("run %q: task at index %d is empty", run.Name, ti)
|
2019-04-10 15:13:51 +00:00
|
|
|
}
|
2019-04-16 09:01:02 +00:00
|
|
|
|
|
|
|
if task.Name == "" {
|
|
|
|
return errors.Errorf("run %q: task at index %d has empty name", run.Name, ti)
|
2019-04-10 15:13:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
if len(task.Name) > maxTaskNameLength {
|
|
|
|
return errors.Errorf("task name %q too long", task.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := seenTasks[task.Name]; ok {
|
|
|
|
return errors.Errorf("duplicate task name: %s", task.Name)
|
|
|
|
}
|
|
|
|
seenTasks[task.Name] = struct{}{}
|
|
|
|
|
|
|
|
// check tasks runtime
|
|
|
|
if task.Runtime == nil {
|
|
|
|
return errors.Errorf("task %q: runtime is not defined", task.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := task.Runtime
|
2019-05-23 21:23:54 +00:00
|
|
|
if r.Type != "" {
|
|
|
|
if r.Type != RuntimeTypePod {
|
|
|
|
return errors.Errorf("task %q runtime: wrong type %q", task.Name, r.Type)
|
|
|
|
}
|
2019-04-16 09:01:02 +00:00
|
|
|
}
|
|
|
|
if len(r.Containers) == 0 {
|
|
|
|
return errors.Errorf("task %q runtime: at least one container must be defined", task.Name)
|
2019-04-13 12:58:56 +00:00
|
|
|
}
|
2019-04-17 13:24:37 +00:00
|
|
|
if r.Arch != "" {
|
2019-07-31 13:39:07 +00:00
|
|
|
if !types.IsValidArch(r.Arch) {
|
2019-04-17 13:24:37 +00:00
|
|
|
return errors.Errorf("task %q runtime: invalid arch %q", task.Name, r.Arch)
|
|
|
|
}
|
|
|
|
}
|
2019-04-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 13:42:32 +00:00
|
|
|
// 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
|
2019-04-16 09:01:02 +00:00
|
|
|
allTasks := map[string]struct{}{}
|
|
|
|
for _, task := range run.Tasks {
|
|
|
|
allTasks[task.Name] = struct{}{}
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
for _, task := range run.Tasks {
|
|
|
|
for _, dep := range task.Depends {
|
|
|
|
if _, ok := allTasks[dep.TaskName]; !ok {
|
|
|
|
return errors.Errorf("run task %q needed by task %q doesn't exist", dep.TaskName, task.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-16 09:01:02 +00:00
|
|
|
for _, task := range run.Tasks {
|
|
|
|
allParents := getAllTaskParents(run, task)
|
2019-03-07 13:42:32 +00:00
|
|
|
for _, parent := range allParents {
|
2019-04-16 09:01:02 +00:00
|
|
|
if parent.Name == task.Name {
|
2019-03-07 13:42:32 +00:00
|
|
|
// TODO(sgotti) get the parent that depends on task to report it
|
|
|
|
dep := []string{}
|
|
|
|
for _, parent := range allParents {
|
2019-04-16 09:01:02 +00:00
|
|
|
pparents := getTaskParents(run, parent)
|
2019-03-07 13:42:32 +00:00
|
|
|
for _, pparent := range pparents {
|
2019-04-16 09:01:02 +00:00
|
|
|
if pparent.Name == task.Name {
|
2019-03-07 13:42:32 +00:00
|
|
|
dep = append(dep, fmt.Sprintf("%q", parent.Name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 09:01:02 +00:00
|
|
|
cerrs.Append(errors.Errorf("circular dependency between task %q and tasks %s", task.Name, strings.Join(dep, " ")))
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2019-04-16 09:01:02 +00:00
|
|
|
for _, task := range run.Tasks {
|
|
|
|
parents := getTaskParents(run, task)
|
2019-03-07 13:42:32 +00:00
|
|
|
for _, parent := range parents {
|
2019-04-16 09:01:02 +00:00
|
|
|
allParentParents := getAllTaskParents(run, parent)
|
2019-08-12 21:11:19 +00:00
|
|
|
for _, p := range parents {
|
2019-03-07 13:42:32 +00:00
|
|
|
for _, pp := range allParentParents {
|
|
|
|
if p.Name == pp.Name {
|
2019-08-12 21:11:19 +00:00
|
|
|
return errors.Errorf("task %q and its dependency %q have both a dependency on task %q", task.Name, parent.Name, p.Name)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
// check duplicate task dependencies
|
|
|
|
for _, run := range config.Runs {
|
|
|
|
for _, task := range run.Tasks {
|
|
|
|
// check duplicate dependencies in task
|
|
|
|
seenDependencies := map[string]struct{}{}
|
|
|
|
for _, dep := range task.Depends {
|
|
|
|
if _, ok := seenDependencies[dep.TaskName]; ok {
|
|
|
|
return errors.Errorf("duplicate task dependency: %s", task.Name)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
2019-04-16 09:01:02 +00:00
|
|
|
seenDependencies[dep.TaskName] = struct{}{}
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 09:57:10 +00:00
|
|
|
for _, run := range config.Runs {
|
|
|
|
for _, task := range run.Tasks {
|
|
|
|
for i, s := range task.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.Command == "" {
|
|
|
|
return errors.Errorf("no command defined for step %d (run) in task %q", i, task.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
case *SaveCacheStep:
|
|
|
|
if step.Key == "" {
|
|
|
|
return errors.Errorf("no key defined for step %d (save_cache) in task %q", i, task.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
case *RestoreCacheStep:
|
|
|
|
if len(step.Keys) == 0 {
|
|
|
|
return errors.Errorf("no keys defined for step %d (restore_cache) in task %q", i, task.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
// Set defaults
|
2019-05-10 09:45:00 +00:00
|
|
|
for _, registryAuth := range config.DockerRegistriesAuth {
|
|
|
|
if registryAuth.Type == "" {
|
|
|
|
registryAuth.Type = DockerRegistryAuthTypeBasic
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 21:42:10 +00:00
|
|
|
for _, run := range config.Runs {
|
2019-04-22 12:38:25 +00:00
|
|
|
// set auth type to basic if not specified
|
|
|
|
for _, registryAuth := range run.DockerRegistriesAuth {
|
|
|
|
if registryAuth.Type == "" {
|
|
|
|
registryAuth.Type = DockerRegistryAuthTypeBasic
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 09:01:02 +00:00
|
|
|
for _, task := range run.Tasks {
|
2019-04-22 12:38:25 +00:00
|
|
|
// set auth type to basic if not specified
|
|
|
|
for _, registryAuth := range task.DockerRegistriesAuth {
|
|
|
|
if registryAuth.Type == "" {
|
|
|
|
registryAuth.Type = DockerRegistryAuthTypeBasic
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:20:45 +00:00
|
|
|
// set task default working dir
|
|
|
|
if task.WorkingDir == "" {
|
|
|
|
task.WorkingDir = defaultWorkingDir
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:23:54 +00:00
|
|
|
// set task runtime type to pod if empty
|
|
|
|
r := task.Runtime
|
|
|
|
if r.Type == "" {
|
|
|
|
r.Type = RuntimeTypePod
|
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
// set steps defaults
|
|
|
|
for i, s := range task.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 (run) in task %q, required since command is more than one line", i, task.Name)
|
|
|
|
}
|
|
|
|
len := len(step.Command)
|
|
|
|
if len > maxStepNameLength {
|
|
|
|
len = maxStepNameLength
|
|
|
|
}
|
|
|
|
step.Name = step.Command[:len]
|
|
|
|
}
|
|
|
|
|
|
|
|
case *SaveCacheStep:
|
|
|
|
for _, content := range step.Contents {
|
|
|
|
if len(content.Paths) == 0 {
|
|
|
|
// default to all files inside the sourceDir
|
|
|
|
content.Paths = []string{"**"}
|
|
|
|
}
|
|
|
|
}
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
// getTaskParents returns direct parents of task.
|
|
|
|
func getTaskParents(run *Run, task *Task) []*Task {
|
|
|
|
parents := []*Task{}
|
|
|
|
for _, el := range run.Tasks {
|
2019-03-07 13:42:32 +00:00
|
|
|
isParent := false
|
2019-04-16 09:01:02 +00:00
|
|
|
for _, d := range task.Depends {
|
|
|
|
if d.TaskName == el.Name {
|
2019-03-07 13:42:32 +00:00
|
|
|
isParent = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isParent {
|
|
|
|
parents = append(parents, el)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parents
|
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
// getAllTaskParents returns all the parents (both direct and ancestors) of a task.
|
2019-03-07 13:42:32 +00:00
|
|
|
// In case of circular dependency it won't loop forever but will also return
|
2019-04-16 09:01:02 +00:00
|
|
|
// the task as parent of itself
|
|
|
|
func getAllTaskParents(run *Run, task *Task) []*Task {
|
|
|
|
pMap := map[string]*Task{}
|
|
|
|
nextParents := getTaskParents(run, task)
|
2019-03-07 13:42:32 +00:00
|
|
|
|
|
|
|
for len(nextParents) > 0 {
|
|
|
|
parents := nextParents
|
2019-04-16 09:01:02 +00:00
|
|
|
nextParents = []*Task{}
|
2019-03-07 13:42:32 +00:00
|
|
|
for _, parent := range parents {
|
|
|
|
if _, ok := pMap[parent.Name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pMap[parent.Name] = parent
|
2019-04-16 09:01:02 +00:00
|
|
|
nextParents = append(nextParents, getTaskParents(run, parent)...)
|
2019-03-07 13:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 09:01:02 +00:00
|
|
|
parents := make([]*Task, 0, len(pMap))
|
2019-03-07 13:42:32 +00:00
|
|
|
for _, v := range pMap {
|
|
|
|
parents = append(parents, v)
|
|
|
|
}
|
|
|
|
return parents
|
|
|
|
}
|