1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-30 18:13:54 +08:00

remove old update code

License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
Jeromy
2015-12-29 07:13:00 -08:00
parent cd1e389360
commit f3f776067a
4 changed files with 0 additions and 547 deletions

View File

@ -104,8 +104,5 @@ var cmdDetailsMap = map[*cmds.Command]cmdDetails{
commandsClientCmd: {doesNotUseRepo: true},
commands.CommandsDaemonCmd: {doesNotUseRepo: true},
commands.VersionCmd: {doesNotUseConfigAsInput: true, doesNotUseRepo: true}, // must be permitted to run before init
commands.UpdateCmd: {preemptsAutoUpdate: true, cannotRunOnDaemon: true},
commands.UpdateCheckCmd: {preemptsAutoUpdate: true},
commands.UpdateLogCmd: {preemptsAutoUpdate: true},
commands.LogCmd: {cannotRunOnClient: true},
}

View File

@ -1,182 +0,0 @@
package commands
import (
"bytes"
"errors"
"fmt"
"io"
cmds "github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/updates"
)
type UpdateOutput struct {
OldVersion string
NewVersion string
}
var UpdateCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Downloads and installs updates for IPFS (disabled)",
ShortDescription: `ipfs update is disabled until we can deploy the binaries to you over ipfs itself.
please use 'go get -u github.com/ipfs/go-ipfs/cmd/ipfs' until then.`,
},
}
// TODO: unexported until we can deploy the binaries over ipfs
var updateCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Downloads and installs updates for IPFS",
ShortDescription: "ipfs update is a utility command used to check for updates and apply them.",
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
output, err := updateApply(n)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(output)
},
Type: UpdateOutput{},
Subcommands: map[string]*cmds.Command{
"check": UpdateCheckCmd,
"log": UpdateLogCmd,
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v := res.Output().(*UpdateOutput)
buf := new(bytes.Buffer)
if v.NewVersion != v.OldVersion {
buf.WriteString(fmt.Sprintf("Successfully updated to IPFS version '%s' (from '%s')\n",
v.NewVersion, v.OldVersion))
} else {
buf.WriteString(fmt.Sprintf("Already updated to latest version ('%s')\n", v.NewVersion))
}
return buf, nil
},
},
}
var UpdateCheckCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Checks if updates are available",
ShortDescription: "'ipfs update check' checks if any updates are available for IPFS.\nNothing will be downloaded or installed.",
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
output, err := updateCheck(n)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(output)
},
Type: UpdateOutput{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v := res.Output().(*UpdateOutput)
buf := new(bytes.Buffer)
if v.NewVersion != v.OldVersion {
buf.WriteString(fmt.Sprintf("A new version of IPFS is available ('%s', currently running '%s')\n",
v.NewVersion, v.OldVersion))
} else {
buf.WriteString(fmt.Sprintf("Already updated to latest version ('%s')\n", v.NewVersion))
}
return buf, nil
},
},
}
var UpdateLogCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List the changelog for the latest versions of IPFS",
ShortDescription: "This command is not yet implemented.",
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
output, err := updateLog(n)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(output)
},
}
// updateApply applies an update of the ipfs binary and shuts down the node if successful
func updateApply(n *core.IpfsNode) (*UpdateOutput, error) {
// TODO: 'force bool' param that stops the daemon (if running) before update
output := &UpdateOutput{
OldVersion: updates.Version,
}
u, err := updates.CheckForUpdate()
if err != nil {
return nil, err
}
if u == nil {
output.NewVersion = updates.Version
return output, nil
}
output.NewVersion = u.Version
if n.OnlineMode() {
return nil, errors.New(`You must stop the IPFS daemon before updating.`)
}
if err = updates.Apply(u); err != nil {
return nil, err
}
return output, nil
}
// updateCheck checks wether there is an update available
func updateCheck(n *core.IpfsNode) (*UpdateOutput, error) {
output := &UpdateOutput{
OldVersion: updates.Version,
}
u, err := updates.CheckForUpdate()
if err != nil {
return nil, err
}
if u == nil {
output.NewVersion = updates.Version
return output, nil
}
output.NewVersion = u.Version
return output, nil
}
// updateLog lists the version available online
func updateLog(n *core.IpfsNode) (interface{}, error) {
// TODO
return nil, errors.New("Not yet implemented")
}

View File

@ -1,302 +0,0 @@
package updates
import (
"errors"
"fmt"
"os"
"time"
config "github.com/ipfs/go-ipfs/repo/config"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log"
semver "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
update "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/inconshreveable/go-update"
check "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/inconshreveable/go-update/check"
)
const (
// Version is the current application's version literal
Version = config.CurrentVersionNumber
updateEndpointURL = "https://api.equinox.io/1/Updates"
updateAppID = "ap_YM8nz6rGm1UPg_bf63Lw6Vjz49"
// this is @jbenet's equinox.io public key.
updatePubKey = `-----BEGIN RSA PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxnwPPE4LNMjTfW/NRz1z
8uAPpwGYSzac+cwZbHbL5xFOxeX301GCdISaMm+Q8OEJqLyXfjYSuRwx00fDzWDD
ajBQOsxO08gTy1i/ow5YdEO+nYeVKO08fQFqVqdTz09BCgzt9iQJTEMeiq1kSWNo
al8usHD4SsNTxwDpSlok5UKWCHcr7D/TWX5A4B5A6ae9HSEcMB4Aum83k63Vzgm1
WTUvK0ed1zd0/KcHqIU36VZpVg4PeV4SWnOBnldQ98CWg/Mnqp3+lXMWYWTmXeX6
xj8JqOGpebzlxeISKE6fDBtrLxUbFTt3DNshl7S5CUGuc5H1MF1FTAyi+8u/nEZB
cQIDAQAB
-----END RSA PUBLIC KEY-----`
/*
You can verify the key above (updatePubKey) is indeed controlled
by @jbenet, ipfs author, with the PGP signed message below. You
can verify it in the commandline, or keybase.io.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
I hereby certify that I control the private key matching the
following public key. This is a key used for go-ipfs auto-updates
over equinox.io. - @jbenet
- -----BEGIN RSA PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxnwPPE4LNMjTfW/NRz1z
8uAPpwGYSzac+cwZbHbL5xFOxeX301GCdISaMm+Q8OEJqLyXfjYSuRwx00fDzWDD
ajBQOsxO08gTy1i/ow5YdEO+nYeVKO08fQFqVqdTz09BCgzt9iQJTEMeiq1kSWNo
al8usHD4SsNTxwDpSlok5UKWCHcr7D/TWX5A4B5A6ae9HSEcMB4Aum83k63Vzgm1
WTUvK0ed1zd0/KcHqIU36VZpVg4PeV4SWnOBnldQ98CWg/Mnqp3+lXMWYWTmXeX6
xj8JqOGpebzlxeISKE6fDBtrLxUbFTt3DNshl7S5CUGuc5H1MF1FTAyi+8u/nEZB
cQIDAQAB
- -----END RSA PUBLIC KEY-----
-----BEGIN PGP SIGNATURE-----
Version: Keybase OpenPGP v1.1.3
Comment: https://keybase.io/crypto
wsFcBAABCgAGBQJUSCX8AAoJEFYC7bhkX9ftBcwQAJuYGSECSKFATJ1wK+zAGUH5
xEbX+yaCYj0PwzJO4Ntu2ifK68ANacKy/GiXdJYeQk7pq21UT0fcn0Uq39URu+Xb
lk3t1YZazjY7wB03jBjcMIaO2TUsWbGIBZAEZjyVDDctDUM0krCd1GIOw6Fbndva
pevlGIA55ewvXYxcWdRyOGWiqd9DKNnmi9UF0XsdpCtDFSkdjnqkqbTRxF6Jw5gI
EAF2E7mU8emDTNgtpCs0ACmEUXVVEEhF9TuR/YdX1m/715TYkkYCii6uV9vSVQd8
nOrDDTrWSjlF6Ms+dYGCheWIjKQcykn9IW021AzVN1P7Mt9qtmDNfZ0VQL3zl/fs
zZ1IHBW7BzriQ4GzWXg5GWpTSz/REvUEfKNVuDV9jX7hv67B5H6qTL5+2zljPEKv
lCas04cCMmEpJUj4qK95hdKQzKJ8b7MrRf/RFYyViRGdxvR+lgGqJ7Yca8es2kCe
XV6c+i6a7X89YL6ZVU+1MlvPwngu0VG+VInH/w9KrNYrLFhfVRiruRbkBkHDXjnU
b4kPqaus+7g0DynCk7A2kTMa3cgtO20CZ9MBJFEPqRRHHksjHVmlxPb42bB348aR
UVsWkRRYOmRML7avTgkX8WFsmdZ1d7E7aQLYnCIel85+5iP7hWyNtEMsAHk02XCL
AAb7RaEDNJOa7qvUFecB
=mzPY
-----END PGP SIGNATURE-----
*/
)
var log = logging.Logger("updates")
var currentVersion *semver.Version
// ErrNoUpdateAvailable returned when a check fails to find a newer update.
var ErrNoUpdateAvailable = check.NoUpdateAvailable
func init() {
var err error
currentVersion, err = parseVersion()
if err != nil {
log.Fatalf("invalid version number in code (must be semver): %q", Version)
}
log.Infof("go-ipfs Version: %s", currentVersion)
}
func parseVersion() (*semver.Version, error) {
return semver.NewVersion(Version)
}
// CheckForUpdate checks the equinox.io api if there is an update available
// NOTE: if equinox says there is a new update, but the version number IS NOT
// larger, we interpret that as no update (you may have gotten a newer version
// by building it yourself).
func CheckForUpdate() (*check.Result, error) {
param := check.Params{
AppVersion: Version,
AppId: updateAppID,
Channel: "stable",
}
up, err := update.New().VerifySignatureWithPEM([]byte(updatePubKey))
if err != nil {
return nil, fmt.Errorf("Failed to parse public key: %v", err)
}
res, err := param.CheckForUpdate(updateEndpointURL, up)
if err != nil {
return res, err
}
newer, err := versionIsNewer(res.Version)
if err != nil {
return nil, err
}
if !newer {
return nil, ErrNoUpdateAvailable
}
return res, err
}
// Apply cheks if the running process is able to update itself
// and than updates to the passed release
func Apply(rel *check.Result) error {
if err := update.New().CanUpdate(); err != nil {
return err
}
if err, errRecover := rel.Update(); err != nil {
err = fmt.Errorf("Update failed: %v\n", err)
if errRecover != nil {
err = fmt.Errorf("%s\nRecovery failed! Cause: %v\nYou may need to recover manually", err, errRecover)
}
return err
}
return nil
}
// ShouldAutoUpdate decides wether a new version should be applied
// checks against config setting and new version string. returns false in case of error
func ShouldAutoUpdate(setting config.AutoUpdateSetting, newVer string) bool {
if setting == config.AutoUpdateNever {
return false
}
nv, err := semver.NewVersion(newVer)
if err != nil {
log.Infof("could not parse version string: %s", err)
return false
}
n := nv.Slice()
c := currentVersion.Slice()
switch setting {
case config.AutoUpdatePatch:
if n[0] < c[0] {
return false
}
if n[1] < c[1] {
return false
}
return n[2] > c[2]
case config.AutoUpdateMinor:
if n[0] != c[0] {
return false
}
return n[1] > c[1] || (n[1] == c[1] && n[2] > c[2])
case config.AutoUpdateMajor:
for i := 0; i < 3; i++ {
if n[i] < c[i] {
return false
}
}
return true
}
return false
}
// CliCheckForUpdates is the automatic update check from the commandline.
func CliCheckForUpdates(cfg *config.Config, repoPath string) error {
// if config says not to, don't check for updates
if !cfg.Version.ShouldCheckForUpdate() {
log.Info("update check skipped.")
return nil
}
log.Info("checking for update")
u, err := CheckForUpdate()
// if there is no update available, record it, and exit. NB: only record
// if we checked successfully.
if err == ErrNoUpdateAvailable {
log.Infof("No update available, checked on %s", time.Now())
r, err := fsrepo.Open(repoPath)
if err != nil {
return err
}
if err := recordUpdateCheck(cfg); err != nil {
return err
}
// NB: r's Config may be newer than cfg. This overwrites regardless.
r.SetConfig(cfg)
if err := r.Close(); err != nil {
return err
}
return nil
}
// if another, unexpected error occurred, note it.
if err != nil {
log.Debugf("Error while checking for update: %v", err)
return nil
}
// there is an update available
// if we autoupdate
if cfg.Version.AutoUpdate != config.AutoUpdateNever {
// and we should auto update
if ShouldAutoUpdate(cfg.Version.AutoUpdate, u.Version) {
log.Infof("Applying update %s", u.Version)
if err = Apply(u); err != nil {
log.Debug(err)
return nil
}
// BUG(cryptix): no good way to restart yet. - tracking https://github.com/inconshreveable/go-update/issues/5
fmt.Printf("update %v applied. please restart.\n", u.Version)
os.Exit(0)
}
}
// autoupdate did not exit, so regular notices.
switch cfg.Version.Check {
case config.CheckError:
return fmt.Errorf(errShouldUpdate, Version, u.Version)
case config.CheckWarn:
// print the warning
fmt.Printf("New version available: %s\n", u.Version)
default: // ignore
}
return nil
}
func versionIsNewer(version string) (bool, error) {
nv, err := semver.NewVersion(version)
if err != nil {
return false, fmt.Errorf("could not parse version string: %s", err)
}
cv := currentVersion
newer := !nv.LessThan(*cv) && nv.String() != cv.String()
return newer, nil
}
var errShouldUpdate = `
Your go-ipfs version is: %s
There is a new version available: %s
Since this is alpha software, it is strongly recommended you update.
To update, run:
ipfs update apply
To disable this notice, run:
ipfs config Version.Check warn
`
// recordUpdateCheck is called to record that an update check was performed,
// showing that the running version is the most recent one.
func recordUpdateCheck(cfg *config.Config) error {
cfg.Version.CheckDate = time.Now()
if cfg.Version.CheckPeriod == "" {
// CheckPeriod was not initialized for some reason (e.g. config file broken)
return errors.New("config.Version.CheckPeriod not set. config broken?")
}
return nil
}

View File

@ -1,60 +0,0 @@
package updates
import (
"testing"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
"github.com/ipfs/go-ipfs/repo/config"
)
// TestParseVersion just makes sure that we dont commit a bad version number
func TestParseVersion(t *testing.T) {
_, err := parseVersion()
if err != nil {
t.Fatal(err)
}
}
func TestShouldAutoUpdate(t *testing.T) {
tests := []struct {
setting config.AutoUpdateSetting
currV, newV string
should bool
}{
{config.AutoUpdateNever, "0.0.1", "1.0.0", false},
{config.AutoUpdateNever, "0.0.1", "0.1.0", false},
{config.AutoUpdateNever, "0.0.1", "0.0.1", false},
{config.AutoUpdateNever, "0.0.1", "0.0.2", false},
{config.AutoUpdatePatch, "0.0.1", "1.0.0", false},
{config.AutoUpdatePatch, "0.0.1", "0.1.0", false},
{config.AutoUpdatePatch, "0.0.1", "0.0.1", false},
{config.AutoUpdatePatch, "0.0.2", "0.0.1", false},
{config.AutoUpdatePatch, "0.0.1", "0.0.2", true},
{config.AutoUpdateMinor, "0.1.1", "1.0.0", false},
{config.AutoUpdateMinor, "0.1.1", "0.2.0", true},
{config.AutoUpdateMinor, "0.1.1", "0.1.2", true},
{config.AutoUpdateMinor, "0.2.1", "0.1.9", false},
{config.AutoUpdateMinor, "0.1.2", "0.1.1", false},
{config.AutoUpdateMajor, "1.0.0", "2.0.0", true},
{config.AutoUpdateMajor, "1.0.0", "1.1.0", true},
{config.AutoUpdateMajor, "1.0.0", "1.0.1", true},
{config.AutoUpdateMajor, "2.0.0", "1.0.0", false}, // don't downgrade
{config.AutoUpdateMajor, "2.5.0", "2.4.0", false},
{config.AutoUpdateMajor, "2.0.2", "2.0.1", false},
}
for i, tc := range tests {
var err error
currentVersion, err = semver.NewVersion(tc.currV)
if err != nil {
t.Fatalf("Could not parse test version: %v", err)
}
if tc.should != ShouldAutoUpdate(tc.setting, tc.newV) {
t.Fatalf("#%d failed for %+v", i, tc)
}
}
}