This repository has been archived by the owner on Jul 29, 2022. It is now read-only.
/
cluster.go
165 lines (140 loc) · 4.13 KB
/
cluster.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
package terraform
import (
"os"
"github.com/mattermost/mattermost-load-test/ltops"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type Cluster struct {
Config *ltops.ClusterConfig
SSHPrivateKeyPEM []byte
DBPassword string
Env *TerraformEnvironment
}
func (c *Cluster) Name() string {
return c.Config.Name
}
func (c *Cluster) Type() string {
return c.Config.Type
}
func (c *Cluster) Configuration() *ltops.ClusterConfig {
return c.Config
}
func (c *Cluster) SSHKey() []byte {
return c.SSHPrivateKeyPEM
}
func (c *Cluster) SiteURL() string {
addrs, err := c.GetProxyInstancesAddrs()
if err != nil || len(addrs) < 1 {
logrus.Error("SiteURL: Unable to get proxy instance addresses.")
return ""
}
return "http://" + addrs[0]
}
func (c *Cluster) GetAppInstancesAddrs() ([]string, error) {
params, err := c.Env.getOutputParams()
if err != nil {
return nil, err
}
instanceIps := params.InstanceIp.Value
return instanceIps, nil
}
func (c *Cluster) GetLoadtestInstancesAddrs() ([]string, error) {
params, err := c.Env.getOutputParams()
if err != nil {
return nil, err
}
instanceIps := params.LoadtestInstanceIp.Value
return instanceIps, nil
}
func (c *Cluster) GetProxyInstancesAddrs() ([]string, error) {
params, err := c.Env.getOutputParams()
if err != nil {
return nil, err
}
instanceIps := params.ProxyIp.Value
return instanceIps, nil
}
func (c *Cluster) GetMetricsAddr() (string, error) {
params, err := c.Env.getOutputParams()
if err != nil {
return "", err
}
return params.MetricsIp.Value, nil
}
func (c *Cluster) DBDriverName() string {
switch c.Config.DBEngineType {
case "aurora-postgresql":
return "postgres"
case "aurora", "aurora-mysql":
return "mysql"
default:
logrus.Errorf("Unable to get db driver name, invalid db-engine-type %v", c.Config.DBEngineType)
return ""
}
}
func (c *Cluster) DBConnectionString() string {
params, err := c.Env.getOutputParams()
if err != nil {
logrus.Error("Unable to get output parameters for DBConnectionString")
return ""
}
databaseEndpoint := params.DBEndpoint.Value
switch c.Config.DBEngineType {
case "aurora-postgresql":
return "postgres://mmuser:" + c.DBPassword + "@" + databaseEndpoint + ":5432/mattermost?sslmode=disable\u0026connect_timeout=30"
case "aurora", "aurora-mysql":
return "mmuser:" + c.DBPassword + "@tcp(" + databaseEndpoint + ":3306)/mattermost?charset=utf8mb4,utf8&readTimeout=20s&writeTimeout=20s&timeout=20s"
default:
logrus.Errorf("Unable to get endpoint, invalid db-engine-type %v", c.Config.DBEngineType)
return ""
}
}
func (c *Cluster) DBReaderConnectionStrings() []string {
params, err := c.Env.getOutputParams()
if err != nil {
logrus.Error("Unable to get output parameters for DBConnectionString")
return nil
}
databaseEndpoint := params.DBReaderEndpoint.Value
switch c.Config.DBEngineType {
case "aurora-postgresql":
return []string{"postgres://mmuser:" + c.DBPassword + "@" + databaseEndpoint + ":5432/mattermost?sslmode=disable\u0026connect_timeout=30"}
case "aurora", "aurora-mysql":
return []string{"mmuser:" + c.DBPassword + "@tcp(" + databaseEndpoint + ":3306)/mattermost?charset=utf8mb4,utf8&readTimeout=20s&writeTimeout=20s&timeout=20s"}
default:
logrus.Errorf("Unable to get endpoint, invalid db-engine-type %v", c.Config.DBEngineType)
return nil
}
}
func (c *Cluster) DBInstanceCount() int {
return c.Config.DBInstanceCount
}
func (c *Cluster) DBSettings() (*ltops.DBSettings, error) {
params, err := c.Env.getOutputParams()
if err != nil {
return nil, errors.Wrap(err, "unable to get output parameters for DBConnectionString")
}
var port int
switch c.Config.DBEngineType {
case "aurora-postgresql":
port = 5432
default:
port = 3306
}
return <ops.DBSettings{
Username: "mmuser",
Password: c.DBPassword,
Endpoint: params.DBEndpoint.Value,
Port: port,
Database: "mattermost",
}, nil
}
func (c *Cluster) Destroy() error {
logrus.Info("Destroying cluster...")
if err := c.Env.destroy(); err != nil {
return errors.Wrap(err, "Unable to destroy terraform cluster.")
}
logrus.Info("Cleaning up files...")
return os.RemoveAll(c.Env.WorkingDirectory)
}