-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupgrade-manager.go
124 lines (106 loc) · 4.1 KB
/
upgrade-manager.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
package versions
import (
"fmt"
"os"
v2_4_0 "github.com/litmuschaos/litmus/litmus-portal/upgrader-agents/control-plane/versions/v2.4.0"
"github.com/litmuschaos/litmus/litmus-portal/upgrader-agents/control-plane/pkg/database"
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap"
)
// UpgradeExecutor holds the details regarding the version and IVersionManager for a particular version
type UpgradeExecutor struct {
NextVersion string
VersionManager IVersionManager
}
// UpgradeManager provides the functionality required to upgrade from the PreviousVersion to the TargetVersion
type UpgradeManager struct {
Logger *zap.Logger
DBClient *mongo.Client
PreviousVersion string
TargetVersion string
}
// NewUpgradeManager creates an instance of a upgrade manager with the proper configurations
func NewUpgradeManager(logger *zap.Logger, dbClient *mongo.Client) (*UpgradeManager, error) {
currentVersion := os.Getenv("VERSION")
if currentVersion == "" {
return nil, fmt.Errorf("current version env data missing")
}
config, err := database.GetVersion(dbClient)
if err != nil {
return nil, fmt.Errorf("failed to get previous version data from db, error=%w", err)
}
if config.Value == nil || config.Value.(string) == "" {
return nil, fmt.Errorf("failed to get previous version data from db, value=%v", config.Value)
}
if config.Value.(string) == currentVersion {
return nil, fmt.Errorf("previous version and current version are same")
}
return &UpgradeManager{
Logger: logger,
DBClient: dbClient,
PreviousVersion: config.Value.(string),
TargetVersion: currentVersion,
}, nil
}
// getUpgradePath returns a map that determines the possible upgrade path for any upgrade
func (m *UpgradeManager) getUpgradePath() map[string]UpgradeExecutor {
// key : previous version,
// value :{ Version Manger that upgrades the system from priv version to next, NextVersion points to next version in the path}
return map[string]UpgradeExecutor{
"2.3.0": {
NextVersion: "2.4.0",
VersionManager: v2_4_0.NewVersionManger(m.Logger, m.DBClient),
},
"2.4.0": {
NextVersion: "2.5.0",
VersionManager: nil,
},
// latest version no more upgrades available
"2.5.0": {
NextVersion: "",
VersionManager: nil,
},
}
}
// verifyPath verifies whether the current upgrade from PreviousVersion to TargetVersion
// is possible given the configured upgrade path
func (m *UpgradeManager) verifyPath(upgradePath map[string]UpgradeExecutor) error {
if m.PreviousVersion == m.TargetVersion {
return fmt.Errorf("previous version and current version are same")
}
_, okP := upgradePath[m.PreviousVersion]
_, okT := upgradePath[m.TargetVersion]
if !okP && !okT {
return fmt.Errorf("previous version=%v or target version=%v not found in upgrade path", m.PreviousVersion, m.TargetVersion)
}
versionIterator := m.PreviousVersion
for versionIterator != "" {
versionIterator = upgradePath[versionIterator].NextVersion
if versionIterator == m.TargetVersion {
return nil
}
}
return fmt.Errorf("upgrade path not found from previous version=%v to target version=%v", m.PreviousVersion, m.TargetVersion)
}
// Run executes all the steps required in the upgrade path from PreviousVersion to TargetVersion
func (m *UpgradeManager) Run() error {
upgradePath := m.getUpgradePath()
// verify if upgrade possible
if err := m.verifyPath(upgradePath); err != nil {
return err
}
// start upgrade from previous version to target version
versionIterator := m.PreviousVersion
// loop till the target version is reached
for versionIterator != m.TargetVersion {
if err := upgradePath[versionIterator].VersionManager.Run(); err != nil {
return fmt.Errorf("failed to upgrade to version %v, error : %w", versionIterator, err)
}
versionIterator = upgradePath[versionIterator].NextVersion
}
err := database.UpdateVersion(m.DBClient, m.TargetVersion)
if err != nil {
return fmt.Errorf("failed to update version in server config collection, error=%w", err)
}
return nil
}