diff --git a/pkg/environment/config.go b/pkg/environment/config.go index 7c51d999c..98d60e6e0 100644 --- a/pkg/environment/config.go +++ b/pkg/environment/config.go @@ -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"` } diff --git a/pkg/upgrade/upgrade.go b/pkg/upgrade/upgrade.go index 8444b2cc3..67f812a12 100644 --- a/pkg/upgrade/upgrade.go +++ b/pkg/upgrade/upgrade.go @@ -2,6 +2,7 @@ package upgrade import ( "context" + "encoding/json" "fmt" "io" "math/rand" @@ -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" @@ -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 { @@ -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