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 <justin.cormack@docker.com>
This commit is contained in:
Justin Cormack 2017-03-16 13:08:37 +00:00
parent 657c97c4fb
commit ff941778e4
4 changed files with 27 additions and 5 deletions

View File

@ -29,7 +29,9 @@ type Moby struct {
Format string
Project string
Bucket string
Family string
Public bool
Replace bool
}
}

View File

@ -42,3 +42,5 @@ outputs:
- format: gce
project: moby
bucket: mobytestjustin
family: moby-dev
replace: true

26
gcp.go
View File

@ -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()

View File

@ -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)
}