Skip to content

Commit

Permalink
Merge branch 'master' into frontend_step_types
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 authored Jul 10, 2023
2 parents 11bf6c1 + 7b97e27 commit 92a8986
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
4 changes: 4 additions & 0 deletions pipeline/backend/docker/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
26 changes: 16 additions & 10 deletions pipeline/backend/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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")
Expand All @@ -159,29 +161,30 @@ 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.
responseBody, perr := e.client.ImagePull(ctx, config.Image, pullopts)
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
}

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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)
}
}
Expand Down

0 comments on commit 92a8986

Please sign in to comment.