-
Notifications
You must be signed in to change notification settings - Fork 111
/
diskspace.go
212 lines (192 loc) · 6.78 KB
/
diskspace.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
package upgradeinspectorv4
import (
"errors"
"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/chef/automate/lib/io/fileutils"
"github.com/fatih/color"
)
const (
MIN_HAB_FREE_SPACE float64 = 5.5
MIN_REQ_SPACE_STR = "The %s directory should have at least %.1fGB of free space"
ENSURE_SPACE = "\nPlease ensure the available free space is %.1fGB\nand run "
UPGRADE_CMD = "chef-automate upgrade run --major"
ES_DATA = "/svc/automate-elasticsearch/data"
)
type DiskSpaceInspection struct {
writer *cli.Writer
isExternal bool
osDestDir string
fileutils fileutils.FileUtils
requiredHabSpace float64
requiredOSDestSpace float64
currentHabSpace float64
currentSpaceInOSDir float64
habDir string
spinnerHab *spinner.Spinner
spinnerOSDestDir *spinner.Spinner
checkDelay time.Duration
exitError error
exitedWithError bool
}
func (ds *DiskSpaceInspection) ShowInfo(index *int) (err error) {
ds.habDir = ds.fileutils.GetHabRootPath()
ds.currentHabSpace, err = ds.fileutils.GetFreeSpaceinGB(ds.habDir)
if err != nil {
return err
}
var esDataSize float64
ds.requiredHabSpace, esDataSize, err = ds.GetSpaceNeeded(ds.habDir)
if err != nil {
return err
}
ds.writer.Printf("%d. "+MIN_REQ_SPACE_STR+". (You have current available space : %.1fGB)\n",
*index, ds.habDir, ds.requiredHabSpace, ds.currentHabSpace)
*index++
if !ds.isExternal && ds.hasOSDestDir() {
ds.requiredOSDestSpace = esDataSize
ds.currentSpaceInOSDir, err = ds.fileutils.GetFreeSpaceinGB(ds.osDestDir)
if err != nil {
return err
}
ds.writer.Printf("%d. "+MIN_REQ_SPACE_STR+". (You have current available space : %.1fGB)\n",
*index, ds.osDestDir, ds.requiredOSDestSpace, ds.currentSpaceInOSDir)
*index++
}
return nil
}
func (ds *DiskSpaceInspection) hasOSDestDir() bool {
if ds.osDestDir != "" && ds.osDestDir != HAB_DIR {
return true
}
return false
}
func NewDiskSpaceInspection(w *cli.Writer, isExternal bool, osDestDir string, fileUtils fileutils.FileUtils) *DiskSpaceInspection {
return &DiskSpaceInspection{
writer: w,
isExternal: isExternal,
osDestDir: osDestDir,
fileutils: fileUtils,
checkDelay: 1000 * time.Millisecond,
}
}
func (ds *DiskSpaceInspection) GetSpaceNeeded(habRootPath string) (habFreeSpace float64, esDataSize float64, err error) {
if ds.isExternal {
return MIN_HAB_FREE_SPACE, -1, nil
} else {
esDataPath := habRootPath + ES_DATA
var rawESDataSize float64
rawESDataSize, err = ds.fileutils.CalDirSizeInGB(esDataPath)
if err != nil {
return -1, -1, err
}
habFreeSpace = MIN_HAB_FREE_SPACE + (rawESDataSize * 110 / 100)
esDataSize = (rawESDataSize * 110 / 100)
if ds.hasOSDestDir() {
return MIN_HAB_FREE_SPACE, esDataSize, nil
}
return
}
}
func (ds *DiskSpaceInspection) showCheckingHab() {
ds.spinnerHab = ds.writer.NewSpinner()
ds.spinnerHab.Suffix = fmt.Sprintf(" [Checking]\t"+MIN_REQ_SPACE_STR, ds.habDir, ds.requiredHabSpace)
ds.spinnerHab.Start()
time.Sleep(ds.checkDelay)
}
func (ds *DiskSpaceInspection) showCheckingOSDest() {
if ds.hasOSDestDir() {
ds.spinnerOSDestDir = ds.writer.NewSpinner()
ds.spinnerOSDestDir.Suffix = fmt.Sprintf(" [Checking]\t"+MIN_REQ_SPACE_STR, ds.osDestDir, ds.requiredOSDestSpace)
ds.spinnerOSDestDir.Start()
time.Sleep(ds.checkDelay)
}
}
func (ds *DiskSpaceInspection) checkHabSpace() bool {
ds.showCheckingHab()
if ds.currentHabSpace > ds.requiredHabSpace {
ds.spinnerHab.FinalMSG = fmt.Sprintf(color.New(color.FgGreen).Sprint("✔")+" ["+color.New(color.FgGreen).Sprint("Passed")+
"]\t"+MIN_REQ_SPACE_STR+"\n", ds.habDir, ds.requiredHabSpace)
ds.spinnerHab.Stop()
return true
} else {
ds.spinnerHab.FinalMSG = fmt.Sprintf(color.New(color.FgRed).Sprint("✖")+" ["+color.New(color.FgRed).Sprint("Failed")+
"]\t"+MIN_REQ_SPACE_STR+"\n", ds.habDir, ds.requiredHabSpace)
ds.spinnerHab.Stop()
ds.skipOSDestCheck()
return false
}
}
func (ds *DiskSpaceInspection) setErrHabSpace() {
errStr := "[" + color.New(color.FgRed).Sprint("Error") + fmt.Sprintf("] Required Space : %.1fGB\n", ds.requiredHabSpace)
errStr += fmt.Sprintf(" Available space : %.1fGB\n", ds.currentHabSpace)
errStr += fmt.Sprintf(ENSURE_SPACE+color.New(color.Bold).Sprint(UPGRADE_CMD)+" command again", ds.requiredHabSpace)
ds.exitError = errors.New(errStr)
ds.exitedWithError = true
}
func (ds *DiskSpaceInspection) checkOSDestSpace() bool {
ds.showCheckingOSDest()
if ds.currentSpaceInOSDir > ds.requiredOSDestSpace {
ds.spinnerOSDestDir.FinalMSG = fmt.Sprintf(color.New(color.FgGreen).Sprint("✔")+" ["+color.New(color.FgGreen).Sprint("Passed")+
"]\t"+MIN_REQ_SPACE_STR+"\n", ds.osDestDir, ds.requiredOSDestSpace)
ds.spinnerOSDestDir.Stop()
return true
} else {
ds.spinnerOSDestDir.FinalMSG = fmt.Sprintf(color.New(color.FgRed).Sprint("✖")+" ["+color.New(color.FgRed).Sprint("Failed")+
"]\t"+MIN_REQ_SPACE_STR+"\n", ds.osDestDir, ds.requiredOSDestSpace)
ds.spinnerOSDestDir.Stop()
return false
}
}
func (ds *DiskSpaceInspection) setOSDestError() {
errStr := "[" + color.New(color.FgRed).Sprint("Error") + fmt.Sprintf("] Required Space : %.1fGB\n", ds.requiredOSDestSpace)
errStr += fmt.Sprintf(" Available space : %.1fGB\n", ds.currentSpaceInOSDir)
errStr += fmt.Sprintf(ENSURE_SPACE+color.New(color.Bold).Sprint(UPGRADE_CMD)+" command again", ds.requiredOSDestSpace)
ds.exitError = errors.New(errStr)
ds.exitedWithError = true
}
func (ds *DiskSpaceInspection) skipOSDestCheck() {
if ds.hasOSDestDir() {
ds.writer.Printf(" ⊖ [Skipped]\t"+MIN_REQ_SPACE_STR+"\n", ds.osDestDir, ds.requiredOSDestSpace)
}
}
func (ds *DiskSpaceInspection) Skip() {
ds.writer.Printf(" ⊖ [Skipped]\t"+MIN_REQ_SPACE_STR+"\n", ds.habDir, ds.requiredHabSpace)
ds.skipOSDestCheck()
}
func (ds *DiskSpaceInspection) Inspect() error {
isHabFree := ds.checkHabSpace()
if !isHabFree {
ds.setErrHabSpace()
return errors.New("failed in Hab Space Check")
}
if ds.hasOSDestDir() {
isOSDestFree := ds.checkOSDestSpace()
if !isOSDestFree {
ds.setOSDestError()
return errors.New("failed in OS Dest Check")
}
}
return nil
}
func (ds *DiskSpaceInspection) GetShortInfo() []string {
msgs := []string{
fmt.Sprintf(MIN_REQ_SPACE_STR, ds.habDir, ds.requiredHabSpace),
}
if ds.hasOSDestDir() {
msgs = append(msgs, fmt.Sprintf(MIN_REQ_SPACE_STR, ds.osDestDir, ds.requiredOSDestSpace))
}
return msgs
}
func (ds *DiskSpaceInspection) GetInstallationType() inspector.InstallationType {
return inspector.BOTH
}
func (ds *DiskSpaceInspection) ExitHandler() error {
if ds.exitedWithError {
ds.writer.Println(ds.exitError.Error())
}
return nil
}