woodpecker/pipeline/frontend/yaml/compiler/convert.go

183 lines
4.8 KiB
Go
Raw Normal View History

2017-03-05 07:56:08 +00:00
package compiler
import (
"fmt"
"path"
2017-04-10 10:39:50 +00:00
"strings"
2017-03-05 07:56:08 +00:00
"github.com/rs/zerolog/log"
backend "github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml"
2017-03-05 07:56:08 +00:00
)
2017-07-21 21:52:52 +00:00
func (c *Compiler) createProcess(name string, container *yaml.Container, section string) *backend.Step {
2017-03-05 07:56:08 +00:00
var (
detached bool
workingdir string
workspace = fmt.Sprintf("%s_default:%s", c.prefix, c.base)
privileged = container.Privileged
entrypoint = container.Entrypoint
command = container.Command
image = expandImage(container.Image)
networkMode = container.NetworkMode
ipcMode = container.IpcMode
2017-03-05 07:56:08 +00:00
// network = container.Network
)
networks := []backend.Conn{
{
2017-03-05 07:56:08 +00:00
Name: fmt.Sprintf("%s_default", c.prefix),
2017-05-16 10:50:06 +00:00
Aliases: []string{container.Name},
2017-03-05 07:56:08 +00:00
},
}
2017-04-10 16:27:34 +00:00
for _, network := range c.networks {
networks = append(networks, backend.Conn{
Name: network,
})
}
2017-03-05 07:56:08 +00:00
2017-03-19 09:07:21 +00:00
var volumes []string
if !c.local {
volumes = append(volumes, workspace)
2017-03-05 07:56:08 +00:00
}
2017-03-19 09:07:21 +00:00
volumes = append(volumes, c.volumes...)
2017-03-05 07:56:08 +00:00
for _, volume := range container.Volumes.Volumes {
volumes = append(volumes, volume.String())
}
// append default environment variables
environment := map[string]string{}
for k, v := range container.Environment {
environment[k] = v
}
for k, v := range c.env {
switch v {
case "", "0", "false":
continue
default:
environment[k] = v
}
}
environment["CI_WORKSPACE"] = path.Join(c.base, c.path)
2017-07-21 21:52:52 +00:00
if section == "services" || container.Detached {
detached = true
2017-03-05 07:56:08 +00:00
}
if !detached || len(container.Commands) != 0 {
2017-07-21 21:52:52 +00:00
workingdir = path.Join(c.base, c.path)
2017-03-05 07:56:08 +00:00
}
if !detached {
if err := paramsToEnv(container.Settings, environment, c.secrets); err != nil {
log.Error().Err(err).Msg("paramsToEnv")
}
2017-03-05 07:56:08 +00:00
}
2017-07-21 21:52:52 +00:00
if len(container.Commands) != 0 {
2017-09-08 00:43:33 +00:00
if c.metadata.Sys.Arch == "windows/amd64" {
2019-04-06 13:44:04 +00:00
entrypoint = []string{"powershell", "-noprofile", "-noninteractive", "-command"}
command = []string{"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($Env:CI_SCRIPT)) | iex"}
2017-09-08 00:43:33 +00:00
environment["CI_SCRIPT"] = generateScriptWindows(container.Commands)
2019-04-06 13:44:04 +00:00
environment["HOME"] = "c:\\root"
environment["SHELL"] = "powershell.exe"
2017-09-08 00:43:33 +00:00
} else {
entrypoint = []string{"/bin/sh", "-c"}
command = []string{"echo $CI_SCRIPT | base64 -d | /bin/sh -e"}
environment["CI_SCRIPT"] = generateScriptPosix(container.Commands)
environment["HOME"] = "/root"
environment["SHELL"] = "/bin/sh"
}
2017-03-05 07:56:08 +00:00
}
2017-07-21 21:52:52 +00:00
if matchImage(container.Image, c.escalated...) {
privileged = true
entrypoint = []string{}
command = []string{}
}
2017-04-06 16:04:25 +00:00
authConfig := backend.Auth{
Username: container.AuthConfig.Username,
Password: container.AuthConfig.Password,
Email: container.AuthConfig.Email,
}
for _, registry := range c.registries {
if matchHostname(image, registry.Hostname) {
authConfig.Username = registry.Username
authConfig.Password = registry.Password
authConfig.Email = registry.Email
break
}
}
2017-04-10 10:39:50 +00:00
for _, requested := range container.Secrets.Secrets {
secret, ok := c.secrets[strings.ToLower(requested.Source)]
if ok && (len(secret.Match) == 0 || matchImage(image, secret.Match...)) {
environment[strings.ToUpper(requested.Target)] = secret.Value
}
}
2017-06-22 19:06:28 +00:00
memSwapLimit := int64(container.MemSwapLimit)
if c.reslimit.MemSwapLimit != 0 {
memSwapLimit = c.reslimit.MemSwapLimit
}
memLimit := int64(container.MemLimit)
if c.reslimit.MemLimit != 0 {
memLimit = c.reslimit.MemLimit
}
shmSize := int64(container.ShmSize)
if c.reslimit.ShmSize != 0 {
shmSize = c.reslimit.ShmSize
}
cpuQuota := int64(container.CPUQuota)
if c.reslimit.CPUQuota != 0 {
cpuQuota = c.reslimit.CPUQuota
}
cpuShares := int64(container.CPUShares)
if c.reslimit.CPUShares != 0 {
cpuShares = c.reslimit.CPUShares
}
cpuSet := container.CPUSet
if c.reslimit.CPUSet != "" {
cpuSet = c.reslimit.CPUSet
}
2017-03-05 07:56:08 +00:00
return &backend.Step{
Name: name,
Alias: container.Name,
Image: image,
Pull: container.Pull,
Detached: detached,
Privileged: privileged,
WorkingDir: workingdir,
Environment: environment,
Labels: container.Labels,
Entrypoint: entrypoint,
Command: command,
ExtraHosts: container.ExtraHosts,
Volumes: volumes,
2017-09-08 00:43:33 +00:00
Tmpfs: container.Tmpfs,
2017-03-05 07:56:08 +00:00
Devices: container.Devices,
Networks: networks,
DNS: container.DNS,
DNSSearch: container.DNSSearch,
2017-06-22 19:06:28 +00:00
MemSwapLimit: memSwapLimit,
MemLimit: memLimit,
ShmSize: shmSize,
2017-11-17 22:49:01 +00:00
Sysctls: container.Sysctls,
2017-06-22 19:06:28 +00:00
CPUQuota: cpuQuota,
CPUShares: cpuShares,
CPUSet: cpuSet,
2017-04-06 16:04:25 +00:00
AuthConfig: authConfig,
OnSuccess: container.Constraints.Status.Match("success"),
2017-03-05 07:56:08 +00:00
OnFailure: (len(container.Constraints.Status.Include)+
len(container.Constraints.Status.Exclude) != 0) &&
container.Constraints.Status.Match("failure"),
NetworkMode: networkMode,
IpcMode: ipcMode,
2017-03-05 07:56:08 +00:00
}
}