func updateBinaryFile(url string, checksum []byte) { fmt.Println("Downloading updated binary") httpResp, err := http.Get(url) if err != nil { glog.Errorf("Cannot download binary: %s", err) os.Exit(1) } defer httpResp.Body.Close() binary := httpResp.Body if httpResp.ContentLength > 0 { bar := pb.New64(httpResp.ContentLength).SetUnits(pb.U_BYTES) bar.Start() binary = bar.NewProxyReader(binary) defer func() { <-time.After(bar.RefreshRate) fmt.Println() }() } err = update.Apply(binary, update.Options{ Hash: crypto.SHA256, Checksum: checksum, }) if err != nil { glog.Errorf("Cannot apply binary update: %s", err) err := update.RollbackError(err) if err != nil { glog.Errorf("Failed to rollback update: %s", err) } os.Exit(1) } }
func downloadChecksum(v semver.Version, downloadBinary, downloadLinkFormat string) ([]byte, error) { fmt.Println("Downloading updated binary checksum to validate updated binary") u := fmt.Sprintf(downloadLinkFormat, v, downloadBinary+".sha256") checksumResp, err := http.Get(u) if err != nil { return nil, err } defer checksumResp.Body.Close() checksum := checksumResp.Body if checksumResp.ContentLength > 0 { bar := pb.New64(checksumResp.ContentLength).SetUnits(pb.U_BYTES) bar.Start() checksum = bar.NewProxyReader(checksum) defer func() { <-time.After(2 * bar.RefreshRate) fmt.Println() }() } b, err := ioutil.ReadAll(checksum) if err != nil { return nil, err } if checksumResp.StatusCode != 200 { return nil, fmt.Errorf("received %d", checksumResp.StatusCode) } return hex.DecodeString(strings.TrimSpace(string(b))) }
func (m *MachineConfig) CacheMinikubeISOFromURL() error { fmt.Println("Downloading ISO") // store the miniube-iso inside the .minikube dir response, err := http.Get(m.MinikubeISO) if err != nil { return err } defer response.Body.Close() if response.StatusCode != http.StatusOK { return fmt.Errorf("Received %d response from %s while trying to download minikube.iso", response.StatusCode, m.MinikubeISO) } iso := response.Body if response.ContentLength > 0 { bar := pb.New64(response.ContentLength).SetUnits(pb.U_BYTES) bar.Start() iso = bar.NewProxyReader(iso) defer func() { <-time.After(bar.RefreshRate) fmt.Println() }() } out, err := os.Create(m.GetISOCacheFilepath()) if err != nil { return err } defer out.Close() if _, err = io.Copy(out, iso); err != nil { return err } return nil }
func main() { token := os.Getenv("GH_TOKEN") var tc *http.Client if len(token) > 0 { fmt.Println("Using GH_TOKEN environment variable") ts := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: token}, ) tc = oauth2.NewClient(oauth2.NoContext, ts) } client := github.NewClient(tc) var ( release *github.RepositoryRelease resp *github.Response err error ) if len(os.Args) > 1 { release, resp, err = client.Repositories.GetReleaseByTag("openshift", "origin", os.Args[1]) } else { release, resp, err = client.Repositories.GetLatestRelease("openshift", "origin") } if err != nil { fmt.Printf("Could not get latest OpenShift release: %s", err) os.Exit(1) } defer resp.Body.Close() assetID := getOpenShiftServerAssetID(release) if assetID == 0 { fmt.Println("Could not get OpenShift release URL") os.Exit(1) } asset, url, err := client.Repositories.DownloadReleaseAsset("openshift", "origin", assetID) if err != nil { fmt.Printf("Could not download OpenShift release asset: %s\n", err) os.Exit(1) } if len(url) > 0 { httpResp, err := http.Get(url) if err != nil { fmt.Printf("Could not download OpenShift release asset: %s\n", err) os.Exit(1) } defer httpResp.Body.Close() asset = httpResp.Body if httpResp.ContentLength > 0 { bar := pb.New64(httpResp.ContentLength).SetUnits(pb.U_BYTES) bar.Start() asset = bar.NewProxyReader(asset) defer func() { <-time.After(bar.RefreshRate) fmt.Println() }() } } gzf, err := gzip.NewReader(asset) if err != nil { fmt.Printf("Could not ungzip OpenShift release asset: %s\n", err) os.Exit(1) } defer gzf.Close() tr := tar.NewReader(gzf) for { hdr, err := tr.Next() if err == io.EOF { // end of tar archive break } if err != nil { fmt.Printf("Could not extract OpenShift release asset: %s\n", err) os.Exit(1) } if hdr.Typeflag != tar.TypeReg || filepath.Base(hdr.Name) != "kube-apiserver" { continue } contents, err := ioutil.ReadAll(tr) if err != nil { fmt.Printf("Could not extract OpenShift release asset: %s\n", err) os.Exit(1) } err = ioutil.WriteFile("out/openshift", contents, os.ModePerm) if err != nil { fmt.Printf("Could not write OpenShift binary: %s\n", err) os.Exit(1) } } }