Skip to content

Commit

Permalink
use api gateway instead of chain client in getting version in zos upg…
Browse files Browse the repository at this point in the history
…rade
  • Loading branch information
rawdaGastan committed Nov 17, 2024
1 parent 41fe8f7 commit b97473f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
5 changes: 3 additions & 2 deletions cmds/identityd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func main() {
version.ShowAndExit(false)
}

client, err := zbus.NewRedisClient(broker)

if farm {
env := environment.MustGet()
fmt.Println(env.FarmID)
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
1 change: 1 addition & 0 deletions pkg/api_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 21 additions & 1 deletion pkg/stubs/api_gateway_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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...)
Expand Down
6 changes: 6 additions & 0 deletions pkg/substrate_gateway/substrate_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down
31 changes: 18 additions & 13 deletions pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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")
}
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
}
Expand Down

0 comments on commit b97473f

Please sign in to comment.