Skip to content

Commit

Permalink
postchecklist new structure
Browse files Browse the repository at this point in the history
Signed-off-by: shaik80 <[email protected]>
  • Loading branch information
shaik80 committed Feb 23, 2022
1 parent 6b36552 commit ce86717
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 212 deletions.
7 changes: 5 additions & 2 deletions components/automate-cli/cmd/chef-automate/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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, &params)
if err != nil {
return nil, err
}
return &params, 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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
Expand Down
Loading

0 comments on commit ce86717

Please sign in to comment.