-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathssh.go
147 lines (134 loc) · 5.97 KB
/
ssh.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
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package docker
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/ava-labs/avalanche-cli/pkg/constants"
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/remoteconfig"
"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanche-cli/pkg/ux"
)
// ValidateComposeFile validates a docker-compose file on a remote host.
func ValidateComposeFile(host *models.Host, composeFile string, timeout time.Duration) error {
if output, err := host.Command(fmt.Sprintf("docker compose -f %s config", composeFile), nil, timeout); err != nil {
return fmt.Errorf("%w: %s", err, string(output))
}
return nil
}
// ComposeSSHSetupNode sets up an AvalancheGo node and dependencies on a remote host over SSH.
func ComposeSSHSetupNode(host *models.Host, network models.Network, avalancheGoVersion string, withMonitoring bool, publicAccessToHTTPPort bool) error {
startTime := time.Now()
folderStructure := remoteconfig.RemoteFoldersToCreateAvalanchego()
for _, dir := range folderStructure {
if err := host.MkdirAll(dir, constants.SSHFileOpsTimeout); err != nil {
return fmt.Errorf("failed to create directory %s: %w", dir, err)
}
}
ux.Logger.Info("avalancheCLI folder structure created on remote host %s after %s", folderStructure, time.Since(startTime))
avagoDockerImage := fmt.Sprintf("%s:%s", constants.AvalancheGoDockerImage, avalancheGoVersion)
ux.Logger.Info("Preparing AvalancheGo Docker image %s on %s[%s]", avagoDockerImage, host.NodeID, host.IP)
if err := PrepareDockerImageWithRepo(host, avagoDockerImage, constants.AvalancheGoGitRepo, avalancheGoVersion); err != nil {
return err
}
ux.Logger.Info("AvalancheGo Docker image %s ready on %s[%s] after %s", avagoDockerImage, host.NodeID, host.IP, time.Since(startTime))
nodeConfFile, cChainConfFile, err := prepareAvalanchegoConfig(host, network, publicAccessToHTTPPort)
if err != nil {
return err
}
defer func() {
if err := os.Remove(nodeConfFile); err != nil {
ux.Logger.Error("Error removing temporary file %s: %s", nodeConfFile, err)
}
if err := os.Remove(cChainConfFile); err != nil {
ux.Logger.Error("Error removing temporary file %s: %s", cChainConfFile, err)
}
}()
if err := host.Upload(nodeConfFile, remoteconfig.GetRemoteAvalancheNodeConfig(), constants.SSHFileOpsTimeout); err != nil {
return err
}
if err := host.Upload(cChainConfFile, remoteconfig.GetRemoteAvalancheCChainConfig(), constants.SSHFileOpsTimeout); err != nil {
return err
}
ux.Logger.Info("AvalancheGo configs uploaded to %s[%s] after %s", host.NodeID, host.IP, time.Since(startTime))
return ComposeOverSSH("Compose Node",
host,
constants.SSHScriptTimeout,
"templates/avalanchego.docker-compose.yml",
dockerComposeInputs{
AvalanchegoVersion: avalancheGoVersion,
WithMonitoring: withMonitoring,
WithAvalanchego: true,
E2E: utils.IsE2E(),
E2EIP: utils.E2EConvertIP(host.IP),
E2ESuffix: utils.E2ESuffix(host.IP),
})
}
func ComposeSSHSetupLoadTest(host *models.Host) error {
return ComposeOverSSH("Compose Node",
host,
constants.SSHScriptTimeout,
"templates/avalanchego.docker-compose.yml",
dockerComposeInputs{
WithMonitoring: true,
WithAvalanchego: false,
})
}
// WasNodeSetupWithMonitoring checks if an AvalancheGo node was setup with monitoring on a remote host.
func WasNodeSetupWithMonitoring(host *models.Host) (bool, error) {
return HasRemoteComposeService(host, utils.GetRemoteComposeFile(), "promtail", constants.SSHScriptTimeout)
}
// ComposeSSHSetupMonitoring sets up monitoring using docker-compose.
func ComposeSSHSetupMonitoring(host *models.Host) error {
grafanaConfigFile, grafanaDashboardsFile, grafanaLokiDatasourceFile, grafanaPromDatasourceFile, err := prepareGrafanaConfig()
if err != nil {
return err
}
defer func() {
if err := os.Remove(grafanaLokiDatasourceFile); err != nil {
ux.Logger.Error("Error removing temporary file %s: %s", grafanaLokiDatasourceFile, err)
}
if err := os.Remove(grafanaPromDatasourceFile); err != nil {
ux.Logger.Error("Error removing temporary file %s: %s", grafanaPromDatasourceFile, err)
}
if err := os.Remove(grafanaDashboardsFile); err != nil {
ux.Logger.Error("Error removing temporary file %s: %s", grafanaDashboardsFile, err)
}
if err := os.Remove(grafanaConfigFile); err != nil {
ux.Logger.Error("Error removing temporary file %s: %s", grafanaConfigFile, err)
}
}()
grafanaLokiDatasourceRemoteFileName := filepath.Join(utils.GetRemoteComposeServicePath("grafana", "provisioning", "datasources"), "loki.yml")
if err := host.Upload(grafanaLokiDatasourceFile, grafanaLokiDatasourceRemoteFileName, constants.SSHFileOpsTimeout); err != nil {
return err
}
grafanaPromDatasourceFileName := filepath.Join(utils.GetRemoteComposeServicePath("grafana", "provisioning", "datasources"), "prometheus.yml")
if err := host.Upload(grafanaPromDatasourceFile, grafanaPromDatasourceFileName, constants.SSHFileOpsTimeout); err != nil {
return err
}
grafanaDashboardsRemoteFileName := filepath.Join(utils.GetRemoteComposeServicePath("grafana", "provisioning", "dashboards"), "dashboards.yml")
if err := host.Upload(grafanaDashboardsFile, grafanaDashboardsRemoteFileName, constants.SSHFileOpsTimeout); err != nil {
return err
}
grafanaConfigRemoteFileName := filepath.Join(utils.GetRemoteComposeServicePath("grafana"), "grafana.ini")
if err := host.Upload(grafanaConfigFile, grafanaConfigRemoteFileName, constants.SSHFileOpsTimeout); err != nil {
return err
}
return ComposeOverSSH("Setup Monitoring",
host,
constants.SSHScriptTimeout,
"templates/monitoring.docker-compose.yml",
dockerComposeInputs{})
}
func ComposeSSHSetupAWMRelayer(host *models.Host, relayerVersion string) error {
return ComposeOverSSH("Setup AWM Relayer",
host,
constants.SSHScriptTimeout,
"templates/awmrelayer.docker-compose.yml",
dockerComposeInputs{
AWMRelayerVersion: relayerVersion,
})
}