Skip to content

Commit

Permalink
Merge pull request #3158 from ipfs/feat/migrations-help
Browse files Browse the repository at this point in the history
fsrepo: improve migrations copy
  • Loading branch information
whyrusleeping authored Aug 31, 2016
2 parents 4286cd3 + b4718a2 commit 40159e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
12 changes: 9 additions & 3 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,25 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
return
case fsrepo.ErrNeedMigration:
domigrate, found, _ := req.Option(migrateKwd).Bool()
fmt.Println("Found old repo version, migrations need to be run.")
fmt.Println("Found outdated fs-repo, migrations need to be run.")

if !found {
domigrate = YesNoPrompt("Run migrations automatically? [y/N]")
domigrate = YesNoPrompt("Run migrations now? [y/N]")
}

if !domigrate {
res.SetError(fmt.Errorf("please run the migrations manually"), cmds.ErrNormal)
fmt.Println("Not running migrations of fs-repo now.")
fmt.Println("Please get fs-repo-migrations from https://dist.ipfs.io")
res.SetError(fmt.Errorf("fs-repo requires migration"), cmds.ErrNormal)
return
}

err = migrate.RunMigration(fsrepo.RepoVersion)
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")
res.SetError(err, cmds.ErrNormal)
return
}
Expand Down
24 changes: 12 additions & 12 deletions repo/fsrepo/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func migrationsBinName() string {
func RunMigration(newv int) error {
migrateBin := migrationsBinName()

fmt.Println(" => checking for migrations binary...")
fmt.Println(" => Looking for suitable fs-repo-migrations binary.")

var err error
migrateBin, err = exec.LookPath(migrateBin)
Expand All @@ -47,15 +47,17 @@ func RunMigration(newv int) error {
}

if err != nil {
fmt.Println(" => usable migrations not found on system, fetching...")
fmt.Println(" => None found, downloading.")

loc, err := GetMigrations()
if err != nil {
fmt.Println(" => Failed to download fs-repo-migrations.")
return err
}

err = verifyMigrationSupportsVersion(loc, newv)
if err != nil {
return fmt.Errorf("no migration binary found that supports version %d - %s", newv, err)
return fmt.Errorf("no fs-repo-migration binary found for version %d: %s", newv, err)
}

migrateBin = loc
Expand All @@ -65,36 +67,35 @@ func RunMigration(newv int) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

fmt.Printf(" => running migration: '%s -to %d -y'\n\n", migrateBin, newv)
fmt.Printf(" => Running: %s -to %d -y\n", migrateBin, newv)

err = cmd.Run()
if err != nil {
fmt.Printf(" => Failed: %s -to %d -y\n", migrateBin, newv)
return fmt.Errorf("migration failed: %s", err)
}

fmt.Println(" => migrations binary completed successfully")
fmt.Printf(" => Success: fs-repo has been migrated to version %d.\n", newv)

return nil
}

func GetMigrations() (string, error) {
latest, err := GetLatestVersion(DistPath, migrations)
if err != nil {
return "", fmt.Errorf("getting latest version of fs-repo-migrations: %s", err)
return "", fmt.Errorf("failed to find latest fs-repo-migrations: %s", err)
}

dir, err := ioutil.TempDir("", "go-ipfs-migrate")
if err != nil {
return "", fmt.Errorf("tempdir: %s", err)
return "", fmt.Errorf("failed to create fs-repo-migrations tempdir: %s", err)
}

out := filepath.Join(dir, migrationsBinName())

err = GetBinaryForVersion(migrations, migrations, DistPath, latest, out)
if err != nil {
fmt.Printf(" => error getting migrations binary: %s\n", err)
fmt.Println(" => could not find or install fs-repo-migrations, please manually install it")
return "", fmt.Errorf("failed to find migrations binary")
return "", fmt.Errorf("failed to download latest fs-repo-migrations: %s", err)
}

err = os.Chmod(out, 0755)
Expand Down Expand Up @@ -184,7 +185,6 @@ func httpGet(url string) (*http.Response, error) {
}

func httpFetch(url string) (io.ReadCloser, error) {
fmt.Printf("fetching url: %s\n", url)
resp, err := httpGet(url)
if err != nil {
return nil, err
Expand All @@ -196,7 +196,7 @@ func httpFetch(url string) (io.ReadCloser, error) {
return nil, fmt.Errorf("error reading error body: %s", err)
}

return nil, fmt.Errorf("%s: %s", resp.Status, string(mes))
return nil, fmt.Errorf("GET %s error: %s: %s", url, resp.Status, string(mes))
}

return resp.Body, nil
Expand Down
14 changes: 7 additions & 7 deletions test/sharness/t0066-migration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ test_expect_success "manually reset repo version to 3" '
'

test_expect_success "ipfs daemon --migrate=false fails" '
test_expect_code 1 ipfs daemon --migrate=false 2> false_out
test_expect_code 1 ipfs daemon --migrate=false > false_out
'

test_expect_success "output looks good" '
grep "please run the migrations manually" false_out
grep "Please get fs-repo-migrations from https://dist.ipfs.io" false_out
'

test_expect_success "ipfs daemon --migrate=true runs migration" '
test_expect_code 1 ipfs daemon --migrate=true > true_out
'

test_expect_success "output looks good" '
grep "running migration" true_out > /dev/null &&
grep "binary completed successfully" true_out > /dev/null
grep "Running: " true_out > /dev/null &&
grep "Success: fs-repo has been migrated to version 4." true_out > /dev/null
'

test_expect_success "'ipfs daemon' prompts to auto migrate" '
test_expect_code 1 ipfs daemon > daemon_out 2> daemon_err
'

test_expect_success "output looks good" '
grep "Found old repo version" daemon_out > /dev/null &&
grep "Run migrations automatically?" daemon_out > /dev/null &&
grep "please run the migrations manually" daemon_err > /dev/null
grep "Found outdated fs-repo" daemon_out > /dev/null &&
grep "Run migrations now?" daemon_out > /dev/null &&
grep "Please get fs-repo-migrations from https://dist.ipfs.io" daemon_out > /dev/null
'

test_done

0 comments on commit 40159e5

Please sign in to comment.