From 7b97e27fea1c2ba43ddce1588b03bc7b21cecc66 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 10 Jul 2023 17:59:25 +0200 Subject: [PATCH] docker: use uuid instead of name as identifyer (#1967) close #1959 --- pipeline/backend/docker/convert.go | 4 ++++ pipeline/backend/docker/docker.go | 26 ++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/pipeline/backend/docker/convert.go b/pipeline/backend/docker/convert.go index 79251a47f..815b61a7a 100644 --- a/pipeline/backend/docker/convert.go +++ b/pipeline/backend/docker/convert.go @@ -54,6 +54,10 @@ func toConfig(step *types.Step) *container.Config { return config } +func toContainerName(step *types.Step) string { + return "wp_" + step.UUID +} + // returns a container host configuration. func toHostConfig(step *types.Step) *container.HostConfig { config := &container.HostConfig{ diff --git a/pipeline/backend/docker/docker.go b/pipeline/backend/docker/docker.go index 9f7fe8800..30e74fcdf 100644 --- a/pipeline/backend/docker/docker.go +++ b/pipeline/backend/docker/docker.go @@ -131,6 +131,7 @@ func (e *docker) Setup(_ context.Context, conf *backend.Config) error { func (e *docker) Exec(ctx context.Context, step *backend.Step) error { config := toConfig(step) hostConfig := toHostConfig(step) + containerName := toContainerName(step) // create pull options with encoded authorization credentials. pullopts := types.ImagePullOptions{} @@ -143,6 +144,7 @@ func (e *docker) Exec(ctx context.Context, step *backend.Step) error { if step.Pull { responseBody, perr := e.client.ImagePull(ctx, config.Image, pullopts) if perr == nil { + // TODO(1936): show image pull progress in web-ui fd, isTerminal := term.GetFdInfo(os.Stdout) if err := jsonmessage.DisplayJSONMessagesStream(responseBody, os.Stdout, fd, isTerminal, nil); err != nil { log.Error().Err(err).Msg("DisplayJSONMessagesStream") @@ -159,7 +161,7 @@ func (e *docker) Exec(ctx context.Context, step *backend.Step) error { // add default volumes to the host configuration hostConfig.Binds = utils.DedupStrings(append(hostConfig.Binds, e.volumes...)) - _, err := e.client.ContainerCreate(ctx, config, hostConfig, nil, nil, step.Name) + _, err := e.client.ContainerCreate(ctx, config, hostConfig, nil, nil, containerName) if client.IsErrNotFound(err) { // automatically pull and try to re-create the image if the // failure is caused because the image does not exist. @@ -167,13 +169,14 @@ func (e *docker) Exec(ctx context.Context, step *backend.Step) error { if perr != nil { return perr } + // TODO(1936): show image pull progress in web-ui fd, isTerminal := term.GetFdInfo(os.Stdout) if err := jsonmessage.DisplayJSONMessagesStream(responseBody, os.Stdout, fd, isTerminal, nil); err != nil { log.Error().Err(err).Msg("DisplayJSONMessagesStream") } responseBody.Close() - _, err = e.client.ContainerCreate(ctx, config, hostConfig, nil, nil, step.Name) + _, err = e.client.ContainerCreate(ctx, config, hostConfig, nil, nil, containerName) } if err != nil { return err @@ -181,7 +184,7 @@ func (e *docker) Exec(ctx context.Context, step *backend.Step) error { if len(step.NetworkMode) == 0 { for _, net := range step.Networks { - err = e.client.NetworkConnect(ctx, net.Name, step.Name, &network.EndpointSettings{ + err = e.client.NetworkConnect(ctx, net.Name, containerName, &network.EndpointSettings{ Aliases: net.Aliases, }) if err != nil { @@ -191,24 +194,26 @@ func (e *docker) Exec(ctx context.Context, step *backend.Step) error { // join the container to an existing network if e.network != "" { - err = e.client.NetworkConnect(ctx, e.network, step.Name, &network.EndpointSettings{}) + err = e.client.NetworkConnect(ctx, e.network, containerName, &network.EndpointSettings{}) if err != nil { return err } } } - return e.client.ContainerStart(ctx, step.Name, startOpts) + return e.client.ContainerStart(ctx, containerName, startOpts) } func (e *docker) Wait(ctx context.Context, step *backend.Step) (*backend.State, error) { - wait, errc := e.client.ContainerWait(ctx, step.Name, "") + containerName := toContainerName(step) + + wait, errc := e.client.ContainerWait(ctx, containerName, "") select { case <-wait: case <-errc: } - info, err := e.client.ContainerInspect(ctx, step.Name) + info, err := e.client.ContainerInspect(ctx, containerName) if err != nil { return nil, err } @@ -224,7 +229,7 @@ func (e *docker) Wait(ctx context.Context, step *backend.Step) (*backend.State, } func (e *docker) Tail(ctx context.Context, step *backend.Step) (io.ReadCloser, error) { - logs, err := e.client.ContainerLogs(ctx, step.Name, logsOpts) + logs, err := e.client.ContainerLogs(ctx, toContainerName(step), logsOpts) if err != nil { return nil, err } @@ -242,10 +247,11 @@ func (e *docker) Tail(ctx context.Context, step *backend.Step) (io.ReadCloser, e func (e *docker) Destroy(_ context.Context, conf *backend.Config) error { for _, stage := range conf.Stages { for _, step := range stage.Steps { - if err := e.client.ContainerKill(noContext, step.Name, "9"); err != nil && !isErrContainerNotFoundOrNotRunning(err) { + containerName := toContainerName(step) + if err := e.client.ContainerKill(noContext, containerName, "9"); err != nil && !isErrContainerNotFoundOrNotRunning(err) { log.Error().Err(err).Msgf("could not kill container '%s'", stage.Name) } - if err := e.client.ContainerRemove(noContext, step.Name, removeOpts); err != nil && !isErrContainerNotFoundOrNotRunning(err) { + if err := e.client.ContainerRemove(noContext, containerName, removeOpts); err != nil && !isErrContainerNotFoundOrNotRunning(err) { log.Error().Err(err).Msgf("could not remove container '%s'", stage.Name) } }