func tryGCECreate(bucket, bucketPath, imageName string) int { client, err := auth.GoogleClient() if err != nil { log.Printf("authentication failed: %v", err) return 1 } api, err := compute.New(client) if err != nil { fmt.Fprintf(os.Stderr, "Api Client creation failed: %v\n", err) os.Exit(1) } // make sure file exists exists, err := fileQuery(client, bucket, bucketPath) if err != nil || !exists { log.Printf("failed to find existance of storage image: %v", err) return 1 } log.Printf("Creating image in GCE: %v...\n", imageName) // create image on gce storageSrc := fmt.Sprintf("https://storage.googleapis.com/%v/%v", bucket, bucketPath) err = platform.GCECreateImage(api, opts.Project, imageName, storageSrc) // if image already exists ask to delete and try again if err != nil && strings.HasSuffix(err.Error(), "alreadyExists") { if gceCreateForce { log.Println("forcing overwrite of existing image...") err = platform.GCEForceCreateImage(api, opts.Project, imageName, storageSrc) } else { log.Printf("skipping upload, image %v already exists", imageName) return 0 } } if err != nil { log.Printf("Creating GCE image failed: %v", err) return 1 } return 0 }
func runUpload(cmd *cobra.Command, args []string) { if len(args) != 0 { fmt.Fprintf(os.Stderr, "Unrecognized args in plume upload cmd: %v\n", args) os.Exit(2) } // if an image name is unspecified try to use version.txt if uploadImageName == "" { var err error uploadImageName, err = sdk.GetVersionFromDir(filepath.Dir(uploadFile)) if err != nil { fmt.Fprintf(os.Stderr, "Unable to get version from image directory, provide a -name flag or include a version.txt in the image directory: %v\n", err) os.Exit(1) } } gsURL, err := url.Parse(uploadBucket) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } if gsURL.Scheme != "gs" { fmt.Fprintf(os.Stderr, "URL missing gs:// scheme prefix: %v\n", uploadBucket) os.Exit(1) } if gsURL.Host == "" { fmt.Fprintf(os.Stderr, "URL missing bucket name %v\n", uploadBucket) os.Exit(1) } // if prefix not specified default name to gs://bucket/$USER/$BOARD/$VERSION if gsURL.Path == "" { if user := os.Getenv("USER"); user != "" { gsURL.Path = "/" + os.Getenv("USER") gsURL.Path += "/" + uploadBoard } } uploadBucket = gsURL.Host uploadImageName = strings.TrimPrefix(gsURL.Path+"/"+uploadImageName, "/") // create equivalent image names for GS and GCE imageNameGCE := gceSanitize(uploadImageName) imageNameGS := uploadImageName + ".tar.gz" var client *http.Client if uploadServiceAuth { client = auth.GoogleServiceClient() err = nil } else { client, err = auth.GoogleClient() } if err != nil { fmt.Fprintf(os.Stderr, "Authentication failed: %v\n", err) os.Exit(1) } api, err := compute.New(client) if err != nil { fmt.Fprintf(os.Stderr, "Api Client creation failed: %v\n", err) os.Exit(1) } // check if this file is already uploaded and give option to skip alreadyExists, err := fileQuery(client, uploadBucket, imageNameGS) if err != nil { fmt.Fprintf(os.Stderr, "Uploading image failed: %v\n", err) os.Exit(1) } if alreadyExists && !uploadForce { var ans string fmt.Printf("File %v already exists on Google Storage. Overwrite? (y/n):", imageNameGS) if _, err = fmt.Scan(&ans); err != nil { fmt.Fprintf(os.Stderr, "Scanning overwrite input: %v", err) os.Exit(1) } switch ans { case "y", "Y", "yes": fmt.Println("Overriding existing file...") err = writeFile(client, uploadBucket, uploadFile, imageNameGS) default: fmt.Println("Skipped file upload") } } else { err = writeFile(client, uploadBucket, uploadFile, imageNameGS) } if err != nil { fmt.Fprintf(os.Stderr, "Uploading image failed: %v\n", err) os.Exit(1) } fmt.Printf("Creating image in GCE: %v...\n", imageNameGCE) // create image on gce storageSrc := fmt.Sprintf("https://storage.googleapis.com/%v/%v", uploadBucket, imageNameGS) if uploadForce { err = platform.GCEForceCreateImage(api, opts.Project, imageNameGCE, storageSrc) } else { err = platform.GCECreateImage(api, opts.Project, imageNameGCE, storageSrc) } // if image already exists ask to delete and try again if err != nil && strings.HasSuffix(err.Error(), "alreadyExists") { var ans string fmt.Printf("Image %v already exists on GCE. Overwrite? (y/n):", imageNameGCE) if _, err = fmt.Scan(&ans); err != nil { fmt.Fprintf(os.Stderr, "Scanning overwrite input: %v", err) os.Exit(1) } switch ans { case "y", "Y", "yes": fmt.Println("Overriding existing image...") err = platform.GCEForceCreateImage(api, opts.Project, imageNameGCE, storageSrc) if err != nil { fmt.Fprintf(os.Stderr, "Creating GCE image failed: %v\n", err) os.Exit(1) } fmt.Printf("Image %v sucessfully created in GCE\n", imageNameGCE) default: fmt.Println("Skipped GCE image creation") } } if err != nil { fmt.Fprintf(os.Stderr, "Creating GCE image failed: %v\n", err) os.Exit(1) } }