mirror of
https://github.com/ipfs/kubo.git
synced 2025-10-25 10:27:01 +08:00
remove migrations code from go-ipfs, in favor of fs-repo-migrations repo
This commit is contained in:
@ -1,69 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
migrate "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-migrate"
|
||||
mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
|
||||
)
|
||||
|
||||
type migration struct {
|
||||
}
|
||||
|
||||
// Version is the int version number. This could be a string
|
||||
// in future versions
|
||||
func (m migration) Versions() string {
|
||||
return "0-to-1"
|
||||
}
|
||||
|
||||
// Reversible returns whether this migration can be reverted.
|
||||
// Endeavor to make them all reversible. This is here only to warn users
|
||||
// in case this is not the case.
|
||||
func (m migration) Reversible() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Apply applies the migration in question.
|
||||
// This migration merely adds a version file.
|
||||
func (m migration) Apply(opts migrate.Options) error {
|
||||
repo := mfsr.RepoPath(opts.Path)
|
||||
|
||||
// first, check if there is a version file.
|
||||
// if there is, bail out.
|
||||
if v, err := repo.Version(); err == nil {
|
||||
return fmt.Errorf("repo at %s is version %s (not 0)", opts.Path, v)
|
||||
} else if !strings.Contains(err.Error(), "no version file in repo") {
|
||||
return err
|
||||
}
|
||||
|
||||
// add the version file
|
||||
if err := repo.WriteVersion("1"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Revert un-applies the migration in question. This should be best-effort.
|
||||
// Some migrations are definitively one-way. If so, return an error.
|
||||
func (m migration) Revert(opts migrate.Options) error {
|
||||
repo := mfsr.RepoPath(opts.Path)
|
||||
|
||||
if err := repo.CheckVersion("1"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// remove the version file
|
||||
if err := os.Remove(repo.VersionFile()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
m := migration{}
|
||||
migrate.Main(&m)
|
||||
}
|
||||
@ -1,248 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
dstore "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
||||
flatfs "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/flatfs"
|
||||
leveldb "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/leveldb"
|
||||
dsq "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/query"
|
||||
migrate "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-migrate"
|
||||
mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
|
||||
|
||||
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
||||
)
|
||||
|
||||
var _ = context.Background
|
||||
|
||||
const peerKeyName = "peer.key"
|
||||
|
||||
type migration struct{}
|
||||
|
||||
func (m migration) Versions() string {
|
||||
return "1-to-2"
|
||||
}
|
||||
|
||||
func (m migration) Reversible() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (m migration) Apply(opts migrate.Options) error {
|
||||
repo := mfsr.RepoPath(opts.Path)
|
||||
|
||||
if err := repo.CheckVersion("1"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 1) run some sanity checks to make sure we should even bother
|
||||
err := sanityChecks(opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 2) Transfer blocks out of leveldb into flatDB
|
||||
err = transferBlocksToFlatDB(opts.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 3) move ipfs path from .go-ipfs to .ipfs
|
||||
newpath, err := moveIpfsDir(opts.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 4) Update version number
|
||||
repo = mfsr.RepoPath(newpath)
|
||||
err = repo.WriteVersion("2")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m migration) Revert(opts migrate.Options) error {
|
||||
repo := mfsr.RepoPath(opts.Path)
|
||||
if err := repo.CheckVersion("2"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 1) Move directory back to .go-ipfs
|
||||
npath, err := reverseIpfsDir(opts.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 2) move blocks back from flatfs to leveldb
|
||||
err = transferBlocksFromFlatDB(npath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 3) change version number back down
|
||||
repo = mfsr.RepoPath(npath)
|
||||
err = repo.WriteVersion("1")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// sanityChecks performs a set of tests to make sure the migration will go
|
||||
// smoothly
|
||||
func sanityChecks(opts migrate.Options) error {
|
||||
npath := strings.Replace(opts.Path, ".go-ipfs", ".ipfs", 1)
|
||||
|
||||
// make sure we can move the repo from .go-ipfs to .ipfs
|
||||
err := os.Mkdir(npath, 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// we can? good, remove it now
|
||||
err = os.Remove(npath)
|
||||
if err != nil {
|
||||
// this is weird... not worth continuing
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func transferBlocksToFlatDB(repopath string) error {
|
||||
ldbpath := path.Join(repopath, "datastore")
|
||||
ldb, err := leveldb.NewDatastore(ldbpath, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
blockspath := path.Join(repopath, "blocks")
|
||||
err = os.Mkdir(blockspath, 0777)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
fds, err := flatfs.New(blockspath, 4)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return transferBlocks(ldb, fds, "/b/", "")
|
||||
}
|
||||
|
||||
func transferBlocksFromFlatDB(repopath string) error {
|
||||
|
||||
ldbpath := path.Join(repopath, "datastore")
|
||||
blockspath := path.Join(repopath, "blocks")
|
||||
fds, err := flatfs.New(blockspath, 4)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ldb, err := leveldb.NewDatastore(ldbpath, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = transferBlocks(fds, ldb, "", "/b/")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Now remove the blocks directory
|
||||
err = os.RemoveAll(blockspath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func transferBlocks(from, to dstore.Datastore, fpref, tpref string) error {
|
||||
q := dsq.Query{Prefix: fpref, KeysOnly: true}
|
||||
res, err := from.Query(q)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Starting query")
|
||||
for result := range res.Next() {
|
||||
nkey := fmt.Sprintf("%s%s", tpref, result.Key[len(fpref):])
|
||||
|
||||
fkey := dstore.NewKey(result.Key)
|
||||
val, err := from.Get(fkey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = to.Put(dstore.NewKey(nkey), val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = from.Delete(fkey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fmt.Println("Query done")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func moveIpfsDir(curpath string) (string, error) {
|
||||
newpath := strings.Replace(curpath, ".go-ipfs", ".ipfs", 1)
|
||||
return newpath, os.Rename(curpath, newpath)
|
||||
}
|
||||
|
||||
func reverseIpfsDir(curpath string) (string, error) {
|
||||
newpath := strings.Replace(curpath, ".ipfs", ".go-ipfs", 1)
|
||||
return newpath, os.Rename(curpath, newpath)
|
||||
}
|
||||
|
||||
func loadConfigJSON(repoPath string) (map[string]interface{}, error) {
|
||||
cfgPath := path.Join(repoPath, "config")
|
||||
fi, err := os.Open(cfgPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var out map[string]interface{}
|
||||
err = json.NewDecoder(fi).Decode(&out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func saveConfigJSON(repoPath string, cfg map[string]interface{}) error {
|
||||
cfgPath := path.Join(repoPath, "config")
|
||||
fi, err := os.Create(cfgPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
out, err := json.MarshalIndent(cfg, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = fi.Write(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
m := migration{}
|
||||
migrate.Main(&m)
|
||||
}
|
||||
@ -1,56 +0,0 @@
|
||||
package mfsr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const VersionFile = "version"
|
||||
|
||||
type RepoPath string
|
||||
|
||||
func (rp RepoPath) VersionFile() string {
|
||||
return path.Join(string(rp), VersionFile)
|
||||
}
|
||||
|
||||
func (rp RepoPath) Version() (string, error) {
|
||||
if rp == "" {
|
||||
return "", fmt.Errorf("invalid repo path \"%s\"", rp)
|
||||
}
|
||||
|
||||
fn := rp.VersionFile()
|
||||
if _, err := os.Stat(fn); os.IsNotExist(err) {
|
||||
return "", errors.New("no version file in repo at " + string(rp))
|
||||
}
|
||||
|
||||
c, err := ioutil.ReadFile(fn)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
s := string(c)
|
||||
s = strings.TrimSpace(s)
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (rp RepoPath) CheckVersion(version string) error {
|
||||
v, err := rp.Version()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if v != version {
|
||||
return fmt.Errorf("versions differ (expected: %s, actual:%s)", version, v)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rp RepoPath) WriteVersion(version string) error {
|
||||
fn := rp.VersionFile()
|
||||
return ioutil.WriteFile(fn, []byte(version+"\n"), 0644)
|
||||
}
|
||||
Reference in New Issue
Block a user