mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-26 11:51:02 +00:00
parent
1eb3c033ea
commit
7b97e27fea
2 changed files with 20 additions and 10 deletions
|
@ -54,6 +54,10 @@ func toConfig(step *types.Step) *container.Config {
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func toContainerName(step *types.Step) string {
|
||||||
|
return "wp_" + step.UUID
|
||||||
|
}
|
||||||
|
|
||||||
// returns a container host configuration.
|
// returns a container host configuration.
|
||||||
func toHostConfig(step *types.Step) *container.HostConfig {
|
func toHostConfig(step *types.Step) *container.HostConfig {
|
||||||
config := &container.HostConfig{
|
config := &container.HostConfig{
|
||||||
|
|
|
@ -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 {
|
func (e *docker) Exec(ctx context.Context, step *backend.Step) error {
|
||||||
config := toConfig(step)
|
config := toConfig(step)
|
||||||
hostConfig := toHostConfig(step)
|
hostConfig := toHostConfig(step)
|
||||||
|
containerName := toContainerName(step)
|
||||||
|
|
||||||
// create pull options with encoded authorization credentials.
|
// create pull options with encoded authorization credentials.
|
||||||
pullopts := types.ImagePullOptions{}
|
pullopts := types.ImagePullOptions{}
|
||||||
|
@ -143,6 +144,7 @@ func (e *docker) Exec(ctx context.Context, step *backend.Step) error {
|
||||||
if step.Pull {
|
if step.Pull {
|
||||||
responseBody, perr := e.client.ImagePull(ctx, config.Image, pullopts)
|
responseBody, perr := e.client.ImagePull(ctx, config.Image, pullopts)
|
||||||
if perr == nil {
|
if perr == nil {
|
||||||
|
// TODO(1936): show image pull progress in web-ui
|
||||||
fd, isTerminal := term.GetFdInfo(os.Stdout)
|
fd, isTerminal := term.GetFdInfo(os.Stdout)
|
||||||
if err := jsonmessage.DisplayJSONMessagesStream(responseBody, os.Stdout, fd, isTerminal, nil); err != nil {
|
if err := jsonmessage.DisplayJSONMessagesStream(responseBody, os.Stdout, fd, isTerminal, nil); err != nil {
|
||||||
log.Error().Err(err).Msg("DisplayJSONMessagesStream")
|
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
|
// add default volumes to the host configuration
|
||||||
hostConfig.Binds = utils.DedupStrings(append(hostConfig.Binds, e.volumes...))
|
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) {
|
if client.IsErrNotFound(err) {
|
||||||
// automatically pull and try to re-create the image if the
|
// automatically pull and try to re-create the image if the
|
||||||
// failure is caused because the image does not exist.
|
// 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 {
|
if perr != nil {
|
||||||
return perr
|
return perr
|
||||||
}
|
}
|
||||||
|
// TODO(1936): show image pull progress in web-ui
|
||||||
fd, isTerminal := term.GetFdInfo(os.Stdout)
|
fd, isTerminal := term.GetFdInfo(os.Stdout)
|
||||||
if err := jsonmessage.DisplayJSONMessagesStream(responseBody, os.Stdout, fd, isTerminal, nil); err != nil {
|
if err := jsonmessage.DisplayJSONMessagesStream(responseBody, os.Stdout, fd, isTerminal, nil); err != nil {
|
||||||
log.Error().Err(err).Msg("DisplayJSONMessagesStream")
|
log.Error().Err(err).Msg("DisplayJSONMessagesStream")
|
||||||
}
|
}
|
||||||
responseBody.Close()
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -181,7 +184,7 @@ func (e *docker) Exec(ctx context.Context, step *backend.Step) error {
|
||||||
|
|
||||||
if len(step.NetworkMode) == 0 {
|
if len(step.NetworkMode) == 0 {
|
||||||
for _, net := range step.Networks {
|
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,
|
Aliases: net.Aliases,
|
||||||
})
|
})
|
||||||
if err != nil {
|
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
|
// join the container to an existing network
|
||||||
if e.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 {
|
if err != nil {
|
||||||
return err
|
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) {
|
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 {
|
select {
|
||||||
case <-wait:
|
case <-wait:
|
||||||
case <-errc:
|
case <-errc:
|
||||||
}
|
}
|
||||||
|
|
||||||
info, err := e.client.ContainerInspect(ctx, step.Name)
|
info, err := e.client.ContainerInspect(ctx, containerName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
func (e *docker) Destroy(_ context.Context, conf *backend.Config) error {
|
||||||
for _, stage := range conf.Stages {
|
for _, stage := range conf.Stages {
|
||||||
for _, step := range stage.Steps {
|
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)
|
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)
|
log.Error().Err(err).Msgf("could not remove container '%s'", stage.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue