-
Notifications
You must be signed in to change notification settings - Fork 111
/
migratorV4Utils.go
211 lines (189 loc) · 6.66 KB
/
migratorV4Utils.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
package migratorv4
import (
"context"
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"syscall"
"time"
"github.com/chef/automate/api/config/deployment"
opensearch "github.com/chef/automate/api/config/opensearch"
api "github.com/chef/automate/api/interservice/deployment"
"github.com/chef/automate/components/automate-deployment/pkg/client"
"github.com/chef/automate/components/automate-deployment/pkg/majorupgradechecklist"
"github.com/chef/automate/components/automate-deployment/pkg/target"
"github.com/chef/automate/lib/majorupgrade_utils"
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/wrapperspb"
)
const (
SPACES_BEFORE_STEPS = " "
AUTOMATE_VERSION_4 string = "4"
SPINNER_TEST_DURATION = 100 * time.Millisecond
)
type MigratorV4Utils interface {
GetEsTotalShardSettings(path string) (int32, error)
PatchOpensearchConfig(es *ESSettings) (string, string, error)
IsExternalElasticSearch(timeout int64) bool
StopAutomate() error
StartAutomate() error
ReadV4Checklist(id, path string) (bool, error)
UpdatePostChecklistFile(id, path string) error
ExecuteCommand(command string, args []string, workingDir string) error
GetServicesStatus() (bool, error)
GetAutomateFQDN(timeout int64) string
GetMaintenanceStatus(timeout int64) (bool, error)
SetMaintenanceMode(timeout int64, status bool) (stdOut, stdErr string, err error)
}
type ESSettings struct {
TotalShardSettings int32 `json:"totalShardSettings" toml:"totalShardSettings"`
IndicesBreakerTotalLimit string `json:"indicesBreakerTotalLimit" toml:"indicesBreakerTotalLimit"`
RuntimeMaxOpenFile string `json:"runtimeMaxOpenFile" toml:"runtimeMaxOpenFile"`
RuntimeMaxLockedMem string `json:"runtimeMaxLockedMem" toml:"runtimeMaxLockedMem"`
HeapMemory string `json:"heapMemory" toml:"heapMemory"`
}
type MigratorV4UtilsImpl struct {
}
func NewMigratorV4Utils() MigratorV4Utils {
return &MigratorV4UtilsImpl{}
}
func (m *MigratorV4UtilsImpl) PatchOpensearchConfig(osConfig *ESSettings) (string, string, error) {
cfg := deployment.NewUserOverrideConfig()
cfg.Opensearch = &opensearch.ConfigRequest{
V1: &opensearch.ConfigRequest_V1{
Sys: &opensearch.ConfigRequest_V1_System{
Cluster: &opensearch.ConfigRequest_V1_Cluster{
MaxShardsPerNode: wrapperspb.Int32(osConfig.TotalShardSettings),
},
Indices: &opensearch.ConfigRequest_V1_Indices{
Breaker: &opensearch.ConfigRequest_V1_Breaker{
TotalLimit: wrapperspb.String(osConfig.IndicesBreakerTotalLimit),
},
},
Runtime: &opensearch.ConfigRequest_V1_Runtime{
MaxOpenFiles: wrapperspb.String(osConfig.RuntimeMaxOpenFile),
MaxLockedMemory: wrapperspb.String(osConfig.RuntimeMaxLockedMem),
Heapsize: wrapperspb.String(osConfig.HeapMemory),
},
},
},
}
tw := majorupgrade_utils.NewCustomWriter()
err := client.PatchAutomateConfig(10, cfg, tw.CliWriter)
if err != nil {
return "", "", errors.Wrap(err, "Failed to patch opensearch config")
}
return tw.WriteBuffer.String(), tw.ErrorBuffer.String(), nil
}
func (m *MigratorV4UtilsImpl) GetEsTotalShardSettings(path string) (int32, error) {
esSetting := &ESSettings{}
jsonData, err := ioutil.ReadFile(path) // nosemgrep
if err != nil {
return esSetting.TotalShardSettings, err
}
err = json.Unmarshal(jsonData, esSetting)
if err != nil {
return esSetting.TotalShardSettings, err
}
return esSetting.TotalShardSettings, nil
}
func (m *MigratorV4UtilsImpl) IsExternalElasticSearch(timeout int64) bool {
return majorupgrade_utils.IsExternalElasticSearch(timeout)
}
func isDevMode() bool {
return os.Getenv("CHEF_DEV_ENVIRONMENT") == "true"
}
func (m *MigratorV4UtilsImpl) StopAutomate() error {
connection, err := client.Connection(client.DefaultClientTimeout)
if err != nil {
return errors.Wrap(err, "Error in connection while stopping automate")
}
if isDevMode() {
_, err = connection.Stop(context.Background(), &api.StopRequest{})
if err != nil {
return errors.Wrap(err, "Error stopping automate in dev mode")
}
} else {
t := target.NewLocalTarget(true)
if err := t.EnsureStopped(); err != nil {
return errors.Wrap(err, "Error stopping automate")
}
}
return nil
}
func (m *MigratorV4UtilsImpl) StartAutomate() error {
if isDevMode() {
if err := exec.Command("hab", "sup", "status").Run(); err == nil {
return nil
}
if err := os.MkdirAll("/hab/sup/default", 0755); err != nil {
return errors.Wrap(err, "Failed trying to start automate dev mode")
}
out, err := os.Create("/hab/sup/default/sup.log")
if err != nil {
return errors.Wrap(err, "Failed trying to start automatedev mode")
}
startSupCmd := exec.Command("hab", "sup", "run")
startSupCmd.Env = os.Environ()
startSupCmd.Env = append(startSupCmd.Env,
"DS_DEV=true",
"CHEF_AUTOMATE_SKIP_SYSTEMD=true",
"HAB_LICENSE=accept-no-persist",
)
startSupCmd.Stdout = out
startSupCmd.Stderr = out
startSupCmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
if err := startSupCmd.Start(); err != nil {
return errors.Wrap(err, "Failed trying to start automate dev mode")
}
} else {
systemctlCmd := exec.Command("systemctl", "start", "chef-automate.service")
systemctlCmd.Stdout = os.Stdout
systemctlCmd.Stderr = os.Stderr
if err := systemctlCmd.Run(); err != nil {
return errors.Wrap(err, "Failed trying to start automate")
}
}
return nil
}
func (m *MigratorV4UtilsImpl) ReadV4Checklist(id, path string) (bool, error) {
ci, err := majorupgradechecklist.NewPostChecklistManager(AUTOMATE_VERSION_4)
if err != nil {
return false, err
}
return ci.ReadPostChecklistById(id, path)
}
func (m *MigratorV4UtilsImpl) UpdatePostChecklistFile(id, path string) error {
ci, err := majorupgradechecklist.NewPostChecklistManager(AUTOMATE_VERSION_4)
if err != nil {
return err
}
return ci.UpdatePostChecklistFile(id, path)
}
func (m *MigratorV4UtilsImpl) ExecuteCommand(command string, args []string, workingDir string) error {
c := exec.Command(command, args...)
if len(workingDir) > 0 {
c.Dir = workingDir
}
err := c.Run()
return err
}
func (m *MigratorV4UtilsImpl) GetServicesStatus() (bool, error) {
return majorupgrade_utils.EnsureStatus()
}
func (m *MigratorV4UtilsImpl) GetAutomateFQDN(timeout int64) string {
res, err := client.GetAutomateConfig(timeout)
if err != nil {
return "http://path.local.automate.instance.io"
}
return res.Config.GetGlobal().GetV1().GetFqdn().Value
}
func (m *MigratorV4UtilsImpl) GetMaintenanceStatus(timeout int64) (bool, error) {
return majorupgrade_utils.GetMaintenanceStatus(timeout)
}
func (m *MigratorV4UtilsImpl) SetMaintenanceMode(timeout int64, status bool) (stdOut, stdErr string, err error) {
return majorupgrade_utils.SetMaintenanceMode(timeout, status)
}