Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 'ipfs repo migrate' command #7658

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func TestCommands(t *testing.T) {
"/repo/stat",
"/repo/verify",
"/repo/version",
"/repo/migrate",
"/resolve",
"/shutdown",
"/stats",
Expand Down
70 changes: 67 additions & 3 deletions core/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import (
"sync"
"text/tabwriter"

humanize "github.com/dustin/go-humanize"
oldcmds "github.com/ipfs/go-ipfs/commands"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
migrate "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
"github.com/ipfs/go-ipfs/repo/fsrepo/migrations/ipfsfetcher"

humanize "github.com/dustin/go-humanize"
cid "github.com/ipfs/go-cid"
bstore "github.com/ipfs/go-ipfs-blockstore"
cmds "github.com/ipfs/go-ipfs-cmds"
Expand All @@ -39,6 +42,7 @@ var RepoCmd = &cmds.Command{
"fsck": repoFsckCmd,
"version": repoVersionCmd,
"verify": repoVerifyCmd,
"migrate": repoMigrateCmd,
},
}

Expand All @@ -49,8 +53,9 @@ type GcResult struct {
}

const (
repoStreamErrorsOptionName = "stream-errors"
repoQuietOptionName = "quiet"
repoStreamErrorsOptionName = "stream-errors"
repoQuietOptionName = "quiet"
repoAllowDowngradeOptionName = "allow-downgrade"
)

var repoGcCmd = &cmds.Command{
Expand Down Expand Up @@ -375,3 +380,62 @@ var repoVersionCmd = &cmds.Command{
}),
},
}

var repoMigrateCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Apply any outstanding migrations to the repo.",
},
Options: []cmds.Option{
cmds.BoolOption(repoAllowDowngradeOptionName, "Allow downgrading to a lower repo version"),
},
NoRemote: true,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
cctx := env.(*oldcmds.Context)
allowDowngrade, _ := req.Options[repoAllowDowngradeOptionName].(bool)

_, err := fsrepo.Open(cctx.ConfigRoot)
if err != fsrepo.ErrNeedMigration {
fmt.Println("Repo does not require migration")
return nil
}

fmt.Println("Found outdated fs-repo, starting migration.")

// Read Migration section of IPFS config
migrationCfg, err := migrate.ReadMigrationConfig(cctx.ConfigRoot)
if err != nil {
return err
}

// Define function to create IPFS fetcher. Do not supply an
// already-constructed IPFS fetcher, because this may be expensive and
// not needed according to migration config. Instead, supply a function
// to construct the particular IPFS fetcher implementation used here,
// which is called only if an IPFS fetcher is needed.
newIpfsFetcher := func(distPath string) migrate.Fetcher {
return ipfsfetcher.NewIpfsFetcher(distPath, 0, &cctx.ConfigRoot)
}

// Fetch migrations from current distribution, or location from environ
fetchDistPath := migrate.GetDistPathEnv(migrate.CurrentIpfsDist)

// Create fetchers according to migrationCfg.DownloadSources
fetcher, err := migrate.GetMigrationFetcher(migrationCfg.DownloadSources, fetchDistPath, newIpfsFetcher)
if err != nil {
return err
}
defer fetcher.Close()

err = migrate.RunMigration(cctx.Context(), fetcher, fsrepo.RepoVersion, "", allowDowngrade)
if err != nil {
fmt.Println("The migrations of fs-repo failed:")
fmt.Printf(" %s\n", err)
fmt.Println("If you think this is a bug, please file an issue and include this whole log output.")
fmt.Println(" https://github.com/ipfs/fs-repo-migrations")
return err
}

fmt.Printf("Success: fs-repo has been migrated to version %d.\n", fsrepo.RepoVersion)
return nil
},
}
2 changes: 1 addition & 1 deletion mk/golang.mk
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ test_go_fmt:
TEST_GO += test_go_fmt

test_go_lint: test/bin/golangci-lint
golangci-lint run ./...
golangci-lint run --timeout 5m ./...
.PHONY: test_go_lint

test_go: $(TEST_GO)
Expand Down
2 changes: 0 additions & 2 deletions repo/fsrepo/migrations/ipfsfetcher/ipfsfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ func (f *IpfsFetcher) startTempNode(ctx context.Context) error {
cancel()
// Wait until ipfs is stopped
<-node.Context().Done()

fmt.Println("migration peer", node.Identity, "shutdown")
}

addrs, err := ipfs.Swarm().LocalAddrs(ctx)
Expand Down
22 changes: 22 additions & 0 deletions test/sharness/t0066-migration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,26 @@ test_expect_success "output looks good" '
grep "Please get fs-repo-migrations from https://dist.ipfs.io" daemon_out > /dev/null
'

test_expect_success "ipfs repo migrate succeed" '
test_expect_code 0 ipfs repo migrate > migrate_out
'

test_expect_success "output looks good" '
grep "Found outdated fs-repo, starting migration." migrate_out > /dev/null &&
grep "Success: fs-repo migrated to version $IPFS_REPO_VER" true_out > /dev/null
'

test_expect_success "manually reset repo version to 11" '
echo "$IPFS_REPO_VER" > "$IPFS_PATH"/version
'

test_expect_success "detect repo does not need migration" '
test_expect_code 0 ipfs repo migrate > migrate_out
'

test_expect_success "output looks good" '
grep "Repo does not require migration" migrate_out > /dev/null
'
cat migrate_out

test_done