Skip to content

Commit

Permalink
Handle migration from Protocols to Services in config (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
samyfodil authored Jun 4, 2024
1 parent ea7c747 commit baff6bd
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/app/config_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func configCommand() *cli.Command {
},
&cli.StringFlag{
Name: "services",
Aliases: []string{"serv"},
Aliases: []string{"serv", "protos", "protocols", "proto"}, // TODO: "protos", "protocols", "proto" to be removed after two releases
Usage: "Services to enable. Use `all` to enable them all.",
},
&cli.StringFlag{
Expand Down
2 changes: 1 addition & 1 deletion cli/app/export_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/taubyte/tau/config"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

func exportConfig(ctx *cli.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion cli/app/export_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/taubyte/tau/config"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
"gotest.tools/v3/assert"
)

Expand Down
34 changes: 34 additions & 0 deletions cli/app/migrate_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package app

import (
"fmt"

"github.com/taubyte/go-seer"
)

// MIGRATION from Protocols to Services
func configMigration(fs seer.Option, shape string) error {
migrate, err := seer.New(fs)
if err != nil {
return fmt.Errorf("reading config folder failed with %w", err)
}

var val []string
err = migrate.Get(shape).Get("protocols").Value(&val)
if err == nil {
err = migrate.Get(shape).Get("protocols").Delete().Commit()
if err != nil {
return fmt.Errorf("migration failed deleting `protocols` with %w", err)
}

fmt.Println(val)
if len(val) != 0 {
err = migrate.Get(shape).Get("services").Set(val).Commit()
if err != nil {
return fmt.Errorf("updating `services` failed with %w", err)
}
}
}

return migrate.Sync()
}
165 changes: 165 additions & 0 deletions cli/app/migrate_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package app

import (
"io"
"testing"

"github.com/spf13/afero"
"github.com/taubyte/go-seer"
"github.com/taubyte/tau/config"
"gopkg.in/yaml.v3"
"gotest.tools/v3/assert"
)

var migrateTestConfig = `
privatekey: fakeKey
swarmkey: keys/swarm.key
protocols:
- auth
- patrick
- monkey
- tns
- hoarder
- substrate
- seer
p2p-listen:
- /ip4/0.0.0.0/tcp/4242
p2p-announce:
- /ip4/1.2.3.4/tcp/4242
ports:
main: 4242
lite: 4247
ipfs: 4252
location:
lat: 40.076897
long: -109.33771
network-fqdn: enterprise.starships.ws
domains:
key:
private: keys/dv_private.pem
public: keys/dv_public.pem
generated: e.ftll.ink
plugins: {}
`

var migrateTestConfigNoProtos = `
privatekey: fakeKey
swarmkey: keys/swarm.key
p2p-listen:
- /ip4/0.0.0.0/tcp/4242
p2p-announce:
- /ip4/1.2.3.4/tcp/4242
ports:
main: 4242
lite: 4247
ipfs: 4252
location:
lat: 40.076897
long: -109.33771
network-fqdn: enterprise.starships.ws
domains:
key:
private: keys/dv_private.pem
public: keys/dv_public.pem
generated: e.ftll.ink
plugins: {}
`

var migrateTestConfigJustServices = `
privatekey: fakeKey
swarmkey: keys/swarm.key
services:
- auth
- patrick
- monkey
- tns
- hoarder
- substrate
- seer
p2p-listen:
- /ip4/0.0.0.0/tcp/4242
p2p-announce:
- /ip4/1.2.3.4/tcp/4242
ports:
main: 4242
lite: 4247
ipfs: 4252
location:
lat: 40.076897
long: -109.33771
network-fqdn: enterprise.starships.ws
domains:
key:
private: keys/dv_private.pem
public: keys/dv_public.pem
generated: e.ftll.ink
plugins: {}
`

func prepMigrationFS(t *testing.T, data string) (afero.Fs, *config.Source) {
fs := afero.NewMemMapFs()

fs.Mkdir("/config", 0540)

cnf, err := fs.Create("/config/shape.yaml")
assert.NilError(t, err)
defer cnf.Close()

_, err = io.WriteString(cnf, data)
assert.NilError(t, err)

cnf.Seek(0, 0)

org := &config.Source{}
err = yaml.NewDecoder(cnf).Decode(&org)
assert.NilError(t, err)

return fs, org
}

func TestConfigMigration(t *testing.T) {
fs, org := prepMigrationFS(t, migrateTestConfig)

err := configMigration(seer.VirtualFS(fs, "/config"), "shape")
assert.NilError(t, err)

cnf, err := fs.Open("/config/shape.yaml")
assert.NilError(t, err)
defer cnf.Close()

src := &config.Source{}
err = yaml.NewDecoder(cnf).Decode(&src)
assert.NilError(t, err)

// make sure protos are moved to services
assert.Equal(t, len(src.Services), 7)

org.Services = src.Services

// make sure eveything else stayed the same
assert.DeepEqual(t, org, src)
}

func TestConfigMigrationNoProtos(t *testing.T) {
fs, _ := prepMigrationFS(t, migrateTestConfigNoProtos)
err := configMigration(seer.VirtualFS(fs, "/config"), "shape")
assert.NilError(t, err)
}

func TestConfigMigrationJustServices(t *testing.T) {
fs, org := prepMigrationFS(t, migrateTestConfigJustServices)

err := configMigration(seer.VirtualFS(fs, "/config"), "shape")
assert.NilError(t, err)

cnf, err := fs.Open("/config/shape.yaml")
assert.NilError(t, err)
defer cnf.Close()

src := &config.Source{}
err = yaml.NewDecoder(cnf).Decode(&src)
assert.NilError(t, err)

// make sure eveything else stayed the same
assert.DeepEqual(t, org, src)
}
9 changes: 8 additions & 1 deletion cli/app/parse_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import (
"github.com/taubyte/tau/config"
"github.com/urfave/cli/v2"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"

"github.com/taubyte/go-seer"
)

// TODO: move to config as a methods
Expand All @@ -39,6 +41,11 @@ func parseSourceConfig(ctx *cli.Context, shape string) (string, *config.Node, *c
configPath = path.Join(configRoot, shape+".yaml")
}

err := configMigration(seer.SystemFS(configRoot), shape)
if err != nil {
return "", nil, nil, fmt.Errorf("migration of `%s` failed with: %w", configPath, err)
}

data, err := os.ReadFile(configPath)
if err != nil {
return "", nil, nil, fmt.Errorf("reading config file path `%s` failed with: %w", configPath, err)
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ require (
golang.org/x/sys v0.17.0
golang.org/x/term v0.17.0
gopkg.in/go-playground/webhooks.v5 v5.17.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
gotest.tools v2.2.0+incompatible
gotest.tools/v3 v3.5.1
Expand Down
Binary file removed pkg/vm-orbit/tests/e2e/go/fixtures/basic.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit baff6bd

Please sign in to comment.