Initial functionality to pull a docker image when booting an environment

This commit is contained in:
Dane Everitt 2019-12-07 14:19:51 -08:00
parent 52ca0667ca
commit 4a68eabd1b
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53

View File

@ -490,6 +490,32 @@ func (d *DockerEnvironment) DisableResourcePolling() error {
return errors.WithStack(err)
}
// Pulls the image from Docker.
//
// @todo handle authorization & local images
func (d *DockerEnvironment) ensureImageExists(c *client.Client) error {
out, err := c.ImagePull(context.Background(), d.Server.Container.Image, types.ImagePullOptions{All: false})
if err != nil {
return err
}
defer out.Close()
zap.S().Debugw("pulling docker image... this could take a bit of time", zap.String("image", d.Server.Container.Image))
// I'm not sure what the best approach here is, but this will block execution until the image
// is done being pulled, which is what we need.
scanner := bufio.NewScanner(out)
for scanner.Scan() {
continue
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
// Creates a new container for the server using all of the data that is currently
// available for it. If the container already exists it will be returned.
//
@ -515,6 +541,11 @@ func (d *DockerEnvironment) Create() error {
return errors.WithStack(err)
}
// Try to pull the requested image before creating the container.
if err := d.ensureImageExists(cli); err != nil {
return errors.WithStack(err)
}
conf := &container.Config{
Hostname: "container",
User: strconv.Itoa(d.User),