diff --git a/pkg/environment/config.go b/pkg/environment/config.go index f0eadd42..f72a4255 100644 --- a/pkg/environment/config.go +++ b/pkg/environment/config.go @@ -30,15 +30,23 @@ type Config struct { RolloutUpgrade struct { TestFarms []uint32 `json:"test_farms"` } `json:"rollout_upgrade"` - RelaysURLs []string `json:"relays_urls"` SubstrateURL []string `json:"substrate_urls"` + RelaysURLs []string `json:"relays_urls"` ActivationURL []string `json:"activation_urls"` GraphQL []string `json:"graphql_urls"` KycURL string `json:"kyc_url"` RegistrarURL string `json:"registrar_url"` - // FlistURL []string `json:"flist_url"` - // HubURL []string `json:"hub_url"` - BinRepo string `json:"bin_repo"` + BinRepo string `json:"bin_repo"` + GeoipURLs []string `json:"geoip_urls"` + + HubURL []string `json:"hub_url"` + V4HubURL []string `json:"v4_hub_url"` + + // we should not be supporting flist url or hub storage from zos-config until we can update them on runtime + // FlistURL string `json:"flist_url"` + // V4FlistURL string `json:"v4_flist_url"` + // HubStorage string `json:"hub_storage"` + // V4HubStorage string `json:"v4_hub_storage"` } // Merge, updates current config with cfg merging and override config diff --git a/pkg/environment/environment.go b/pkg/environment/environment.go index 228af3f1..fec6a9d7 100644 --- a/pkg/environment/environment.go +++ b/pkg/environment/environment.go @@ -17,12 +17,24 @@ import ( const ( baseExtendedURL = "https://raw.githubusercontent.com/threefoldtech/zos-config/main/" - hubURL = "https://hub.threefold.me" - v4HubURL = "https://v4.hub.threefold.me" - flistURL = "redis://hub.threefold.me:9900" - v4FlistURL = "redis://v4.hub.threefold.me:9940" + + defaultHubURL = "https://hub.threefold.me" + defaultV4HubURL = "https://v4.hub.threefold.me" + + defaultFlistURL = "redis://hub.threefold.me:9900" + defaultV4FlistURL = "redis://v4.hub.threefold.me:9940" + + defaultHubStorage = "zdb://hub.threefold.me:9900" + defaultV4HubStorage = "zdb://v4.hub.threefold.me:9940" ) +var defaultGeoipURLs = []string{ + "https://geoip.threefold.me/", + "https://geoip.grid.tf/", + "https://02.geoip.grid.tf/", + "https://03.geoip.grid.tf/", +} + // PubMac specify how the mac address of the public nic // (in case of public-config) is calculated type PubMac string @@ -42,8 +54,12 @@ const ( type Environment struct { RunningMode RunMode - FlistURL string - BinRepo string + FlistURL string + HubStorage string + BinRepo string + + HubURL string + V4HubURL string FarmID pkg.FarmID Orphan bool @@ -56,9 +72,9 @@ type Environment struct { relaysURLs []string ActivationURL []string GraphQL []string + GeoipURLs []string KycURL string RegistrarURL string - HubURL string // private vlan to join // if set, zos will use this as its priv vlan @@ -126,15 +142,18 @@ var ( "https://activation.dev.grid.tf/activation/activate", "https://activation.02.dev.grid.tf/activation/activate", }, - FlistURL: flistURL, - HubURL: hubURL, - BinRepo: "tf-zos-v3-bins.dev", + HubURL: defaultHubURL, + V4HubURL: defaultV4HubURL, + FlistURL: defaultFlistURL, + HubStorage: defaultHubStorage, + BinRepo: "tf-zos-v3-bins.dev", GraphQL: []string{ "https://graphql.dev.grid.tf/graphql", "https://graphql.02.dev.grid.tf/graphql", }, KycURL: "https://kyc.dev.grid.tf", RegistrarURL: "http://registrar.dev4.grid.tf", + GeoipURLs: defaultGeoipURLs, } envTest = Environment{ @@ -150,15 +169,18 @@ var ( "https://activation.test.grid.tf/activation/activate", "https://activation.02.test.grid.tf/activation/activate", }, - FlistURL: flistURL, - HubURL: hubURL, - BinRepo: "tf-zos-v3-bins.test", + HubURL: defaultHubURL, + V4HubURL: defaultV4HubURL, + FlistURL: defaultFlistURL, + HubStorage: defaultHubStorage, + BinRepo: "tf-zos-v3-bins.test", GraphQL: []string{ "https://graphql.test.grid.tf/graphql", "https://graphql.02.test.grid.tf/graphql", }, KycURL: "https://kyc.test.grid.tf", RegistrarURL: "http://registrar.test4.grid.tf", + GeoipURLs: defaultGeoipURLs, } envQA = Environment{ @@ -174,15 +196,18 @@ var ( "https://activation.qa.grid.tf/activation/activate", "https://activation.02.qa.grid.tf/activation/activate", }, - FlistURL: flistURL, - HubURL: hubURL, - BinRepo: "tf-zos-v3-bins.qanet", + HubURL: defaultHubURL, + V4HubURL: defaultV4HubURL, + FlistURL: defaultFlistURL, + HubStorage: defaultHubStorage, + BinRepo: "tf-zos-v3-bins.qanet", GraphQL: []string{ "https://graphql.qa.grid.tf/graphql", "https://graphql.02.qa.grid.tf/graphql", }, KycURL: "https://kyc.qa.grid.tf", RegistrarURL: "https://registrar.qa4.grid.tf", + GeoipURLs: defaultGeoipURLs, } envProd = Environment{ @@ -201,17 +226,22 @@ var ( "https://activation.grid.threefold.me/activation/activate", "https://activation.grid.tf/activation/activate", "https://activation.02.grid.tf/activation/activate", + "https://activation.grid.threefold.me/activation/activate", }, - HubURL: hubURL, - FlistURL: flistURL, - BinRepo: "tf-zos-v3-bins", + HubURL: defaultHubURL, + V4HubURL: defaultV4HubURL, + FlistURL: defaultFlistURL, + HubStorage: defaultHubStorage, + BinRepo: "tf-zos-v3-bins", GraphQL: []string{ "https://graphql.grid.threefold.me/graphql", "https://graphql.grid.tf/graphql", "https://graphql.02.grid.tf/graphql", + "https://graphql.grid.threefold.me/graphql", }, KycURL: "https://kyc.threefold.me", RegistrarURL: "https://registrar.prod4.threefold.me", + GeoipURLs: defaultGeoipURLs, } ) @@ -295,29 +325,24 @@ func getEnvironmentFromParams(params kernel.Params) (Environment, error) { if err != nil { // maybe the node can't reach the internet right now // this will enforce node to skip config + // or we can keep retrying untill it can fetch config config = Config{} } - if substrate, ok := params.Get("substrate"); ok { - if len(substrate) > 0 { - env.SubstrateURL = substrate - } + if substrate, ok := params.Get("substrate"); ok && len(substrate) > 0 { + env.SubstrateURL = substrate } else if substrate := config.SubstrateURL; len(substrate) > 0 { env.SubstrateURL = substrate } - if relay, ok := params.Get("relay"); ok { - if len(relay) > 0 { - env.relaysURLs = relay - } + if relay, ok := params.Get("relay"); ok && len(relay) > 0 { + env.relaysURLs = relay } else if relay := config.RelaysURLs; len(relay) > 0 { env.relaysURLs = relay } - if activation, ok := params.Get("activation"); ok { - if len(activation) > 0 { - env.ActivationURL = activation - } + if activation, ok := params.Get("activation"); ok && len(activation) > 0 { + env.ActivationURL = activation } else if activation := config.ActivationURL; len(activation) > 0 { env.ActivationURL = activation } @@ -326,14 +351,6 @@ func getEnvironmentFromParams(params kernel.Params) (Environment, error) { env.GraphQL = graphql } - // if flist := config.FlistURL; len(flist) > 0 { - // env.FlistURL = flist[0] - // } - - // if hub := config.HubURL; len(hub) > 0 { - // env.HubURL = hub[0] - // } - if bin := config.BinRepo; len(bin) > 0 { env.BinRepo = bin } @@ -346,6 +363,43 @@ func getEnvironmentFromParams(params kernel.Params) (Environment, error) { env.RegistrarURL = registrar } + if geoip := config.GeoipURLs; len(geoip) > 0 { + env.GeoipURLs = geoip + } + + // flist url and hub storage urls shouldn't listen to changes in config as long as we can't change it at run time. + // it would cause breakage in vmd that needs a reboot to be recovered. + // if flist := config.FlistURL; len(flist) > 0 { + // env.FlistURL = flist[0] + // } + // + // if storage := config.HubStorage; len(storage) > 0 { + // env.HubStorage = storage + // } + + // maybe we should verify that we're using a working hub url + if hub := config.HubURL; len(hub) > 0 { + env.HubURL = hub[0] + } + + // some modules needs v3 hub url even if the node is of v4 + if hub := config.V4HubURL; len(hub) > 0 { + env.V4HubURL = hub[0] + } + + // if the node running v4 chage urls to use v4 hub + if params.IsV4() { + env.FlistURL = defaultV4FlistURL + // if flist := config.V4FlistURL; len(flist) > 0 { + // env.FlistURL = flist + // } + + env.HubStorage = defaultV4HubStorage + // if storage := config.V4HubStorage; len(storage) > 0 { + // env.HubStorage = storage + // } + } + if farmSecret, ok := params.Get("secret"); ok { if len(farmSecret) > 0 { env.FarmSecret = farmSecret[len(farmSecret)-1] @@ -423,11 +477,5 @@ func getEnvironmentFromParams(params kernel.Params) (Environment, error) { env.BinRepo = e } - // if the node running v4 chage flisturl to use v4.hub.threefold.me - if params.IsV4() { - env.FlistURL = v4FlistURL - env.HubURL = v4HubURL - } - return env, nil } diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go index 72ca179d..ed668023 100644 --- a/pkg/geoip/geoip.go +++ b/pkg/geoip/geoip.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/go-retryablehttp" "github.com/rs/zerolog/log" + "github.com/threefoldtech/zosbase/pkg/environment" ) // Location holds the result of a geoip request @@ -19,15 +20,12 @@ type Location struct { City string `json:"city_name"` } -var ( - geoipURLs = []string{"https://geoip.threefold.me/", "https://geoip.grid.tf/", "https://02.geoip.grid.tf/", "https://03.geoip.grid.tf/"} - - defaultHTTPClient = retryablehttp.NewClient() -) +var defaultHTTPClient = retryablehttp.NewClient() // Fetch retrieves the location of the system calling this function func Fetch() (Location, error) { - for _, url := range geoipURLs { + env := environment.MustGet() + for _, url := range env.GeoipURLs { l, err := getLocation(url) if err != nil { log.Err(err).Str("url", url).Msg("failed to fetch location from geoip service") diff --git a/pkg/geoip/geoip_test.go b/pkg/geoip/geoip_test.go index 351f041d..eef9a91e 100644 --- a/pkg/geoip/geoip_test.go +++ b/pkg/geoip/geoip_test.go @@ -6,6 +6,7 @@ import ( "github.com/jarcoal/httpmock" "github.com/stretchr/testify/require" + "github.com/threefoldtech/zosbase/pkg/environment" ) func TestGetLocation(t *testing.T) { @@ -21,7 +22,8 @@ func TestGetLocation(t *testing.T) { City: "Cairo", } - for _, url := range geoipURLs { + env := environment.MustGet() + for _, url := range env.GeoipURLs { httpmock.RegisterResponder("GET", url, func(req *http.Request) (*http.Response, error) { return httpmock.NewJsonResponse(200, l) @@ -41,7 +43,8 @@ func TestGetLocation(t *testing.T) { } t.Run("test 404 status code", func(t *testing.T) { - for _, url := range geoipURLs { + env := environment.MustGet() + for _, url := range env.GeoipURLs { httpmock.RegisterResponder("GET", url, func(req *http.Request) (*http.Response, error) { return httpmock.NewJsonResponse(404, l) @@ -55,7 +58,8 @@ func TestGetLocation(t *testing.T) { }) t.Run("test invalid response data", func(t *testing.T) { - for _, url := range geoipURLs { + env := environment.MustGet() + for _, url := range env.GeoipURLs { httpmock.RegisterResponder("GET", url, func(req *http.Request) (*http.Response, error) { resp, err := httpmock.NewJsonResponse(200, "Cairo") diff --git a/pkg/perf/cache_test.go b/pkg/perf/cache_test.go index 035fa9c8..46e99b96 100644 --- a/pkg/perf/cache_test.go +++ b/pkg/perf/cache_test.go @@ -63,7 +63,7 @@ func TestPerformanceMonitor_SetCache_Mock(t *testing.T) { ctx := context.Background() taskResult := createTestTaskResult("test-task", uint64(time.Now().Unix())) s.Close() - + err := pm.setCache(ctx, taskResult) assert.Error(t, err) }) @@ -79,7 +79,7 @@ func TestPerformanceMonitor_Get_Mock(t *testing.T) { t.Run("successful Get", func(t *testing.T) { taskResult := createTestTaskResult("test-task", uint64(time.Now().Unix())) - + ctx := context.Background() err := pm.setCache(ctx, taskResult) require.NoError(t, err) @@ -109,8 +109,9 @@ func TestPerformanceMonitor_Get_Mock(t *testing.T) { t.Run("Get with invalid JSON", func(t *testing.T) { s2 := miniredis.RunT(t) pm2, err := newTestPerformanceMonitorWithMockRedis(s2.Addr()) - require.NoError(t, err) - s2.Set(generateKey("test-task"), "{invalid json") + require.NoError(t, err) + err = s2.Set(generateKey("test-task"), "{invalid json") + require.NoError(t, err) result, err := pm2.Get("test-task") assert.Error(t, err) assert.Contains(t, err.Error(), "failed to unmarshal data from json") @@ -164,7 +165,7 @@ func TestPerformanceMonitor_GetAll_Mock(t *testing.T) { ctx := context.Background() task1 := createTestTaskResult("task1", uint64(time.Now().Unix())) task2 := createTestTaskResult("task2", uint64(time.Now().Unix()-100)) - + err := pm.setCache(ctx, task1) require.NoError(t, err) err = pm.setCache(ctx, task2) @@ -173,7 +174,7 @@ func TestPerformanceMonitor_GetAll_Mock(t *testing.T) { results, err := pm.GetAll() assert.NoError(t, err) assert.Len(t, results, 2) - + taskNames := []string{results[0].Name, results[1].Name} assert.Contains(t, taskNames, "task1") assert.Contains(t, taskNames, "task2") @@ -201,13 +202,14 @@ func TestPerformanceMonitor_GetAll_Mock(t *testing.T) { s3 := miniredis.RunT(t) pm3, err := newTestPerformanceMonitorWithMockRedis(s3.Addr()) require.NoError(t, err) - + ctx := context.Background() validTask := createTestTaskResult("valid-task", uint64(time.Now().Unix())) err = pm3.setCache(ctx, validTask) require.NoError(t, err) - - s3.Set(generateKey("corrupted-task"), "{invalid json") + + err = s3.Set(generateKey("corrupted-task"), "{invalid json") + require.NoError(t, err) results, err := pm3.GetAll() assert.NoError(t, err) @@ -235,4 +237,4 @@ func TestGenerateKey(t *testing.T) { assert.Equal(t, tc.expected, result) }) } -} \ No newline at end of file +} diff --git a/pkg/perf/publicip/publicip_task.go b/pkg/perf/publicip/publicip_task.go index feb61a16..e7a41e66 100644 --- a/pkg/perf/publicip/publicip_task.go +++ b/pkg/perf/publicip/publicip_task.go @@ -246,7 +246,7 @@ func isLeastValidNode(ctx context.Context, farmID uint32, substrateGateway *stub // stop at three and quiet output err = exec.CommandContext(ctx, "ping", "-c", "3", "-q", ip).Run() if err != nil { - log.Err(err).Msgf("failed to ping node %d", node.NodeID) + log.Debug().Err(err).Msgf("failed to ping node %d", node.NodeID) continue } return false, nil @@ -336,7 +336,6 @@ func getPublicIPFromSTUN(stunServer string) (net.IP, error) { return } }) - if err != nil { return nil, fmt.Errorf("STUN request failed: %w", err) } diff --git a/pkg/registrar/registrar.go b/pkg/registrar/registrar.go index 5f2f312c..d34ad044 100644 --- a/pkg/registrar/registrar.go +++ b/pkg/registrar/registrar.go @@ -71,7 +71,7 @@ type Registrar struct { mutex sync.RWMutex } -func NewRegistrar(ctx context.Context, cl zbus.Client, env environment.Environment, info RegistrationInfo) *Registrar { +func NewRegistrar(ctx context.Context, cl zbus.Client, info RegistrationInfo) *Registrar { r := Registrar{ State{ 0, @@ -82,7 +82,7 @@ func NewRegistrar(ctx context.Context, cl zbus.Client, env environment.Environme sync.RWMutex{}, } - go r.register(ctx, cl, env, info) + go r.register(ctx, cl, info) return &r } @@ -100,7 +100,7 @@ func (r *Registrar) getState() State { // register a node and then blocks forever watching the node account. It tries to re-activate the // account if needed -func (r *Registrar) register(ctx context.Context, cl zbus.Client, env environment.Environment, info RegistrationInfo) { +func (r *Registrar) register(ctx context.Context, cl zbus.Client, info RegistrationInfo) { if app.CheckFlag(app.LimitedCache) { r.setState(FailedState(errors.New("no disks"))) return @@ -116,6 +116,7 @@ func (r *Registrar) register(ctx context.Context, cl zbus.Client, env environmen bo := backoff.WithContext(exp, ctx) register := func() { err := backoff.RetryNotify(func() error { + env := environment.MustGet() nodeID, twinID, err := r.registration(ctx, cl, env, info) if err != nil { r.setState(FailedState(err)) @@ -145,6 +146,7 @@ func (r *Registrar) register(ctx context.Context, cl zbus.Client, env environmen select { case <-ctx.Done(): case <-time.After(monitorAccountEvery): + env := environment.MustGet() if err := r.reActivate(ctx, cl, env); err != nil { log.Error().Err(err).Msg("failed to reactivate account") } diff --git a/pkg/registrar_light/registrar.go b/pkg/registrar_light/registrar.go index 15a0fe5f..84d6b0ce 100644 --- a/pkg/registrar_light/registrar.go +++ b/pkg/registrar_light/registrar.go @@ -71,7 +71,7 @@ type Registrar struct { mutex sync.RWMutex } -func NewRegistrar(ctx context.Context, cl zbus.Client, env environment.Environment, info RegistrationInfo) *Registrar { +func NewRegistrar(ctx context.Context, cl zbus.Client, info RegistrationInfo) *Registrar { r := Registrar{ State{ 0, @@ -81,7 +81,7 @@ func NewRegistrar(ctx context.Context, cl zbus.Client, env environment.Environme }, sync.RWMutex{}, } - go r.register(ctx, cl, env, info) + go r.register(ctx, cl, info) return &r } @@ -99,7 +99,7 @@ func (r *Registrar) getState() State { // register a node and then blocks forever watching the node account. It tries to re-activate the // account if needed -func (r *Registrar) register(ctx context.Context, cl zbus.Client, env environment.Environment, info RegistrationInfo) { +func (r *Registrar) register(ctx context.Context, cl zbus.Client, info RegistrationInfo) { if app.CheckFlag(app.LimitedCache) { r.setState(FailedState(errors.New("no disks"))) return @@ -115,6 +115,7 @@ func (r *Registrar) register(ctx context.Context, cl zbus.Client, env environmen bo := backoff.WithContext(exp, ctx) register := func() { err := backoff.RetryNotify(func() error { + env := environment.MustGet() nodeID, twinID, err := r.registration(ctx, cl, env, info) if err != nil { r.setState(FailedState(err)) @@ -144,6 +145,7 @@ func (r *Registrar) register(ctx context.Context, cl zbus.Client, env environmen select { case <-ctx.Done(): case <-time.After(monitorAccountEvery): + env := environment.MustGet() if err := r.reActivate(ctx, cl, env); err != nil { log.Error().Err(err).Msg("failed to reactivate account") } diff --git a/pkg/upgrade/hub/hub.go b/pkg/upgrade/hub/hub.go index d3f22988..4ba242a8 100644 --- a/pkg/upgrade/hub/hub.go +++ b/pkg/upgrade/hub/hub.go @@ -15,18 +15,11 @@ import ( "github.com/pkg/errors" "github.com/rs/zerolog/log" "github.com/threefoldtech/0-fs/meta" + "github.com/threefoldtech/zosbase/pkg/environment" "github.com/threefoldtech/zosbase/pkg/kernel" ) const ( - // hubBaseURL base hub url - hubBaseURL = "https://hub.threefold.me/" - hubv4BaseURL = "https://v4.hub.threefold.me" - - // hubStorage default hub db - hubStorage = "zdb://hub.threefold.me:9900" - hubv4Storage = "zdb://hub.threefold.me:9940" - defaultHubCallTimeout = 20 * time.Second ) @@ -94,18 +87,21 @@ func (h *HubClient) MountURL(flist string) string { // StorageURL return hub storage url func (h *HubClient) StorageURL() string { - if kernel.GetParams().IsV4() { - return hubv4Storage - } - return hubStorage + env := environment.MustGet() + return env.HubStorage + // if kernel.GetParams().IsV4() { + // return hubv4Storage + // } + // return hubStorage } // StorageURL return hub storage url func (h *HubClient) HubBaseURL() string { + env := environment.MustGet() if kernel.GetParams().IsV4() { - return hubv4BaseURL + return env.V4HubURL } - return hubBaseURL + return env.HubURL } // Info gets flist info from hub @@ -304,10 +300,12 @@ func (b *Regular) Files(repo string) ([]FileInfo, error) { var content struct { Content []FileInfo `json:"content"` } - baseURL := hubBaseURL + env := environment.MustGet() + baseURL := env.HubURL if kernel.GetParams().IsV4() { - baseURL = hubv4BaseURL + baseURL = env.V4HubURL } + u, err := url.Parse(baseURL) if err != nil { panic("invalid base url") diff --git a/pkg/upgrade/upgrade.go b/pkg/upgrade/upgrade.go index 7a7a8211..30cc59d4 100644 --- a/pkg/upgrade/upgrade.go +++ b/pkg/upgrade/upgrade.go @@ -42,9 +42,7 @@ var ( const ( service = "upgrader" - defaultHubStorage = "zdb://hub.threefold.me:9900" - defaultHubv4Storage = "zdb://v4.hub.threefold.me:9940" - defaultZinitSocket = "/var/run/zinit.sock" + defaultZinitSocket = "/var/run/zinit.sock" checkForUpdateEvery = 60 * time.Minute checkJitter = 10 // minutes @@ -161,13 +159,14 @@ func NewUpgrader(root string, opts ...UpgraderOption) (*Upgrader, error) { return nil, err } } - hub_storage := defaultHubStorage - if kernel.GetParams().IsV4() { - hub_storage = defaultHubv4Storage - } + env := environment.MustGet() + hubStorage := env.HubStorage + // if kernel.GetParams().IsV4() { + // hub_storage = defaultHubv4Storage + // } if u.storage == nil { // no storage option was set. use default - if err := Storage(hub_storage)(u); err != nil { + if err := Storage(hubStorage)(u); err != nil { return nil, err } } @@ -303,7 +302,6 @@ func (u *Upgrader) update(ctx context.Context) error { return nil } } else { - if env.RunningMode != environment.RunningDev && (remoteVer != chainVer.Version) { // nothing to do! hub version is not the same as the chain return nil diff --git a/pkg/upgrade/upgrade_test.go b/pkg/upgrade/upgrade_test.go index 1f245900..4fca868c 100644 --- a/pkg/upgrade/upgrade_test.go +++ b/pkg/upgrade/upgrade_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/require" "github.com/threefoldtech/0-fs/meta" + "github.com/threefoldtech/zosbase/pkg/environment" "github.com/threefoldtech/zosbase/pkg/upgrade/hub" ) @@ -19,7 +20,8 @@ func TestUpgraderDownload(t *testing.T) { hub: hubClient, } - err := Storage(defaultHubStorage)(up) + env := environment.MustGet() + err := Storage(env.HubStorage)(up) require.NoError(err) const repo = "azmy.3bot"