-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add backup subcmd Signed-off-by: ysicing <[email protected]>
- Loading branch information
Showing
10 changed files
with
194 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) 2021-2023 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.com) All rights reserved. | ||
// Use of this source code is covered by the following dual licenses: | ||
// (1) Z PUBLIC LICENSE 1.2 (ZPL 1.2) | ||
// (2) Affero General Public License 3.0 (AGPL 3.0) | ||
// license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/easysoft/qcadmin/cmd/backup" | ||
"github.com/easysoft/qcadmin/internal/pkg/util/factory" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newCmdBackup(f factory.Factory) *cobra.Command { | ||
backupCmd := &cobra.Command{ | ||
Use: "backup", | ||
Short: "Backup commands", | ||
Version: "20230925", | ||
} | ||
backupCmd.AddCommand(backup.NewCmdBackupCluster(f)) | ||
backupCmd.AddCommand(backup.NewCmdBackupApp(f)) | ||
return backupCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2021-2023 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.com) All rights reserved. | ||
// Use of this source code is covered by the following dual licenses: | ||
// (1) Z PUBLIC LICENSE 1.2 (ZPL 1.2) | ||
// (2) Affero General Public License 3.0 (AGPL 3.0) | ||
// license that can be found in the LICENSE file. | ||
|
||
package backup | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
|
||
"github.com/easysoft/qcadmin/internal/api/cne" | ||
"github.com/easysoft/qcadmin/internal/pkg/util/factory" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmdBackupApp(f factory.Factory) *cobra.Command { | ||
var app, ns, backupName string | ||
var err error | ||
log := f.GetLog() | ||
bc := &cobra.Command{ | ||
Use: "app", | ||
Short: "backup app", | ||
Long: "backup app", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
log.Infof("start backup app: %s", app) | ||
cneClient := cne.NewCneAPI() | ||
if backupName == "" { | ||
backupName, err = cneClient.AppBackUP(ns, app) | ||
if err != nil { | ||
log.Errorf("backup app %s failed, reason: %v", app, err) | ||
return | ||
} | ||
} | ||
|
||
timeout := 5 * time.Minute | ||
costtime := time.Now() | ||
deadline := time.Now().Add(timeout) | ||
for { | ||
backupStatus, err := cneClient.AppBackUPStatus(ns, app, backupName) | ||
if err != nil { | ||
log.Errorf("backup app %s failed, reason: %v", app, err) | ||
return | ||
} | ||
if strings.ToLower(backupStatus.Status) == "completed" { | ||
log.Infof("backup app %s(%s) success, cost: %vs", app, backupName, time.Since(costtime).Seconds()) | ||
return | ||
} | ||
if strings.ToLower(backupStatus.Status) == "failed" { | ||
log.Errorf("backup app %s(%s) failed, reason: %s", app, backupName, backupStatus.Reason) | ||
return | ||
} | ||
if time.Now().After(deadline) { | ||
log.Errorf("backup app %s(%s) timeout", app, backupName) | ||
return | ||
} | ||
log.Infof("backup app %s(%s) status: %s", app, backupName, backupStatus.Status) | ||
time.Sleep(5 * time.Second) | ||
} | ||
}, | ||
} | ||
bc.Flags().StringVar(&app, "app", "", "app chart name") | ||
bc.Flags().StringVarP(&ns, "namespace", "n", "", "namespace") | ||
bc.Flags().StringVarP(&backupName, "backupName", "b", "", "existing backup name") | ||
return bc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2021-2023 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.com) All rights reserved. | ||
// Use of this source code is covered by the following dual licenses: | ||
// (1) Z PUBLIC LICENSE 1.2 (ZPL 1.2) | ||
// (2) Affero General Public License 3.0 (AGPL 3.0) | ||
// license that can be found in the LICENSE file. | ||
|
||
package cne | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/easysoft/qcadmin/common" | ||
"github.com/easysoft/qcadmin/internal/app/config" | ||
"github.com/imroc/req/v3" | ||
) | ||
|
||
type CneAPI struct { | ||
Req *req.Request | ||
Endpoint string | ||
} | ||
|
||
func NewCneAPI() *CneAPI { | ||
config, _ := config.LoadConfig() | ||
c := req.C().R().SetHeader(common.CneAPITokenHeader, config.APIToken) | ||
return &CneAPI{ | ||
Req: c, | ||
Endpoint: fmt.Sprintf("http://%s:32380", config.Cluster.InitNode), | ||
} | ||
} | ||
|
||
func (c *CneAPI) AppBackUP(ns, chartName string) (string, error) { | ||
var result AppBackUPResp | ||
appbackup := &AppBackUPReq{Namespace: ns, Name: chartName} | ||
resp, err := c.Req.SetBody(appbackup). | ||
SetSuccessResult(&result). | ||
Post(c.Endpoint + "/api/cne/app/backup") | ||
if resp.IsSuccessState() { | ||
return result.Data.BackupName, nil | ||
} | ||
return "", err | ||
} | ||
|
||
func (c *CneAPI) AppBackUPStatus(ns, chartName, backupName string) (*AppBackUPStatus, error) { | ||
var result AppBackUPStatusResp | ||
resp, err := c.Req.SetQueryParams(map[string]string{ | ||
"namespace": ns, | ||
"name": chartName, | ||
"backup_name": backupName, | ||
}). | ||
SetSuccessResult(&result). | ||
Get(c.Endpoint + "/api/cne/app/backup/status") | ||
if resp.IsSuccessState() { | ||
return &result.Data, nil | ||
} | ||
return nil, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) 2021-2023 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.com) All rights reserved. | ||
// Use of this source code is covered by the following dual licenses: | ||
// (1) Z PUBLIC LICENSE 1.2 (ZPL 1.2) | ||
// (2) Affero General Public License 3.0 (AGPL 3.0) | ||
// license that can be found in the LICENSE file. | ||
|
||
package cne | ||
|
||
type AppBackUPReq struct { | ||
Cluster string `json:"cluster,omitempty"` | ||
Namespace string `json:"namespace"` | ||
Name string `json:"name"` // chartname | ||
UserName string `json:"username,omitempty"` // 操作用户 | ||
} | ||
|
||
type AppBackUPResp struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
Data AppBackUPData `json:"data"` | ||
} | ||
|
||
type AppBackUPData struct { | ||
BackupName string `json:"backup_name"` | ||
CreateTime int64 `json:"create_time"` | ||
} | ||
|
||
type AppBackUPStatusResp struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
Data AppBackUPStatus `json:"data"` | ||
} | ||
|
||
type AppBackUPStatus struct { | ||
Reason string `json:"reason,omitempty"` | ||
Status string `json:"status"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters