Skip to content

Commit 3f75d10

Browse files
authored
Merge pull request #33 from threefoldtech/load-config-urls-from-zos-config
use urls from zos-config if exists
2 parents 708725d + 609137e commit 3f75d10

10 files changed

Lines changed: 151 additions & 92 deletions

File tree

pkg/environment/config.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,23 @@ type Config struct {
3030
RolloutUpgrade struct {
3131
TestFarms []uint32 `json:"test_farms"`
3232
} `json:"rollout_upgrade"`
33-
RelaysURLs []string `json:"relays_urls"`
3433
SubstrateURL []string `json:"substrate_urls"`
34+
RelaysURLs []string `json:"relays_urls"`
3535
ActivationURL []string `json:"activation_urls"`
3636
GraphQL []string `json:"graphql_urls"`
3737
KycURL string `json:"kyc_url"`
3838
RegistrarURL string `json:"registrar_url"`
39-
// FlistURL []string `json:"flist_url"`
40-
// HubURL []string `json:"hub_url"`
41-
BinRepo string `json:"bin_repo"`
39+
BinRepo string `json:"bin_repo"`
40+
GeoipURLs []string `json:"geoip_urls"`
41+
42+
HubURL []string `json:"hub_url"`
43+
V4HubURL []string `json:"v4_hub_url"`
44+
45+
// we should not be supporting flist url or hub storage from zos-config until we can update them on runtime
46+
FlistURL string `json:"flist_url"`
47+
V4FlistURL string `json:"v4_flist_url"`
48+
HubStorage string `json:"hub_storage"`
49+
V4HubStorage string `json:"v4_hub_storage"`
4250
}
4351

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

pkg/environment/environment.go

Lines changed: 93 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,24 @@ import (
1717

1818
const (
1919
baseExtendedURL = "https://raw.githubusercontent.com/threefoldtech/zos-config/main/"
20-
hubURL = "https://hub.threefold.me"
21-
v4HubURL = "https://v4.hub.threefold.me"
22-
flistURL = "redis://hub.threefold.me:9900"
23-
v4FlistURL = "redis://v4.hub.threefold.me:9940"
20+
21+
defaultHubURL = "https://hub.threefold.me"
22+
defaultV4HubURL = "https://v4.hub.threefold.me"
23+
24+
defaultFlistURL = "redis://hub.threefold.me:9900"
25+
defaultV4FlistURL = "redis://v4.hub.threefold.me:9940"
26+
27+
defaultHubStorage = "zdb://hub.threefold.me:9900"
28+
defaultV4HubStorage = "zdb://v4.hub.threefold.me:9940"
2429
)
2530

31+
var defaultGeoipURLs = []string{
32+
"https://geoip.threefold.me/",
33+
"https://geoip.grid.tf/",
34+
"https://02.geoip.grid.tf/",
35+
"https://03.geoip.grid.tf/",
36+
}
37+
2638
// PubMac specify how the mac address of the public nic
2739
// (in case of public-config) is calculated
2840
type PubMac string
@@ -42,8 +54,12 @@ const (
4254
type Environment struct {
4355
RunningMode RunMode
4456

45-
FlistURL string
46-
BinRepo string
57+
FlistURL string
58+
HubStorage string
59+
BinRepo string
60+
61+
HubURL string
62+
V4HubURL string
4763

4864
FarmID pkg.FarmID
4965
Orphan bool
@@ -56,9 +72,9 @@ type Environment struct {
5672
relaysURLs []string
5773
ActivationURL []string
5874
GraphQL []string
75+
GeoipURLs []string
5976
KycURL string
6077
RegistrarURL string
61-
HubURL string
6278

6379
// private vlan to join
6480
// if set, zos will use this as its priv vlan
@@ -126,15 +142,18 @@ var (
126142
"https://activation.dev.grid.tf/activation/activate",
127143
"https://activation.02.dev.grid.tf/activation/activate",
128144
},
129-
FlistURL: flistURL,
130-
HubURL: hubURL,
131-
BinRepo: "tf-zos-v3-bins.dev",
145+
HubURL: defaultHubURL,
146+
V4HubURL: defaultV4HubURL,
147+
FlistURL: defaultFlistURL,
148+
HubStorage: defaultHubStorage,
149+
BinRepo: "tf-zos-v3-bins.dev",
132150
GraphQL: []string{
133151
"https://graphql.dev.grid.tf/graphql",
134152
"https://graphql.02.dev.grid.tf/graphql",
135153
},
136154
KycURL: "https://kyc.dev.grid.tf",
137155
RegistrarURL: "http://registrar.dev4.grid.tf",
156+
GeoipURLs: defaultGeoipURLs,
138157
}
139158

140159
envTest = Environment{
@@ -150,15 +169,18 @@ var (
150169
"https://activation.test.grid.tf/activation/activate",
151170
"https://activation.02.test.grid.tf/activation/activate",
152171
},
153-
FlistURL: flistURL,
154-
HubURL: hubURL,
155-
BinRepo: "tf-zos-v3-bins.test",
172+
HubURL: defaultHubURL,
173+
V4HubURL: defaultV4HubURL,
174+
FlistURL: defaultFlistURL,
175+
HubStorage: defaultHubStorage,
176+
BinRepo: "tf-zos-v3-bins.test",
156177
GraphQL: []string{
157178
"https://graphql.test.grid.tf/graphql",
158179
"https://graphql.02.test.grid.tf/graphql",
159180
},
160181
KycURL: "https://kyc.test.grid.tf",
161182
RegistrarURL: "http://registrar.test4.grid.tf",
183+
GeoipURLs: defaultGeoipURLs,
162184
}
163185

164186
envQA = Environment{
@@ -174,15 +196,18 @@ var (
174196
"https://activation.qa.grid.tf/activation/activate",
175197
"https://activation.02.qa.grid.tf/activation/activate",
176198
},
177-
FlistURL: flistURL,
178-
HubURL: hubURL,
179-
BinRepo: "tf-zos-v3-bins.qanet",
199+
HubURL: defaultHubURL,
200+
V4HubURL: defaultV4HubURL,
201+
FlistURL: defaultFlistURL,
202+
HubStorage: defaultHubStorage,
203+
BinRepo: "tf-zos-v3-bins.qanet",
180204
GraphQL: []string{
181205
"https://graphql.qa.grid.tf/graphql",
182206
"https://graphql.02.qa.grid.tf/graphql",
183207
},
184208
KycURL: "https://kyc.qa.grid.tf",
185209
RegistrarURL: "https://registrar.qa4.grid.tf",
210+
GeoipURLs: defaultGeoipURLs,
186211
}
187212

188213
envProd = Environment{
@@ -201,17 +226,22 @@ var (
201226
"https://activation.grid.threefold.me/activation/activate",
202227
"https://activation.grid.tf/activation/activate",
203228
"https://activation.02.grid.tf/activation/activate",
229+
"https://activation.grid.threefold.me/activation/activate",
204230
},
205-
HubURL: hubURL,
206-
FlistURL: flistURL,
207-
BinRepo: "tf-zos-v3-bins",
231+
HubURL: defaultHubURL,
232+
V4HubURL: defaultV4HubURL,
233+
FlistURL: defaultFlistURL,
234+
HubStorage: defaultHubStorage,
235+
BinRepo: "tf-zos-v3-bins",
208236
GraphQL: []string{
209237
"https://graphql.grid.threefold.me/graphql",
210238
"https://graphql.grid.tf/graphql",
211239
"https://graphql.02.grid.tf/graphql",
240+
"https://graphql.grid.threefold.me/graphql",
212241
},
213242
KycURL: "https://kyc.threefold.me",
214243
RegistrarURL: "https://registrar.prod4.threefold.me",
244+
GeoipURLs: defaultGeoipURLs,
215245
}
216246
)
217247

@@ -295,29 +325,24 @@ func getEnvironmentFromParams(params kernel.Params) (Environment, error) {
295325
if err != nil {
296326
// maybe the node can't reach the internet right now
297327
// this will enforce node to skip config
328+
// or we can keep retrying untill it can fetch config
298329
config = Config{}
299330
}
300331

301-
if substrate, ok := params.Get("substrate"); ok {
302-
if len(substrate) > 0 {
303-
env.SubstrateURL = substrate
304-
}
332+
if substrate, ok := params.Get("substrate"); ok && len(substrate) > 0 {
333+
env.SubstrateURL = substrate
305334
} else if substrate := config.SubstrateURL; len(substrate) > 0 {
306335
env.SubstrateURL = substrate
307336
}
308337

309-
if relay, ok := params.Get("relay"); ok {
310-
if len(relay) > 0 {
311-
env.relaysURLs = relay
312-
}
338+
if relay, ok := params.Get("relay"); ok && len(relay) > 0 {
339+
env.relaysURLs = relay
313340
} else if relay := config.RelaysURLs; len(relay) > 0 {
314341
env.relaysURLs = relay
315342
}
316343

317-
if activation, ok := params.Get("activation"); ok {
318-
if len(activation) > 0 {
319-
env.ActivationURL = activation
320-
}
344+
if activation, ok := params.Get("activation"); ok && len(activation) > 0 {
345+
env.ActivationURL = activation
321346
} else if activation := config.ActivationURL; len(activation) > 0 {
322347
env.ActivationURL = activation
323348
}
@@ -326,14 +351,6 @@ func getEnvironmentFromParams(params kernel.Params) (Environment, error) {
326351
env.GraphQL = graphql
327352
}
328353

329-
// if flist := config.FlistURL; len(flist) > 0 {
330-
// env.FlistURL = flist[0]
331-
// }
332-
333-
// if hub := config.HubURL; len(hub) > 0 {
334-
// env.HubURL = hub[0]
335-
// }
336-
337354
if bin := config.BinRepo; len(bin) > 0 {
338355
env.BinRepo = bin
339356
}
@@ -346,6 +363,43 @@ func getEnvironmentFromParams(params kernel.Params) (Environment, error) {
346363
env.RegistrarURL = registrar
347364
}
348365

366+
if geoip := config.GeoipURLs; len(geoip) > 0 {
367+
env.GeoipURLs = geoip
368+
}
369+
370+
// flist url and hub storage urls shouldn't listen to changes in config as long as we can't change it at run time.
371+
// it would cause breakage in vmd that needs a reboot to be recovered.
372+
if flist := config.FlistURL; len(flist) > 0 {
373+
env.FlistURL = flist
374+
}
375+
376+
if storage := config.HubStorage; len(storage) > 0 {
377+
env.HubStorage = storage
378+
}
379+
380+
// maybe we should verify that we're using a working hub url
381+
if hub := config.HubURL; len(hub) > 0 {
382+
env.HubURL = hub[0]
383+
}
384+
385+
// some modules needs v3 hub url even if the node is of v4
386+
if hub := config.V4HubURL; len(hub) > 0 {
387+
env.V4HubURL = hub[0]
388+
}
389+
390+
// if the node running v4 chage urls to use v4 hub
391+
if params.IsV4() {
392+
env.FlistURL = defaultV4FlistURL
393+
if flist := config.V4FlistURL; len(flist) > 0 {
394+
env.FlistURL = flist
395+
}
396+
397+
env.HubStorage = defaultV4HubStorage
398+
if storage := config.V4HubStorage; len(storage) > 0 {
399+
env.HubStorage = storage
400+
}
401+
}
402+
349403
if farmSecret, ok := params.Get("secret"); ok {
350404
if len(farmSecret) > 0 {
351405
env.FarmSecret = farmSecret[len(farmSecret)-1]
@@ -423,11 +477,5 @@ func getEnvironmentFromParams(params kernel.Params) (Environment, error) {
423477
env.BinRepo = e
424478
}
425479

426-
// if the node running v4 chage flisturl to use v4.hub.threefold.me
427-
if params.IsV4() {
428-
env.FlistURL = v4FlistURL
429-
env.HubURL = v4HubURL
430-
}
431-
432480
return env, nil
433481
}

pkg/geoip/geoip.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/hashicorp/go-retryablehttp"
99
"github.com/rs/zerolog/log"
10+
"github.com/threefoldtech/zosbase/pkg/environment"
1011
)
1112

1213
// Location holds the result of a geoip request
@@ -19,15 +20,12 @@ type Location struct {
1920
City string `json:"city_name"`
2021
}
2122

22-
var (
23-
geoipURLs = []string{"https://geoip.threefold.me/", "https://geoip.grid.tf/", "https://02.geoip.grid.tf/", "https://03.geoip.grid.tf/"}
24-
25-
defaultHTTPClient = retryablehttp.NewClient()
26-
)
23+
var defaultHTTPClient = retryablehttp.NewClient()
2724

2825
// Fetch retrieves the location of the system calling this function
2926
func Fetch() (Location, error) {
30-
for _, url := range geoipURLs {
27+
env := environment.MustGet()
28+
for _, url := range env.GeoipURLs {
3129
l, err := getLocation(url)
3230
if err != nil {
3331
log.Err(err).Str("url", url).Msg("failed to fetch location from geoip service")

pkg/geoip/geoip_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/jarcoal/httpmock"
88
"github.com/stretchr/testify/require"
9+
"github.com/threefoldtech/zosbase/pkg/environment"
910
)
1011

1112
func TestGetLocation(t *testing.T) {
@@ -21,7 +22,8 @@ func TestGetLocation(t *testing.T) {
2122
City: "Cairo",
2223
}
2324

24-
for _, url := range geoipURLs {
25+
env := environment.MustGet()
26+
for _, url := range env.GeoipURLs {
2527
httpmock.RegisterResponder("GET", url,
2628
func(req *http.Request) (*http.Response, error) {
2729
return httpmock.NewJsonResponse(200, l)
@@ -41,7 +43,8 @@ func TestGetLocation(t *testing.T) {
4143
}
4244

4345
t.Run("test 404 status code", func(t *testing.T) {
44-
for _, url := range geoipURLs {
46+
env := environment.MustGet()
47+
for _, url := range env.GeoipURLs {
4548
httpmock.RegisterResponder("GET", url,
4649
func(req *http.Request) (*http.Response, error) {
4750
return httpmock.NewJsonResponse(404, l)
@@ -55,7 +58,8 @@ func TestGetLocation(t *testing.T) {
5558
})
5659

5760
t.Run("test invalid response data", func(t *testing.T) {
58-
for _, url := range geoipURLs {
61+
env := environment.MustGet()
62+
for _, url := range env.GeoipURLs {
5963
httpmock.RegisterResponder("GET", url,
6064
func(req *http.Request) (*http.Response, error) {
6165
resp, err := httpmock.NewJsonResponse(200, "Cairo")

pkg/perf/publicip/publicip_task.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func isLeastValidNode(ctx context.Context, farmID uint32, substrateGateway *stub
246246
// stop at three and quiet output
247247
err = exec.CommandContext(ctx, "ping", "-c", "3", "-q", ip).Run()
248248
if err != nil {
249-
log.Err(err).Msgf("failed to ping node %d", node.NodeID)
249+
log.Warn().Err(err).Msgf("failed to ping node %d", node.NodeID)
250250
continue
251251
}
252252
return false, nil
@@ -336,7 +336,6 @@ func getPublicIPFromSTUN(stunServer string) (net.IP, error) {
336336
return
337337
}
338338
})
339-
340339
if err != nil {
341340
return nil, fmt.Errorf("STUN request failed: %w", err)
342341
}

0 commit comments

Comments
 (0)