-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathenvironment.go
359 lines (312 loc) · 8.16 KB
/
environment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package environment
import (
"os"
"slices"
"strconv"
"sync"
"github.com/pkg/errors"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/zos/pkg"
"github.com/threefoldtech/zos/pkg/kernel"
)
const (
baseExtendedURL = "https://raw.githubusercontent.com/threefoldtech/zos-config/main/"
)
// PubMac specify how the mac address of the public nic
// (in case of public-config) is calculated
type PubMac string
const (
// PubMacRandom means the mac of the public nic will be chosen by the system
// the value won't change across reboots, but is based on the node id
// (default)
PubMacRandom PubMac = "random"
// PubMacSwap means the value of the mac is swapped with the physical nic
// where the public traffic is eventually going through
PubMacSwap PubMac = "swap"
)
// Environment holds information about running environment of a node
// it defines the different constant based on the running mode (dev, test, prod)
type Environment struct {
RunningMode RunMode
FlistURL string
BinRepo string
FarmID pkg.FarmID
Orphan bool
FarmSecret string
SubstrateURL []string
// IMPORTANT NOTICE:
// SINCE RELAYS FOR A NODE IS STORED ON THE CHAIN IN A LIMITED SPACE
// PLEASE MAKE SURE THAT ANY ENV HAS NO MORE THAN FOUR RELAYS CONFIGURED
RelayURL []string
ActivationURL []string
GraphQL []string
KycURL string
// private vlan to join
// if set, zos will use this as its priv vlan
PrivVlan *uint16
// pub vlan to join
// if set, zos will use this as it's pub vlan
// only in a single nic setup
PubVlan *uint16
// PubMac value from environment
PubMac PubMac
}
// RunMode type
type RunMode string
func (r RunMode) String() string {
switch r {
case RunningDev:
return "development"
case RunningQA:
return "qa"
case RunningMain:
return "production"
case RunningTest:
return "testing"
}
return "unknown"
}
// Possible running mode of a node
const (
// RunningDev mode
RunningDev RunMode = "dev"
// RunningQA mode
RunningQA RunMode = "qa"
// RunningTest mode
RunningTest RunMode = "test"
// RunningMain mode
RunningMain RunMode = "prod"
// Orphanage is the default farmid where nodes are registered
// if no farmid were specified on the kernel command line
OrphanageDev pkg.FarmID = 0
OrphanageTest pkg.FarmID = 0
OrphanageMain pkg.FarmID = 0
)
var (
pool substrate.Manager
poolOnce sync.Once
envDev = Environment{
RunningMode: RunningDev,
SubstrateURL: []string{
"wss://tfchain.dev.grid.tf/",
"wss://tfchain.02.dev.grid.tf",
},
RelayURL: []string{
"wss://relay.dev.grid.tf",
"wss://relay.02.dev.grid.tf",
},
ActivationURL: []string{
"https://activation.dev.grid.tf/activation/activate",
"https://activation.02.dev.grid.tf/activation/activate",
},
FlistURL: "redis://hub.grid.tf:9900",
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",
}
envTest = Environment{
RunningMode: RunningTest,
SubstrateURL: []string{
"wss://tfchain.test.grid.tf/",
"wss://tfchain.02.test.grid.tf",
},
RelayURL: []string{
"wss://relay.test.grid.tf",
"wss://relay.02.test.grid.tf",
},
ActivationURL: []string{
"https://activation.test.grid.tf/activation/activate",
"https://activation.02.test.grid.tf/activation/activate",
},
FlistURL: "redis://hub.grid.tf:9900",
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",
}
envQA = Environment{
RunningMode: RunningQA,
SubstrateURL: []string{
"wss://tfchain.qa.grid.tf/",
"wss://tfchain.02.qa.grid.tf/",
},
RelayURL: []string{
"wss://relay.qa.grid.tf",
"wss://relay.02.qa.grid.tf",
},
ActivationURL: []string{
"https://activation.qa.grid.tf/activation/activate",
"https://activation.02.qa.grid.tf/activation/activate",
},
FlistURL: "redis://hub.grid.tf:9900",
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",
}
envProd = Environment{
RunningMode: RunningMain,
SubstrateURL: []string{
"wss://tfchain.grid.tf/",
"wss://tfchain.02.grid.tf",
"wss://02.tfchain.grid.tf/",
"wss://03.tfchain.grid.tf/",
"wss://04.tfchain.grid.tf/",
},
RelayURL: []string{
"wss://relay.grid.tf",
"wss://relay.02.grid.tf",
},
ActivationURL: []string{
"https://activation.grid.tf/activation/activate",
"https://activation.02.grid.tf/activation/activate",
},
FlistURL: "redis://hub.grid.tf:9900",
BinRepo: "tf-zos-v3-bins",
GraphQL: []string{
"https://graphql.grid.tf/graphql",
"https://graphql.02.grid.tf/graphql",
},
KycURL: "https://kyc.grid.tf",
}
)
// MustGet returns the running environment of the node
// panics on error
func MustGet() Environment {
env, err := Get()
if err != nil {
panic(err)
}
return env
}
// Get return the running environment of the node
func Get() (Environment, error) {
params := kernel.GetParams()
return getEnvironmentFromParams(params)
}
// GetSubstrate gets a client to subsrate blockchain
func GetSubstrate() (substrate.Manager, error) {
env, err := Get()
if err != nil {
return nil, errors.Wrap(err, "failed to get boot environment")
}
poolOnce.Do(func() {
pool = substrate.NewManager(env.SubstrateURL...)
})
return pool, nil
}
func getEnvironmentFromParams(params kernel.Params) (Environment, error) {
var env Environment
runmode := ""
if modes, ok := params.Get("runmode"); ok {
if len(modes) >= 1 {
runmode = modes[0]
}
} else {
runmode = os.Getenv("ZOS_RUNMODE")
}
if len(runmode) == 0 {
runmode = string(RunningMain)
}
switch RunMode(runmode) {
case RunningDev:
env = envDev
case RunningQA:
env = envQA
case RunningTest:
env = envTest
case RunningMain:
env = envProd
default:
env = envProd
}
if substrate, ok := params.Get("substrate"); ok {
if len(substrate) > 0 {
env.SubstrateURL = substrate
}
}
if relay, ok := params.Get("relay"); ok {
if len(relay) > 0 {
env.RelayURL = relay
}
}
if activation, ok := params.Get("activation"); ok {
if len(activation) > 0 {
env.ActivationURL = activation
}
}
if farmSecret, ok := params.Get("secret"); ok {
if len(farmSecret) > 0 {
env.FarmSecret = farmSecret[len(farmSecret)-1]
}
}
farmerID, found := params.Get("farmer_id")
if !found || len(farmerID) < 1 || farmerID[0] == "" {
// fmt.Println("Warning: no valid farmer_id found in kernel parameter, fallback to orphanage")
env.Orphan = true
switch env.RunningMode {
case RunningDev:
env.FarmID = OrphanageDev
case RunningTest:
env.FarmID = OrphanageTest
case RunningMain:
env.FarmID = OrphanageMain
}
} else {
env.Orphan = false
id, err := strconv.ParseUint(farmerID[0], 10, 32)
if err != nil {
return env, errors.Wrap(err, "wrong format for farm ID")
}
env.FarmID = pkg.FarmID(id)
}
if vlan, found := params.GetOne("vlan:priv"); found {
if !slices.Contains([]string{"none", "untagged", "un"}, vlan) {
tag, err := strconv.ParseUint(vlan, 10, 16)
if err != nil {
return env, errors.Wrap(err, "failed to parse priv vlan value")
}
tagU16 := uint16(tag)
env.PrivVlan = &tagU16
}
}
if vlan, found := params.GetOne("vlan:pub"); found {
if !slices.Contains([]string{"none", "untagged", "un"}, vlan) {
tag, err := strconv.ParseUint(vlan, 10, 16)
if err != nil {
return env, errors.Wrap(err, "failed to parse pub vlan value")
}
tagU16 := uint16(tag)
env.PubVlan = &tagU16
}
}
if mac, found := params.GetOne("pub:mac"); found {
v := PubMac(mac)
if slices.Contains([]PubMac{PubMacRandom, PubMacSwap}, v) {
env.PubMac = v
} else {
env.PubMac = PubMacRandom
}
} else {
env.PubMac = PubMacRandom
}
// Checking if there environment variable
// override default settings
if e := os.Getenv("ZOS_SUBSTRATE_URL"); e != "" {
env.SubstrateURL = []string{e}
}
if e := os.Getenv("ZOS_FLIST_URL"); e != "" {
env.FlistURL = e
}
if e := os.Getenv("ZOS_BIN_REPO"); e != "" {
env.BinRepo = e
}
return env, nil
}