From ce86717fa4790961f1e7fb64e78ef83a27c7fef8 Mon Sep 17 00:00:00 2001 From: shaik80 Date: Wed, 23 Feb 2022 15:29:05 +0530 Subject: [PATCH] postchecklist new structure Signed-off-by: shaik80 --- .../automate-cli/cmd/chef-automate/upgrade.go | 7 +- .../checklist_manager.go | 194 +----------------- .../checklist_manager_test.go | 10 +- .../post_checklist_manager.go | 127 ++++++++++++ .../majorupgradechecklist/upgrade_utils.go | 57 +++++ .../pkg/majorupgradechecklist/v3.go | 20 +- .../automate-deployment/pkg/server/server.go | 6 +- 7 files changed, 209 insertions(+), 212 deletions(-) create mode 100644 components/automate-deployment/pkg/majorupgradechecklist/post_checklist_manager.go create mode 100644 components/automate-deployment/pkg/majorupgradechecklist/upgrade_utils.go diff --git a/components/automate-cli/cmd/chef-automate/upgrade.go b/components/automate-cli/cmd/chef-automate/upgrade.go index e176b830365f..c318cd49f1e3 100644 --- a/components/automate-cli/cmd/chef-automate/upgrade.go +++ b/components/automate-cli/cmd/chef-automate/upgrade.go @@ -121,7 +121,7 @@ func runUpgradeCmd(cmd *cobra.Command, args []string) error { } if upgradeRunCmdFlags.isMajorUpgrade { - ci, err := majorupgradechecklist.NewChecklistManager(writer, validatedResp.TargetVersion, "") + ci, err := majorupgradechecklist.NewChecklistManager(writer, validatedResp.TargetVersion, validatedResp.TargetMajor) if err != nil { return status.Wrap( err, @@ -272,7 +272,10 @@ func statusUpgradeCmd(cmd *cobra.Command, args []string) error { _, isMajorVersion := manifest.IsSemVersionFmt(resp.CurrentVersion) if isMajorVersion { - ci := majorupgradechecklist.NewCRUDChecklist(resp.CurrentVersion) + ci := majorupgradechecklist.NewPostChecklistManager(resp.CurrentVersion) + if err != nil { + return err + } resp, err := ci.ReadPendingPostChecklistFile() if err != nil { return status.Wrap( diff --git a/components/automate-deployment/pkg/majorupgradechecklist/checklist_manager.go b/components/automate-deployment/pkg/majorupgradechecklist/checklist_manager.go index 25c1d1f0a239..733c44ac9a1e 100644 --- a/components/automate-deployment/pkg/majorupgradechecklist/checklist_manager.go +++ b/components/automate-deployment/pkg/majorupgradechecklist/checklist_manager.go @@ -1,48 +1,15 @@ package majorupgradechecklist import ( - "bytes" - "encoding/json" - "io/ioutil" - "github.com/chef/automate/components/automate-cli/pkg/status" "github.com/chef/automate/components/automate-deployment/pkg/cli" - "github.com/chef/automate/components/automate-deployment/pkg/manifest" - "github.com/pkg/errors" ) -type PreChecklistManager interface { +type ChecklistManager interface { RunChecklist() error } -type PerCheckList struct { - Msg string `json:"msg"` - Response string `json:"-"` -} - -type PostCheckList struct { - Id string `json:"id"` - Msg string `json:"msg"` - Cmd string `json:"cmd"` - Optional bool `json:"optional"` - IsExecuted bool `json:"is_executed"` -} - -type PerPostChecklist struct { - currentMajorVersion string - isMajorVersion bool - Version string `json:"version"` - PostChecklist []PostCheckList `json:"post_checklist"` - Seen bool `json:"seen"` -} - -const ( - upgrade_metadata = "/hab/svc/deployment-service/var/upgrade_metadata.json" -) - -//NewChecklistManager returns the checklist inspector for given major release - -func NewChecklistManager(writer cli.FormatWriter, version, major string) (PreChecklistManager, error) { +func NewChecklistManager(writer cli.FormatWriter, version, major string) (ChecklistManager, error) { if major == "" { major, _ = GetMajorVersion(version) @@ -55,160 +22,3 @@ func NewChecklistManager(writer cli.FormatWriter, version, major string) (PreChe return nil, status.Errorf(status.UpgradeError, "invalid major version") } } - -type Checklist struct { - Name string - Description string - TestFunc func(ChecklistHelper) error -} - -type ChecklistHelper struct { - Writer cli.FormatWriter -} -type PostChecklistManager interface { - CreatePostChecklistFile() error - ReadPendingPostChecklistFile() ([]string, error) - ReadPostChecklistById(id string) (bool, error) - UpdatePostChecklistFile(id string) error -} - -func NewCRUDChecklist(version string) PostChecklistManager { - return NewPerPostChecklist(version) -} - -func NewPerPostChecklist(version string) *PerPostChecklist { - majorVersion, isMajorVersion := GetMajorVersion(version) - - prepost := &PerPostChecklist{} - prepost.currentMajorVersion = majorVersion - prepost.isMajorVersion = isMajorVersion - return prepost -} - -func (ci *PerPostChecklist) CreatePostChecklistFile() error { - params := PerPostChecklist{} - if isExternalPG() { - params.PostChecklist = append(params.PostChecklist, postChecklistExternal...) - } else { - params.PostChecklist = append(params.PostChecklist, postChecklistEmbedded...) - } - - params.Version = ci.currentMajorVersion - err := CreateJsonFile(params, upgrade_metadata) - if err != nil { - return err - } - - return nil -} - -func (ci *PerPostChecklist) ReadPostChecklistById(id string) (bool, error) { - ChecklistId_Found := false - res, err := ReadJsonFile(upgrade_metadata) - if err != nil { - return false, err - } - for i := 0; i < len(res.PostChecklist); i++ { - - if res.PostChecklist[i].Id == id { - ChecklistId_Found = res.PostChecklist[i].IsExecuted - break - - } - } - return ChecklistId_Found, nil -} - -func ReadJsonFile(path string) (*PerPostChecklist, error) { - byteValue, err := ioutil.ReadFile(path) - if err != nil { - return nil, err - } - params := PerPostChecklist{} - - err = json.Unmarshal(byteValue, ¶ms) - if err != nil { - return nil, err - } - return ¶ms, nil -} - -func CreateJsonFile(params PerPostChecklist, path string) error { - var buffer bytes.Buffer - data, err := json.Marshal(params) - if err != nil { - return err - } - buffer.Write(data) - buffer.WriteString("\n") - err = ioutil.WriteFile(path, buffer.Bytes(), 0644) - if err != nil { - return err - } - return nil -} - -func (ci *PerPostChecklist) ReadPendingPostChecklistFile() ([]string, error) { - var postCmdList []string - var showPostChecklist = false - res, err := ReadJsonFile(upgrade_metadata) - if err != nil { - return nil, err - } - - if res.Version == ci.currentMajorVersion { - if (isExternalPG() && !ci.Seen) || !isExternalPG() { - for i := 0; i < len(res.PostChecklist); i++ { - if (!res.PostChecklist[i].Optional && !res.PostChecklist[i].IsExecuted) || !res.Seen { - showPostChecklist = true - break - } - } - - if showPostChecklist { - for i := 0; i < len(res.PostChecklist); i++ { - if !res.PostChecklist[i].IsExecuted { - postCmdList = append(postCmdList, res.PostChecklist[i].Msg) - } - } - } - - if isExternalPG() { - res.Seen = true - err = CreateJsonFile(*res, upgrade_metadata) - if err != nil { - return nil, err - } - } - return postCmdList, nil - } - } - - return nil, errors.Errorf("Failed to read checklist since version didn't match") -} - -func (ci *PerPostChecklist) UpdatePostChecklistFile(id string) error { - res, err := ReadJsonFile(upgrade_metadata) - if err != nil { - return err - } - for i, v := range res.PostChecklist { - if v.Id == id { - res.PostChecklist[i].IsExecuted = true - } - } - - err = CreateJsonFile(*res, upgrade_metadata) - if err != nil { - return err - } - return nil -} - -func GetMajorVersion(version string) (string, bool) { - resp, is_major_version := manifest.IsSemVersionFmt(version) - if is_major_version { - return resp, is_major_version - } - return version, false -} diff --git a/components/automate-deployment/pkg/majorupgradechecklist/checklist_manager_test.go b/components/automate-deployment/pkg/majorupgradechecklist/checklist_manager_test.go index dd042b9cbcad..2f5eaf18706a 100644 --- a/components/automate-deployment/pkg/majorupgradechecklist/checklist_manager_test.go +++ b/components/automate-deployment/pkg/majorupgradechecklist/checklist_manager_test.go @@ -15,14 +15,14 @@ func TestCreatePostChecklistFile(t *testing.T) { // remove json file removeFile() // Create json file - cl := NewCRUDChecklist("3") + cl := NewPostChecklistManager("3") err := cl.CreatePostChecklistFile() // return nil if json file is created successfully assert.Equal(t, err, nil) } func TestReadPostChecklistByIdSuccess(t *testing.T) { - cl := NewCRUDChecklist("3") + cl := NewPostChecklistManager("3") IsExecuted, err := cl.ReadPostChecklistById("pg_migrate") // return nil if checklist by id found assert.Equal(t, err, nil) @@ -31,7 +31,7 @@ func TestReadPostChecklistByIdSuccess(t *testing.T) { } func TestReadPostChecklistSuccess(t *testing.T) { - cl := NewCRUDChecklist("3") + cl := NewPostChecklistManager("3") result, err := cl.ReadPendingPostChecklistFile() assert.Equal(t, err, nil) //get json data as result @@ -41,7 +41,7 @@ func TestReadPostChecklistSuccess(t *testing.T) { func TestReadPostChecklistByIdFailure(t *testing.T) { // remove json file removeFile() - cl := NewCRUDChecklist("3") + cl := NewPostChecklistManager("3") IsExecuted, err := cl.ReadPostChecklistById("pg_migrate") // return nil if checklist by id found assert.Equal(t, err.Error(), "open /hab/svc/deployment-service/var/upgrade_metadata.json: no such file or directory") @@ -52,7 +52,7 @@ func TestReadPostChecklistByIdFailure(t *testing.T) { func TestReadPostChecklistFailure(t *testing.T) { // remove json file removeFile() - cl := NewCRUDChecklist("3") + cl := NewPostChecklistManager("3") result, err := cl.ReadPendingPostChecklistFile() assert.Equal(t, err.Error(), "open /hab/svc/deployment-service/var/upgrade_metadata.json: no such file or directory") //get json data as result diff --git a/components/automate-deployment/pkg/majorupgradechecklist/post_checklist_manager.go b/components/automate-deployment/pkg/majorupgradechecklist/post_checklist_manager.go new file mode 100644 index 000000000000..faaad163c855 --- /dev/null +++ b/components/automate-deployment/pkg/majorupgradechecklist/post_checklist_manager.go @@ -0,0 +1,127 @@ +package majorupgradechecklist + +import ( + "github.com/pkg/errors" +) + +const ( + upgrade_metadata = "/hab/svc/deployment-service/var/upgrade_metadata.json" +) + +type PostChecklistManager struct { + version string +} + +type PostCheckListItem struct { + Id string `json:"id"` + Msg string `json:"msg"` + Cmd string `json:"cmd"` + Optional bool `json:"optional"` + IsExecuted bool `json:"is_executed"` +} + +type PostChecklist struct { + Version string `json:"version"` + PostChecklist []PostCheckListItem `json:"post_checklist"` + Seen bool `json:"seen"` +} + +func NewPostChecklistManager(version string) PostChecklistManager { + majorVersion, _ := GetMajorVersion(version) + + return PostChecklistManager{ + version: majorVersion, + } +} + +func (ci *PostChecklistManager) CreatePostChecklistFile() error { + params := PostChecklist{} + if isExternalPG() { + params.PostChecklist = append(params.PostChecklist, postChecklistExternal...) + } else { + params.PostChecklist = append(params.PostChecklist, postChecklistEmbedded...) + } + + params.Version = ci.version + err := CreateJsonFile(¶ms, upgrade_metadata) + if err != nil { + return err + } + + return nil +} + +func (ci *PostChecklistManager) ReadPostChecklistById(id string) (bool, error) { + ChecklistId_Found := false + res, err := ReadJsonFile(upgrade_metadata) + if err != nil { + return false, err + } + for i := 0; i < len(res.PostChecklist); i++ { + + if res.PostChecklist[i].Id == id { + ChecklistId_Found = res.PostChecklist[i].IsExecuted + break + + } + } + return ChecklistId_Found, nil +} + +func (ci *PostChecklistManager) ReadPendingPostChecklistFile() ([]string, error) { + var postCmdList []string + var showPostChecklist = false + res, err := ReadJsonFile(upgrade_metadata) + if err != nil { + return nil, err + } + + if res.Version == ci.version { + if (isExternalPG() && !res.Seen) || !isExternalPG() { + for i := 0; i < len(res.PostChecklist); i++ { + if (!res.PostChecklist[i].Optional && !res.PostChecklist[i].IsExecuted) || !res.Seen { + showPostChecklist = true + break + } + } + + if showPostChecklist { + for i := 0; i < len(res.PostChecklist); i++ { + if !res.PostChecklist[i].IsExecuted { + postCmdList = append(postCmdList, res.PostChecklist[i].Msg) + } + } + } + + if isExternalPG() { + res.Seen = true + err = CreateJsonFile(res, upgrade_metadata) + if err != nil { + return nil, err + } + } + return postCmdList, nil + } + } else { + return nil, errors.Errorf("Failed to read checklist since version didn't match") + } + return nil, nil +} + +func (ci *PostChecklistManager) UpdatePostChecklistFile(id string) error { + res, err := ReadJsonFile(upgrade_metadata) + if err != nil { + return err + } + for i, v := range res.PostChecklist { + if v.Id == id { + res.PostChecklist[i].IsExecuted = true + } + } + + err = CreateJsonFile(res, upgrade_metadata) + if err != nil { + return err + } + return nil +} diff --git a/components/automate-deployment/pkg/majorupgradechecklist/upgrade_utils.go b/components/automate-deployment/pkg/majorupgradechecklist/upgrade_utils.go new file mode 100644 index 000000000000..11c13696c051 --- /dev/null +++ b/components/automate-deployment/pkg/majorupgradechecklist/upgrade_utils.go @@ -0,0 +1,57 @@ +package majorupgradechecklist + +import ( + "bytes" + "encoding/json" + "io/ioutil" + + "github.com/chef/automate/components/automate-deployment/pkg/cli" + "github.com/chef/automate/components/automate-deployment/pkg/manifest" +) + +type Checklist struct { + Name string + Description string + TestFunc func(ChecklistHelper) error +} + +type ChecklistHelper struct { + Writer cli.FormatWriter +} + +func ReadJsonFile(path string) (*PostChecklist, error) { + byteValue, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + params := PostChecklist{} + + err = json.Unmarshal(byteValue, ¶ms) + if err != nil { + return nil, err + } + return ¶ms, nil +} + +func CreateJsonFile(params *PostChecklist, path string) error { + var buffer bytes.Buffer + data, err := json.Marshal(*params) + if err != nil { + return err + } + buffer.Write(data) + buffer.WriteString("\n") + err = ioutil.WriteFile(path, buffer.Bytes(), 0644) + if err != nil { + return err + } + return nil +} + +func GetMajorVersion(version string) (string, bool) { + resp, is_major_version := manifest.IsSemVersionFmt(version) + if is_major_version { + return resp, is_major_version + } + return version, false +} diff --git a/components/automate-deployment/pkg/majorupgradechecklist/v3.go b/components/automate-deployment/pkg/majorupgradechecklist/v3.go index 5dac8167aeca..609d287e0419 100644 --- a/components/automate-deployment/pkg/majorupgradechecklist/v3.go +++ b/components/automate-deployment/pkg/majorupgradechecklist/v3.go @@ -57,7 +57,7 @@ Post Upgrade Steps: ` ) -var postChecklistEmbedded = []PostCheckList{ +var postChecklistEmbedded = []PostCheckListItem{ { Id: "upgrade_status", Msg: run_chef_automate_upgrade_status, @@ -84,7 +84,7 @@ var postChecklistEmbedded = []PostCheckList{ }, } -var postChecklistExternal = []PostCheckList{ +var postChecklistExternal = []PostCheckListItem{ { Id: "patch_new_config", Msg: patch_new_conf, @@ -113,14 +113,14 @@ var postChecklistExternal = []PostCheckList{ } type V3ChecklistManager struct { - writer cli.FormatWriter - version string + writer cli.FormatWriter + version string } func NewV3ChecklistManager(writer cli.FormatWriter, version string) *V3ChecklistManager { return &V3ChecklistManager{ - writer: writer, - version: version, + writer: writer, + version: version, } } @@ -147,7 +147,7 @@ func (ci *V3ChecklistManager) RunChecklist() error { var dbType string checklists := []Checklist{} - var postcheck []PostCheckList + var postcheck []PostCheckListItem if isExternalPG() { dbType = "External" @@ -158,7 +158,7 @@ func (ci *V3ChecklistManager) RunChecklist() error { postcheck = postChecklistEmbedded checklists = append(checklists, preChecklist(diskSpaceCheck)...) } - checklists = append(checklists, ci.showPostChecklist(postcheck), promptUpgradeContinue()) + checklists = append(checklists, showPostChecklist(&postcheck), promptUpgradeContinue()) helper := ChecklistHelper{ Writer: ci.writer, @@ -177,13 +177,13 @@ func (ci *V3ChecklistManager) RunChecklist() error { return nil } -func (ci *V3ChecklistManager) showPostChecklist(postCheck []PostCheckList) Checklist { +func showPostChecklist(postCheck *[]PostCheckListItem) Checklist { return Checklist{ Name: "Show_Post_Checklist", Description: "Show Post Checklist", TestFunc: func(h ChecklistHelper) error { displayed := false - for i, item := range postCheck { + for i, item := range *postCheck { if !item.IsExecuted { if !displayed { h.Writer.Println(post_upgrade_header) diff --git a/components/automate-deployment/pkg/server/server.go b/components/automate-deployment/pkg/server/server.go index 7483a3a8aa13..68db80f8f6e3 100644 --- a/components/automate-deployment/pkg/server/server.go +++ b/components/automate-deployment/pkg/server/server.go @@ -1364,10 +1364,10 @@ func (s *server) doConverge( errHandler(eDeploy.err) // json file if os.Getenv(isUpgradeMajorEnv) == "true" { - ci := majorupgradechecklist.NewCRUDChecklist(s.deployment.CurrentReleaseManifest.Version()) - err = ci.CreatePostChecklistFile() + ci := majorupgradechecklist.NewPostChecklistManager(s.deployment.CurrentReleaseManifest.Version()) + err := ci.CreatePostChecklistFile() if err != nil { - return + errHandler(err) } } }()