-
Notifications
You must be signed in to change notification settings - Fork 42
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
feat: migration from 15 to 16 #190
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.PHONY: build clean | ||
|
||
build: | ||
go build -mod=vendor | ||
|
||
clean: | ||
go clean | ||
|
||
test: | ||
@cd test-e2e && ./test.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Package atomicfile provides the ability to write a file with an eventual | ||
// rename on Close (using os.Rename). This allows for a file to always be in a | ||
// consistent state and never represent an in-progress write. | ||
// | ||
// NOTE: `os.Rename` may not be atomic on your operating system. | ||
package atomicfile | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// File behaves like os.File, but does an atomic rename operation at Close. | ||
type File struct { | ||
*os.File | ||
path string | ||
} | ||
|
||
// New creates a new temporary file that will replace the file at the given | ||
// path when Closed. | ||
func New(path string, mode os.FileMode) (*File, error) { | ||
f, err := ioutil.TempFile(filepath.Dir(path), filepath.Base(path)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := os.Chmod(f.Name(), mode); err != nil { | ||
f.Close() | ||
os.Remove(f.Name()) | ||
return nil, err | ||
} | ||
return &File{File: f, path: path}, nil | ||
} | ||
|
||
// Close the file replacing the configured file. | ||
func (f *File) Close() error { | ||
if err := f.File.Close(); err != nil { | ||
os.Remove(f.File.Name()) | ||
return err | ||
} | ||
if err := os.Rename(f.Name(), f.path); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Abort closes the file and removes it instead of replacing the configured | ||
// file. This is useful if after starting to write to the file you decide you | ||
// don't want it anymore. | ||
func (f *File) Abort() error { | ||
if err := f.File.Close(); err != nil { | ||
os.Remove(f.Name()) | ||
return err | ||
} | ||
if err := os.Remove(f.Name()); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/ipfs/fs-repo-migrations/fs-repo-15-to-16 | ||
|
||
go 1.22 | ||
|
||
require github.com/ipfs/fs-repo-migrations/tools v0.0.0-20211209222258-754a2dcb82ea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/ipfs/fs-repo-migrations/tools v0.0.0-20211209222258-754a2dcb82ea h1:lgfk2PMrJI3bh8FflcBTXyNi3rPLqa75J7KcoUfRJmc= | ||
github.com/ipfs/fs-repo-migrations/tools v0.0.0-20211209222258-754a2dcb82ea/go.mod h1:fADeaHKxwS+SKhc52rsL0P1MUcnyK31a9AcaG0KcfY8= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package main | ||
|
||
import ( | ||
mg15 "github.com/ipfs/fs-repo-migrations/fs-repo-15-to-16/migration" | ||
migrate "github.com/ipfs/fs-repo-migrations/tools/go-migrate" | ||
) | ||
|
||
func main() { | ||
m := mg15.Migration{} | ||
migrate.Main(m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
// package mg15 contains the code to perform 15-16 repository migration in Kubo. | ||
// This handles the following: | ||
// - Add /webrtc-direct listener if preexisting /udp/ /quic-v1 exists | ||
package mg15 | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
|
||
migrate "github.com/ipfs/fs-repo-migrations/tools/go-migrate" | ||
mfsr "github.com/ipfs/fs-repo-migrations/tools/mfsr" | ||
lock "github.com/ipfs/fs-repo-migrations/tools/repolock" | ||
log "github.com/ipfs/fs-repo-migrations/tools/stump" | ||
|
||
"github.com/ipfs/fs-repo-migrations/fs-repo-15-to-16/atomicfile" | ||
) | ||
|
||
const backupSuffix = ".15-to-16.bak" | ||
|
||
// Migration implements the migration described above. | ||
type Migration struct{} | ||
|
||
// Versions returns the current version string for this migration. | ||
func (m Migration) Versions() string { | ||
return "15-to-16" | ||
} | ||
|
||
// Reversible returns true, as we keep old config around | ||
func (m Migration) Reversible() bool { | ||
return true | ||
} | ||
|
||
// Apply update the config. | ||
func (m Migration) Apply(opts migrate.Options) error { | ||
log.Verbose = opts.Verbose | ||
log.Log("applying %s repo migration", m.Versions()) | ||
|
||
log.VLog("locking repo at %q", opts.Path) | ||
lk, err := lock.Lock2(opts.Path) | ||
if err != nil { | ||
return err | ||
} | ||
defer lk.Close() | ||
|
||
repo := mfsr.RepoPath(opts.Path) | ||
|
||
log.VLog(" - verifying version is '15'") | ||
if err := repo.CheckVersion("15"); err != nil { | ||
return err | ||
} | ||
|
||
log.Log("> Upgrading config to new format") | ||
|
||
path := filepath.Join(opts.Path, "config") | ||
in, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// make backup | ||
backup, err := atomicfile.New(path+backupSuffix, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
if _, err := backup.ReadFrom(in); err != nil { | ||
panicOnError(backup.Abort()) | ||
return err | ||
} | ||
if _, err := in.Seek(0, io.SeekStart); err != nil { | ||
panicOnError(backup.Abort()) | ||
return err | ||
} | ||
|
||
// Create a temp file to write the output to on success | ||
out, err := atomicfile.New(path, 0600) | ||
if err != nil { | ||
panicOnError(backup.Abort()) | ||
panicOnError(in.Close()) | ||
return err | ||
} | ||
|
||
if err := convert(in, out); err != nil { | ||
panicOnError(out.Abort()) | ||
panicOnError(backup.Abort()) | ||
panicOnError(in.Close()) | ||
return err | ||
} | ||
|
||
if err := in.Close(); err != nil { | ||
panicOnError(out.Abort()) | ||
panicOnError(backup.Abort()) | ||
} | ||
|
||
if err := repo.WriteVersion("16"); err != nil { | ||
log.Error("failed to update version file to 16") | ||
// There was an error so abort writing the output and clean up temp file | ||
panicOnError(out.Abort()) | ||
panicOnError(backup.Abort()) | ||
return err | ||
} else { | ||
// Write the output and clean up temp file | ||
panicOnError(out.Close()) | ||
panicOnError(backup.Close()) | ||
} | ||
|
||
log.Log("updated version file") | ||
|
||
log.Log("Migration 15 to 16 succeeded") | ||
return nil | ||
} | ||
|
||
// panicOnError is reserved for checks we can't solve transactionally if an error occurs | ||
func panicOnError(e error) { | ||
if e != nil { | ||
panic(fmt.Errorf("error can't be dealt with transactionally: %w", e)) | ||
} | ||
} | ||
|
||
func (m Migration) Revert(opts migrate.Options) error { | ||
log.Verbose = opts.Verbose | ||
log.Log("reverting migration") | ||
lk, err := lock.Lock2(opts.Path) | ||
if err != nil { | ||
return err | ||
} | ||
defer lk.Close() | ||
|
||
repo := mfsr.RepoPath(opts.Path) | ||
if err := repo.CheckVersion("16"); err != nil { | ||
return err | ||
} | ||
|
||
cfg := filepath.Join(opts.Path, "config") | ||
if err := os.Rename(cfg+backupSuffix, cfg); err != nil { | ||
return err | ||
} | ||
|
||
if err := repo.WriteVersion("15"); err != nil { | ||
return err | ||
} | ||
if opts.Verbose { | ||
log.Log("lowered version number to 15") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var quicRegex = regexp.MustCompilePOSIX("/quic-v1$") | ||
|
||
// convert converts the config from one version to another | ||
func convert(in io.Reader, out io.Writer) error { | ||
confMap := make(map[string]any) | ||
if err := json.NewDecoder(in).Decode(&confMap); err != nil { | ||
return err | ||
} | ||
|
||
// Append /webrtc-direct listener if /udp/../quic-v1 is present in any of .Addresses fields | ||
if err := func() error { | ||
a, ok := confMap["Addresses"] | ||
if !ok { | ||
return nil | ||
} | ||
addresses, ok := a.(map[string]any) | ||
if !ok { | ||
fmt.Printf("invalid type for .Addresses got %T expected json map; skipping .Addresses\n", a) | ||
return nil | ||
} | ||
|
||
for _, addressToRemove := range [...]string{"Swarm", "Announce", "AppendAnnounce", "NoAnnounce"} { | ||
s, ok := addresses[addressToRemove] | ||
if !ok { | ||
continue | ||
} | ||
|
||
swarm, ok := s.([]interface{}) | ||
if !ok { | ||
fmt.Printf("invalid type for .Addresses.%s got %T expected json array; skipping .Addresses.%s\n", addressToRemove, s, addressToRemove) | ||
continue | ||
} | ||
|
||
var newSwarm []interface{} | ||
uniq := map[string]struct{}{} | ||
for _, v := range swarm { | ||
if addr, ok := v.(string); ok { | ||
|
||
// if /quic-v1, add /webrtc-direct under the same port | ||
if quicRegex.MatchString(addr) { | ||
newAddr := quicRegex.ReplaceAllString(addr, "/webrtc-direct") | ||
|
||
if _, ok := uniq[newAddr]; ok { | ||
continue | ||
} | ||
uniq[newAddr] = struct{}{} | ||
|
||
newSwarm = append(newSwarm, newAddr) | ||
} | ||
|
||
// keep original addr | ||
if _, ok := uniq[addr]; ok { | ||
continue | ||
} | ||
uniq[addr] = struct{}{} | ||
|
||
newSwarm = append(newSwarm, addr) | ||
continue | ||
} | ||
newSwarm = append(newSwarm, v) | ||
} | ||
addresses[addressToRemove] = newSwarm | ||
} | ||
return nil | ||
}(); err != nil { | ||
return err | ||
} | ||
|
||
// Save new config | ||
fixed, err := json.MarshalIndent(confMap, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := out.Write(fixed); err != nil { | ||
return err | ||
} | ||
_, err = out.Write([]byte("\n")) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
repotest |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ This PR includes a lot of boilerplate, but the only code that changed is in
fs-repo-15-to-16/migration.go
+ tests infs-repo-15-to-16/test-e2e