()
| 9 | ) |
| 10 | |
| 11 | func pushGCPCmd() *cobra.Command { |
| 12 | var ( |
| 13 | keysFlag string |
| 14 | projectFlag string |
| 15 | bucketFlag string |
| 16 | publicFlag bool |
| 17 | familyFlag string |
| 18 | nameFlag string |
| 19 | nestedVirt bool |
| 20 | uefi bool |
| 21 | ) |
| 22 | cmd := &cobra.Command{ |
| 23 | Use: "gcp", |
| 24 | Short: "push image to GCP", |
| 25 | Long: `Push image to GCP. |
| 26 | First argument specifies the path to a disk file. |
| 27 | It will be uploaded to GCS and GCP VM image will be created from it. |
| 28 | `, |
| 29 | Args: cobra.ExactArgs(1), |
| 30 | RunE: func(cmd *cobra.Command, args []string) error { |
| 31 | path := args[0] |
| 32 | |
| 33 | keys := getStringValue(keysVar, keysFlag, "") |
| 34 | project := getStringValue(projectVar, projectFlag, "") |
| 35 | bucket := getStringValue(bucketVar, bucketFlag, "") |
| 36 | public := getBoolValue(publicVar, publicFlag) |
| 37 | family := getStringValue(familyVar, familyFlag, "") |
| 38 | name := getStringValue(nameVar, nameFlag, "") |
| 39 | |
| 40 | const suffix = ".img.tar.gz" |
| 41 | if name == "" { |
| 42 | name = strings.TrimSuffix(path, suffix) |
| 43 | name = filepath.Base(name) |
| 44 | } |
| 45 | |
| 46 | client, err := NewGCPClient(keys, project) |
| 47 | if err != nil { |
| 48 | return fmt.Errorf("unable to connect to GCP: %v", err) |
| 49 | } |
| 50 | |
| 51 | if bucket == "" { |
| 52 | return fmt.Errorf("please specify the bucket to use") |
| 53 | } |
| 54 | |
| 55 | err = client.UploadFile(path, name+suffix, bucket, public) |
| 56 | if err != nil { |
| 57 | return fmt.Errorf("error copying to Google Storage: %v", err) |
| 58 | } |
| 59 | err = client.CreateImage(name, "https://storage.googleapis.com/"+bucket+"/"+name+suffix, family, nestedVirt, uefi, true) |
| 60 | if err != nil { |
| 61 | return fmt.Errorf("error creating Google Compute Image: %v", err) |
| 62 | } |
| 63 | |
| 64 | return nil |
| 65 | }, |
| 66 | } |
| 67 | |
| 68 | cmd.Flags().StringVar(&keysFlag, "keys", "", "Path to Service Account JSON key file") |
no test coverage detected