2022-10-30 23:26:49 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2024-01-09 05:01:34 +00:00
|
|
|
"maps"
|
2019-04-06 13:44:04 +00:00
|
|
|
"regexp"
|
2017-03-05 07:56:08 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-09-26 12:43:14 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2021-09-26 19:51:59 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/common"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
|
2017-03-05 07:56:08 +00:00
|
|
|
)
|
|
|
|
|
2024-03-15 17:00:25 +00:00
|
|
|
// Valid container volumes must have at least two components, source and destination.
|
|
|
|
const minVolumeComponents = 2
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
// returns a container configuration.
|
2023-11-01 14:38:37 +00:00
|
|
|
func (e *docker) toConfig(step *types.Step) *container.Config {
|
2017-03-05 07:56:08 +00:00
|
|
|
config := &container.Config{
|
2024-01-09 05:01:34 +00:00
|
|
|
Image: step.Image,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"wp_uuid": step.UUID,
|
|
|
|
"wp_step": step.Name,
|
|
|
|
},
|
2022-10-28 15:38:53 +00:00
|
|
|
WorkingDir: step.WorkingDir,
|
2017-03-05 07:56:08 +00:00
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
2024-07-21 19:28:10 +00:00
|
|
|
Volumes: toVol(step.Volumes),
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2024-01-09 15:28:10 +00:00
|
|
|
configEnv := make(map[string]string)
|
|
|
|
maps.Copy(configEnv, step.Environment)
|
2022-10-30 23:26:49 +00:00
|
|
|
|
2024-05-02 12:52:01 +00:00
|
|
|
if len(step.Commands) > 0 {
|
|
|
|
env, entry := common.GenerateContainerConf(step.Commands, e.info.OSType)
|
2022-10-30 23:26:49 +00:00
|
|
|
for k, v := range env {
|
2024-01-09 15:28:10 +00:00
|
|
|
configEnv[k] = v
|
2022-10-30 23:26:49 +00:00
|
|
|
}
|
|
|
|
config.Entrypoint = entry
|
2024-05-02 12:52:01 +00:00
|
|
|
}
|
|
|
|
if len(step.Entrypoint) > 0 {
|
|
|
|
config.Entrypoint = step.Entrypoint
|
2022-10-30 23:26:49 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 15:28:10 +00:00
|
|
|
if len(configEnv) != 0 {
|
|
|
|
config.Env = toEnv(configEnv)
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2023-07-10 15:59:25 +00:00
|
|
|
func toContainerName(step *types.Step) string {
|
|
|
|
return "wp_" + step.UUID
|
|
|
|
}
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
// returns a container host configuration.
|
2022-10-28 15:38:53 +00:00
|
|
|
func toHostConfig(step *types.Step) *container.HostConfig {
|
2017-03-05 07:56:08 +00:00
|
|
|
config := &container.HostConfig{
|
|
|
|
Resources: container.Resources{
|
2022-10-28 15:38:53 +00:00
|
|
|
CPUQuota: step.CPUQuota,
|
|
|
|
CPUShares: step.CPUShares,
|
|
|
|
CpusetCpus: step.CPUSet,
|
|
|
|
Memory: step.MemLimit,
|
|
|
|
MemorySwap: step.MemSwapLimit,
|
2017-03-05 07:56:08 +00:00
|
|
|
},
|
2017-06-22 19:06:28 +00:00
|
|
|
LogConfig: container.LogConfig{
|
|
|
|
Type: "json-file",
|
|
|
|
},
|
2022-10-28 15:38:53 +00:00
|
|
|
Privileged: step.Privileged,
|
|
|
|
ShmSize: step.ShmSize,
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2017-06-22 19:06:28 +00:00
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
if len(step.NetworkMode) != 0 {
|
|
|
|
config.NetworkMode = container.NetworkMode(step.NetworkMode)
|
2017-09-08 00:43:33 +00:00
|
|
|
}
|
2022-10-28 15:38:53 +00:00
|
|
|
if len(step.DNS) != 0 {
|
|
|
|
config.DNS = step.DNS
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2022-10-28 15:38:53 +00:00
|
|
|
if len(step.DNSSearch) != 0 {
|
|
|
|
config.DNSSearch = step.DNSSearch
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2023-12-22 23:42:30 +00:00
|
|
|
extraHosts := []string{}
|
|
|
|
for _, hostAlias := range step.ExtraHosts {
|
|
|
|
extraHosts = append(extraHosts, hostAlias.Name+":"+hostAlias.IP)
|
|
|
|
}
|
2022-10-28 15:38:53 +00:00
|
|
|
if len(step.ExtraHosts) != 0 {
|
2023-12-22 23:42:30 +00:00
|
|
|
config.ExtraHosts = extraHosts
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2022-10-28 15:38:53 +00:00
|
|
|
if len(step.Devices) != 0 {
|
|
|
|
config.Devices = toDev(step.Devices)
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2022-10-28 15:38:53 +00:00
|
|
|
if len(step.Volumes) != 0 {
|
|
|
|
config.Binds = step.Volumes
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2017-09-08 00:43:33 +00:00
|
|
|
config.Tmpfs = map[string]string{}
|
2022-10-28 15:38:53 +00:00
|
|
|
for _, path := range step.Tmpfs {
|
2021-11-23 14:36:52 +00:00
|
|
|
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{} {
|
2024-07-21 19:28:10 +00:00
|
|
|
if len(paths) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
set := make(map[string]struct{})
|
2017-03-05 07:56:08 +00:00
|
|
|
for _, path := range paths {
|
2019-04-06 13:44:04 +00:00
|
|
|
parts, err := splitVolumeParts(path)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-03-15 17:00:25 +00:00
|
|
|
if len(parts) < minVolumeComponents {
|
2017-03-05 07:56:08 +00:00
|
|
|
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 {
|
2024-07-21 19:28:10 +00:00
|
|
|
if k != "" {
|
|
|
|
envs = append(envs, k+"="+v)
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
return envs
|
|
|
|
}
|
|
|
|
|
2024-03-15 17:00:25 +00:00
|
|
|
// toDev converts a slice of volume paths to a set of device mappings for
|
|
|
|
// use in a Docker container config. It handles splitting the volume paths
|
|
|
|
// into host and container paths, and setting default permissions.
|
2017-03-05 07:56:08 +00:00
|
|
|
func toDev(paths []string) []container.DeviceMapping {
|
|
|
|
var devices []container.DeviceMapping
|
2024-03-15 17:00:25 +00:00
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
for _, path := range paths {
|
2019-04-06 13:44:04 +00:00
|
|
|
parts, err := splitVolumeParts(path)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-03-15 17:00:25 +00:00
|
|
|
if len(parts) < minVolumeComponents {
|
2017-03-05 07:56:08 +00:00
|
|
|
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.
|
2021-11-26 02:34:48 +00:00
|
|
|
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
|
|
|
|
2024-03-15 17:00:25 +00:00
|
|
|
// splitVolumeParts splits a volume string into its constituent parts.
|
|
|
|
//
|
|
|
|
// The parts are:
|
|
|
|
//
|
|
|
|
// 1. The path on the host machine
|
|
|
|
// 2. The path inside the container
|
|
|
|
// 3. The read/write mode
|
|
|
|
//
|
|
|
|
// It handles Windows and Linux style volume paths.
|
2019-04-06 13:44:04 +00:00
|
|
|
func splitVolumeParts(volumeParts string) ([]string, error) {
|
2024-05-24 20:35:04 +00:00
|
|
|
// cspell:disable-next-line
|
2019-04-06 13:44:04 +00:00
|
|
|
pattern := `^((?:[\w]\:)?[^\:]*)\:((?:[\w]\:)?[^\:]*)(?:\:([rwom]*))?`
|
|
|
|
r, err := regexp.Compile(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
if r.MatchString(volumeParts) {
|
|
|
|
results := r.FindStringSubmatch(volumeParts)[1:]
|
2021-09-24 14:29:26 +00:00
|
|
|
var cleanResults []string
|
2019-04-06 13:44:04 +00:00
|
|
|
for _, item := range results {
|
|
|
|
if item != "" {
|
|
|
|
cleanResults = append(cleanResults, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cleanResults, nil
|
|
|
|
}
|
2021-12-01 13:22:06 +00:00
|
|
|
return strings.Split(volumeParts, ":"), nil
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|