-
Notifications
You must be signed in to change notification settings - Fork 502
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
[WIP] Building a CLI for migrating between storage backends #1609
Draft
arschles
wants to merge
3
commits into
gomods:main
Choose a base branch
from
arschles:migration-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
"github.com/gomods/athens/pkg/storage" | ||
) | ||
|
||
// returns the info bytes, mod, and zip (in that order) | ||
func getCompleteMod( | ||
ctx context.Context, | ||
getter storage.Getter, | ||
mod string, | ||
ver string, | ||
) ([]byte, []byte, io.ReadCloser, error) { | ||
infoBytes, err := getter.Info(ctx, mod, ver) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
modBytes, err := getter.GoMod(ctx, mod, ver) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
zip, err := getter.Zip(ctx, mod, ver) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
return infoBytes, modBytes, zip, nil | ||
} | ||
|
||
func transfer( | ||
ctx context.Context, | ||
cataloger storage.Cataloger, | ||
from storage.Getter, | ||
to storage.Saver, | ||
) error { | ||
token := "" | ||
// TODO: parallelize this | ||
for { | ||
pathParams, newToken, err := cataloger.Catalog(ctx, token, 100) | ||
if err != nil { | ||
return err | ||
} | ||
for _, pathParam := range pathParams { | ||
mod := pathParam.Module | ||
ver := pathParam.Version | ||
infoBytes, modBytes, zip, err := getCompleteMod( | ||
ctx, | ||
from, | ||
mod, | ||
ver, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
saveErr := to.Save( | ||
ctx, | ||
mod, | ||
ver, | ||
modBytes, | ||
zip, | ||
infoBytes, | ||
) | ||
if saveErr != nil { | ||
return err | ||
} | ||
} | ||
if newToken == "" { | ||
return nil | ||
} | ||
token = newToken | ||
} | ||
} |
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 cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func errLog(fmtString string, args ...interface{}) { | ||
fmt.Printf(fmtString, args...) | ||
os.Exit(1) | ||
} |
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,105 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gomods/athens/cmd/proxy/actions" | ||
"github.com/gomods/athens/pkg/config" | ||
"github.com/gomods/athens/pkg/storage" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type migrateCmd struct { | ||
from *string | ||
to *string | ||
} | ||
|
||
func (m *migrateCmd) run(cmd *cobra.Command, args []string) { | ||
cfg, err := config.ParseConfigFile(cfgFile) | ||
if err != nil { | ||
errLog("Couldn't read config file (%s)", err) | ||
} | ||
// TODO: make this a flag | ||
timeout := 1 * time.Second | ||
ctx := context.Background() | ||
|
||
fromStorage, err := actions.GetStorage( | ||
*m.from, | ||
cfg.Storage, | ||
timeout, | ||
) | ||
if err != nil { | ||
errLog( | ||
"Error getting 'from' storage %s (%s)", | ||
*m.from, | ||
err, | ||
) | ||
} | ||
toStorage, err := actions.GetStorage( | ||
*m.to, | ||
cfg.Storage, | ||
timeout, | ||
) | ||
if err != nil { | ||
errLog( | ||
"Error getting 'to' storage %s (%s)", | ||
*m.to, | ||
err, | ||
) | ||
} | ||
|
||
cataloger, ok := fromStorage.(storage.Cataloger) | ||
if !ok { | ||
errLog( | ||
"'from' storage %s doesn't support cataloging, sorry :(", | ||
*m.from, | ||
) | ||
} | ||
if err := transfer( | ||
ctx, | ||
cataloger, | ||
fromStorage, | ||
toStorage, | ||
); err != nil { | ||
errLog("Error transfering (%s)", err) | ||
} | ||
|
||
fmt.Println("migrate called") | ||
} | ||
|
||
func init() { | ||
cmd := &migrateCmd{} | ||
cobraCmd := &cobra.Command{ | ||
Use: "migrate", | ||
Short: "A brief description of your command", | ||
Long: `A longer description that spans multiple lines and likely contains examples | ||
and usage of using your command. For example: | ||
|
||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
Run: cmd.run, | ||
} | ||
rootCmd.AddCommand(cobraCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
cmd.from = cobraCmd.PersistentFlags().String( | ||
"from", | ||
"disk", | ||
"The Athens storage backend to migrate from", | ||
) | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
cmd.to = cobraCmd.PersistentFlags().String( | ||
"to", | ||
"s3", | ||
"The Athens storage backend to migrate to", | ||
) | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// migrateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} |
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,77 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
homedir "github.com/mitchellh/go-homedir" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var cfgFile string | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "migrator", | ||
Short: "A brief description of your application", | ||
Long: `A longer description that spans multiple lines and likely contains | ||
examples and usage of using your application. For example: | ||
|
||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
// Uncomment the following line if your bare application | ||
// has an action associated with it: | ||
// Run: func(cmd *cobra.Command, args []string) { }, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
// Here you will define your flags and configuration settings. | ||
// Cobra supports persistent flags, which, if defined here, | ||
// will be global for your application. | ||
|
||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.migrator.yaml)") | ||
|
||
// Cobra also supports local flags, which will only run | ||
// when this action is called directly. | ||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} | ||
|
||
// initConfig reads in config file and ENV variables if set. | ||
func initConfig() { | ||
if cfgFile != "" { | ||
// Use config file from the flag. | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
// Find home directory. | ||
home, err := homedir.Dir() | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
// Search config in home directory with name ".migrator" (without extension). | ||
viper.AddConfigPath(home) | ||
viper.SetConfigName(".migrator") | ||
} | ||
|
||
viper.AutomaticEnv() // read in environment variables that match | ||
|
||
// If a config file is found, read it in. | ||
if err := viper.ReadInConfig(); err == nil { | ||
fmt.Println("Using config file:", viper.ConfigFileUsed()) | ||
} | ||
} |
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,7 @@ | ||
package main | ||
|
||
import "github.com/gomods/athens/cmd/migrator/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |
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
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.
moving storages is likely a very timely process, we should do a couple of things:
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.
@marwan-at-work for (1), I agree that it should be parallelized . I was waiting to see if the general structure of this looks ok.
If you're ok with my suggestion for a follow-up, I can continue on with parallelizing and doing reads before writes in here. Let me know ✌️