diff --git a/cmd/upgrade/quickon.go b/cmd/upgrade/quickon.go index 29d5cc1e..6c7144a5 100644 --- a/cmd/upgrade/quickon.go +++ b/cmd/upgrade/quickon.go @@ -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 } @@ -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 } @@ -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) } diff --git a/pkg/qucheng/upgrade/types.go b/pkg/qucheng/upgrade/types.go index a85d06a6..9df79d57 100644 --- a/pkg/qucheng/upgrade/types.go +++ b/pkg/qucheng/upgrade/types.go @@ -6,6 +6,8 @@ package upgrade +import "github.com/easysoft/qcadmin/common" + type Version struct { Components []ComponentVersion `json:",omitempty"` } @@ -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, + }, +} diff --git a/pkg/qucheng/upgrade/upgrade.go b/pkg/qucheng/upgrade/upgrade.go index 1076a573..b4cb1092 100644 --- a/pkg/qucheng/upgrade/upgrade.go +++ b/pkg/qucheng/upgrade/upgrade.go @@ -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" @@ -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 } @@ -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) @@ -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)