woodpecker/yaml/transform/image.go

74 lines
1.8 KiB
Go
Raw Normal View History

2016-05-09 18:28:49 +00:00
package transform
import (
2016-07-22 15:53:36 +00:00
"fmt"
2016-05-09 18:28:49 +00:00
"path/filepath"
"strings"
"github.com/drone/drone/yaml"
)
2016-05-10 05:57:57 +00:00
// ImagePull transforms the Yaml to automatically pull the latest image.
2016-05-09 18:28:49 +00:00
func ImagePull(conf *yaml.Config, pull bool) error {
for _, plugin := range conf.Pipeline {
2016-05-10 05:57:57 +00:00
if !isPlugin(plugin) {
2016-05-09 18:28:49 +00:00
continue
}
plugin.Pull = pull
}
return nil
}
2016-05-10 05:57:57 +00:00
// ImageTag transforms the Yaml to use the :latest image tag when empty.
2016-05-09 18:28:49 +00:00
func ImageTag(conf *yaml.Config) error {
for _, image := range conf.Pipeline {
if !strings.Contains(image.Image, ":") {
image.Image = image.Image + ":latest"
}
}
for _, image := range conf.Services {
if !strings.Contains(image.Image, ":") {
image.Image = image.Image + ":latest"
}
}
return nil
}
2016-05-10 05:57:57 +00:00
// ImageName transforms the Yaml to replace underscores with dashes.
2016-05-09 18:28:49 +00:00
func ImageName(conf *yaml.Config) error {
for _, image := range conf.Pipeline {
image.Image = strings.Replace(image.Image, "_", "-", -1)
}
return nil
}
2016-05-10 05:57:57 +00:00
// ImageNamespace transforms the Yaml to use a default namepsace for plugins.
2016-05-09 18:28:49 +00:00
func ImageNamespace(conf *yaml.Config, namespace string) error {
for _, image := range conf.Pipeline {
if strings.Contains(image.Image, "/") {
continue
}
2016-05-10 05:57:57 +00:00
if !isPlugin(image) {
2016-05-09 18:28:49 +00:00
continue
}
image.Image = filepath.Join(namespace, image.Image)
}
return nil
}
2016-05-10 05:57:57 +00:00
// ImageEscalate transforms the Yaml to automatically enable privileged mode
// for a subset of white-listed plugins matching the given patterns.
2016-05-09 18:28:49 +00:00
func ImageEscalate(conf *yaml.Config, patterns []string) error {
for _, c := range conf.Pipeline {
for _, pattern := range patterns {
if ok, _ := filepath.Match(pattern, c.Image); ok {
2016-07-22 15:53:36 +00:00
if len(c.Commands) != 0 {
return fmt.Errorf("Custom commands disabled for the %s plugin", c.Image)
}
2016-05-09 18:28:49 +00:00
c.Privileged = true
}
}
}
return nil
}