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 (
|
|
|
|
"context"
|
|
|
|
"io"
|
2019-04-07 12:58:40 +00:00
|
|
|
"os"
|
2022-04-29 13:15:32 +00:00
|
|
|
"strconv"
|
2022-03-08 15:21:43 +00:00
|
|
|
"strings"
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2021-09-26 12:43:14 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
|
|
|
"github.com/docker/docker/api/types/volume"
|
2021-09-26 19:51:59 +00:00
|
|
|
"github.com/moby/moby/client"
|
|
|
|
"github.com/moby/moby/pkg/jsonmessage"
|
|
|
|
"github.com/moby/moby/pkg/stdcopy"
|
|
|
|
"github.com/moby/term"
|
2021-11-23 14:36:52 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-09-26 12:43:14 +00:00
|
|
|
|
2021-11-26 02:34:48 +00:00
|
|
|
backend "github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
2017-03-05 07:56:08 +00:00
|
|
|
)
|
|
|
|
|
2021-11-27 01:29:14 +00:00
|
|
|
type docker struct {
|
2022-04-29 13:15:32 +00:00
|
|
|
client client.APIClient
|
|
|
|
enableIPv6 bool
|
|
|
|
network string
|
2022-09-26 14:59:26 +00:00
|
|
|
volumes []string
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 01:29:14 +00:00
|
|
|
// make sure docker implements Engine
|
|
|
|
var _ backend.Engine = &docker{}
|
|
|
|
|
2021-11-26 02:34:48 +00:00
|
|
|
// New returns a new Docker Engine.
|
|
|
|
func New() backend.Engine {
|
2021-11-27 01:29:14 +00:00
|
|
|
return &docker{
|
2021-11-26 02:34:48 +00:00
|
|
|
client: nil,
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 01:29:14 +00:00
|
|
|
func (e *docker) Name() string {
|
2021-11-26 02:34:48 +00:00
|
|
|
return "docker"
|
|
|
|
}
|
|
|
|
|
2021-11-27 01:29:14 +00:00
|
|
|
func (e *docker) IsAvailable() bool {
|
2022-02-08 23:08:20 +00:00
|
|
|
if os.Getenv("DOCKER_HOST") != "" {
|
|
|
|
return true
|
|
|
|
}
|
2021-11-27 01:29:14 +00:00
|
|
|
_, err := os.Stat("/var/run/docker.sock")
|
|
|
|
return err == nil
|
2021-11-26 02:34:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load new client for Docker Engine using environment variables.
|
2021-11-27 01:29:14 +00:00
|
|
|
func (e *docker) Load() error {
|
2021-09-26 19:51:59 +00:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2017-03-05 07:56:08 +00:00
|
|
|
if err != nil {
|
2021-11-26 02:34:48 +00:00
|
|
|
return err
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2021-11-26 02:34:48 +00:00
|
|
|
e.client = cli
|
|
|
|
|
2022-04-29 13:15:32 +00:00
|
|
|
e.enableIPv6, _ = strconv.ParseBool(os.Getenv("WOODPECKER_BACKEND_DOCKER_ENABLE_IPV6"))
|
|
|
|
|
|
|
|
e.network = os.Getenv("WOODPECKER_BACKEND_DOCKER_NETWORK")
|
|
|
|
|
2022-09-26 14:59:26 +00:00
|
|
|
volumes := strings.Split(os.Getenv("WOODPECKER_BACKEND_DOCKER_VOLUMES"), ",")
|
|
|
|
e.volumes = make([]string, 0, len(volumes))
|
|
|
|
// Validate provided volume definitions
|
|
|
|
for _, v := range volumes {
|
2022-09-27 09:43:35 +00:00
|
|
|
if v == "" {
|
|
|
|
continue
|
|
|
|
}
|
2022-09-26 14:59:26 +00:00
|
|
|
parts, err := splitVolumeParts(v)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("invalid volume '%s' provided in WOODPECKER_BACKEND_DOCKER_VOLUMES", v)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
e.volumes = append(e.volumes, strings.Join(parts, ":"))
|
|
|
|
}
|
|
|
|
|
2021-11-26 02:34:48 +00:00
|
|
|
return nil
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 01:29:14 +00:00
|
|
|
func (e *docker) Setup(_ context.Context, conf *backend.Config) error {
|
2017-03-05 07:56:08 +00:00
|
|
|
for _, vol := range conf.Volumes {
|
2021-09-26 12:43:14 +00:00
|
|
|
_, err := e.client.VolumeCreate(noContext, volume.VolumeCreateBody{
|
2017-03-05 07:56:08 +00:00
|
|
|
Name: vol.Name,
|
|
|
|
Driver: vol.Driver,
|
|
|
|
DriverOpts: vol.DriverOpts,
|
|
|
|
// Labels: defaultLabels,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
for _, n := range conf.Networks {
|
|
|
|
_, err := e.client.NetworkCreate(noContext, n.Name, types.NetworkCreate{
|
2022-04-29 13:15:32 +00:00
|
|
|
Driver: n.Driver,
|
|
|
|
Options: n.DriverOpts,
|
|
|
|
EnableIPv6: e.enableIPv6,
|
2017-03-05 07:56:08 +00:00
|
|
|
// Labels: defaultLabels,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
func (e *docker) Exec(ctx context.Context, step *backend.Step) error {
|
|
|
|
config := toConfig(step)
|
|
|
|
hostConfig := toHostConfig(step)
|
2017-03-05 07:56:08 +00:00
|
|
|
|
|
|
|
// create pull options with encoded authorization credentials.
|
|
|
|
pullopts := types.ImagePullOptions{}
|
2022-10-28 15:38:53 +00:00
|
|
|
if step.AuthConfig.Username != "" && step.AuthConfig.Password != "" {
|
|
|
|
pullopts.RegistryAuth, _ = encodeAuthToBase64(step.AuthConfig)
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// automatically pull the latest version of the image if requested
|
|
|
|
// by the process configuration.
|
2022-10-28 15:38:53 +00:00
|
|
|
if step.Pull {
|
2019-04-07 12:58:40 +00:00
|
|
|
responseBody, perr := e.client.ImagePull(ctx, config.Image, pullopts)
|
2017-03-05 07:56:08 +00:00
|
|
|
if perr == nil {
|
2019-04-07 12:58:40 +00:00
|
|
|
defer responseBody.Close()
|
2020-05-18 14:46:13 +00:00
|
|
|
|
2019-04-07 12:58:40 +00:00
|
|
|
fd, isTerminal := term.GetFdInfo(os.Stdout)
|
2021-11-23 14:36:52 +00:00
|
|
|
if err := jsonmessage.DisplayJSONMessagesStream(responseBody, os.Stdout, fd, isTerminal, nil); err != nil {
|
|
|
|
log.Error().Err(err).Msg("DisplayJSONMessagesStream")
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2022-09-26 12:52:28 +00:00
|
|
|
// Fix "Show warning when fail to auth to docker registry"
|
|
|
|
// (https://web.archive.org/web/20201023145804/https://github.com/drone/drone/issues/1917)
|
2022-10-28 15:38:53 +00:00
|
|
|
if perr != nil && step.AuthConfig.Password != "" {
|
2017-03-05 07:56:08 +00:00
|
|
|
return perr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 14:59:26 +00:00
|
|
|
// add default volumes to the host configuration
|
|
|
|
hostConfig.Binds = append(hostConfig.Binds, e.volumes...)
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
_, err := e.client.ContainerCreate(ctx, config, hostConfig, nil, nil, step.Name)
|
2021-09-26 12:43:14 +00:00
|
|
|
if client.IsErrNotFound(err) {
|
2017-03-05 07:56:08 +00:00
|
|
|
// automatically pull and try to re-create the image if the
|
|
|
|
// failure is caused because the image does not exist.
|
2019-04-07 12:58:40 +00:00
|
|
|
responseBody, perr := e.client.ImagePull(ctx, config.Image, pullopts)
|
2017-03-05 07:56:08 +00:00
|
|
|
if perr != nil {
|
|
|
|
return perr
|
|
|
|
}
|
2019-04-07 12:58:40 +00:00
|
|
|
defer responseBody.Close()
|
|
|
|
fd, isTerminal := term.GetFdInfo(os.Stdout)
|
2021-11-23 14:36:52 +00:00
|
|
|
if err := jsonmessage.DisplayJSONMessagesStream(responseBody, os.Stdout, fd, isTerminal, nil); err != nil {
|
|
|
|
log.Error().Err(err).Msg("DisplayJSONMessagesStream")
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
_, err = e.client.ContainerCreate(ctx, config, hostConfig, nil, nil, step.Name)
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
if len(step.NetworkMode) == 0 {
|
|
|
|
for _, net := range step.Networks {
|
|
|
|
err = e.client.NetworkConnect(ctx, net.Name, step.Name, &network.EndpointSettings{
|
2017-06-03 15:29:02 +00:00
|
|
|
Aliases: net.Aliases,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 13:15:32 +00:00
|
|
|
// join the container to an existing network
|
|
|
|
if e.network != "" {
|
2022-10-28 15:38:53 +00:00
|
|
|
err = e.client.NetworkConnect(ctx, e.network, step.Name, &network.EndpointSettings{})
|
2022-04-29 13:15:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
return e.client.ContainerStart(ctx, step.Name, startOpts)
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
func (e *docker) Wait(ctx context.Context, step *backend.Step) (*backend.State, error) {
|
|
|
|
wait, errc := e.client.ContainerWait(ctx, step.Name, "")
|
2020-05-18 14:46:13 +00:00
|
|
|
select {
|
|
|
|
case <-wait:
|
|
|
|
case <-errc:
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
info, err := e.client.ContainerInspect(ctx, step.Name)
|
2017-03-05 07:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-25 16:15:36 +00:00
|
|
|
// if info.State.Running {
|
|
|
|
// TODO
|
|
|
|
// }
|
2017-03-05 07:56:08 +00:00
|
|
|
|
|
|
|
return &backend.State{
|
|
|
|
Exited: true,
|
|
|
|
ExitCode: info.State.ExitCode,
|
|
|
|
OOMKilled: info.State.OOMKilled,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
func (e *docker) Tail(ctx context.Context, step *backend.Step) (io.ReadCloser, error) {
|
|
|
|
logs, err := e.client.ContainerLogs(ctx, step.Name, logsOpts)
|
2017-03-05 07:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rc, wc := io.Pipe()
|
|
|
|
|
2021-11-23 14:36:52 +00:00
|
|
|
// de multiplex 'logs' who contains two streams, previously multiplexed together using StdWriter
|
2017-03-05 07:56:08 +00:00
|
|
|
go func() {
|
2021-11-23 14:36:52 +00:00
|
|
|
_, _ = stdcopy.StdCopy(wc, wc, logs)
|
|
|
|
_ = logs.Close()
|
|
|
|
_ = wc.Close()
|
|
|
|
_ = rc.Close()
|
2017-03-05 07:56:08 +00:00
|
|
|
}()
|
|
|
|
return rc, nil
|
|
|
|
}
|
|
|
|
|
2021-11-27 01:29:14 +00:00
|
|
|
func (e *docker) Destroy(_ context.Context, conf *backend.Config) error {
|
2017-03-05 07:56:08 +00:00
|
|
|
for _, stage := range conf.Stages {
|
|
|
|
for _, step := range stage.Steps {
|
2022-03-08 15:21:43 +00:00
|
|
|
if err := e.client.ContainerKill(noContext, step.Name, "9"); err != nil && !isErrContainerNotFoundOrNotRunning(err) {
|
2021-11-23 14:36:52 +00:00
|
|
|
log.Error().Err(err).Msgf("could not kill container '%s'", stage.Name)
|
|
|
|
}
|
2022-03-08 15:21:43 +00:00
|
|
|
if err := e.client.ContainerRemove(noContext, step.Name, removeOpts); err != nil && !isErrContainerNotFoundOrNotRunning(err) {
|
2021-11-23 14:36:52 +00:00
|
|
|
log.Error().Err(err).Msgf("could not remove container '%s'", stage.Name)
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
for _, v := range conf.Volumes {
|
2021-11-23 14:36:52 +00:00
|
|
|
if err := e.client.VolumeRemove(noContext, v.Name, true); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("could not remove volume '%s'", v.Name)
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
for _, n := range conf.Networks {
|
2021-11-23 14:36:52 +00:00
|
|
|
if err := e.client.NetworkRemove(noContext, n.Name); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("could not remove network '%s'", n.Name)
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
noContext = context.Background()
|
|
|
|
|
|
|
|
startOpts = types.ContainerStartOptions{}
|
|
|
|
|
|
|
|
removeOpts = types.ContainerRemoveOptions{
|
|
|
|
RemoveVolumes: true,
|
|
|
|
RemoveLinks: false,
|
|
|
|
Force: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
logsOpts = types.ContainerLogsOptions{
|
|
|
|
Follow: true,
|
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Details: false,
|
|
|
|
Timestamps: false,
|
|
|
|
}
|
|
|
|
)
|
2022-03-08 15:21:43 +00:00
|
|
|
|
|
|
|
func isErrContainerNotFoundOrNotRunning(err error) bool {
|
|
|
|
// Error response from daemon: Cannot kill container: ...: No such container: ...
|
|
|
|
// Error response from daemon: Cannot kill container: ...: Container ... is not running"
|
|
|
|
// Error: No such container: ...
|
|
|
|
return err != nil && (strings.Contains(err.Error(), "No such container") || strings.Contains(err.Error(), "is not running"))
|
|
|
|
}
|