/
ha_node_add_onprem.go
270 lines (252 loc) · 8.71 KB
/
ha_node_add_onprem.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
package main
import (
"container/list"
"fmt"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/chef/automate/components/automate-cli/pkg/status"
cli "github.com/chef/automate/components/automate-deployment/pkg/cli"
"github.com/chef/automate/lib/io/fileutils"
"github.com/chef/automate/lib/stringutils"
"github.com/spf13/cobra"
)
const (
TYPE_ERROR = `Cannot %s OpenSearch or Postgresql nodes if external.database.type is either aws or self-managed.
Please set external.database.type to empty if you want to add OpenSearch or Postgresql nodes`
TYPE_AWS = "aws"
TYPE_SELF_MANAGED = "self-managed"
)
type AddDeleteNodeHACmdFlags struct {
automateIp string
chefServerIp string
opensearchIp string
postgresqlIp string
automateCount int
chefServerCount int
opensearchCount int
postgresqlCount int
onPremMode bool
awsMode bool
autoAccept bool
}
type AddNodeOnPremImpl struct {
config ExistingInfraConfigToml
copyConfigForUserPrompt ExistingInfraConfigToml
automateIpList []string
chefServerIpList []string
opensearchIpList []string
postgresqlIp []string
nodeUtils NodeOpUtils
flags AddDeleteNodeHACmdFlags
configpath string
terraformPath string
writer *cli.Writer
fileutils fileutils.FileUtils
sshUtil SSHUtil
}
func NewAddNodeOnPrem(writer *cli.Writer, flags AddDeleteNodeHACmdFlags, nodeUtils NodeOpUtils, haDirPath string, fileUtils fileutils.FileUtils, sshUtil SSHUtil) HAModifyAndDeploy {
return &AddNodeOnPremImpl{
flags: flags,
writer: writer,
nodeUtils: nodeUtils,
configpath: filepath.Join(haDirPath, "config.toml"),
terraformPath: filepath.Join(haDirPath, "terraform"),
fileutils: fileUtils,
sshUtil: sshUtil,
}
}
func (ani *AddNodeOnPremImpl) Execute(c *cobra.Command, args []string) error {
if !ani.nodeUtils.isA2HARBFileExist() {
return errors.New(AUTOMATE_HA_INVALID_BASTION)
}
if ani.flags.automateIp == "" &&
ani.flags.chefServerIp == "" &&
ani.flags.opensearchIp == "" &&
ani.flags.postgresqlIp == "" {
c.Help()
return status.New(status.InvalidCommandArgsError, "Please provide service name and ip address of the node which you want to add")
}
err := ani.validate()
if err != nil {
return err
}
err = ani.modifyConfig()
if err != nil {
return err
}
err = ani.nodeUtils.saveConfigToBastion()
if err != nil {
return err
}
if !ani.flags.autoAccept {
res, err := ani.promptUserConfirmation()
if err != nil {
return err
}
if !res {
return nil
}
}
ani.prepare()
return ani.runDeploy()
}
func (ani *AddNodeOnPremImpl) prepare() error {
return ani.nodeUtils.taintTerraform(ani.terraformPath)
}
func (ani *AddNodeOnPremImpl) validate() error {
updatedConfig, err := ani.nodeUtils.pullAndUpdateConfig(&ani.sshUtil, []string{})
if err != nil {
return err
}
ani.config = *updatedConfig
ani.copyConfigForUserPrompt = ani.config
ani.automateIpList, ani.chefServerIpList, ani.opensearchIpList, ani.postgresqlIp = splitIPCSV(
ani.flags.automateIp,
ani.flags.chefServerIp,
ani.flags.opensearchIp,
ani.flags.postgresqlIp,
)
if ani.nodeUtils.isManagedServicesOn() {
if len(ani.opensearchIpList) > 0 || len(ani.postgresqlIp) > 0 {
return status.New(status.ConfigError, fmt.Sprintf(TYPE_ERROR, "add"))
}
}
errorList := ani.validateCmdArgs()
if errorList != nil && errorList.Len() > 0 {
return status.Wrap(getSingleErrorFromList(errorList), status.ConfigError, "IP address validation failed")
}
return nil
}
func (ani *AddNodeOnPremImpl) modifyConfig() error {
err := modifyConfigForAddNewNode(
&ani.config.Automate.Config.InstanceCount,
&ani.config.ExistingInfra.Config.AutomatePrivateIps,
ani.automateIpList,
&ani.config.Automate.Config.CertsByIP,
)
if err != nil {
return status.Wrap(err, status.ConfigError, "Error modifying automate instance count")
}
err = modifyConfigForAddNewNode(
&ani.config.ChefServer.Config.InstanceCount,
&ani.config.ExistingInfra.Config.ChefServerPrivateIps,
ani.chefServerIpList,
&ani.config.ChefServer.Config.CertsByIP,
)
if err != nil {
return status.Wrap(err, status.ConfigError, "Error modifying chef-server instance count")
}
err = modifyConfigForAddNewNode(
&ani.config.Opensearch.Config.InstanceCount,
&ani.config.ExistingInfra.Config.OpensearchPrivateIps,
ani.opensearchIpList,
&ani.config.Opensearch.Config.CertsByIP,
)
if err != nil {
return status.Wrap(err, status.ConfigError, "Error modifying opensearch instance count")
}
err = modifyConfigForAddNewNode(
&ani.config.Postgresql.Config.InstanceCount,
&ani.config.ExistingInfra.Config.PostgresqlPrivateIps,
ani.postgresqlIp,
&ani.config.Postgresql.Config.CertsByIP,
)
if err != nil {
return status.Wrap(err, status.ConfigError, "Error modifying postgresql instance count")
}
return nil
}
func (ani *AddNodeOnPremImpl) promptUserConfirmation() (bool, error) {
ani.writer.Println("Existing nodes:")
ani.writer.Println("================================================")
ani.writer.Println("Automate => " + strings.Join(ani.copyConfigForUserPrompt.ExistingInfra.Config.AutomatePrivateIps, ", "))
ani.writer.Println("Chef-Server => " + strings.Join(ani.copyConfigForUserPrompt.ExistingInfra.Config.ChefServerPrivateIps, ", "))
ani.writer.Println("OpenSearch => " + strings.Join(ani.copyConfigForUserPrompt.ExistingInfra.Config.OpensearchPrivateIps, ", "))
ani.writer.Println("Postgresql => " + strings.Join(ani.copyConfigForUserPrompt.ExistingInfra.Config.PostgresqlPrivateIps, ", "))
ani.writer.Println("")
ani.writer.Println("New nodes to be added:")
ani.writer.Println("================================================")
if len(ani.automateIpList) > 0 {
ani.writer.Println("Automate => " + strings.Join(ani.automateIpList, ", "))
}
if len(ani.chefServerIpList) > 0 {
ani.writer.Println("Chef-Server => " + strings.Join(ani.chefServerIpList, ", "))
}
if len(ani.opensearchIpList) > 0 {
ani.writer.Println("OpenSearch => " + strings.Join(ani.opensearchIpList, ", "))
}
if len(ani.postgresqlIp) > 0 {
ani.writer.Println("Postgresql => " + strings.Join(ani.postgresqlIp, ", "))
}
return ani.writer.Confirm("This will add the new nodes to your existing setup. It might take a while. Are you sure you want to continue?")
}
func (ani *AddNodeOnPremImpl) runDeploy() error {
err := ani.nodeUtils.writeHAConfigFiles(existingNodesA2harbTemplate, ani.config, DEPLOY)
if err != nil {
return err
}
argsdeploy := []string{"-y"}
err = ani.nodeUtils.executeAutomateClusterCtlCommandAsync("deploy", argsdeploy, upgradeHaHelpDoc)
syncErr := ani.nodeUtils.syncConfigToAllNodes()
if syncErr != nil {
if err != nil {
return errors.Wrap(err, syncErr.Error())
}
return syncErr
}
return err
}
func (ani *AddNodeOnPremImpl) validateCmdArgs() *list.List {
errorList := list.New()
if len(ani.automateIpList) > 0 {
errorList.PushBackList(ani.validateIPAddresses(ani.automateIpList, "Automate"))
}
if len(ani.chefServerIpList) > 0 {
errorList.PushBackList(ani.validateIPAddresses(ani.chefServerIpList, "Chef-Server"))
}
if len(ani.opensearchIpList) > 0 {
errorList.PushBackList(ani.validateIPAddresses(ani.opensearchIpList, "OpenSearch"))
}
if len(ani.postgresqlIp) > 0 {
errorList.PushBackList(ani.validateIPAddresses(ani.postgresqlIp, "Postgresql"))
}
return errorList
}
func (ani *AddNodeOnPremImpl) validateIPAddresses(ips []string, errorPrefix string) *list.List {
errorList := list.New()
prefixAdder := ""
if errorPrefix != "" {
prefixAdder = " "
}
for _, ip := range ips {
if stringutils.SliceContains(ani.config.ExistingInfra.Config.AutomatePrivateIps, ip) ||
stringutils.SliceContains(ani.config.ExistingInfra.Config.ChefServerPrivateIps, ip) ||
stringutils.SliceContains(ani.config.ExistingInfra.Config.OpensearchPrivateIps, ip) ||
stringutils.SliceContains(ani.config.ExistingInfra.Config.PostgresqlPrivateIps, ip) {
errorList.PushBack(fmt.Sprintf("%s%sIp %s is already configured for a node. Please use a different private ip.", errorPrefix, prefixAdder, ip))
}
err := checkIPAddress(ip)
if err != nil {
errorList.PushBack(fmt.Sprintf("Incorrect %s IP address format for ip %s", errorPrefix, ip))
}
err = ani.validateConnection(ip)
if err != nil {
errorList.PushBack(fmt.Sprintf("%s IP address %s is unreachable", errorPrefix, ip))
}
}
return errorList
}
func (ani *AddNodeOnPremImpl) validateConnection(ip string) error {
_, sshConfig, err := ani.nodeUtils.getHaInfraDetails()
if err != nil {
return err
}
sshConfig.hostIP = ip
ani.sshUtil.setSSHConfig(sshConfig)
_, err = ani.sshUtil.connectAndExecuteCommandOnRemote("sudo echo 1", true)
if err != nil {
return err
}
return nil
}