Handle image pull

This commit is contained in:
nemunaire 2021-05-09 18:02:48 +02:00
parent 1fd0a2d8f3
commit 8259aaa474
2 changed files with 91 additions and 0 deletions

79
engine/docker/images.go Normal file
View File

@ -0,0 +1,79 @@
package docker
import (
"context"
"io"
"os"
"strings"
"github.com/docker/docker/api/types"
)
func GetImagesList() ([]string, error) {
cli, err := newCli()
if err != nil {
return nil, err
}
imgs, err := cli.ImageList(context.Background(), types.ImageListOptions{})
if err != nil {
return nil, err
}
var ret []string
for _, img := range imgs {
ret = append(ret, img.RepoTags...)
}
return ret, nil
}
func normalizeImageName(image string) string {
if !strings.Contains(image, ":") {
image += ":latest"
}
return image
}
func HasImage(image string) (bool, error) {
cli, err := newCli()
if err != nil {
return false, err
}
image = normalizeImageName(image)
imgs, err := cli.ImageList(context.Background(), types.ImageListOptions{})
if err != nil {
return false, err
}
for _, img := range imgs {
for _, name := range img.RepoTags {
if name == image {
return true, nil
}
}
}
return false, nil
}
func PullImage(image string) error {
cli, err := newCli()
if err != nil {
return err
}
image = normalizeImageName(image)
reader, err := cli.ImagePull(context.Background(), image, types.ImagePullOptions{})
if err != nil {
return err
}
defer reader.Close()
_, err = io.Copy(os.Stdout, reader)
return err
}

12
jobs.go
View File

@ -30,6 +30,18 @@ func RunJob(jobtype string) (string, error) {
mnts = append(mnts, *myVolume) mnts = append(mnts, *myVolume)
} }
// Check if the image is here
hasimg, err := docker.HasImage(jobs[jobtype].Image)
if err != nil {
return "", err
}
if !hasimg {
err = docker.PullImage(jobs[jobtype].Image)
if err != nil {
return "", err
}
}
return docker.Run( return docker.Run(
jobtype, jobtype,
jobs[jobtype].Image, jobs[jobtype].Image,