From 77dde60c08c260671a15ff2801431e577e6d398c Mon Sep 17 00:00:00 2001 From: skanehira Date: Wed, 6 May 2020 13:22:19 +0900 Subject: [PATCH] Pull image if without when create service Signed-off-by: skanehira --- compose-ref.go | 6 ++++++ internal/image.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 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..1e9c0fd --- /dev/null +++ b/internal/image.go @@ -0,0 +1,45 @@ +/* + 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 use github.com/docker/cli/cli/streams to dispaly stream message + } + + return nil +}