From 808e337a8cf16e2dbc0e098a64ac8116fb242f77 Mon Sep 17 00:00:00 2001 From: skanehira Date: Wed, 6 May 2020 14:25:33 +0900 Subject: [PATCH] Pull image if without when create service Signed-off-by: skanehira --- compose-ref.go | 6 ++++++ internal/image.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 internal/image.go diff --git a/compose-ref.go b/compose-ref.go index 2b01dba..a523ede 100644 --- a/compose-ref.go +++ b/compose-ref.go @@ -201,6 +201,7 @@ func doUp(project string, config *compose.Config) error { if err != nil { return err } + return createService(cli, project, prjDir, service, networks) }) @@ -221,6 +222,11 @@ func doUp(project string, config *compose.Config) error { func createService(cli *client.Client, project string, prjDir string, s compose.ServiceConfig, networks map[string]string) error { ctx := context.Background() + err := internal.PullImageIfWithout(cli, ctx, s.Name) + if err != nil { + return err + } + var shmSize int64 if s.ShmSize != "" { v, err := units.RAMInBytes(s.ShmSize) diff --git a/internal/image.go b/internal/image.go new file mode 100644 index 0000000..9e31d8c --- /dev/null +++ b/internal/image.go @@ -0,0 +1,44 @@ +/* + Copyright 2020 The Compose Specification Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package internal + +import ( + "context" + "fmt" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/filters" + "github.com/docker/docker/client" +) + +func PullImageIfWithout(cli *client.Client, ctx context.Context, name string) error { + args := filters.NewArgs(filters.Arg("reference", name)) + images, err := cli.ImageList(ctx, types.ImageListOptions{All: true, Filters: args}) + if err != nil { + return err + } + if len(images) == 0 { + fmt.Println("pulling image: " + name) + _, err := cli.ImagePull(ctx, name, types.ImagePullOptions{}) + if err != nil { + return err + } + + // TODO print json message stream + } + return nil +}