-
Notifications
You must be signed in to change notification settings - Fork 19
/
visor_folders.go
133 lines (105 loc) · 4.01 KB
/
visor_folders.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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package visor
import (
"context"
"fmt"
"os"
"path"
"code.vegaprotocol.io/vega/logging"
"code.vegaprotocol.io/vega/visor/config"
"code.vegaprotocol.io/vega/visor/github"
"code.vegaprotocol.io/vega/visor/utils"
)
var vegaDataNodeStartCmdArgs = []string{"datanode", "start"}
func (v *Visor) setCurrentFolder(sourceFolder, currentFolder string) error {
v.log.Info("Setting current folder",
logging.String("sourceFolder", sourceFolder),
logging.String("currentFolder", currentFolder),
)
runConfPath := path.Join(sourceFolder, config.RunConfigFileName)
runConfExists, err := utils.PathExists(runConfPath)
if err != nil {
return err
}
if !runConfExists {
return fmt.Errorf("missing run config in %q folder", runConfPath)
}
if err := os.RemoveAll(currentFolder); err != nil {
return fmt.Errorf("failed to remove current folder: %w", err)
}
if err := os.Symlink(sourceFolder, currentFolder); err != nil {
return fmt.Errorf("failed to set current folder as current: %w", err)
}
return nil
}
func (v *Visor) installUpgradeFolder(ctx context.Context, folder, releaseTag string, conf config.AutoInstallConfig) error {
v.log.Info("Automatically installing upgrade folder")
runConf, err := config.ParseRunConfig(v.conf.CurrentRunConfigPath())
if err != nil {
return err
}
if conf.Asset.Name == "" {
return missingAutoInstallAssetError("vega")
}
if err := os.MkdirAll(folder, 0o755); err != nil {
return fmt.Errorf("failed to create upgrade folder %q, %w", folder, err)
}
assetsFetcher := github.NewAssetsFetcher(
conf.GithubRepositoryOwner,
conf.GithubRepository,
[]string{conf.Asset.Name},
)
v.log.Info("Downloading asset from Github", logging.String("asset", conf.Asset.Name))
if err := assetsFetcher.Download(ctx, releaseTag, folder); err != nil {
return fmt.Errorf("failed to download release assets for tag %q: %w", releaseTag, err)
}
runConf.Name = releaseTag
runConf.Vega.Binary.Path = conf.Asset.GetBinaryPath()
if runConf.DataNode != nil {
runConf.DataNode.Binary.Path = conf.Asset.GetBinaryPath()
if len(runConf.DataNode.Binary.Args) != 0 && runConf.DataNode.Binary.Args[0] != vegaDataNodeStartCmdArgs[0] {
runConf.DataNode.Binary.Args = append(vegaDataNodeStartCmdArgs, runConf.DataNode.Binary.Args[1:]...)
}
}
runConfPath := path.Join(folder, config.RunConfigFileName)
if err := runConf.WriteToFile(runConfPath); err != nil {
return fmt.Errorf("failed to create run config %q: %w", runConfPath, err)
}
return nil
}
func (v *Visor) prepareNextUpgradeFolder(ctx context.Context, releaseTag string) error {
v.log.Debug("preparing next upgrade folder",
logging.String("vegaTagVersion", releaseTag),
)
upgradeFolder := v.conf.UpgradeFolder(releaseTag)
upgradeFolderExists, err := utils.PathExists(upgradeFolder)
if err != nil {
return err
}
if !upgradeFolderExists {
autoInstallConf := v.conf.AutoInstall()
if !autoInstallConf.Enabled {
return fmt.Errorf("required upgrade folder %q is missing", upgradeFolder)
}
if err := v.installUpgradeFolder(ctx, upgradeFolder, releaseTag, autoInstallConf); err != nil {
return fmt.Errorf("failed to auto install folder %q for release %q: %w", upgradeFolder, releaseTag, err)
}
}
if err := v.setCurrentFolder(upgradeFolder, v.conf.CurrentFolder()); err != nil {
return fmt.Errorf("failed to set current folder to %q: %w", v.conf.CurrentFolder(), err)
}
return nil
}
func missingAutoInstallAssetError(asset string) error {
return fmt.Errorf("missing required auto install %s asset definition in Visor config", asset)
}