2015-04-16 07:10:17 +00:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
2015-05-17 20:51:42 +00:00
|
|
|
common "github.com/drone/drone/pkg/types"
|
2015-05-17 18:36:12 +00:00
|
|
|
"github.com/drone/drone/pkg/yaml/inject"
|
|
|
|
"github.com/drone/drone/pkg/yaml/matrix"
|
2015-08-13 01:21:51 +00:00
|
|
|
"github.com/drone/drone/pkg/yaml/transform"
|
2015-04-16 07:10:17 +00:00
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/gopkg.in/yaml.v2"
|
2015-04-16 07:10:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Opts specifies parser options that will permit
|
|
|
|
// or deny certain Yaml settings.
|
|
|
|
type Opts struct {
|
|
|
|
Volumes bool
|
|
|
|
Network bool
|
|
|
|
Privileged bool
|
2015-08-19 22:49:27 +00:00
|
|
|
Caching bool
|
2015-05-20 06:19:59 +00:00
|
|
|
Whitelist []string
|
2015-04-16 07:10:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 22:48:06 +00:00
|
|
|
var DefaultOpts = &Opts{
|
2015-04-16 07:10:17 +00:00
|
|
|
Volumes: false,
|
|
|
|
Network: false,
|
|
|
|
Privileged: false,
|
2015-08-19 22:49:27 +00:00
|
|
|
Caching: true,
|
2015-05-20 06:19:59 +00:00
|
|
|
Whitelist: []string{"plugins/*"},
|
2015-04-16 07:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses a build matrix and returns
|
|
|
|
// a list of build configurations for each axis
|
|
|
|
// using the default parsing options.
|
2015-08-17 23:24:47 +00:00
|
|
|
func Parse(raw string, r *common.Repo) ([]*common.Config, error) {
|
|
|
|
return ParseOpts(raw, DefaultOpts, r)
|
2015-04-16 07:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseOpts parses a build matrix and returns
|
|
|
|
// a list of build configurations for each axis
|
|
|
|
// using the provided parsing options.
|
2015-08-17 23:24:47 +00:00
|
|
|
func ParseOpts(raw string, opts *Opts, r *common.Repo) ([]*common.Config, error) {
|
2015-04-16 07:10:17 +00:00
|
|
|
axis, err := matrix.Parse(raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
confs := []*common.Config{}
|
|
|
|
|
|
|
|
// when no matrix values exist we should return
|
|
|
|
// a single config value with an empty label.
|
|
|
|
if len(axis) == 0 {
|
2015-08-17 23:24:47 +00:00
|
|
|
conf, err := ParseSingle(raw, opts, r)
|
2015-04-16 07:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
confs = append(confs, conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ax := range axis {
|
|
|
|
// inject the matrix values into the raw script
|
|
|
|
injected := inject.Inject(raw, ax)
|
2015-08-17 23:24:47 +00:00
|
|
|
conf, err := ParseSingle(injected, opts, r)
|
2015-04-16 07:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conf.Axis = common.Axis(ax)
|
|
|
|
confs = append(confs, conf)
|
|
|
|
}
|
2015-04-21 22:48:06 +00:00
|
|
|
|
2015-04-16 07:10:17 +00:00
|
|
|
return confs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper funtion to parse a yaml configuration file.
|
2015-08-17 23:24:47 +00:00
|
|
|
func ParseSingle(raw string, opts *Opts, r *common.Repo) (*common.Config, error) {
|
2015-04-16 07:10:17 +00:00
|
|
|
conf := &common.Config{}
|
|
|
|
err := yaml.Unmarshal([]byte(raw), conf)
|
2015-04-21 22:48:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-19 18:23:32 +00:00
|
|
|
|
2015-08-13 01:21:51 +00:00
|
|
|
// apply rules / transforms
|
2015-08-17 23:24:47 +00:00
|
|
|
transform.Defaults(conf)
|
2015-04-21 22:48:06 +00:00
|
|
|
if !opts.Network {
|
2015-08-17 23:24:47 +00:00
|
|
|
transform.RemoveNetwork(conf)
|
2015-04-21 22:48:06 +00:00
|
|
|
}
|
|
|
|
if !opts.Volumes {
|
2015-08-17 23:24:47 +00:00
|
|
|
transform.RemoveVolumes(conf)
|
2015-04-21 22:48:06 +00:00
|
|
|
}
|
|
|
|
if !opts.Privileged {
|
2015-08-17 23:24:47 +00:00
|
|
|
transform.RemovePrivileged(conf)
|
2015-04-21 22:48:06 +00:00
|
|
|
}
|
2015-08-17 23:24:47 +00:00
|
|
|
transform.Repo(conf, r)
|
2015-08-19 22:49:27 +00:00
|
|
|
if !opts.Caching {
|
|
|
|
transform.RemoveVolumes(conf)
|
|
|
|
}
|
2015-08-19 18:23:32 +00:00
|
|
|
|
|
|
|
// lint the yaml file
|
|
|
|
err = Lint(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-08 00:26:25 +00:00
|
|
|
err = LintPlugins(conf, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-16 07:10:17 +00:00
|
|
|
return conf, err
|
|
|
|
}
|
2015-04-30 17:39:16 +00:00
|
|
|
|
|
|
|
func ParseCondition(raw string) (*common.Condition, error) {
|
|
|
|
c := struct {
|
|
|
|
Condition *common.Condition `yaml:"when"`
|
|
|
|
}{}
|
|
|
|
err := yaml.Unmarshal([]byte(raw), c)
|
|
|
|
return c.Condition, err
|
|
|
|
}
|