// Fetch retrieves the file contents. // // Callers must call Close() on the returned io.ReadCloser. func (b ResultsFileLocation) Fetch() (io.ReadCloser, error) { if strings.HasPrefix(b.URI, "file://") { return os.Open(b.URI[6:]) } else { for i := 0; i < MAX_URI_GET_TRIES; i++ { glog.Infof("Fetching: %s", b.Name) request, err := gs.RequestForStorageURL(b.URI) if err != nil { glog.Warningf("Unable to create Storage MediaURI request: %s\n", err) continue } resp, err := client.Do(request) if err != nil { glog.Warningf("Unable to retrieve URI while creating file iterator: %s", err) continue } if resp.StatusCode != 200 { glog.Errorf("Failed to retrieve: %d %s", resp.StatusCode, resp.Status) } glog.Infof("GS FETCH %s", b.URI) return resp.Body, nil } return nil, fmt.Errorf("Failed fetching JSON after %d attempts", MAX_URI_GET_TRIES) } }
// gsFetch fetch the object's data from google storage func gsFetch(object *storage.Object, sc storageClient) (io.ReadCloser, int64, error) { request, err := gs.RequestForStorageURL(object.MediaLink) if err != nil { return nil, -1, err } resp, err := sc.httpClient.Do(request) if err != nil { return nil, -1, err } if resp.StatusCode != 200 { _ = resp.Body.Close() return nil, -1, fmt.Errorf("Failed to retrieve: %s %d %s", object.MediaLink, resp.StatusCode, resp.Status) } return resp.Body, resp.ContentLength, nil }
// Returns the response body of the specified GS object. Tries MAX_URI_GET_TRIES // times if download is unsuccessful. Client must close the response body when // finished with it. func (fs *FileDiffStore) getRespBody(res *storage.Object) (io.ReadCloser, error) { request, err := gs.RequestForStorageURL(res.MediaLink) if err != nil { return nil, fmt.Errorf("Unable to create Storage MediaURI request: %s\n", err) } resp, err := fs.client.Do(request) if err != nil { return nil, fmt.Errorf("Unable to retrieve Storage MediaURI: %s", err) } if resp.StatusCode != 200 { defer util.Close(resp.Body) return nil, fmt.Errorf("Failed to retrieve: %d %s", resp.StatusCode, resp.Status) } return resp.Body, nil }
func openUri(uriPath string) (*http.Response, error) { uri := GS_TEST_DATA_ROOT_URI + uriPath client := util.NewTimeoutClient() request, err := gs.RequestForStorageURL(uri) if err != nil { return nil, err } resp, err := client.Do(request) if err != nil { return nil, err } if resp.StatusCode != 200 { return nil, fmt.Errorf("Downloading %s failed. Got response status: %d", uri, resp.StatusCode) } return resp, nil }
// See ResultFileLocation interface. func (g *gsResultFileLocation) Open() (io.ReadCloser, error) { for i := 0; i < MAX_URI_GET_TRIES; i++ { request, err := gs.RequestForStorageURL(g.uri) if err != nil { glog.Errorf("Unable to create Storage MediaURI request: %s\n", err) continue } resp, err := g.client.Do(request) if err != nil { glog.Errorf("Unable to retrieve URI while creating file iterator: %s", err) continue } if resp.StatusCode != 200 { glog.Errorf("Failed to retrieve %s. Error: %d %s", g.uri, resp.StatusCode, resp.Status) } glog.Infof("GSFILE READ %s", g.name) return resp.Body, nil } return nil, fmt.Errorf("Failed fetching %s after %d attempts", g.uri, MAX_URI_GET_TRIES) }
// Install downloads and installs a debian package from Google Storage. func Install(client *http.Client, store *storage.Service, name string) error { glog.Infof("Installing: %s", name) obj, err := store.Objects.Get("skia-push", "debs/"+name).Do() if err != nil { return fmt.Errorf("Failed to retrieve Google Storage metadata about debian package: %s", err) } req, err := gs.RequestForStorageURL(obj.MediaLink) if err != nil { return fmt.Errorf("Failed to construct request object for media: %s", err) } resp, err := client.Do(req) if err != nil { return fmt.Errorf("Failed to retrieve packages file: %s", err) } defer util.Close(resp.Body) f, err := ioutil.TempFile("", "skia-pull") if err != nil { return fmt.Errorf("Failed to create tmp file: %s", err) } _, copyErr := io.Copy(f, resp.Body) if err := f.Close(); err != nil { return fmt.Errorf("Failed to close temporary file: %v", err) } if copyErr != nil { return fmt.Errorf("Failed to download file: %s", copyErr) } if err := installDependencies(f.Name()); err != nil { return fmt.Errorf("Error installing dependencies: %s", err) } cmd := exec.Command("sudo", "dpkg", "-i", f.Name()) var out bytes.Buffer cmd.Stdout = &out if err := cmd.Run(); err != nil { glog.Errorf("Install package stdout: %s", out.String()) return fmt.Errorf("Failed to install package: %s", err) } glog.Infof("Install package stdout: %s", out.String()) return nil }
// Returns the response body of the specified GS object. Tries MAX_URI_GET_TRIES // times if download is unsuccessful. Client must close the response body when // finished with it. func getRespBody(res *storage.Object, client *http.Client) (io.ReadCloser, error) { for i := 0; i < MAX_URI_GET_TRIES; i++ { glog.Infof("Fetching: %s", res.Name) request, err := gs.RequestForStorageURL(res.MediaLink) if err != nil { glog.Warningf("Unable to create Storage MediaURI request: %s\n", err) continue } resp, err := client.Do(request) if err != nil { glog.Warningf("Unable to retrieve Storage MediaURI: %s", err) continue } if resp.StatusCode != 200 { glog.Warningf("Failed to retrieve: %d %s", resp.StatusCode, resp.Status) util.Close(resp.Body) continue } return resp.Body, nil } return nil, fmt.Errorf("Failed fetching file after %d attempts", MAX_URI_GET_TRIES) }
// InstalledForServer returns a list of package names of installed packages for // the given server. func InstalledForServer(client *http.Client, store *storage.Service, serverName string) (*Installed, error) { ret := &Installed{ Names: []string{}, Generation: -1, } filename := "server/" + serverName + ".json" obj, err := store.Objects.Get("skia-push", filename).Do() if err != nil { return ret, fmt.Errorf("Failed to retrieve Google Storage metadata about packages file %q: %s", filename, err) } glog.Infof("Fetching: %s", obj.MediaLink) req, err := gs.RequestForStorageURL(obj.MediaLink) if err != nil { return ret, fmt.Errorf("Failed to construct request object for media: %s", err) } resp, err := client.Do(req) if err != nil { return ret, fmt.Errorf("Failed to retrieve packages file: %s", err) } defer util.Close(resp.Body) if resp.StatusCode != 200 { return ret, fmt.Errorf("Wrong status code: %#v", *resp) } dec := json.NewDecoder(resp.Body) value := []string{} if err := dec.Decode(&value); err != nil { return ret, fmt.Errorf("Failed to decode packages file: %s", err) } sort.Strings(value) ret.Names = value ret.Generation = obj.Generation return ret, nil }