2019-01-13 12:45:25 +08:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2020-02-07 14:17:58 +08:00
|
|
|
"context"
|
2021-05-06 00:37:17 +08:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2019-01-13 12:45:25 +08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
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"
|
2021-03-29 12:08:40 +08:00
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
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
|
2021-03-29 12:08:40 +08:00
|
|
|
Platform string
|
2021-05-06 00:37:17 +08:00
|
|
|
Username string
|
|
|
|
Password string
|
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 {
|
2021-03-29 12:08:40 +08:00
|
|
|
imageExists, err := ImageExistsLocally(ctx, input.Image, input.Platform)
|
2020-02-07 14:17:58 +08:00
|
|
|
log.Debugf("Image exists? %v", imageExists)
|
|
|
|
if err != nil {
|
2021-03-29 12:08:40 +08:00
|
|
|
return errors.WithMessagef(err, "unable to determine if image already exists for image %q (%s)", input.Image, input.Platform)
|
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)
|
2021-03-29 12:08:40 +08:00
|
|
|
logger.Debugf("pulling image '%v' (%s)", imageRef, input.Platform)
|
2019-01-13 12:45:25 +08:00
|
|
|
|
2020-05-04 12:15:42 +08:00
|
|
|
cli, err := GetDockerClient(ctx)
|
2019-01-13 12:45:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:37:17 +08:00
|
|
|
imagePullOptions, err := getImagePullOptions(ctx, input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := cli.ImagePull(ctx, imageRef, imagePullOptions)
|
|
|
|
|
2020-02-07 14:17:58 +08:00
|
|
|
_ = logDockerResponse(logger, reader, err != nil)
|
2019-01-13 12:45:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:37:17 +08:00
|
|
|
func getImagePullOptions(ctx context.Context, input NewDockerPullExecutorInput) (types.ImagePullOptions, error) {
|
|
|
|
imagePullOptions := types.ImagePullOptions{
|
|
|
|
Platform: input.Platform,
|
|
|
|
}
|
|
|
|
if input.Username != "" && input.Password != "" {
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
logger.Debugf("using authentication for docker pull")
|
|
|
|
|
|
|
|
authConfig := types.AuthConfig{
|
|
|
|
Username: input.Username,
|
|
|
|
Password: input.Password,
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedJSON, err := json.Marshal(authConfig)
|
|
|
|
if err != nil {
|
|
|
|
return imagePullOptions, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imagePullOptions.RegistryAuth = base64.URLEncoding.EncodeToString(encodedJSON)
|
|
|
|
}
|
|
|
|
|
|
|
|
return imagePullOptions, nil
|
|
|
|
}
|
|
|
|
|
2019-01-13 12:45:25 +08:00
|
|
|
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
|
|
|
|
}
|