diff --git a/cmds/identityd/main.go b/cmds/identityd/main.go index bd1156868..f2060d424 100644 --- a/cmds/identityd/main.go +++ b/cmds/identityd/main.go @@ -68,6 +68,8 @@ func main() { version.ShowAndExit(false) } + client, err := zbus.NewRedisClient(broker) + if farm { env := environment.MustGet() fmt.Println(env.FarmID) @@ -78,7 +80,6 @@ func main() { os.Exit(0) } else if id || address { ctx := context.Background() - client, err := zbus.NewRedisClient(broker) if err != nil { log.Fatal().Err(err).Msg("failed to connect to zbus") } @@ -107,7 +108,7 @@ func main() { log.Fatal().Err(err).Msg("failed to create identity manager") } - upgrader, err := upgrade.NewUpgrader(root, upgrade.NoZosUpgrade(debug)) + upgrader, err := upgrade.NewUpgrader(root, upgrade.NoZosUpgrade(debug), upgrade.ZbusClient(client)) if err != nil { log.Fatal().Err(err).Msg("failed to initialize upgrader") } diff --git a/pkg/api_gateway.go b/pkg/api_gateway.go index 61bc389d2..714167aeb 100644 --- a/pkg/api_gateway.go +++ b/pkg/api_gateway.go @@ -30,6 +30,7 @@ type SubstrateGateway interface { UpdateNode(node substrate.Node) (uint32, error) UpdateNodeUptimeV2(uptime uint64, timestampHint uint64) (hash types.Hash, err error) GetTime() (time.Time, error) + GetZosVersion() (string, error) } type SubstrateError struct { diff --git a/pkg/stubs/api_gateway_stub.go b/pkg/stubs/api_gateway_stub.go index 60e3230bd..498e2e536 100644 --- a/pkg/stubs/api_gateway_stub.go +++ b/pkg/stubs/api_gateway_stub.go @@ -6,11 +6,12 @@ package stubs import ( "context" + "time" + types "github.com/centrifuge/go-substrate-rpc-client/v4/types" tfchainclientgo "github.com/threefoldtech/tfchain/clients/tfchain-client-go" zbus "github.com/threefoldtech/zbus" pkg "github.com/threefoldtech/zos/pkg" - "time" ) type SubstrateGatewayStub struct { @@ -30,6 +31,25 @@ func NewSubstrateGatewayStub(client zbus.Client) *SubstrateGatewayStub { } } +func (s *SubstrateGatewayStub) GetZosVersion(ctx context.Context) (ret0 string, ret1 error) { + args := []interface{}{} + result, err := s.client.RequestContext(ctx, s.module, s.object, "GetZosVersion", args...) + if err != nil { + panic(err) + } + + result.PanicOnError() + ret1 = result.CallError() + loader := zbus.Loader{ + &ret0, + } + + if err := result.Unmarshal(&loader); err != nil { + panic(err) + } + return +} + func (s *SubstrateGatewayStub) CreateNode(ctx context.Context, arg0 tfchainclientgo.Node) (ret0 uint32, ret1 error) { args := []interface{}{arg0} result, err := s.client.RequestContext(ctx, s.module, s.object, "CreateNode", args...) diff --git a/pkg/substrate_gateway/substrate_gateway.go b/pkg/substrate_gateway/substrate_gateway.go index 855161aef..8af9277d1 100644 --- a/pkg/substrate_gateway/substrate_gateway.go +++ b/pkg/substrate_gateway/substrate_gateway.go @@ -31,6 +31,12 @@ func NewSubstrateGateway(manager substrate.Manager, identity substrate.Identity) return gw, nil } +func (g *substrateGateway) GetZosVersion() (string, error) { + log.Debug().Str("method", "GetZosVersion").Msg("method called") + + return g.sub.GetZosVersion() +} + func (g *substrateGateway) CreateNode(node substrate.Node) (uint32, error) { log.Debug(). Str("method", "CreateNode"). diff --git a/pkg/upgrade/upgrade.go b/pkg/upgrade/upgrade.go index db1a0f912..6a6ed154e 100644 --- a/pkg/upgrade/upgrade.go +++ b/pkg/upgrade/upgrade.go @@ -20,9 +20,10 @@ import ( "github.com/threefoldtech/0-fs/meta" "github.com/threefoldtech/0-fs/rofs" "github.com/threefoldtech/0-fs/storage" - substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" + "github.com/threefoldtech/zbus" "github.com/threefoldtech/zos/pkg/app" "github.com/threefoldtech/zos/pkg/environment" + "github.com/threefoldtech/zos/pkg/stubs" "github.com/threefoldtech/zos/pkg/upgrade/hub" "github.com/threefoldtech/zos/pkg/zinit" @@ -56,20 +57,13 @@ type ChainVersion struct { Version string `json:"version"` } -func getRolloutConfig(env environment.Environment) (ChainVersion, []uint32, error) { +func getRolloutConfig(ctx context.Context, gw *stubs.SubstrateGatewayStub) (ChainVersion, []uint32, error) { config, err := environment.GetConfig() if err != nil { return ChainVersion{}, nil, errors.Wrap(err, "failed to get network config") } - manager := substrate.NewManager(env.SubstrateURL...) - - con, err := manager.Substrate() - if err != nil { - return ChainVersion{}, nil, err - } - - v, err := con.GetZosVersion() + v, err := gw.GetZosVersion(ctx) if err != nil { return ChainVersion{}, nil, errors.Wrap(err, "failed to get zos version from chain") } @@ -92,6 +86,7 @@ func getRolloutConfig(env environment.Environment) (ChainVersion, []uint32, erro type Upgrader struct { boot Boot zinit *zinit.Client + zcl zbus.Client root string noZosUpgrade bool hub *hub.HubClient @@ -112,6 +107,15 @@ func NoZosUpgrade(o bool) UpgraderOption { } } +// ZbusClient option, adds a zbus client to the upgrader +func ZbusClient(cl zbus.Client) UpgraderOption { + return func(u *Upgrader) error { + u.zcl = cl + + return nil + } +} + // Storage option overrides the default hub storage url // default value is hub.grid.tf func Storage(url string) UpgraderOption { @@ -203,7 +207,7 @@ func (u *Upgrader) Run(ctx context.Context) error { // if the booting method is bootstrap then we run update periodically // after u.nextUpdate to make sure all the modules are always up to date for { - err := u.update() + err := u.update(ctx) if errors.Is(err, ErrRestartNeeded) { return err } else if err != nil { @@ -255,7 +259,7 @@ func (u *Upgrader) remote() (remote hub.TagLink, err error) { return hub.NewTagLink(matches[0]), nil } -func (u *Upgrader) update() error { +func (u *Upgrader) update(ctx context.Context) error { // here we need to do a normal full update cycle current, err := u.boot.Current() if err != nil { @@ -275,7 +279,8 @@ func (u *Upgrader) update() error { } env := environment.MustGet() - chainVer, testFarms, err := getRolloutConfig(env) + gw := stubs.NewSubstrateGatewayStub(u.zcl) + chainVer, testFarms, err := getRolloutConfig(ctx, gw) if err != nil { return errors.Wrap(err, "failed to get rollout config and version") }