Skip to content

Commit

Permalink
feat(SelfUpdate): added check for version being out of date
Browse files Browse the repository at this point in the history
while we're here, might as well use an ipns dnslink entry to check
if the qri verion if out of date by comparing a hard-coded version
of the 2-n back version of qri against the hardcoded hash of the
1-n back version stored within the app itself.
  • Loading branch information
b5 committed Mar 6, 2018
1 parent 101809d commit 0d87261
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ func (s *Server) Serve() (err error) {
go s.ServeRPC()
go s.ServeWebapp()

if node, err := s.qriNode.IPFSNode(); err == nil {
go func() {
if err := core.CheckVersion(context.Background(), node.Namesys); err == core.ErrUpdateRequired {
s.log.Info("This version of qri is out of date, please refer to https://github.com/qri-io/qri/releases/latest for more info")
}
}()
}

// http.ListenAndServe will not return unless there's an error
return StartServer(s.cfg, server)
}
Expand Down Expand Up @@ -181,6 +189,7 @@ func (s *Server) HandleIPNSPath(w http.ResponseWriter, r *http.Request) {
apiutil.WriteErrResponse(w, http.StatusBadRequest, fmt.Errorf("no IPFS node present: %s", err.Error()))
return
}

p, err := node.Namesys.Resolve(r.Context(), r.URL.Path[len("/ipns/"):])
if err != nil {
apiutil.WriteErrResponse(w, http.StatusBadRequest, fmt.Errorf("error resolving IPNS Name: %s", err.Error()))
Expand Down
41 changes: 41 additions & 0 deletions core/self_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package core

import (
"context"
"fmt"

namesys "gx/ipfs/QmViBzgruNUoLNBnXcx8YWbDNwV8MNGEGKkLo6JGetygdw/go-ipfs/namesys"
)

/*
* self-updating checks IPNS entry that represents the desired hash of the previous version of
* this program, which is the result of adding the complied binary of this program to IPFS.
* If the returned value of a lookup differs, we have a version mismatch, and need to perform
* an update.
*
* In the future we'll do this automatically, but for now we can at least warn users that they need
* to update their version
*/

// previousVersion is a hard-coded reference the gx "lastpubver" file of the previous release
const previousVersion = "/ipfs/QmcXZCLAgUdvXpt1fszjNGVGn6WnhsrJahkQXY3JJqxUWJ"

// prevIPNSName is the dnslink address to check for version agreement
const prevIPNSName = "/ipns/cli.previous.qri.io"

// ErrUpdateRequired means this version of qri is out of date
var ErrUpdateRequired = fmt.Errorf("update required")

// CheckVersion uses a name resolver to lookup prevIPNSName, checking if the hard-coded previousVersion
// and the returned lookup match. If they don't, CheckVersion returns ErrUpdateRequired
func CheckVersion(ctx context.Context, res namesys.Resolver) error {
p, err := res.Resolve(ctx, prevIPNSName)
if err != nil {
return fmt.Errorf("error resolving name: %s", err.Error())
}

if p.String() != previousVersion {
return ErrUpdateRequired
}
return nil
}

0 comments on commit 0d87261

Please sign in to comment.