2019-01-12 22:45:25 -06:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2020-02-07 00:17:58 -06:00
|
|
|
"context"
|
2019-01-12 22:45:25 -06:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/client"
|
2020-02-04 18:38:41 -06:00
|
|
|
"github.com/nektos/act/pkg/common"
|
2020-02-07 00:17:58 -06:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-01-12 22:45:25 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewDockerPullExecutorInput the input for the NewDockerPullExecutor function
|
|
|
|
type NewDockerPullExecutorInput struct {
|
2020-02-07 00:17:58 -06:00
|
|
|
Image string
|
|
|
|
ForcePull bool
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDockerPullExecutor function to create a run executor for the container
|
|
|
|
func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor {
|
2020-02-07 00:17:58 -06:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
2020-02-11 11:10:35 -06:00
|
|
|
logger.Infof("%sdocker pull %v", logPrefix, input.Image)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06: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 {
|
|
|
|
return fmt.Errorf("unable to determine if image already exists for image %q", input.Image)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !imageExists {
|
|
|
|
pull = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pull {
|
2019-01-12 22:45:25 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
imageRef := cleanImage(input.Image)
|
2020-02-07 00:17:58 -06:00
|
|
|
logger.Debugf("pulling image '%v'", imageRef)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2019-01-15 23:54:37 -06:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
cli.NegotiateAPIVersion(ctx)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
reader, err := cli.ImagePull(ctx, imageRef, types.ImagePullOptions{})
|
|
|
|
_ = logDockerResponse(logger, reader, err != nil)
|
2019-01-12 22:45:25 -06: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
|
|
|
|
}
|