-
Notifications
You must be signed in to change notification settings - Fork 55
/
node.go
242 lines (222 loc) · 5.28 KB
/
node.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
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package commands
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"gopkg.in/yaml.v3"
"github.com/ava-labs/avalanche-cli/pkg/constants"
"github.com/onsi/gomega"
)
const (
e2eKeyPairName = "runner-avalanche-cli-keypair"
ExpectFail = false
ExpectSuccess = true
)
func NodeCreate(network, version string, numNodes int, separateMonitoring bool, numAPINodes int, expectSuccess bool) string {
home, err := os.UserHomeDir()
gomega.Expect(err).Should(gomega.BeNil())
_, err = os.Open(filepath.Join(home, ".ssh", e2eKeyPairName))
gomega.Expect(err).Should(gomega.BeNil())
_, err = os.Open(filepath.Join(home, ".ssh", e2eKeyPairName+".pub"))
gomega.Expect(err).Should(gomega.BeNil())
cmdVersion := "--latest-avalanchego-version=true"
if version != "latest" && version != "" {
cmdVersion = "--custom-avalanchego-version=" + version
}
cmdAPI := ""
if numAPINodes > 0 {
cmdAPI = "--num-apis=" + strconv.Itoa(numAPINodes)
}
/* #nosec G204 */
cmd := exec.Command(
CLIBinary,
"node",
"create",
constants.E2EClusterName,
"--use-static-ip=false",
cmdVersion,
"--enable-monitoring="+strconv.FormatBool(separateMonitoring),
"--region=local",
"--num-validators="+strconv.Itoa(numNodes),
"--"+network,
"--node-type=docker",
)
if cmdAPI != "" {
cmd.Args = append(cmd.Args, cmdAPI)
}
cmd.Env = os.Environ()
fmt.Println("About to run: " + cmd.String())
output, err := cmd.CombinedOutput()
fmt.Println("---------------->")
fmt.Println(string(output))
fmt.Println(err)
fmt.Println("---------------->")
if expectSuccess {
gomega.Expect(err).Should(gomega.BeNil())
} else {
gomega.Expect(err).Should(gomega.Not(gomega.BeNil()))
}
return string(output)
}
func NodeDevnet(numNodes int, numAPINodes int) string {
/* #nosec G204 */
cmd := exec.Command(
CLIBinary,
"node",
"create",
constants.E2EClusterName,
"--use-static-ip=false",
"--enable-monitoring=false",
"--latest-avalanchego-version=true",
"--region=local",
"--num-validators="+strconv.Itoa(numNodes),
"--num-apis="+strconv.Itoa(numAPINodes),
"--devnet",
"--node-type=docker",
)
return runCmd(cmd, ExpectSuccess)
}
func NodeStatus() string {
/* #nosec G204 */
cmd := exec.Command(
CLIBinary,
"node",
"status",
constants.E2EClusterName,
)
return runCmd(cmd, ExpectSuccess)
}
func NodeSSH(name, command string) string {
/* #nosec G204 */
cmd := exec.Command(
CLIBinary,
"node",
"ssh",
name,
command,
)
multilineText := runCmd(cmd, ExpectSuccess)
// filter out additional output
pattern := `\[Node docker.*?\(NodeID-[^\s]+\)`
re := regexp.MustCompile(pattern)
lines := strings.Split(multilineText, "\n")
var output []string
for _, line := range lines {
if re.MatchString(line) {
continue
} else {
output = append(output, line)
}
}
return strings.Join(output, "\n")
}
func ConfigMetrics() {
/* #nosec G204 */
cmd := exec.Command(
CLIBinary,
"config",
"metrics",
"disable",
)
_, err := cmd.Output()
gomega.Expect(err).Should(gomega.BeNil())
}
func NodeList() string {
/* #nosec G204 */
cmd := exec.Command(
CLIBinary,
"node",
"list",
)
return runCmd(cmd, ExpectSuccess)
}
func NodeWhitelistSSH(sshPubKey string) string {
/* #nosec G204 */
cmd := exec.Command(
CLIBinary,
"node",
"whitelist",
constants.E2EClusterName,
"--ssh",
"\""+sshPubKey+"\"",
)
return runCmd(cmd, ExpectSuccess)
}
func NodeUpgrade() string {
/* #nosec G204 */
cmd := exec.Command(
CLIBinary,
"node",
"upgrade",
constants.E2EClusterName,
)
return runCmd(cmd, ExpectSuccess)
}
func NodeExport(filename string, withSecrets bool) string {
/* #nosec G204 */
cmd := exec.Command(
CLIBinary,
"node",
"export",
constants.E2EClusterName,
"--file",
filename,
)
if withSecrets {
cmd.Args = append(cmd.Args, "--include-secrets")
}
return runCmd(cmd, ExpectSuccess)
}
func NodeImport(filename string, clusterName string) string {
/* #nosec G204 */
cmd := exec.Command(
CLIBinary,
"node",
"import",
clusterName,
"--file",
filename,
)
return runCmd(cmd, ExpectSuccess)
}
type StaticConfig struct {
Targets []string `yaml:"targets"`
}
type ScrapeConfig struct {
JobName string `yaml:"job_name"`
StaticConfigs []StaticConfig `yaml:"static_configs"`
}
type PrometheusConfig struct {
ScrapeConfigs []ScrapeConfig `yaml:"scrape_configs"`
}
// ParsePrometheusYamlConfig parses prometheus config YAML file installed in separate monitoring
// host in /etc/prometheus/prometheus.yml
func ParsePrometheusYamlConfig(filePath string) PrometheusConfig {
data, err := os.ReadFile(filePath)
gomega.Expect(err).Should(gomega.BeNil())
var prometheusConfig PrometheusConfig
err = yaml.Unmarshal(data, &prometheusConfig)
gomega.Expect(err).Should(gomega.BeNil())
return prometheusConfig
}
func runCmd(cmd *exec.Cmd, expectSuccess bool) string { //nolint:all
cmd.Env = os.Environ()
fmt.Println("About to run: " + cmd.String())
output, err := cmd.CombinedOutput()
fmt.Println("---------------->")
fmt.Println(string(output))
fmt.Println(err)
fmt.Println("---------------->")
if expectSuccess {
gomega.Expect(err).Should(gomega.BeNil())
} else {
gomega.Expect(err).Should(gomega.Not(gomega.BeNil()))
}
return string(output)
}