2019-01-13 12:45:25 +08:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2020-02-07 14:17:58 +08:00
|
|
|
"context"
|
2019-01-13 12:45:25 +08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/client"
|
2020-02-05 08:38:41 +08:00
|
|
|
"github.com/nektos/act/pkg/common"
|
2020-04-23 14:04:28 +08:00
|
|
|
"github.com/pkg/errors"
|
2020-02-07 14:17:58 +08:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-01-13 12:45:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewDockerPullExecutorInput the input for the NewDockerPullExecutor function
|
|
|
|
type NewDockerPullExecutorInput struct {
|
2020-02-07 14:17:58 +08:00
|
|
|
Image string
|
|
|
|
ForcePull bool
|
2019-01-13 12:45:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDockerPullExecutor function to create a run executor for the container
|
|
|
|
func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor {
|
2020-02-07 14:17:58 +08:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
2020-02-24 07:01:25 +08:00
|
|
|
logger.Debugf("%sdocker pull %v", logPrefix, input.Image)
|
2019-01-13 12:45:25 +08:00
|
|
|
|
2020-02-07 14:17:58 +08:00
|
|
|
if common.Dryrun(ctx) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pull := input.ForcePull
|
|
|
|
if !pull {
|
|
|
|
imageExists, err := ImageExistsLocally(ctx, input.Image)
|
|
|
|
log.Debugf("Image exists? %v", imageExists)
|
|
|
|
if err != nil {
|
2020-04-23 14:04:28 +08:00
|
|
|
return errors.WithMessagef(err, "unable to determine if image already exists for image %q", input.Image)
|
2020-02-07 14:17:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if !imageExists {
|
|
|
|
pull = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pull {
|
2019-01-13 12:45:25 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
imageRef := cleanImage(input.Image)
|
2020-02-07 14:17:58 +08:00
|
|
|
logger.Debugf("pulling image '%v'", imageRef)
|
2019-01-13 12:45:25 +08:00
|
|
|
|
2019-01-16 13:54:37 +08:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2019-01-13 12:45:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-07 14:17:58 +08:00
|
|
|
cli.NegotiateAPIVersion(ctx)
|
2019-01-13 12:45:25 +08:00
|
|
|
|
2020-02-07 14:17:58 +08:00
|
|
|
reader, err := cli.ImagePull(ctx, imageRef, types.ImagePullOptions{})
|
|
|
|
_ = logDockerResponse(logger, reader, err != nil)
|
2019-01-13 12:45:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanImage(image string) string {
|
|
|
|
imageParts := len(strings.Split(image, "/"))
|
|
|
|
if imageParts == 1 {
|
|
|
|
image = fmt.Sprintf("docker.io/library/%s", image)
|
|
|
|
} else if imageParts == 2 {
|
|
|
|
image = fmt.Sprintf("docker.io/%s", image)
|
|
|
|
}
|
|
|
|
|
|
|
|
return image
|
|
|
|
}
|