Skip to content

Commit 895849a

Browse files
authored
Merge pull request #2493 from threefoldtech/development_use_api_gateway_in_upgrade
use api gateway instead of chain client in getting version in zos upgrade
2 parents 41fe8f7 + b97473f commit 895849a

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

cmds/identityd/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ func main() {
6868
version.ShowAndExit(false)
6969
}
7070

71+
client, err := zbus.NewRedisClient(broker)
72+
7173
if farm {
7274
env := environment.MustGet()
7375
fmt.Println(env.FarmID)
@@ -78,7 +80,6 @@ func main() {
7880
os.Exit(0)
7981
} else if id || address {
8082
ctx := context.Background()
81-
client, err := zbus.NewRedisClient(broker)
8283
if err != nil {
8384
log.Fatal().Err(err).Msg("failed to connect to zbus")
8485
}
@@ -107,7 +108,7 @@ func main() {
107108
log.Fatal().Err(err).Msg("failed to create identity manager")
108109
}
109110

110-
upgrader, err := upgrade.NewUpgrader(root, upgrade.NoZosUpgrade(debug))
111+
upgrader, err := upgrade.NewUpgrader(root, upgrade.NoZosUpgrade(debug), upgrade.ZbusClient(client))
111112
if err != nil {
112113
log.Fatal().Err(err).Msg("failed to initialize upgrader")
113114
}

pkg/api_gateway.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type SubstrateGateway interface {
3030
UpdateNode(node substrate.Node) (uint32, error)
3131
UpdateNodeUptimeV2(uptime uint64, timestampHint uint64) (hash types.Hash, err error)
3232
GetTime() (time.Time, error)
33+
GetZosVersion() (string, error)
3334
}
3435

3536
type SubstrateError struct {

pkg/stubs/api_gateway_stub.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ package stubs
66

77
import (
88
"context"
9+
"time"
10+
911
types "github.com/centrifuge/go-substrate-rpc-client/v4/types"
1012
tfchainclientgo "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
1113
zbus "github.com/threefoldtech/zbus"
1214
pkg "github.com/threefoldtech/zos/pkg"
13-
"time"
1415
)
1516

1617
type SubstrateGatewayStub struct {
@@ -30,6 +31,25 @@ func NewSubstrateGatewayStub(client zbus.Client) *SubstrateGatewayStub {
3031
}
3132
}
3233

34+
func (s *SubstrateGatewayStub) GetZosVersion(ctx context.Context) (ret0 string, ret1 error) {
35+
args := []interface{}{}
36+
result, err := s.client.RequestContext(ctx, s.module, s.object, "GetZosVersion", args...)
37+
if err != nil {
38+
panic(err)
39+
}
40+
41+
result.PanicOnError()
42+
ret1 = result.CallError()
43+
loader := zbus.Loader{
44+
&ret0,
45+
}
46+
47+
if err := result.Unmarshal(&loader); err != nil {
48+
panic(err)
49+
}
50+
return
51+
}
52+
3353
func (s *SubstrateGatewayStub) CreateNode(ctx context.Context, arg0 tfchainclientgo.Node) (ret0 uint32, ret1 error) {
3454
args := []interface{}{arg0}
3555
result, err := s.client.RequestContext(ctx, s.module, s.object, "CreateNode", args...)

pkg/substrate_gateway/substrate_gateway.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ func NewSubstrateGateway(manager substrate.Manager, identity substrate.Identity)
3131
return gw, nil
3232
}
3333

34+
func (g *substrateGateway) GetZosVersion() (string, error) {
35+
log.Debug().Str("method", "GetZosVersion").Msg("method called")
36+
37+
return g.sub.GetZosVersion()
38+
}
39+
3440
func (g *substrateGateway) CreateNode(node substrate.Node) (uint32, error) {
3541
log.Debug().
3642
Str("method", "CreateNode").

pkg/upgrade/upgrade.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import (
2020
"github.com/threefoldtech/0-fs/meta"
2121
"github.com/threefoldtech/0-fs/rofs"
2222
"github.com/threefoldtech/0-fs/storage"
23-
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
23+
"github.com/threefoldtech/zbus"
2424
"github.com/threefoldtech/zos/pkg/app"
2525
"github.com/threefoldtech/zos/pkg/environment"
26+
"github.com/threefoldtech/zos/pkg/stubs"
2627
"github.com/threefoldtech/zos/pkg/upgrade/hub"
2728
"github.com/threefoldtech/zos/pkg/zinit"
2829

@@ -56,20 +57,13 @@ type ChainVersion struct {
5657
Version string `json:"version"`
5758
}
5859

59-
func getRolloutConfig(env environment.Environment) (ChainVersion, []uint32, error) {
60+
func getRolloutConfig(ctx context.Context, gw *stubs.SubstrateGatewayStub) (ChainVersion, []uint32, error) {
6061
config, err := environment.GetConfig()
6162
if err != nil {
6263
return ChainVersion{}, nil, errors.Wrap(err, "failed to get network config")
6364
}
6465

65-
manager := substrate.NewManager(env.SubstrateURL...)
66-
67-
con, err := manager.Substrate()
68-
if err != nil {
69-
return ChainVersion{}, nil, err
70-
}
71-
72-
v, err := con.GetZosVersion()
66+
v, err := gw.GetZosVersion(ctx)
7367
if err != nil {
7468
return ChainVersion{}, nil, errors.Wrap(err, "failed to get zos version from chain")
7569
}
@@ -92,6 +86,7 @@ func getRolloutConfig(env environment.Environment) (ChainVersion, []uint32, erro
9286
type Upgrader struct {
9387
boot Boot
9488
zinit *zinit.Client
89+
zcl zbus.Client
9590
root string
9691
noZosUpgrade bool
9792
hub *hub.HubClient
@@ -112,6 +107,15 @@ func NoZosUpgrade(o bool) UpgraderOption {
112107
}
113108
}
114109

110+
// ZbusClient option, adds a zbus client to the upgrader
111+
func ZbusClient(cl zbus.Client) UpgraderOption {
112+
return func(u *Upgrader) error {
113+
u.zcl = cl
114+
115+
return nil
116+
}
117+
}
118+
115119
// Storage option overrides the default hub storage url
116120
// default value is hub.grid.tf
117121
func Storage(url string) UpgraderOption {
@@ -203,7 +207,7 @@ func (u *Upgrader) Run(ctx context.Context) error {
203207
// if the booting method is bootstrap then we run update periodically
204208
// after u.nextUpdate to make sure all the modules are always up to date
205209
for {
206-
err := u.update()
210+
err := u.update(ctx)
207211
if errors.Is(err, ErrRestartNeeded) {
208212
return err
209213
} else if err != nil {
@@ -255,7 +259,7 @@ func (u *Upgrader) remote() (remote hub.TagLink, err error) {
255259
return hub.NewTagLink(matches[0]), nil
256260
}
257261

258-
func (u *Upgrader) update() error {
262+
func (u *Upgrader) update(ctx context.Context) error {
259263
// here we need to do a normal full update cycle
260264
current, err := u.boot.Current()
261265
if err != nil {
@@ -275,7 +279,8 @@ func (u *Upgrader) update() error {
275279
}
276280

277281
env := environment.MustGet()
278-
chainVer, testFarms, err := getRolloutConfig(env)
282+
gw := stubs.NewSubstrateGatewayStub(u.zcl)
283+
chainVer, testFarms, err := getRolloutConfig(ctx, gw)
279284
if err != nil {
280285
return errors.Wrap(err, "failed to get rollout config and version")
281286
}

0 commit comments

Comments
 (0)