From ff941778e4af96b8b8ce9c6b92396ee75079ae8f Mon Sep 17 00:00:00 2001 From: Justin Cormack Date: Thu, 16 Mar 2017 13:08:37 +0000 Subject: [PATCH] GCP image handling enhancements - the `public` option was not previously implemented - add `replace` only for GCP images which will error otherwise. Only recommended for use in development, in production use the `--name` option to provide a different name eaxch time. Note only applies to GCP images, will document these options properly soon. - add a `family` option; this allows you to upload many images and the user can select the latest using the `family` option instead of a specific image. Signed-off-by: Justin Cormack --- config.go | 2 ++ examples/gcp.yaml | 2 ++ gcp.go | 26 ++++++++++++++++++++++---- output.go | 2 +- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index 65d4a63fe..2a32c1002 100644 --- a/config.go +++ b/config.go @@ -29,7 +29,9 @@ type Moby struct { Format string Project string Bucket string + Family string Public bool + Replace bool } } diff --git a/examples/gcp.yaml b/examples/gcp.yaml index 2323e0128..7eec38df3 100644 --- a/examples/gcp.yaml +++ b/examples/gcp.yaml @@ -42,3 +42,5 @@ outputs: - format: gce project: moby bucket: mobytestjustin + family: moby-dev + replace: true diff --git a/gcp.go b/gcp.go index ae93fffd2..5390af538 100644 --- a/gcp.go +++ b/gcp.go @@ -34,7 +34,8 @@ func uploadGS(filename, project, bucket string, public bool) error { } defer f.Close() - wc := client.Bucket(bucket).Object(filename).NewWriter(ctx) + obj := client.Bucket(bucket).Object(filename) + wc := obj.NewWriter(ctx) _, err = io.Copy(wc, f) if err != nil { return err @@ -44,14 +45,19 @@ func uploadGS(filename, project, bucket string, public bool) error { return err } - // TODO make public if requested + if public { + err = obj.ACL().Set(ctx, storage.AllUsers, storage.RoleReader) + if err != nil { + return err + } + } fmt.Println("gs://" + bucket + "/" + filename) return nil } -func imageGS(filename, project, storage string) error { +func imageGS(filename, project, storage, family string, replace bool) error { if project != "" { err := os.Setenv("GOOGLE_CLOUD_PROJECT", project) if err != nil { @@ -68,7 +74,19 @@ func imageGS(filename, project, storage string) error { if err != nil { return errors.New("Please install the gcloud binary") } - args := []string{"compute", "images", "create", "--source-uri", storage, filename} + + if replace { + args := []string{"compute", "images", "delete", filename} + cmd := exec.Command(gcloud, args...) + // ignore failures; it may not exist + _ = cmd.Run() + } + + args := []string{"compute", "images", "create", "--source-uri", storage} + if family != "" { + args = append(args, "--family", family) + } + args = append(args, filename) cmd := exec.Command(gcloud, args...) out, err := cmd.CombinedOutput() diff --git a/output.go b/output.go index c15ed3cde..e3499c00d 100644 --- a/output.go +++ b/output.go @@ -63,7 +63,7 @@ func outputs(m *Moby, base string, bzimage []byte, initrd []byte) error { if err != nil { return fmt.Errorf("Error copying to Google Storage: %v", err) } - err = imageGS(base, o.Project, "https://storage.googleapis.com/"+o.Bucket+"/"+base+".img.tar.gz") + err = imageGS(base, o.Project, "https://storage.googleapis.com/"+o.Bucket+"/"+base+".img.tar.gz", o.Family, o.Replace) if err != nil { return fmt.Errorf("Error creating Google Compute Image: %v", err) }