Example #1
0
func runDownloadImage(cmd *cobra.Command, args []string) {
	if len(args) != 0 {
		plog.Fatalf("Unrecognized arguments: %v", args)
	}

	if downloadImageCacheDir == "" {
		plog.Fatal("Missing --cache-dir=FILEPATH")
	}
	if len(downloadImagePlatformList) == 0 {
		plog.Fatal("Must specify 1 or more platforms to download")
	}
	if downloadImageVerify == false {
		plog.Notice("Warning: image verification turned off")
	}

	versionFile := filepath.Join(downloadImageCacheDir, "version.txt")
	versionURL := strings.TrimRight(downloadImageRoot, "/") + "/" + "version.txt"
	if err := sdk.UpdateFile(versionFile, versionURL); err != nil {
		plog.Fatalf("downloading version.txt: %v", err)
	}

	for _, suffix := range downloadImagePlatformList {
		fileName := downloadImagePrefix + suffix
		filePath := filepath.Join(downloadImageCacheDir, fileName)

		// path.Join doesn't work with urls
		url := strings.TrimRight(downloadImageRoot, "/") + "/" + fileName

		if downloadImageVerify {
			plog.Noticef("Verifying and updating to latest image %v", fileName)
			err := sdk.UpdateSignedFile(filePath, url)
			if err != nil {
				plog.Fatalf("updating signed file: %v", err)
			}
		} else {
			plog.Noticef("Starting non-verified image update %v", fileName)
			if err := sdk.UpdateFile(filePath, url); err != nil {
				plog.Fatalf("downloading image: %v", err)
			}
		}
	}
}
Example #2
0
func runDownloadImage(cmd *cobra.Command, args []string) {
	if len(args) != 0 {
		plog.Fatalf("Unrecognized arguments: %v", args)
	}

	if downloadImageCacheDir == "" {
		plog.Fatal("Missing --cache-dir=FILEPATH")
	}
	if len(downloadImagePlatformList) == 0 {
		plog.Fatal("Must specify 1 or more platforms to download")
	}
	if downloadImageVerify == false {
		plog.Notice("Warning: image verification turned off")
	}

	// check for shorthand names of image roots
	downloadImageRoot = convertSpecialPaths(downloadImageRoot)

	imageURL, err := url.Parse(downloadImageRoot)
	if err != nil {
		plog.Fatalf("Failed parsing image root as url: %v", err)
	}

	// support Google storage buckets URLs
	var client *http.Client
	if imageURL.Scheme == "gs" {
		if downloadImageJSONKeyFile != "" {
			b, err := ioutil.ReadFile(downloadImageJSONKeyFile)
			if err != nil {
				plog.Fatal(err)
			}
			client, err = auth.GoogleClientFromJSONKey(b, "https://www.googleapis.com/auth/devstorage.read_only")
		} else {
			client, err = auth.GoogleClient()
		}
		if err != nil {
			plog.Fatal(err)
		}
	}

	versionFile := filepath.Join(downloadImageCacheDir, "version.txt")
	versionURL := strings.TrimRight(downloadImageRoot, "/") + "/" + "version.txt"
	if err := sdk.UpdateFile(versionFile, versionURL, client); err != nil {
		plog.Fatalf("downloading version.txt: %v", err)
	}

	for _, suffix := range downloadImagePlatformList {
		fileName := downloadImagePrefix + suffix
		filePath := filepath.Join(downloadImageCacheDir, fileName)

		// path.Join doesn't work with urls
		url := strings.TrimRight(downloadImageRoot, "/") + "/" + fileName

		if downloadImageVerify {
			plog.Noticef("Verifying and updating to latest image %v", fileName)
			err := sdk.UpdateSignedFile(filePath, url, client)
			if err != nil {
				plog.Fatalf("updating signed file: %v", err)
			}
		} else {
			plog.Noticef("Starting non-verified image update %v", fileName)
			if err := sdk.UpdateFile(filePath, url, client); err != nil {
				plog.Fatalf("downloading image: %v", err)
			}
		}
	}
}