-
Notifications
You must be signed in to change notification settings - Fork 111
/
stop.go
306 lines (270 loc) · 9.93 KB
/
stop.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright © 2017 Chef Software
package main
import (
"container/list"
"context"
"fmt"
"os"
"os/exec"
"github.com/pkg/errors"
"github.com/spf13/cobra"
api "github.com/chef/automate/api/interservice/deployment"
"github.com/chef/automate/components/automate-cli/pkg/docs"
"github.com/chef/automate/components/automate-cli/pkg/status"
"github.com/chef/automate/components/automate-deployment/pkg/client"
)
const (
ERROR_ON_MANAGED_SERVICE_STOP = "Stopping the service for externally configured %s is not supported"
)
var stopCmdFlags = struct {
automate bool
chef_server bool
opensearch bool
postgresql bool
node string
}{}
func init() {
var stopCmd = &cobra.Command{
Use: "stop",
Short: "Stop deployment",
Long: "Stop a running deployment of Automate.",
RunE: runStopCmd,
Annotations: map[string]string{
docs.Tag: docs.BastionHost,
},
}
stopCmd.PersistentFlags().BoolVarP(&stopCmdFlags.automate, "automate", "a", false, "Stop chef automate services of automate nodes")
stopCmd.PersistentFlags().SetAnnotation("automate", docs.Compatibility, []string{docs.CompatiblewithHA})
stopCmd.PersistentFlags().BoolVar(&stopCmdFlags.automate, "a2", false, "Stop chef automate services of automate nodes")
stopCmd.PersistentFlags().SetAnnotation("a2", docs.Compatibility, []string{docs.CompatiblewithHA})
stopCmd.PersistentFlags().BoolVarP(&stopCmdFlags.chef_server, "chef_server", "c", false, "Stop chef automate services of chef_server nodes")
stopCmd.PersistentFlags().SetAnnotation("chef_server", docs.Compatibility, []string{docs.CompatiblewithHA})
stopCmd.PersistentFlags().BoolVar(&stopCmdFlags.chef_server, "cs", false, "Stop chef automate services of chef_server nodes")
stopCmd.PersistentFlags().SetAnnotation("cs", docs.Compatibility, []string{docs.CompatiblewithHA})
stopCmd.PersistentFlags().BoolVarP(&stopCmdFlags.opensearch, "opensearch", "o", false, "Stop hab-sup service of opensearch nodes")
stopCmd.PersistentFlags().SetAnnotation("opensearch", docs.Compatibility, []string{docs.CompatiblewithHA})
stopCmd.PersistentFlags().BoolVar(&stopCmdFlags.opensearch, "os", false, "Stop hab-sup service of opensearch nodes")
stopCmd.PersistentFlags().SetAnnotation("os", docs.Compatibility, []string{docs.CompatiblewithHA})
stopCmd.PersistentFlags().BoolVarP(&stopCmdFlags.postgresql, "postgresql", "p", false, "Stop hab-sup service of postgresql nodes")
stopCmd.PersistentFlags().SetAnnotation("postgresql", docs.Compatibility, []string{docs.CompatiblewithHA})
stopCmd.PersistentFlags().BoolVar(&stopCmdFlags.postgresql, "pg", false, "Stop hab-sup service of postgresql nodes")
stopCmd.PersistentFlags().SetAnnotation("pg", docs.Compatibility, []string{docs.CompatiblewithHA})
stopCmd.PersistentFlags().StringVar(&stopCmdFlags.node, "node", "", "Node Ip address")
stopCmd.PersistentFlags().SetAnnotation("node", docs.Compatibility, []string{docs.CompatiblewithHA})
RootCmd.AddCommand(stopCmd)
}
func runStopCmd(cmd *cobra.Command, args []string) error {
if isA2HARBFileExist() {
if err := isFlagEnabled(cmd); err != nil {
return err
}
infra, err := getAutomateHAInfraDetails()
if err != nil {
return err
}
if err = runStopCommandHA(infra, isManagedServicesOn()); err != nil {
return err
}
} else if isDevMode() {
if err := runStopDevMode(); err != nil {
return err
}
} else {
if err := runStopStandalone(); err != nil {
return err
}
}
return nil
}
func runStopStandalone() error {
writer.Title("Stopping Chef Automate")
systemctlCmd := exec.Command("systemctl", "stop", "chef-automate.service")
systemctlCmd.Stdout = os.Stdout
systemctlCmd.Stderr = os.Stderr
if err := systemctlCmd.Run(); err != nil {
return status.Annotate(err, status.ServiceUnloadError)
}
writer.Title("Chef Automate Stopped")
return nil
}
func runStopDevMode() error {
writer.Title("Stopping Chef Automate")
connection, err := client.Connection(client.DefaultClientTimeout)
if err != nil {
return err
}
_, err = connection.Stop(context.Background(), &api.StopRequest{})
if err != nil {
return status.Wrap(
err,
status.DeploymentServiceCallError,
"Request to stop services failed",
)
}
writer.Title("Chef Automate Stopped")
return nil
}
func runStopCommandHA(infra *AutomateHAInfraDetails, isManagedServices bool) error {
errorList := list.New()
count := getFlagCount()
if count > 1 && stopCmdFlags.node != "" {
return errors.New("Please remove node flag if you have given multiple service flags.")
}
if stopCmdFlags.automate {
getAndStopHaNodes(AUTOMATE, infra, errorList)
}
if stopCmdFlags.chef_server {
getAndStopHaNodes(CHEF_SERVER, infra, errorList)
}
if stopCmdFlags.opensearch {
if isManagedServices {
return status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICE_STOP, OPENSEARCH)
}
getAndStopHaNodes(OPENSEARCH, infra, errorList)
}
if stopCmdFlags.postgresql {
if isManagedServices {
return status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICE_STOP, POSTGRESQL)
}
getAndStopHaNodes(POSTGRESQL, infra, errorList)
}
if errorList.Len() > 0 {
return status.New(status.ServiceUnloadError, getSingleErrorFromList(errorList).Error())
}
return nil
}
func getAndStopHaNodes(remoteService string, infra *AutomateHAInfraDetails, errorList *list.List) {
sshConfig := getSshDetails(infra)
var ips []string
if stopCmdFlags.node != "" {
isValid := validateEachIp(remoteService, infra, stopCmdFlags.node)
if !isValid {
errorList.PushBack(fmt.Sprintf("Please Enter Valid %s IP", remoteService))
return
}
ips = append(ips, stopCmdFlags.node)
} else {
ips = getIps(remoteService, infra)
}
stopCommandImplHA(*sshConfig, ips, remoteService, errorList)
}
func stopCommandImplHA(sshConfig SSHConfig, ips []string, remoteService string, errorList *list.List) {
sshUtilMap := getMapSSHUtils(ips, &sshConfig)
err := checkNodeType(sshUtilMap, ips, remoteService)
if err != nil {
errorList.PushBack(err.Error())
}
}
func checkNodeType(sshUtilMap map[string]SSHUtil, ips []string, remoteService string) error {
if len(ips) == 0 {
writer.Errorf("No %s IPs are found", remoteService)
return status.Errorf(1, "No %s IPs are found", remoteService)
}
if remoteService == OPENSEARCH || remoteService == POSTGRESQL {
return stopBackEndNodes(sshUtilMap, ips, remoteService)
}
return stopFrontEndNodes(sshUtilMap, ips, remoteService)
}
func stopFrontEndNodes(sshUtilMap map[string]SSHUtil, ips []string, remoteService string) error {
resultChan := make(chan Result, len(ips))
errorList := list.New()
scriptCommands := CHEF_AUTOMATE_STOP_FE_CMD
for _, hostIP := range ips {
sshUtil := sshUtilMap[hostIP]
go commandExecuteFrontEnd(scriptCommands, sshUtil, resultChan)
}
printMessageForStopResultChan(resultChan, ips, remoteService, errorList)
close(resultChan)
if errorList != nil && errorList.Len() > 0 {
return status.Wrapf(getSingleErrorFromList(errorList), status.ServiceUnloadError, "Not able to stop one or more nodes in %s", remoteService)
}
return nil
}
func stopBackEndNodes(sshUtilMap map[string]SSHUtil, ips []string, remoteService string) error {
resultChan := make(chan Result, len(ips))
scriptCommands := STOP_BE_SERVICES_CMD
errorList := list.New()
for _, hostIP := range ips {
sshUtil := sshUtilMap[hostIP]
go commandExecuteBackendNode(scriptCommands, sshUtil, resultChan)
}
printMessageForStopResultChan(resultChan, ips, remoteService, errorList)
close(resultChan)
if errorList != nil && errorList.Len() > 0 {
return status.Wrapf(getSingleErrorFromList(errorList), status.ServiceUnloadError, "Not able to stop one or more nodes in %s", remoteService)
}
return nil
}
func printMessageForStopResultChan(resultChan chan Result, ips []string, remoteService string, errorList *list.List) {
for i := 0; i < len(ips); i++ {
result := <-resultChan
printStopConnectionMessage(remoteService, result.HostIP)
if result.Error != nil {
printStopErrorMessage(remoteService, result.HostIP, result.Error)
errorList.PushBack(result.Error.Error())
} else {
writer.Printf("Output for Host IP %s : %s", result.HostIP, result.Output+"\n")
printStopSuccessMessage(remoteService, result.HostIP)
}
}
}
func printStopConnectionMessage(remoteService string, hostIP string) {
writer.Println("Connecting to the " + remoteService + NODE + hostIP)
writer.BufferWriter().Flush()
}
func printStopErrorMessage(remoteService string, hostIP string, err error) {
writer.Failf("Stop Command failed on "+remoteService+NODE+hostIP+" with error:\n%v \n", err)
writer.BufferWriter().Flush()
}
func printStopSuccessMessage(remoteService string, hostIP string) {
writer.Success("Stop Command is completed on " + remoteService + NODE + hostIP + "\n")
writer.BufferWriter().Flush()
}
func validateEachIp(remoteService string, infra *AutomateHAInfraDetails, ip string) bool {
ips := getIps(remoteService, infra)
for i := 0; i < len(ips); i++ {
if ips[i] == ip {
return true
}
}
return false
}
func getIps(remoteService string, infra *AutomateHAInfraDetails) []string {
if remoteService == AUTOMATE {
return infra.Outputs.AutomatePrivateIps.Value
} else if remoteService == CHEF_SERVER {
return infra.Outputs.ChefServerPrivateIps.Value
} else if remoteService == POSTGRESQL {
return infra.Outputs.PostgresqlPrivateIps.Value
} else if remoteService == OPENSEARCH {
return infra.Outputs.OpensearchPrivateIps.Value
}
return []string{}
}
func getFlagCount() int {
count := 0
if stopCmdFlags.automate {
count++
}
if stopCmdFlags.chef_server {
count++
}
if stopCmdFlags.opensearch {
count++
}
if stopCmdFlags.postgresql {
count++
}
return count
}
func isFlagEnabled(cmd *cobra.Command) error {
if !stopCmdFlags.automate && !stopCmdFlags.chef_server && !stopCmdFlags.opensearch && !stopCmdFlags.postgresql {
if stopCmdFlags.node != "" {
writer.Println("Please Provide service flag")
return errors.New("Please provide service flag")
}
writer.Println(cmd.UsageString())
return errors.New("No flag is enabled. Please provide any flag")
}
return nil
}