-
Notifications
You must be signed in to change notification settings - Fork 111
/
enablemaintenance.go
119 lines (105 loc) · 2.87 KB
/
enablemaintenance.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
package upgradeinspectorv4
import (
"fmt"
"time"
"github.com/briandowns/spinner"
"github.com/chef/automate/components/automate-deployment/pkg/cli"
"github.com/chef/automate/components/automate-deployment/pkg/inspector"
"github.com/fatih/color"
)
type EnableMaintenanceInspection struct {
writer *cli.Writer
upgradeUtils UpgradeV4Utils
timeout int64
isExecuted bool
spinner *spinner.Spinner
exitError error
exitedWithError bool
}
func NewEnableMaintenanceInspection(w *cli.Writer, utls UpgradeV4Utils, timeout int64) *EnableMaintenanceInspection {
return &EnableMaintenanceInspection{
writer: w,
upgradeUtils: utls,
timeout: timeout,
}
}
func (em *EnableMaintenanceInspection) ShowInfo(index *int) error {
return nil
}
func (em *EnableMaintenanceInspection) Skip() {
return
}
func (em *EnableMaintenanceInspection) GetShortInfo() []string {
return nil
}
func (em *EnableMaintenanceInspection) Inspect() (err error) {
em.showTurningOn()
isMaintenanceOn, err := em.upgradeUtils.GetMaintenanceStatus(em.timeout)
if err != nil {
em.showError()
em.setExitError(err)
return err
}
if isMaintenanceOn {
em.showSuccess()
return nil
}
_, _, err = em.upgradeUtils.SetMaintenanceMode(em.timeout, true)
if err != nil {
em.showError()
em.setExitError(err)
return err
}
em.setExecuted()
em.showSuccess()
return nil
}
func (em *EnableMaintenanceInspection) setExitError(err error) {
em.exitedWithError = true
em.exitError = err
}
func (em *EnableMaintenanceInspection) showTurningOn() {
em.spinner = em.writer.NewSpinner()
em.spinner.Suffix = fmt.Sprintf(" Turning ON maintenance mode")
em.spinner.Start()
time.Sleep(time.Second)
}
func (em *EnableMaintenanceInspection) showSuccess() {
em.spinner.FinalMSG = "\n " + fmt.Sprintf(color.New(color.FgGreen).Sprint("✔")+" Maintenance mode turned ON successfully\n")
em.spinner.Stop()
em.writer.Println("")
}
func (em *EnableMaintenanceInspection) showError() {
em.spinner.FinalMSG = " " + color.New(color.FgRed).Sprint("✖") + " Failed to turn maintenance mode ON"
em.spinner.Stop()
em.writer.Println("")
}
func (em *EnableMaintenanceInspection) setExecuted() {
em.isExecuted = true
}
func (em *EnableMaintenanceInspection) RollBackHandler() (err error) {
if !em.isExecuted {
return nil
}
isMaintenanceOn, err := em.upgradeUtils.GetMaintenanceStatus(em.timeout)
if err != nil {
return err
}
if !isMaintenanceOn {
return nil
}
_, _, err = em.upgradeUtils.SetMaintenanceMode(em.timeout, false)
if err != nil {
return err
}
return nil
}
func (em *EnableMaintenanceInspection) GetInstallationType() inspector.InstallationType {
return inspector.BOTH
}
func (em *EnableMaintenanceInspection) ExitHandler() error {
if em.exitedWithError {
em.writer.Println(fmt.Errorf("["+color.New(color.FgRed).Sprint("Error")+"] %w", em.exitError).Error())
}
return nil
}