Skip to content

Commit

Permalink
fix(config migration): add config migration to update p2p.QriBootstra…
Browse files Browse the repository at this point in the history
…pAddrs

Merge pull request #636 from qri-io/config_migrations
  • Loading branch information
b5 authored Dec 12, 2018
2 parents 9ff0bdf + 3189974 commit 3ea2244
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/qri.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (o *QriOptions) init() (err error) {
// TODO - need to remove global config state in lib, then remove this
lib.ConfigFilepath = cfgPath

if err = lib.LoadConfig(cfgPath); err != nil {
if err = lib.LoadConfig(o.IOStreams, cfgPath); err != nil {
return
}
o.config = lib.Config
Expand Down
10 changes: 9 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ import (
"github.com/qri-io/jsonschema"
)

// CurrentConfigRevision is the latest configuration revision configurations
// that don't match this revision number should be migrated up
const CurrentConfigRevision = 1

// Config encapsulates all configuration details for qri
type Config struct {
Revision int
Profile *ProfilePod
Repo *Repo
Store *Store
Expand All @@ -35,6 +40,7 @@ type Config struct {
// DefaultConfigWithoutKeys gives a new default configuration without any crypto keys
func DefaultConfigWithoutKeys() *Config {
return &Config{
Revision: CurrentConfigRevision,
Profile: DefaultProfileWithoutKeys(),
Repo: DefaultRepo(),
Store: DefaultStore(),
Expand Down Expand Up @@ -287,7 +293,9 @@ func (cfg Config) Validate() error {

// Copy returns a deep copy of the Config struct
func (cfg *Config) Copy() *Config {
res := &Config{}
res := &Config{
Revision: cfg.Revision,
}

if cfg.Profile != nil {
res.Profile = cfg.Profile.Copy()
Expand Down
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestWriteToFileWithExtraData(t *testing.T) {
path := filepath.Join(os.TempDir(), "config.yaml")
t.Log(path)
cfg := Config{
Revision: CurrentConfigRevision,
Profile: &ProfilePod{
ID: "QmU27VdAEUL5NGM6oB56htTxvHLfcGZgsgxrJTdVr2k4zs",
Peername: "test_peername",
Expand Down
52 changes: 52 additions & 0 deletions config/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Package migrate defines migrations for qri configuration files
package migrate

import (
"github.com/qri-io/ioes"
"github.com/qri-io/qri/config"
)

// RunMigrations checks to see if any migrations runs them
func RunMigrations(streams ioes.IOStreams, cfg *config.Config) (migrated bool, err error) {
if cfg.Revision != config.CurrentConfigRevision {
streams.Print("migrating configuration...")
if err := ZeroToOne(cfg); err != nil {
return false, err
}
streams.Print("done!")
return true, nil
}
return false, nil
}

// ZeroToOne migrates a configuration from Revision Zero (no revision number) to Revision 1
func ZeroToOne(cfg *config.Config) error {
if cfg.P2P != nil {
removes := map[string]bool{
"/ip4/130.211.198.23/tcp/4001/ipfs/QmNX9nSos8sRFvqGTwdEme6LQ8R1eJ8EuFgW32F9jjp2Pb": true, // mojo
"/ip4/35.193.162.149/tcp/4001/ipfs/QmTZxETL4YCCzB1yFx4GT1te68henVHD1XPQMkHZ1N22mm": true, // epa
"/ip4/35.226.92.45/tcp/4001/ipfs/QmP6sbnHXANXgQ7JeCCeCKdJrgpvUd8s75YNfzdkHf6Mpi": true, // 538
"/ip4/35.192.140.245/tcp/4001/ipfs/QmUUVNiTz2K9zQSH9PxerKWXmN1p3DBo3oJXurvYziFzqh": true, // EDGI
}

adds := config.NewP2P().QriBootstrapAddrs

for i, addr := range cfg.P2P.QriBootstrapAddrs {
// remove any old, invalid addresses
if removes[addr] {
cfg.P2P.QriBootstrapAddrs = append(cfg.P2P.QriBootstrapAddrs[:i], cfg.P2P.QriBootstrapAddrs[i+1:]...)
}
// remove address from list of additions if already configured
for j, add := range adds {
if addr == add {
adds = append(adds[:j], adds[j+1:]...)
}
}
}

cfg.P2P.QriBootstrapAddrs = append(cfg.P2P.QriBootstrapAddrs, adds...)
}

cfg.Revision = 1
return nil
}
11 changes: 7 additions & 4 deletions config/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ func NewP2P() *P2P {
// One day it would be super nice to bootstrap from a stored history & only
// use these for first-round bootstrapping.
QriBootstrapAddrs: []string{
"/ip4/130.211.198.23/tcp/4001/ipfs/QmNX9nSos8sRFvqGTwdEme6LQ8R1eJ8EuFgW32F9jjp2Pb", // mojo
"/ip4/35.193.162.149/tcp/4001/ipfs/QmTZxETL4YCCzB1yFx4GT1te68henVHD1XPQMkHZ1N22mm", // epa
"/ip4/35.226.92.45/tcp/4001/ipfs/QmP6sbnHXANXgQ7JeCCeCKdJrgpvUd8s75YNfzdkHf6Mpi", // 538
"/ip4/35.192.140.245/tcp/4001/ipfs/QmUUVNiTz2K9zQSH9PxerKWXmN1p3DBo3oJXurvYziFzqh", // EDGI
"/ip4/35.239.80.82/tcp/4001/ipfs/QmdpGkbqDYRPCcwLYnEm8oYGz2G9aUZn9WwPjqvqw3XUAc", // red
"/ip4/35.225.152.38/tcp/4001/ipfs/QmTRqTLbKndFC2rp6VzpyApxHCLrFV35setF1DQZaRWPVf", // orange
"/ip4/35.202.155.225/tcp/4001/ipfs/QmegNYmwHUQFc3v3eemsYUVf3WiSg4RcMrh3hovA5LncJ2", // yellow
"/ip4/35.238.10.180/tcp/4001/ipfs/QmessbA6uGLJ7HTwbUJ2niE49WbdPfzi27tdYXdAaGRB4G", // green
"/ip4/35.238.105.35/tcp/4001/ipfs/Qmc353gHY5Wx5iHKHPYj3QDqHP4hVA1MpoSsT6hwSyVx3r", // blue
"/ip4/35.239.138.186/tcp/4001/ipfs/QmT9YHJF2YkysLqWhhiVTL5526VFtavic3bVueF9rCsjVi", // indigo
"/ip4/35.226.44.58/tcp/4001/ipfs/QmQS2ryqZrjJtPKDy9VTkdPwdUSpTi1TdpGUaqAVwfxcNh", // violet
},
ProfileReplication: "full",
}
Expand Down
1 change: 1 addition & 0 deletions config/testdata/simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ RPC: null
Registry: null
Render: null
Repo: null
Revision: 1
Store: null
Webapp: null
15 changes: 13 additions & 2 deletions lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (

"github.com/ghodss/yaml"
golog "github.com/ipfs/go-log"
"github.com/qri-io/ioes"
"github.com/qri-io/qri/config"
"github.com/qri-io/qri/config/migrate"
)

var (
Expand All @@ -25,7 +27,7 @@ var SaveConfig = func() error {
}

// LoadConfig loads the global default configuration
func LoadConfig(path string) (err error) {
func LoadConfig(streams ioes.IOStreams, path string) (err error) {
var cfg *config.Config
cfg, err = config.ReadFromFile(path)

Expand All @@ -48,7 +50,16 @@ func LoadConfig(path string) (err error) {

Config = cfg

return err
migrated, err := migrate.RunMigrations(streams, cfg)
if err != nil {
return err
}

if migrated {
return SaveConfig()
}

return nil
}

// GetConfigParams are the params needed to format/specify the fields in bytes returned from the GetConfig function
Expand Down
3 changes: 2 additions & 1 deletion lib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"testing"

"github.com/qri-io/ioes"
"github.com/qri-io/qri/config"
)

Expand All @@ -25,7 +26,7 @@ func TestLoadConfig(t *testing.T) {
if err := config.DefaultConfigForTesting().WriteToFile(cfgPath); err != nil {
t.Fatal(err.Error())
}
if err := LoadConfig(cfgPath); err != nil {
if err := LoadConfig(ioes.NewDiscardIOStreams(), cfgPath); err != nil {
t.Error(err.Error())
}
}
Expand Down

0 comments on commit 3ea2244

Please sign in to comment.