-
Notifications
You must be signed in to change notification settings - Fork 111
/
migrationscript.go
92 lines (81 loc) · 2.44 KB
/
migrationscript.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
package migratorv4
import (
"fmt"
"time"
"github.com/briandowns/spinner"
"github.com/chef/automate/components/automate-deployment/pkg/cli"
"github.com/chef/automate/lib/io/fileutils"
"github.com/fatih/color"
"github.com/pkg/errors"
)
const (
FSCRIPT = `
mv %[1]v/svc/automate-opensearch/data %[1]v/svc/automate-opensearch/data.os;
mv %[1]v/svc/automate-opensearch/var %[1]v/svc/automate-opensearch/var.os;
cp -r %[1]v/svc/automate-elasticsearch/data %[1]v/svc/automate-opensearch/;
cp -r %[1]v/svc/automate-elasticsearch/var %[1]v/svc/automate-opensearch/;
chown -RL hab:hab %[1]v/svc/automate-opensearch/data;
chown -RL hab:hab %[1]v/svc/automate-opensearch/var;`
)
type MigrationScript struct {
writer *cli.Writer
utils MigratorV4Utils
fileutils fileutils.FileUtils
spinner *spinner.Spinner
runError error
hasError bool
isExecuted bool
spinnerTimeout time.Duration
}
func NewMigrationScript(w *cli.Writer, utils MigratorV4Utils, fileutils fileutils.FileUtils, spinnerTimeout time.Duration) *MigrationScript {
return &MigrationScript{
writer: w,
utils: utils,
fileutils: fileutils,
spinnerTimeout: spinnerTimeout,
}
}
func (ms *MigrationScript) setError(err error) error {
ms.runError = err
ms.hasError = true
return err
}
func (ms *MigrationScript) showCopyError() {
ms.spinner.FinalMSG = SPACES_BEFORE_STEPS + " " + color.New(color.FgRed).Sprint("✖") + " Failed to copy data"
ms.spinner.Stop()
ms.writer.Println("")
}
func (ms *MigrationScript) showCopied() {
ms.spinner.FinalMSG = SPACES_BEFORE_STEPS + " " + color.New(color.FgGreen).Sprint("✔") + " Data Copied Successfully"
ms.spinner.Stop()
ms.writer.Println("")
}
func (ms *MigrationScript) showCopying() {
ms.spinner = ms.writer.NewSpinnerWithTab()
ms.spinner.Suffix = fmt.Sprintf(" Copying Data")
ms.spinner.Start()
time.Sleep(ms.spinnerTimeout)
}
func (ms *MigrationScript) Run() error {
ms.showCopying()
habRoot := ms.fileutils.GetHabRootPath()
script := fmt.Sprintf(FSCRIPT, habRoot)
args := []string{
"-c",
script,
}
err := ms.utils.ExecuteCommand("/bin/sh", args, "")
if err != nil {
err = errors.Wrap(err, "Failed to copy data while migration")
ms.showCopyError()
ms.setError(err)
return err
}
ms.showCopied()
return nil
}
func (ms *MigrationScript) ErrorHandler() {
if ms.hasError {
ms.writer.Println("[" + color.New(color.FgRed).Sprint("Error") + "] " + ms.runError.Error())
}
}