Skip to content

Commit

Permalink
use chain to get version and safe to upgrade flag
Browse files Browse the repository at this point in the history
  • Loading branch information
rawdaGastan committed Oct 2, 2024
1 parent f5a711d commit 996cbbe
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
3 changes: 1 addition & 2 deletions pkg/environment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ type Config struct {
Authorized []string `json:"authorized"`
} `json:"users"`
RolloutUpgrade struct {
TestFarms []uint32 `json:"test_farms"`
SafeToUpgrade bool `json:"safe_to_upgrade"`
TestFarms []uint32 `json:"test_farms"`
} `json:"rollout_upgrade"`
}

Expand Down
49 changes: 44 additions & 5 deletions pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package upgrade

import (
"context"
"encoding/json"
"fmt"
"io"
"math/rand"
Expand All @@ -19,6 +20,7 @@ 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/zos/pkg/app"
"github.com/threefoldtech/zos/pkg/environment"
"github.com/threefoldtech/zos/pkg/upgrade/hub"
Expand Down Expand Up @@ -49,6 +51,38 @@ const (
ZosPackage = "zos.flist"
)

type ChainVersion struct {
SafeToUpgrade bool `json:"safe_to_upgrade"`
Version string `json:"version"`
}

func getRolloutConfig(env environment.Environment) (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()
if err != nil {
return ChainVersion{}, nil, errors.Wrap(err, "failed to get zos version from chain")
}

var chainVersion ChainVersion
err = json.Unmarshal([]byte(v), &chainVersion)
if err != nil {
return ChainVersion{}, nil, errors.Wrap(err, "failed to unmarshal chain version")
}

return chainVersion, config.RolloutUpgrade.TestFarms, nil
}

// Upgrader is the component that is responsible
// to keep 0-OS up to date
type Upgrader struct {
Expand Down Expand Up @@ -237,15 +271,20 @@ func (u *Upgrader) update() error {
}

env := environment.MustGet()
config, err := environment.GetConfig()
chainVer, testFarms, err := getRolloutConfig(env)
if err != nil {
return errors.Wrap(err, "failed to get network config")
return errors.Wrap(err, "failed to get rollout config and version")
}

rolloutConfigs := config.RolloutUpgrade
remoteVer := remote.Target[strings.LastIndex(remote.Target, "/")+1:]

if remoteVer != chainVer.Version {
// nothing to do! hub version is not the same as the chain
return nil
}

if !rolloutConfigs.SafeToUpgrade {
if !slices.Contains(rolloutConfigs.TestFarms, uint32(env.FarmID)) {
if !chainVer.SafeToUpgrade {
if !slices.Contains(testFarms, uint32(env.FarmID)) {
// nothing to do! waiting for the flag `safe to upgrade to be enabled after A/B testing`
// node is not a part of A/B testing
return nil
Expand Down

0 comments on commit 996cbbe

Please sign in to comment.