func (tdr *TestDynamoDBRunner) downloadIfNecessary() error {
	// does the jar file exist?
	jarPath := tdr.jarFilePath()
	if _, err := os.Stat(jarPath); err == nil {
		return nil
	}

	// create the tmp directory if it doesn't exist
	if _, err := os.Stat(tdr.tmpDir()); os.IsNotExist(err) {
		if err := os.Mkdir(tdr.tmpDir(), os.ModeDir|os.ModePerm); err != nil {
			return err
		}
	}

	// download
	response, err := http.Get(LocalDynamoDBDownloadURI)
	if err != nil {
		return err
	}
	defer libkb.DiscardAndCloseBody(response)

	// read body into buffer and compute the hash
	var buf bytes.Buffer
	if _, err := buf.ReadFrom(response.Body); err != nil {
		return err
	}
	hash := sha256.New()
	hash.Write(buf.Bytes())
	sha256Hash := hex.EncodeToString(hash.Sum(nil))

	// verify the hash
	if sha256Hash != LocalDynamoDBSha256Hash {
		return fmt.Errorf("Expected hash %s, got: %s\n",
			LocalDynamoDBSha256Hash, sha256Hash)
	}

	// create the download file
	path := filepath.Join(tdr.tmpDir(), "dynamodb_local.tar.gz")
	out, err := os.Create(path)
	if err != nil {
		return err
	}
	defer out.Close()

	// write it out
	_, err = io.Copy(out, bytes.NewReader(buf.Bytes()))
	if err != nil {
		return err
	}

	// untar
	untar := exec.Command("tar", "-C", tdr.tmpDir(), "-xzvf", path)
	return untar.Run()
}
Exemple #2
0
func (k KeybaseUpdateSource) FindUpdate(options keybase1.UpdateOptions) (*keybase1.Update, error) {
	channel := k.defaultChannel
	if options.Channel != "" {
		channel = options.Channel
	}

	u, err := url.Parse("https://keybase.io/_/api/1.0/pkg/update_legacy.json")
	if err != nil {
		return nil, err
	}

	urlValues := url.Values{}
	urlValues.Add("legacy_id", k.legacyID)
	urlValues.Add("version", options.Version)
	urlValues.Add("platform", options.Platform)
	urlValues.Add("run_mode", string(k.runMode))
	urlValues.Add("channel", channel)
	u.RawQuery = urlValues.Encode()
	urlString := u.String()

	req, err := http.NewRequest("GET", urlString, nil)
	if err != nil {
		return nil, err
	}
	client := http.Client{
		Timeout: time.Minute,
	}
	k.log.Info("Request %#v", urlString)
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer libkb.DiscardAndCloseBody(resp)

	if resp.StatusCode == http.StatusNotFound {
		return nil, nil
	}
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("Find update returned bad HTTP status %v", resp.Status)
	}

	var reader io.Reader = resp.Body
	var update keybase1.Update
	if err = json.NewDecoder(reader).Decode(&update); err != nil {
		return nil, fmt.Errorf("Invalid API response %s", err)
	}

	k.log.Debug("Received update response: %#v", update)

	return &update, nil
}
Exemple #3
0
func (r RemoteUpdateSource) FindUpdate(options keybase1.UpdateOptions) (update *keybase1.Update, err error) {
	sourceURL := ""
	if options.URL != "" {
		sourceURL = options.URL
	} else if r.defaultURI != "" {
		sourceURL = r.defaultSourceURL(options)
	}
	if sourceURL == "" {
		err = fmt.Errorf("No source URL for remote")
		return
	}
	req, err := http.NewRequest("GET", sourceURL, nil)
	client := &http.Client{}
	r.log.Info("Request %#v", sourceURL)
	resp, err := client.Do(req)
	if resp != nil {
		defer libkb.DiscardAndCloseBody(resp)
	}
	if err != nil {
		return
	}

	if resp.StatusCode != http.StatusOK {
		err = fmt.Errorf("Updater remote returned bad status %v", resp.Status)
		return
	}

	var reader io.Reader = resp.Body
	var obj keybase1.Update
	if err = json.NewDecoder(reader).Decode(&obj); err != nil {
		err = fmt.Errorf("Bad updater remote response %s", err)
		return
	}
	update = &obj

	r.log.Debug("Received update %#v", update)

	return
}