updates.go 1.46 KB
Newer Older
1 2 3
package updates

import (
4
	"fmt"
5 6
	"os"

7 8
	u "github.com/jbenet/go-ipfs/util"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
10 11
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/inconshreveable/go-update"
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/inconshreveable/go-update/check"
12 13 14
)

const (
15 16 17 18 19 20 21 22 23
	// Version is the current application's version literal
	Version = "0.1.1"

	updateEndpointURL = "https://api.equinox.io/1/Updates"
	updateAppID       = "CHANGEME"

	updatePubKey = `-----BEGIN RSA PUBLIC KEY-----
CHANGEME
-----END RSA PUBLIC KEY-----`
24 25
)

26 27
var log = u.Logger("updates")

28 29 30 31
var currentVersion *semver.Version

func init() {
	var err error
32
	currentVersion, err = parseVersion()
33
	if err != nil {
34
		log.Error("illegal version number in code: %q\n", Version)
35 36 37 38
		os.Exit(1)
	}
}

39 40 41
func parseVersion() (*semver.Version, error) {
	return semver.NewVersion(Version)
}
42

43
// CheckForUpdate checks the equinox.io api if there is an update available
44 45 46
func CheckForUpdate() (*check.Result, error) {
	param := check.Params{
		AppVersion: Version,
47
		AppId:      updateAppID,
48
		Channel:    "stable",
49
	}
50

51 52 53 54 55 56 57 58 59 60 61
	up, err := update.New().VerifySignatureWithPEM([]byte(updatePubKey))
	if err != nil {
		return nil, fmt.Errorf("Failed to parse public key: %v", err)
	}

	return param.CheckForUpdate(updateEndpointURL, up)
}

// AbleToApply cheks if the running process is able to update itself
func AbleToApply() error {
	return update.New().CanUpdate()
62
}