Merge pull request #1319 from justincormack/gcp-delimage

GCP image handling enhancements
This commit is contained in:
Justin Cormack 2017-03-16 13:29:45 +00:00 committed by GitHub
commit c15f4f6b14
4 changed files with 27 additions and 5 deletions

View File

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

View File

@ -42,3 +42,5 @@ outputs:
- format: gce - format: gce
project: moby project: moby
bucket: mobytestjustin 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() 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) _, err = io.Copy(wc, f)
if err != nil { if err != nil {
return err return err
@ -44,14 +45,19 @@ func uploadGS(filename, project, bucket string, public bool) error {
return err 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) fmt.Println("gs://" + bucket + "/" + filename)
return nil return nil
} }
func imageGS(filename, project, storage string) error { func imageGS(filename, project, storage, family string, replace bool) error {
if project != "" { if project != "" {
err := os.Setenv("GOOGLE_CLOUD_PROJECT", project) err := os.Setenv("GOOGLE_CLOUD_PROJECT", project)
if err != nil { if err != nil {
@ -68,7 +74,19 @@ func imageGS(filename, project, storage string) error {
if err != nil { if err != nil {
return errors.New("Please install the gcloud binary") 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...) cmd := exec.Command(gcloud, args...)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()

View File

@ -63,7 +63,7 @@ func outputs(m *Moby, base string, bzimage []byte, initrd []byte) error {
if err != nil { if err != nil {
return fmt.Errorf("Error copying to Google Storage: %v", err) 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 { if err != nil {
return fmt.Errorf("Error creating Google Compute Image: %v", err) return fmt.Errorf("Error creating Google Compute Image: %v", err)
} }