Skip to content

Commit

Permalink
fix(upgrade): fix custom upgrade
Browse files Browse the repository at this point in the history
fix custom upgrade

Signed-off-by: ysicing <[email protected]>
  • Loading branch information
ysicing committed Oct 16, 2023
1 parent fe2d57e commit 85437ba
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
4 changes: 3 additions & 1 deletion cmd/upgrade/quickon.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
// Option is a struct that defines a command call for "upgrade"
type Option struct {
Version string
Test bool
log log.Logger
}

Expand All @@ -38,6 +39,7 @@ func NewUpgradePlatform(f factory.Factory) *cobra.Command {
return upcmd.Run()
},
}
up.Flags().BoolVar(&upcmd.Test, "t", false, "upgrade test mode, only for test")
return up
}

Expand All @@ -46,7 +48,7 @@ func (cmd *Option) Run() error {
// Run the upgrade command
cmd.log.Info("check update...")
cmd.log.Debugf("gen new version manifest")
err := upgrade.Upgrade(cmd.Version, cmd.log)
err := upgrade.Upgrade(cmd.Version, cmd.Test, cmd.log)
if err != nil {
return errors.Errorf("couldn't upgrade: %v", err)
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/qucheng/upgrade/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package upgrade

import "github.com/easysoft/qcadmin/common"

type Version struct {
Components []ComponentVersion `json:",omitempty"`
}
Expand All @@ -22,3 +24,32 @@ type CVersion struct {
AppVersion string
ChartVersion string
}

type ZtUpgrade struct {
Name string
Key common.QuickonType
Version string
}

var selectItems = []ZtUpgrade{
{
Name: "开源版",
Key: common.ZenTaoOSSType,
Version: common.DefaultZentaoDevOPSOSSVersion,
},
{
Name: "企业版",
Key: common.ZenTaoBizType,
Version: common.DefaultZentaoDevOPSBizVersion,
},
{
Name: "旗舰版",
Key: common.ZenTaoMaxType,
Version: common.DefaultZentaoDevOPSMaxVersion,
},
{
Name: "IPD",
Key: common.ZenTaoIPDType,
Version: common.DefaultZentaoDevOPSIPDVersion,
},
}
46 changes: 42 additions & 4 deletions pkg/qucheng/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/easysoft/qcadmin/common"
"github.com/easysoft/qcadmin/internal/app/config"
"github.com/ergoapi/util/color"
"github.com/ergoapi/util/confirm"
"github.com/manifoldco/promptui"

"github.com/cockroachdb/errors"
"github.com/easysoft/qcadmin/internal/pkg/util/helm"
Expand Down Expand Up @@ -56,7 +58,7 @@ func (opt *Option) Fetch(ns, name string) (ComponentVersion, error) {
if cmv.CanUpgrade {
cmv.UpgradeMessage = fmt.Sprintf("Now you can use %s to upgrade component %s to the latest version", color.SGreen("%s upgrade %s", os.Args[0], name), name)
}
opt.log.Debugf("local: %s(%s), remote : %s(%s), upgrade: %v", localcv, localav, remotecv, remoteav, cmv.CanUpgrade)
opt.log.Debugf("local: %s(%s), remote: %s(%s), upgrade: %v", localcv, localav, remotecv, remoteav, cmv.CanUpgrade)
return cmv, err
}

Expand Down Expand Up @@ -86,7 +88,7 @@ func (opt *Option) fetchCR(ns, name string) (string, string, error) {
return last.Chart.Version, last.Chart.AppVersion, nil
}

func Upgrade(flagVersion string, log log.Logger) error {
func Upgrade(flagVersion string, testmode bool, log log.Logger) error {
helmClient, _ := helm.NewClient(&helm.Config{Namespace: common.GetDefaultSystemNamespace(true)})
if err := helmClient.UpdateRepo(); err != nil {
log.Errorf("update repo failed, reason: %v", err)
Expand All @@ -110,9 +112,45 @@ func Upgrade(flagVersion string, log log.Logger) error {
product := deploy.(map[string]interface{})["product"]
versions := deploy.(map[string]interface{})["versions"]
appoldVersion := versions.(map[string]interface{})[product.(string)]
appnewVersion := fmt.Sprintf("%s%s.k8s", product, common.GetVersion(true, product.(string), ""))
log.Infof("devops mode, product: %v, oldversion: %v, newversion: %v", product, appoldVersion, appnewVersion)
switch product {
case common.ZenTaoBizType.String():
selectItems = selectItems[1:]
case common.ZenTaoMaxType.String():
selectItems = selectItems[2:]
case common.ZenTaoIPDType.String():
selectItems = selectItems[3:]
}
log.Infof("current version: %v(%v)", product, appoldVersion)
selectApp := promptui.Select{
Label: "select upgrade version",
Items: selectItems,
Templates: &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "\U0001F449 {{ .Name | cyan }} {{ .Version }}",
Inactive: " {{ .Name | red| cyan }} {{ .Version }}",
Selected: "\U0001F389 {{ .Name | green | cyan }} {{ .Version }}",
},
Size: 5,
}
it, _, _ := selectApp.Run()
appnewVersion := fmt.Sprintf("%s%s.k8s", selectItems[it].Key.String(), common.GetVersion(true, selectItems[it].Key.String(), ""))
log.Debugf("devops mode, product: %v, oldversion: %v, newversion: %v", product, appoldVersion, appnewVersion)
defaultValue["deploy"].(map[string]interface{})["versions"].(map[string]interface{})[product.(string)] = appnewVersion
defaultValue["deploy"].(map[string]interface{})["product"] = selectItems[it].Key.String()
if selectItems[it].Key != common.ZenTaoOSSType && selectItems[it].Key.String() != product.(string) {
log.Warnf("切换版本升级(如开源版升级到企业版), 可能导致因版本授权问题无法正常使用, 如有问题请联系技术支持!")
}
msg := fmt.Sprintf("Are you sure to upgrade from %v(%v) to %v(%v)", product, appoldVersion, selectItems[it].Key.String(), appnewVersion)
status, _ := confirm.Confirm(msg)
if !status {
log.Warnf("upgrade %s canceled", cv.Name)
return nil
}
if testmode {
defaultValue["image"] = map[string]interface{}{
"repository": "test/zentao",
}
}
}
if _, err := helmClient.Upgrade(cv.Name, common.DefaultHelmRepoName, cv.Name, "", defaultValue); err != nil {
log.Warnf("upgrade %s failed, reason: %v", cv.Name, err)
Expand Down

0 comments on commit 85437ba

Please sign in to comment.