Skip to content

Commit 55e8310

Browse files
committed
add rollout configs for version updates
1 parent 9213b66 commit 55e8310

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

pkg/environment/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ type Config struct {
2727
Users struct {
2828
Authorized []string `json:"authorized"`
2929
} `json:"users"`
30+
RolloutUpgrade struct {
31+
TestFarms []uint32 `json:"test_farms"`
32+
} `json:"rollout_upgrade"`
3033
}
3134

3235
// Merge, updates current config with cfg merging and override config

pkg/upgrade/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ Then run the upgrader `upgrader.Run(ctx)`
2727

2828
## How it works
2929

30-
The upgrader module has two running modes depeding on the booting method.
30+
The upgrader module has two running modes depending on the booting method.
3131

3232
### Bootstrap Method
3333

3434
Running the upgrader on a node run with `bootstrap` will periodically check the hub for latest tag,
3535
and if that tag differs from the current one, it updates the local packages to latest.
3636

37-
If the update failed, the upgrader would attempts to install the packages again every `10 secounds` until all packages are successfully updated to prevent partial updates.
37+
If the update failed, the upgrader would attempts to install the packages again every `10 seconds` until all packages are successfully updated to prevent partial updates.
3838

3939
The upgrader runs periodically every hour to check for new updates.
4040

pkg/upgrade/upgrade.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package upgrade
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"io"
78
"math/rand"
@@ -19,7 +20,9 @@ import (
1920
"github.com/threefoldtech/0-fs/meta"
2021
"github.com/threefoldtech/0-fs/rofs"
2122
"github.com/threefoldtech/0-fs/storage"
23+
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
2224
"github.com/threefoldtech/zos/pkg/app"
25+
"github.com/threefoldtech/zos/pkg/environment"
2326
"github.com/threefoldtech/zos/pkg/upgrade/hub"
2427
"github.com/threefoldtech/zos/pkg/zinit"
2528

@@ -48,6 +51,42 @@ const (
4851
ZosPackage = "zos.flist"
4952
)
5053

54+
type ChainVersion struct {
55+
SafeToUpgrade bool `json:"safe_to_upgrade"`
56+
Version string `json:"version"`
57+
}
58+
59+
func getRolloutConfig(env environment.Environment) (ChainVersion, []uint32, error) {
60+
config, err := environment.GetConfig()
61+
if err != nil {
62+
return ChainVersion{}, nil, errors.Wrap(err, "failed to get network config")
63+
}
64+
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()
73+
if err != nil {
74+
return ChainVersion{}, nil, errors.Wrap(err, "failed to get zos version from chain")
75+
}
76+
77+
var chainVersion ChainVersion
78+
err = json.Unmarshal([]byte(v), &chainVersion)
79+
if err != nil {
80+
log.Debug().Err(err).Msg("failed to unmarshal chain version and safe to upgrade flag")
81+
chainVersion = ChainVersion{
82+
SafeToUpgrade: true,
83+
Version: v,
84+
}
85+
}
86+
87+
return chainVersion, config.RolloutUpgrade.TestFarms, nil
88+
}
89+
5190
// Upgrader is the component that is responsible
5291
// to keep 0-OS up to date
5392
type Upgrader struct {
@@ -131,7 +170,7 @@ func NewUpgrader(root string, opts ...UpgraderOption) (*Upgrader, error) {
131170
return u, nil
132171
}
133172

134-
// Run strats the upgrader module and run the update according to the detected boot method
173+
// Run starts the upgrader module and run the update according to the detected boot method
135174
func (u *Upgrader) Run(ctx context.Context) error {
136175
method := u.boot.DetectBootMethod()
137176
if method == BootMethodOther {
@@ -236,6 +275,27 @@ func (u *Upgrader) update() error {
236275
return nil
237276
}
238277

278+
env := environment.MustGet()
279+
chainVer, testFarms, err := getRolloutConfig(env)
280+
if err != nil {
281+
return errors.Wrap(err, "failed to get rollout config and version")
282+
}
283+
284+
remoteVer := remote.Target[strings.LastIndex(remote.Target, "/")+1:]
285+
286+
if remoteVer != chainVer.Version {
287+
// nothing to do! hub version is not the same as the chain
288+
return nil
289+
}
290+
291+
if !chainVer.SafeToUpgrade {
292+
if !slices.Contains(testFarms, uint32(env.FarmID)) {
293+
// nothing to do! waiting for the flag `safe to upgrade to be enabled after A/B testing`
294+
// node is not a part of A/B testing
295+
return nil
296+
}
297+
}
298+
239299
log.Info().Str("version", filepath.Base(remote.Target)).Msg("updating system...")
240300
if err := u.updateTo(remote, &current); err != nil {
241301
return errors.Wrapf(err, "failed to update to new tag '%s'", remote.Target)

0 commit comments

Comments
 (0)