-
Notifications
You must be signed in to change notification settings - Fork 19
/
visor_config.go
407 lines (347 loc) · 10.6 KB
/
visor_config.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program 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.
//
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
package config
import (
"context"
"fmt"
"path"
"runtime"
"sync"
"time"
"code.vegaprotocol.io/vega/logging"
"code.vegaprotocol.io/vega/paths"
"github.com/fsnotify/fsnotify"
"golang.org/x/sync/errgroup"
)
const (
currentFolder = "current"
genesisFolder = "genesis"
configFileName = "config.toml"
RunConfigFileName = "run-config.toml"
VegaBinaryName = "vega"
DataNodeBinaryName = "data-node"
)
/*
description: Defines the name of the asset to be downloaded.
*/
type AssetsConfig struct {
// description: Name of the asset file on Github.
Name string `toml:"name"`
/*
description: |
Name of the binary if the asset is a zip file and the binary is included inside of it.
*/
BinaryName *string `toml:"binaryName"`
}
func (a AssetsConfig) GetBinaryPath() string {
if a.BinaryName != nil {
return *a.BinaryName
}
return a.Name
}
/*
description: Determines if the assets should be automatically downloaded and installed. If so this defines the assets that should be downloaded from GitHub for a specific release.
example:
type: toml
value: |
[autoInstall]
enabled = true
repositoryOwner = "vegaprotocol"
repository = "vega"
[autoInstall.asset]
name = "vega-darwin-amd64.zip"
binaryName = "vega"
*/
type AutoInstallConfig struct {
/*
description: Auto Install flag
default: true
*/
Enabled bool `toml:"enabled"`
/*
description: Owner of the repository from where the assets should be downloaded.
default: vegaprotocol
*/
GithubRepositoryOwner string `toml:"repositoryOwner"`
/*
description: Name of the repository from where the assets should be downloaded.
default: vega
*/
GithubRepository string `toml:"repository"`
/*
description: |
Definition of the asset that should be downloaded from the GitHub repository.
If the asset is contained in a zip file, the name of the binary is given.
example:
type: toml
value: |
[autoInstall.asset]
name = "vega-darwin-amd64.zip"
binaryName = "vega"
*/
Asset AssetsConfig `toml:"asset"`
}
/*
description: Root of the config file
example:
type: toml
value: |
maxNumberOfRestarts = 3
restartsDelaySeconds = 5
[upgradeFolders]
"vX.X.X" = "vX.X.X"
[autoInstall]
enabled = false
*/
type VisorConfigFile struct {
/*
description: |
Visor communicates with the core node via RPC API.
This variable allows a validator to specify how many times Visor should try to establish a connection to the core node before the Visor process fails.
The `maxNumberOfFirstConnectionRetries` is only taken into account during the first start up of the Core node process - not restarts.
note: |
There is a 2 second delay between each attempt. Setting the max retry number to 5 means Visor will try to establish a connection 5 times in 10 seconds.
default: 10
*/
MaxNumberOfFirstConnectionRetries int `toml:"maxNumberOfFirstConnectionRetries,optional"`
/*
description: |
Defines the maximum number of restarts in case any of
the processes have failed before the Visor process fails.
note: |
The amount of time Visor waits between restarts can be set by `restartsDelaySeconds`.
default: 3
*/
MaxNumberOfRestarts int `toml:"maxNumberOfRestarts,optional"`
/*
description: |
Number of seconds that Visor waits before it tries to re-start the processes.
default: 5
*/
RestartsDelaySeconds int `toml:"restartsDelaySeconds,optional"`
/*
description: |
Number of seconds that Visor waits before it sends a termination signal (SIGTERM) to running processes
that are ready for upgrade.
After the time has elapsed Visor stops the running binaries (SIGTERM).
default: 0
*/
StopDelaySeconds int `toml:"stopDelaySeconds,optional"`
/*
description: |
Number of seconds that Visor waits after it sends termination signal (SIGTERM) to running processes.
After the time has elapsed Visor force-kills (SIGKILL) any running processes.
default: 15
*/
StopSignalTimeoutSeconds int `toml:"stopSignalTimeoutSeconds,optional"`
/*
description: |
During the upgrade, by default Visor looks for a folder with a name identical to the upgrade version.
The default behaviour can be changed by providing mapping between `version` and `custom_folder_name`.
If a custom mapping is provided, during the upgrade Visor uses the folder given in the mapping for specific version.
example:
type: toml
value: |
[upgradeFolders]
"v99.9.9" = "custom_upgrade_folder_name"
*/
UpgradeFolders map[string]string `toml:"upgradeFolders,optional"`
/*
description: |
Defines the assets that should be automatically downloaded from Github for a specific release.
example:
type: toml
value: |
[autoInstall]
enabled = true
repositoryOwner = "vegaprotocol"
repository = "vega"
[autoInstall.assets]
[autoInstall.assets.vega]
asset_name = "vega-darwin-amd64.zip"
binary_name = "vega"
*/
AutoInstall AutoInstallConfig `toml:"autoInstall"`
}
func parseAndValidateVisorConfigFile(path string) (*VisorConfigFile, error) {
conf := VisorConfigFile{}
if err := paths.ReadStructuredFile(path, &conf); err != nil {
// Do not wrap error as underlying errors are meaningful enough.
return nil, err
}
return &conf, nil
}
type VisorConfig struct {
mut sync.RWMutex
configPath string
homePath string
data *VisorConfigFile
log *logging.Logger
}
func DefaultVisorConfig(log *logging.Logger, homePath string) *VisorConfig {
return &VisorConfig{
log: log,
homePath: homePath,
configPath: path.Join(homePath, configFileName),
data: &VisorConfigFile{
UpgradeFolders: map[string]string{"vX.X.X": "vX.X.X"},
MaxNumberOfRestarts: 3,
MaxNumberOfFirstConnectionRetries: 175000,
RestartsDelaySeconds: 5,
StopDelaySeconds: 0,
StopSignalTimeoutSeconds: 15,
AutoInstall: AutoInstallConfig{
Enabled: true,
GithubRepositoryOwner: "vegaprotocol",
GithubRepository: "vega",
Asset: AssetsConfig{
Name: fmt.Sprintf("vega-%s-%s.zip", runtime.GOOS, "amd64"),
BinaryName: toPointer("vega"),
},
},
},
}
}
func NewVisorConfig(log *logging.Logger, homePath string) (*VisorConfig, error) {
configPath := path.Join(homePath, configFileName)
dataFile, err := parseAndValidateVisorConfigFile(configPath)
if err != nil {
return nil, fmt.Errorf("could not retrieve configuration at %q: %w", configPath, err)
}
return &VisorConfig{
configPath: configPath,
homePath: homePath,
data: dataFile,
log: log,
}, nil
}
func (pc *VisorConfig) reload() error {
pc.log.Info("Reloading config")
dataFile, err := parseAndValidateVisorConfigFile(pc.configPath)
if err != nil {
return fmt.Errorf("could not retrieve configuration: %w", err)
}
pc.mut.Lock()
pc.data.MaxNumberOfFirstConnectionRetries = dataFile.MaxNumberOfFirstConnectionRetries
pc.data.MaxNumberOfRestarts = dataFile.MaxNumberOfRestarts
pc.data.RestartsDelaySeconds = dataFile.RestartsDelaySeconds
pc.data.StopSignalTimeoutSeconds = dataFile.StopSignalTimeoutSeconds
pc.data.StopDelaySeconds = dataFile.StopDelaySeconds
pc.data.UpgradeFolders = dataFile.UpgradeFolders
pc.data.AutoInstall = dataFile.AutoInstall
pc.mut.Unlock()
pc.log.Info("Reloading config success")
return nil
}
func (pc *VisorConfig) WatchForUpdate(ctx context.Context) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
var eg errgroup.Group
eg.Go(func() error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case event, ok := <-watcher.Events:
if !ok {
return nil
}
if event.Has(fsnotify.Write) {
// add a small sleep here in order to handle vi
// vi do not send a write event / edit the file in place,
// it always create a temporary file, then delete the original one,
// and then rename the temp file with the name of the original file.
// if we try to update the conf as soon as we get the event, the file is not
// always created and we get a no such file or directory error
time.Sleep(50 * time.Millisecond)
pc.reload()
}
case err, ok := <-watcher.Errors:
if !ok {
return nil
}
return err
}
}
})
if err := watcher.Add(pc.configPath); err != nil {
return err
}
return eg.Wait()
}
func (pc *VisorConfig) CurrentFolder() string {
pc.mut.RLock()
defer pc.mut.RUnlock()
return path.Join(pc.homePath, currentFolder)
}
func (pc *VisorConfig) CurrentRunConfigPath() string {
pc.mut.RLock()
defer pc.mut.RUnlock()
return path.Join(pc.CurrentFolder(), RunConfigFileName)
}
func (pc *VisorConfig) GenesisFolder() string {
pc.mut.RLock()
defer pc.mut.RUnlock()
return path.Join(pc.homePath, genesisFolder)
}
func (pc *VisorConfig) UpgradeFolder(releaseTag string) string {
pc.mut.RLock()
defer pc.mut.RUnlock()
if folderName, ok := pc.data.UpgradeFolders[releaseTag]; ok {
return path.Join(pc.homePath, folderName)
}
return path.Join(pc.homePath, releaseTag)
}
func (pc *VisorConfig) MaxNumberOfRestarts() int {
pc.mut.RLock()
defer pc.mut.RUnlock()
return pc.data.MaxNumberOfRestarts
}
func (pc *VisorConfig) MaxNumberOfFirstConnectionRetries() int {
pc.mut.RLock()
defer pc.mut.RUnlock()
return pc.data.MaxNumberOfFirstConnectionRetries
}
func (pc *VisorConfig) RestartsDelaySeconds() int {
pc.mut.RLock()
defer pc.mut.RUnlock()
return pc.data.RestartsDelaySeconds
}
func (pc *VisorConfig) StopSignalTimeoutSeconds() int {
pc.mut.RLock()
defer pc.mut.RUnlock()
return pc.data.StopSignalTimeoutSeconds
}
func (pc *VisorConfig) StopDelaySeconds() int {
pc.mut.RLock()
defer pc.mut.RUnlock()
return pc.data.StopDelaySeconds
}
func (pc *VisorConfig) AutoInstall() AutoInstallConfig {
pc.mut.RLock()
defer pc.mut.RUnlock()
return pc.data.AutoInstall
}
func (pc *VisorConfig) WriteToFile() error {
pc.mut.RLock()
defer pc.mut.RUnlock()
return paths.WriteStructuredFile(pc.configPath, pc.data)
}
func toPointer[T any](val T) *T {
return &val
}