summaryrefslogtreecommitdiff
path: root/netdeploy/remote/nodecfg/nodeDir.go
blob: 8d17f01d53f88fc33fac7fbe92a4fdb7eeecce2e (plain)
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
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package nodecfg

import (
	"encoding/json"
	"fmt"
	"net/url"
	"os"
	"path/filepath"
	"strings"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/netdeploy/remote"
	"github.com/algorand/go-algorand/shared/algoh"
	"github.com/algorand/go-algorand/util/tokens"
)

type nodeDir struct {
	remote.NodeConfig
	dataDir      string
	config       config.Local
	delaySave    bool
	configurator *nodeConfigurator
}

// * Configure:
//   - IsRelay
//   - NetAddress
//   - APIEndpoint
//   - APIToken
//   - EnableTelemetry
//   - TelemetryURI
//   - EnableMetrics
//   - EnableService
//   - CronTabSchedule
//   - EnableBlockStats
//   - DashboardEndpoint
//   - DeadlockOverride
func (nd *nodeDir) configure() (err error) {
	fmt.Fprintf(os.Stdout, "Configuring Node %s\n", nd.Name)
	if err = nd.configureRelay(nd.IsRelay()); err != nil {
		fmt.Fprintf(os.Stdout, "Error during configureRelay: %s\n", err)
		return
	}
	if err = nd.configureAPIEndpoint(nd.APIEndpoint); err != nil {
		fmt.Fprintf(os.Stdout, "Error during configureAPIEndpoint: %s\n", err)
		return
	}
	if err = nd.configureAPIToken(nd.APIToken); err != nil {
		fmt.Fprintf(os.Stdout, "Error during configureAPIToken: %s\n", err)
		return
	}
	if err = nd.configureAdminAPIToken(nd.AdminAPIToken); err != nil {
		fmt.Fprintf(os.Stdout, "Error during configureAdminAPIToken: %s\n", err)
		return
	}
	if err = nd.configureTelemetry(nd.EnableTelemetry); err != nil {
		fmt.Fprintf(os.Stdout, "Error during configureTelemetry: %s\n", err)
		return
	}
	if err = nd.configureMetrics(nd.EnableMetrics, nd.MetricsURI); err != nil {
		fmt.Fprintf(os.Stdout, "Error during configureMetrics: %s\n", err)
		return
	}
	if err = nd.configureDeadlock(nd.DeadlockOverride); err != nil {
		fmt.Fprintf(os.Stdout, "Error during configureDeadlock: %s\n", err)
		return
	}
	if err = nd.configureAlgoh(nd.EnableBlockStats); err != nil {
		fmt.Fprintf(os.Stdout, "Error configuring algoh: %s\n", err)
		return
	}
	if err = nd.configureOverrides(nd.ConfigJSONOverride); err != nil {
		fmt.Fprintf(os.Stdout, "Error applying config.json overrides: %s\n", err)
		return
	}
	// Configure DNSBootstrap - if not otherwise set, use the algodev default: <network>.algodev.network
	if err = nd.configureDNSBootstrap(); err != nil {
		fmt.Fprintf(os.Stdout, "Error during configureDNSBootstrap: %s\n", err)
		return
	}
	// Do this after reconciling the DNSBootstrap ID because we'll extract the network name
	// from it (eg <network>.algoblah.network -> algoblah.network)
	if err = nd.configureNetAddress(); err != nil {
		fmt.Fprintf(os.Stdout, "Error during configureNetAddress: %s\n", err)
		return
	}
	fmt.Println("Done configuring node directory.")
	return
}

func (nd *nodeDir) isConfigLoaded() bool {
	return nd.config.GossipFanout != 0
}

func (nd *nodeDir) ensureConfig() (err error) {
	if nd.isConfigLoaded() {
		return
	}
	nd.config, err = config.LoadConfigFromDisk(nd.dataDir)
	if os.IsNotExist(err) {
		err = nil
	}
	return
}

func (nd *nodeDir) saveConfig() (err error) {
	if nd.delaySave {
		return nil
	}
	if !nd.isConfigLoaded() {
		return nil
	}
	err = nd.config.SaveToDisk(nd.dataDir)
	return
}

func (nd *nodeDir) configureRelay(enable bool) (err error) {
	if err = nd.ensureConfig(); err != nil {
		return
	}
	// Nothing extra to configure here - we'll ensure DNS and Bootstrap entries elsewhere
	err = nd.saveConfig()
	return
}

func (nd *nodeDir) configureNetAddress() (err error) {
	if err = nd.ensureConfig(); err != nil {
		return
	}
	fmt.Fprintf(os.Stdout, " - Assigning NetAddress: %s\n", nd.NetAddress)
	nd.config.NetAddress = nd.NetAddress
	if nd.IsRelay() && nd.NetAddress[0] == ':' {
		fmt.Fprintf(os.Stdout, " - adding to relay addresses\n")
		for _, bootstrapRecord := range nd.config.DNSBootstrapArray(nd.configurator.genesisData.Network) {
			nd.configurator.addRelaySrv(bootstrapRecord.PrimarySRVBootstrap, nd.NetAddress)
		}
	}
	err = nd.saveConfig()
	return
}

func (nd *nodeDir) configureAPIEndpoint(address string) (err error) {
	if err = nd.ensureConfig(); err != nil {
		return
	}
	// Blank means leave default; if not blank and different from default, update it
	if address == "" || address == nd.config.EndpointAddress {
		return
	}
	fmt.Fprintf(os.Stdout, " - Assigning API Endpoint: %s\n", address)
	nd.config.EndpointAddress = address
	err = nd.saveConfig()
	return
}

func (nd *nodeDir) configureAPIToken(token string) (err error) {
	if token == "" {
		return
	}
	if err = nd.ensureConfig(); err != nil {
		return
	}
	fmt.Fprintf(os.Stdout, " - Assigning APIToken: %s\n", token)
	err = os.WriteFile(filepath.Join(nd.dataDir, tokens.AlgodTokenFilename), []byte(token), 0600)
	if err != nil {
		return err
	}
	return nd.saveConfig()
}

func (nd *nodeDir) configureAdminAPIToken(token string) (err error) {
	if token == "" {
		return
	}
	if err = nd.ensureConfig(); err != nil {
		return
	}
	fmt.Fprintf(os.Stdout, " - Assigning AdminAPIToken: %s\n", token)
	err = os.WriteFile(filepath.Join(nd.dataDir, tokens.AlgodAdminTokenFilename), []byte(token), 0600)
	if err != nil {
		return err
	}
	return nd.saveConfig()
}

func (nd *nodeDir) configureTelemetry(enable bool) (err error) {
	cfg, created, cfgErr := logging.EnsureTelemetryConfigCreated(nil, "")
	if cfgErr != nil {
		return cfgErr
	}

	// Override default enabling of new telemetry config
	if created {
		cfg.Enable = false
	}

	telemetryURI := strings.Replace(nd.TelemetryURI, "<network>", string(nd.configurator.genesisData.Network), -1)
	if !strings.HasPrefix(telemetryURI, "http") {
		telemetryURI = "http://" + telemetryURI
	}

	if enable == cfg.Enable &&
		(nd.TelemetryURI == "" || telemetryURI == cfg.URI) &&
		cfg.Name == nd.configurator.config.Name {
		return
	}

	// For now, don't disable - otherwise we NEED to configure telemetry
	// for every node on the Host.
	if enable {
		fmt.Fprintf(os.Stdout, " - Configuring telemetry (%v) => %s\n", enable, telemetryURI)
		cfg.Enable = enable
	} else {
		fmt.Fprintf(os.Stdout, " - Configuring telemetry (%v) => %s\n (not disabling)", enable, telemetryURI)
	}
	if nd.TelemetryURI != "" {
		cfg.URI = telemetryURI
	}
	cfg.Name = nd.configurator.config.Name
	cfg.Save(cfg.FilePath)
	return
}

func (nd *nodeDir) configureMetrics(enable bool, address string) (err error) {
	if err = nd.ensureConfig(); err != nil {
		return
	}
	if nd.config.EnableMetricReporting == enable {
		return
	}
	nd.config.EnableMetricReporting = enable

	metricsURI := strings.Replace(address, "<network>", string(nd.configurator.genesisData.Network), -1)

	// turn URI into parsable URI with scheme
	if !strings.HasPrefix(metricsURI, "http") {
		metricsURI = "http://" + metricsURI
	}

	// Split URI into local listening port and external SRV record name
	metricsURL, err := url.Parse(metricsURI)
	if err != nil {
		fmt.Fprintf(os.Stdout, "Error occurred parsing metrics URI (%s): %s", metricsURI, err)
		return
	}

	metricsPort := metricsURL.Port()
	if metricsPort != "" {
		nd.config.NodeExporterListenAddress = ":" + metricsPort
	}
	metricsSRV := metricsURL.Hostname()

	fmt.Fprintf(os.Stdout, " - Configuring metrics (%v) => %s - %s\n", enable, metricsSRV, nd.config.NodeExporterListenAddress)
	if nd.config.NodeExporterListenAddress != "" && nd.config.NodeExporterListenAddress[0] == ':' {
		nd.configurator.registerMetricsSrv(metricsSRV, nd.config.NodeExporterListenAddress)
	}
	err = nd.saveConfig()
	return
}

func (nd *nodeDir) configureDeadlock(value int) (err error) {
	if err = nd.ensureConfig(); err != nil {
		return
	}
	if value == nd.config.DeadlockDetection {
		return
	}
	fmt.Fprintf(os.Stdout, " - Updating DeadlockDetection: %v\n", value)
	nd.config.DeadlockDetection = value
	err = nd.saveConfig()
	return
}

func (nd *nodeDir) configureAlgoh(enableBlockStats bool) error {
	configFile := filepath.Join(nd.dataDir, algoh.ConfigFilename)
	config, err := algoh.LoadConfigFromFile(configFile)
	if err != nil && !os.IsNotExist(err) {
		return fmt.Errorf("error loading algoh configuration: %v", err)
	}
	if config.SendBlockStats != enableBlockStats {
		fmt.Fprintf(os.Stdout, " - Configuring algoh SendBlockStats (%v)\n", enableBlockStats)
		config.SendBlockStats = enableBlockStats
		return config.Save(configFile)
	}
	return nil
}

func (nd *nodeDir) configureOverrides(overrideJSON string) (err error) {
	if overrideJSON == "" {
		return
	}

	if err = nd.ensureConfig(); err != nil {
		return
	}

	reader := strings.NewReader(overrideJSON)
	dec := json.NewDecoder(reader)
	if err = dec.Decode(&nd.config); err != nil {
		return
	}

	fmt.Fprintf(os.Stdout, " - Merged config overrides: %s\n", overrideJSON)

	err = nd.saveConfig()
	return
}

func (nd *nodeDir) configureDNSBootstrap() (err error) {
	if err = nd.ensureConfig(); err != nil {
		return
	}

	if nd.config.DNSBootstrapID == config.GetDefaultLocal().DNSBootstrapID {
		// Ensure using our testing network without fallback support
		nd.config.DNSBootstrapID = "<network>.algodev.network"
		err = nd.saveConfig()
	}
	return
}