Skip to content

Commit f74321b

Browse files
committed
add check upgrade in ua
Signed-off-by: Patrick Zhao <zhaoyu@koderover.com>
1 parent bea178b commit f74321b

4 files changed

Lines changed: 125 additions & 2 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright 2021 The KodeRover Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
"github.com/spf13/viper"
24+
25+
"github.com/koderover/zadig/v2/pkg/shared/client/plutusenterprise"
26+
e "github.com/koderover/zadig/v2/pkg/tool/errors"
27+
"github.com/koderover/zadig/v2/pkg/tool/log"
28+
)
29+
30+
func init() {
31+
rootCmd.AddCommand(checkUpgradeCmd)
32+
33+
checkUpgradeCmd.PersistentFlags().StringP("from-version", "f", oldestVersion, "current version to migrate from")
34+
checkUpgradeCmd.PersistentFlags().StringP("to-version", "t", "", "target version to migrate to")
35+
_ = viper.BindPFlag("fromVersion", checkUpgradeCmd.PersistentFlags().Lookup("from-version"))
36+
_ = viper.BindPFlag("toVersion", checkUpgradeCmd.PersistentFlags().Lookup("to-version"))
37+
}
38+
39+
var checkUpgradeCmd = &cobra.Command{
40+
Use: "check",
41+
Short: "check if the upgrade is possible",
42+
Long: `check if the upgrade is possible.`,
43+
PreRunE: func(cmd *cobra.Command, args []string) error {
44+
return preRun()
45+
},
46+
Run: func(cmd *cobra.Command, args []string) {
47+
if err := runCheckUpgrade(); err != nil {
48+
log.Fatal(err)
49+
}
50+
},
51+
PostRun: func(cmd *cobra.Command, args []string) {
52+
if err := postRun(); err != nil {
53+
fmt.Println(err)
54+
}
55+
},
56+
}
57+
58+
func runCheckUpgrade() error {
59+
from := viper.GetString("fromVersion")
60+
to := viper.GetString("toVersion")
61+
62+
if len(from) == 0 {
63+
from = oldestVersion
64+
}
65+
if len(to) == 0 {
66+
return fmt.Errorf("target version not assigned")
67+
}
68+
69+
log.Infof("Checking upgrade from %s to %s", from, to)
70+
71+
resp, err := plutusenterprise.New().CheckUpgrade(from, to)
72+
if err != nil {
73+
nerr := e.ErrUpgradeNotAllowed.AddErr(err)
74+
log.Error(nerr)
75+
return nerr
76+
}
77+
if !resp.AllowUpgrade {
78+
nerr := e.ErrUpgradeNotAllowed
79+
log.Error(nerr)
80+
return nerr
81+
}
82+
83+
log.Info("Upgrade check passed")
84+
return nil
85+
}

pkg/cli/upgradeassistant/cmd/migrate.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
_ "github.com/koderover/zadig/v2/pkg/cli/upgradeassistant/cmd/migrate"
3030
"github.com/koderover/zadig/v2/pkg/cli/upgradeassistant/internal/upgradepath"
3131
"github.com/koderover/zadig/v2/pkg/setting"
32+
"github.com/koderover/zadig/v2/pkg/shared/client/plutusenterprise"
33+
e "github.com/koderover/zadig/v2/pkg/tool/errors"
3234
"github.com/koderover/zadig/v2/pkg/tool/log"
3335
mongotool "github.com/koderover/zadig/v2/pkg/tool/mongo"
3436
)
@@ -159,7 +161,19 @@ func run() error {
159161
upgradepath.AddHandler(upgradepath.VersionDatas.VersionIndex(rh.FromVersion), upgradepath.VersionDatas.VersionIndex(rh.ToVersion), rh.Fn)
160162
}
161163

162-
err := upgradepath.UpgradeWithBestPath(from, to)
164+
resp, err := plutusenterprise.New().CheckUpgrade(from, to)
165+
if err != nil {
166+
nerr := e.ErrUpgradeNotAllowed.AddErr(err)
167+
log.Error(nerr)
168+
return nerr
169+
}
170+
if !resp.AllowUpgrade {
171+
nerr := e.ErrUpgradeNotAllowed
172+
log.Error(nerr)
173+
return nerr
174+
}
175+
176+
err = upgradepath.UpgradeWithBestPath(from, to)
163177
if err == nil {
164178
log.Info("Migration finished")
165179
}

pkg/shared/client/plutusenterprise/plutusenterprise.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,26 @@ func (c *Client) Health() error {
7272
_, err := c.Get(url)
7373
return err
7474
}
75+
76+
type UpgradeCheckResponse struct {
77+
AllowUpgrade bool `json:"allow_upgrade"`
78+
IsUpgrade bool `json:"is_upgrade"`
79+
FromVersion string `json:"from_version"`
80+
ToVersion string `json:"to_version"`
81+
MaintenanceExpireAt int64 `json:"maintenance_expire_at"`
82+
}
83+
84+
type checkUpgradePermissionReq struct {
85+
FromVersion string `json:"from_version"`
86+
ToVersion string `json:"to_version"`
87+
}
88+
89+
func (c *Client) CheckUpgrade(fromVersion, toVersion string) (*UpgradeCheckResponse, error) {
90+
url := "/license/upgrade/check"
91+
res := &UpgradeCheckResponse{}
92+
_, err := c.Post(url, httpclient.SetBody(&checkUpgradePermissionReq{
93+
FromVersion: fromVersion,
94+
ToVersion: toVersion,
95+
}), httpclient.SetResult(res))
96+
return res, err
97+
}

pkg/tool/errors/http_errors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,8 @@ var (
932932
//-----------------------------------------------------------------------------------------------
933933
// License APIs Range: 7050 - 7059
934934
//-----------------------------------------------------------------------------------------------
935-
ErrLicenseInvalid = NewHTTPError(7050, "用户许可证不可用,请检查许可证后重试")
935+
ErrLicenseInvalid = NewHTTPError(7050, "用户许可证不可用,请检查许可证后重试")
936+
ErrUpgradeNotAllowed = NewHTTPError(6695, "当前许可证维保期已过,不允许升级")
936937

937938
//-----------------------------------------------------------------------------------------------
938939
// Istio Grayscale APIs Range: 7060 - 7069

0 commit comments

Comments
 (0)