woodpecker/pipeline/backend/docker/convert.go

176 lines
4.1 KiB
Go
Raw Normal View History

2017-03-05 07:56:08 +00:00
package docker
import (
"encoding/base64"
"encoding/json"
2019-04-06 13:44:04 +00:00
"regexp"
2017-03-05 07:56:08 +00:00
"strings"
"github.com/docker/docker/api/types/container"
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
2017-03-05 07:56:08 +00:00
)
// returns a container configuration.
func toConfig(proc *types.Step) *container.Config {
2017-03-05 07:56:08 +00:00
config := &container.Config{
Image: proc.Image,
Labels: proc.Labels,
WorkingDir: proc.WorkingDir,
AttachStdout: true,
AttachStderr: true,
}
if len(proc.Environment) != 0 {
config.Env = toEnv(proc.Environment)
}
if len(proc.Command) != 0 {
config.Cmd = proc.Command
}
if len(proc.Entrypoint) != 0 {
config.Entrypoint = proc.Entrypoint
}
if len(proc.Volumes) != 0 {
config.Volumes = toVol(proc.Volumes)
}
return config
}
// returns a container host configuration.
func toHostConfig(proc *types.Step) *container.HostConfig {
2017-03-05 07:56:08 +00:00
config := &container.HostConfig{
Resources: container.Resources{
CPUQuota: proc.CPUQuota,
CPUShares: proc.CPUShares,
CpusetCpus: proc.CPUSet,
Memory: proc.MemLimit,
MemorySwap: proc.MemSwapLimit,
},
2017-06-22 19:06:28 +00:00
LogConfig: container.LogConfig{
Type: "json-file",
},
2017-03-05 07:56:08 +00:00
Privileged: proc.Privileged,
ShmSize: proc.ShmSize,
2017-11-17 22:49:01 +00:00
Sysctls: proc.Sysctls,
2017-03-05 07:56:08 +00:00
}
2017-06-22 19:06:28 +00:00
2017-03-05 07:56:08 +00:00
// if len(proc.VolumesFrom) != 0 {
// config.VolumesFrom = proc.VolumesFrom
// }
if len(proc.NetworkMode) != 0 {
2017-09-08 00:43:33 +00:00
config.NetworkMode = container.NetworkMode(proc.NetworkMode)
}
if len(proc.IpcMode) != 0 {
config.IpcMode = container.IpcMode(proc.IpcMode)
}
2017-03-05 07:56:08 +00:00
if len(proc.DNS) != 0 {
config.DNS = proc.DNS
}
if len(proc.DNSSearch) != 0 {
config.DNSSearch = proc.DNSSearch
}
if len(proc.ExtraHosts) != 0 {
config.ExtraHosts = proc.ExtraHosts
}
if len(proc.Devices) != 0 {
config.Devices = toDev(proc.Devices)
}
if len(proc.Volumes) != 0 {
config.Binds = proc.Volumes
}
2017-09-08 00:43:33 +00:00
config.Tmpfs = map[string]string{}
for _, path := range proc.Tmpfs {
if !strings.Contains(path, ":") {
2017-09-08 00:43:33 +00:00
config.Tmpfs[path] = ""
continue
}
2019-04-06 13:44:04 +00:00
parts, err := splitVolumeParts(path)
if err != nil {
continue
}
2017-09-08 00:43:33 +00:00
config.Tmpfs[parts[0]] = parts[1]
}
2017-03-05 07:56:08 +00:00
return config
}
// helper function that converts a slice of volume paths to a set of
// unique volume names.
func toVol(paths []string) map[string]struct{} {
set := map[string]struct{}{}
for _, path := range paths {
2019-04-06 13:44:04 +00:00
parts, err := splitVolumeParts(path)
if err != nil {
continue
}
2017-03-05 07:56:08 +00:00
if len(parts) < 2 {
continue
}
set[parts[1]] = struct{}{}
}
return set
}
// helper function that converts a key value map of environment variables to a
// string slice in key=value format.
func toEnv(env map[string]string) []string {
var envs []string
for k, v := range env {
envs = append(envs, k+"="+v)
}
return envs
}
// helper function that converts a slice of device paths to a slice of
// container.DeviceMapping.
func toDev(paths []string) []container.DeviceMapping {
var devices []container.DeviceMapping
for _, path := range paths {
2019-04-06 13:44:04 +00:00
parts, err := splitVolumeParts(path)
if err != nil {
continue
}
2017-03-05 07:56:08 +00:00
if len(parts) < 2 {
continue
}
2019-04-06 13:44:04 +00:00
if strings.HasSuffix(parts[1], ":ro") || strings.HasSuffix(parts[1], ":rw") {
parts[1] = parts[1][:len(parts[1])-1]
}
2017-03-05 07:56:08 +00:00
devices = append(devices, container.DeviceMapping{
PathOnHost: parts[0],
PathInContainer: parts[1],
CgroupPermissions: "rwm",
})
}
return devices
}
// helper function that serializes the auth configuration as JSON
// base64 payload.
func encodeAuthToBase64(authConfig types.Auth) (string, error) {
2017-03-05 07:56:08 +00:00
buf, err := json.Marshal(authConfig)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(buf), nil
}
2019-04-06 13:44:04 +00:00
// helper function that split volume path
func splitVolumeParts(volumeParts string) ([]string, error) {
pattern := `^((?:[\w]\:)?[^\:]*)\:((?:[\w]\:)?[^\:]*)(?:\:([rwom]*))?`
r, err := regexp.Compile(pattern)
if err != nil {
return []string{}, err
}
if r.MatchString(volumeParts) {
results := r.FindStringSubmatch(volumeParts)[1:]
var cleanResults []string
2019-04-06 13:44:04 +00:00
for _, item := range results {
if item != "" {
cleanResults = append(cleanResults, item)
}
}
return cleanResults, nil
}
return strings.Split(volumeParts, ":"), nil
2019-04-06 13:44:04 +00:00
}