Skip to content

Commit 2693565

Browse files
authored
Merge pull request #8054 from ipfs/chore/migration-output-stdout
fix: make migrations log output to stdout
2 parents fb4542b + 7c8df87 commit 2693565

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

repo/fsrepo/migrations/migrations.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s
3838
return fmt.Errorf("downgrade not allowed from %d to %d", fromVer, targetVer)
3939
}
4040

41-
log.Print("Looking for suitable migration binaries.")
41+
logger := log.New(os.Stdout, "", 0)
42+
43+
logger.Print("Looking for suitable migration binaries.")
4244

4345
migrations, binPaths, err := findMigrations(ctx, fromVer, targetVer)
4446
if err != nil {
@@ -54,17 +56,17 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s
5456
}
5557
}
5658

57-
log.Println("Need", len(missing), "migrations, downloading.")
59+
logger.Println("Need", len(missing), "migrations, downloading.")
5860

5961
tmpDir, err := ioutil.TempDir("", "migrations")
6062
if err != nil {
6163
return err
6264
}
6365
defer os.RemoveAll(tmpDir)
6466

65-
fetched, err := fetchMigrations(ctx, fetcher, missing, tmpDir)
67+
fetched, err := fetchMigrations(ctx, fetcher, missing, tmpDir, logger)
6668
if err != nil {
67-
log.Print("Failed to download migrations.")
69+
logger.Print("Failed to download migrations.")
6870
return err
6971
}
7072
for i := range missing {
@@ -77,13 +79,13 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s
7779
revert = true
7880
}
7981
for _, migration := range migrations {
80-
log.Println("Running migration", migration, "...")
81-
err = runMigration(ctx, binPaths[migration], ipfsDir, revert)
82+
logger.Println("Running migration", migration, "...")
83+
err = runMigration(ctx, binPaths[migration], ipfsDir, revert, logger)
8284
if err != nil {
8385
return fmt.Errorf("migration %s failed: %s", migration, err)
8486
}
8587
}
86-
log.Printf("Success: fs-repo migrated to version %d.\n", targetVer)
88+
logger.Printf("Success: fs-repo migrated to version %d.\n", targetVer)
8789

8890
return nil
8991
}
@@ -142,14 +144,14 @@ func findMigrations(ctx context.Context, from, to int) ([]string, map[string]str
142144
return migrations, binPaths, nil
143145
}
144146

145-
func runMigration(ctx context.Context, binPath, ipfsDir string, revert bool) error {
147+
func runMigration(ctx context.Context, binPath, ipfsDir string, revert bool, logger *log.Logger) error {
146148
pathArg := fmt.Sprintf("-path=%s", ipfsDir)
147149
var cmd *exec.Cmd
148150
if revert {
149-
log.Println(" => Running:", binPath, pathArg, "-verbose=true -revert")
151+
logger.Println(" => Running:", binPath, pathArg, "-verbose=true -revert")
150152
cmd = exec.CommandContext(ctx, binPath, pathArg, "-verbose=true", "-revert")
151153
} else {
152-
log.Println(" => Running:", binPath, pathArg, "-verbose=true")
154+
logger.Println(" => Running:", binPath, pathArg, "-verbose=true")
153155
cmd = exec.CommandContext(ctx, binPath, pathArg, "-verbose=true")
154156
}
155157
cmd.Stdout = os.Stdout
@@ -159,7 +161,7 @@ func runMigration(ctx context.Context, binPath, ipfsDir string, revert bool) err
159161

160162
// fetchMigrations downloads the requested migrations, and returns a slice with
161163
// the paths of each binary, in the same order specified by needed.
162-
func fetchMigrations(ctx context.Context, fetcher Fetcher, needed []string, destDir string) ([]string, error) {
164+
func fetchMigrations(ctx context.Context, fetcher Fetcher, needed []string, destDir string, logger *log.Logger) ([]string, error) {
163165
osv, err := osWithVariant()
164166
if err != nil {
165167
return nil, err
@@ -173,21 +175,21 @@ func fetchMigrations(ctx context.Context, fetcher Fetcher, needed []string, dest
173175
bins := make([]string, len(needed))
174176
// Download and unpack all requested migrations concurrently.
175177
for i, name := range needed {
176-
log.Printf("Downloading migration: %s...", name)
178+
logger.Printf("Downloading migration: %s...", name)
177179
go func(i int, name string) {
178180
defer wg.Done()
179181
dist := path.Join(distMigsRoot, name)
180182
ver, err := LatestDistVersion(ctx, fetcher, dist, false)
181183
if err != nil {
182-
log.Printf("could not get latest version of migration %s: %s", name, err)
184+
logger.Printf("could not get latest version of migration %s: %s", name, err)
183185
return
184186
}
185187
loc, err := FetchBinary(ctx, fetcher, dist, ver, name, destDir)
186188
if err != nil {
187-
log.Printf("could not download %s: %s", name, err)
189+
logger.Printf("could not download %s: %s", name, err)
188190
return
189191
}
190-
log.Printf("Downloaded and unpacked migration: %s (%s)", loc, ver)
192+
logger.Printf("Downloaded and unpacked migration: %s (%s)", loc, ver)
191193
bins[i] = loc
192194
}(i, name)
193195
}

repo/fsrepo/migrations/migrations_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package migrations
22

33
import (
44
"context"
5+
"fmt"
56
"io/ioutil"
7+
"log"
68
"os"
79
"path/filepath"
810
"strings"
@@ -126,7 +128,10 @@ func TestFetchMigrations(t *testing.T) {
126128
defer os.RemoveAll(tmpDir)
127129

128130
needed := []string{"fs-repo-1-to-2", "fs-repo-2-to-3"}
129-
fetched, err := fetchMigrations(ctx, fetcher, needed, tmpDir)
131+
buf := new(strings.Builder)
132+
buf.Grow(256)
133+
logger := log.New(buf, "", 0)
134+
fetched, err := fetchMigrations(ctx, fetcher, needed, tmpDir, logger)
130135
if err != nil {
131136
t.Fatal(err)
132137
}
@@ -137,6 +142,18 @@ func TestFetchMigrations(t *testing.T) {
137142
t.Error("expected file to exist:", bin)
138143
}
139144
}
145+
146+
// Check expected log output
147+
for _, mig := range needed {
148+
logOut := fmt.Sprintf("Downloading migration: %s", mig)
149+
if !strings.Contains(buf.String(), logOut) {
150+
t.Fatalf("did not find expected log output %q", logOut)
151+
}
152+
logOut = fmt.Sprintf("Downloaded and unpacked migration: %s", filepath.Join(tmpDir, mig))
153+
if !strings.Contains(buf.String(), logOut) {
154+
t.Fatalf("did not find expected log output %q", logOut)
155+
}
156+
}
140157
}
141158

142159
func TestRunMigrations(t *testing.T) {

0 commit comments

Comments
 (0)