2018-05-25 18:24:17 +08:00
|
|
|
// RAINBOND, Application Management Platform
|
|
|
|
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version. For any non-GPL usage of Rainbond,
|
|
|
|
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
|
|
|
|
// must be obtained first.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package exector
|
|
|
|
|
|
|
|
import (
|
2018-05-28 11:14:56 +08:00
|
|
|
"context"
|
2018-05-25 18:24:17 +08:00
|
|
|
"fmt"
|
2020-03-10 02:01:05 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2020-03-18 16:55:01 +08:00
|
|
|
"strconv"
|
2020-03-10 02:01:05 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-05-28 11:14:56 +08:00
|
|
|
"github.com/coreos/etcd/clientv3"
|
2019-06-20 13:59:19 +08:00
|
|
|
"github.com/docker/docker/client"
|
2020-03-18 17:37:45 +08:00
|
|
|
"github.com/goodrain/rainbond/builder"
|
2019-11-05 11:32:55 +08:00
|
|
|
"github.com/goodrain/rainbond/builder/cloudos"
|
2020-03-18 17:37:45 +08:00
|
|
|
"github.com/goodrain/rainbond/builder/parser"
|
2018-05-25 18:24:17 +08:00
|
|
|
"github.com/goodrain/rainbond/builder/sources"
|
|
|
|
"github.com/goodrain/rainbond/db"
|
2019-10-14 17:21:31 +08:00
|
|
|
"github.com/goodrain/rainbond/db/errors"
|
2019-06-20 13:59:19 +08:00
|
|
|
dbmodel "github.com/goodrain/rainbond/db/model"
|
2018-05-25 18:24:17 +08:00
|
|
|
"github.com/goodrain/rainbond/event"
|
|
|
|
"github.com/goodrain/rainbond/util"
|
|
|
|
"github.com/pquerna/ffjson/ffjson"
|
2020-11-25 16:39:38 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-05-25 18:24:17 +08:00
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
)
|
|
|
|
|
|
|
|
//BackupAPPRestore restrore the group app backup
|
|
|
|
type BackupAPPRestore struct {
|
|
|
|
//full-online,full-offline
|
|
|
|
EventID string
|
|
|
|
BackupID string `json:"backup_id"`
|
|
|
|
TenantID string `json:"tenant_id"`
|
|
|
|
Logger event.Logger
|
|
|
|
//RestoreMode(cdct) current datacenter and current tenant
|
|
|
|
//RestoreMode(cdot) current datacenter and other tenant
|
|
|
|
//RestoreMode(od) other datacenter
|
2018-05-28 12:36:41 +08:00
|
|
|
RestoreMode string `json:"restore_mode"`
|
|
|
|
RestoreID string `json:"restore_id"`
|
|
|
|
DockerClient *client.Client
|
|
|
|
cacheDir string
|
|
|
|
//serviceChange key: oldServiceID
|
2018-05-25 18:24:17 +08:00
|
|
|
serviceChange map[string]*Info
|
2020-03-18 16:55:01 +08:00
|
|
|
volumeIDMap map[uint]uint
|
2018-05-28 11:14:56 +08:00
|
|
|
etcdcli *clientv3.Client
|
2019-11-05 11:32:55 +08:00
|
|
|
|
|
|
|
S3Config struct {
|
|
|
|
Provider string `json:"provider"`
|
|
|
|
Endpoint string `json:"endpoint"`
|
|
|
|
AccessKey string `json:"access_key"`
|
|
|
|
SecretKey string `json:"secret_key"`
|
|
|
|
BucketName string `json:"bucket_name"`
|
|
|
|
} `json:"s3_config"`
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Info service cache info
|
|
|
|
type Info struct {
|
|
|
|
ServiceID string
|
|
|
|
ServiceAlias string
|
|
|
|
Status string
|
|
|
|
LBPorts map[int]int
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RegisterWorker("backup_apps_restore", BackupAPPRestoreCreater)
|
|
|
|
}
|
|
|
|
|
|
|
|
//BackupAPPRestoreCreater create
|
2018-05-28 11:14:56 +08:00
|
|
|
func BackupAPPRestoreCreater(in []byte, m *exectorManager) (TaskWorker, error) {
|
2018-05-25 18:24:17 +08:00
|
|
|
eventID := gjson.GetBytes(in, "event_id").String()
|
|
|
|
logger := event.GetManager().GetLogger(eventID)
|
|
|
|
backupRestore := &BackupAPPRestore{
|
|
|
|
Logger: logger,
|
|
|
|
EventID: eventID,
|
2018-05-28 11:14:56 +08:00
|
|
|
DockerClient: m.DockerClient,
|
|
|
|
etcdcli: m.EtcdCli,
|
2018-05-25 18:24:17 +08:00
|
|
|
serviceChange: make(map[string]*Info, 0),
|
2020-03-18 16:55:01 +08:00
|
|
|
volumeIDMap: make(map[uint]uint),
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
|
|
|
if err := ffjson.Unmarshal(in, &backupRestore); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return backupRestore, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//Run Run
|
|
|
|
func (b *BackupAPPRestore) Run(timeout time.Duration) error {
|
|
|
|
//download or copy backup data
|
|
|
|
backup, err := db.GetManager().AppBackupDao().GetAppBackup(b.BackupID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-05 11:32:55 +08:00
|
|
|
if backup.Status != "success" || backup.SourceDir == "" || backup.BackupMode == "" {
|
2018-05-25 18:24:17 +08:00
|
|
|
return fmt.Errorf("backup can not be restore")
|
|
|
|
}
|
2019-11-05 11:32:55 +08:00
|
|
|
|
|
|
|
cacheDir := fmt.Sprintf("/grdata/cache/tmp/%s/%s", b.BackupID, util.NewUUID())
|
2018-05-25 18:24:17 +08:00
|
|
|
if err := util.CheckAndCreateDir(cacheDir); err != nil {
|
|
|
|
return fmt.Errorf("create cache dir error %s", err.Error())
|
|
|
|
}
|
2021-08-04 11:52:59 +08:00
|
|
|
// delete the cache data
|
2021-08-17 19:40:10 +08:00
|
|
|
defer b.deleteCache(cacheDir)
|
2021-08-04 11:52:59 +08:00
|
|
|
|
2018-05-25 18:24:17 +08:00
|
|
|
b.cacheDir = cacheDir
|
2019-11-05 11:32:55 +08:00
|
|
|
switch backup.BackupMode {
|
|
|
|
case "full-online":
|
|
|
|
if err := b.downloadFromS3(backup.SourceDir); err != nil {
|
|
|
|
return fmt.Errorf("error downloading file from s3: %v", err)
|
|
|
|
}
|
2018-05-25 18:24:17 +08:00
|
|
|
default:
|
2018-05-28 12:18:29 +08:00
|
|
|
b.downloadFromLocal(backup)
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
2019-11-05 11:34:23 +08:00
|
|
|
|
2018-05-25 18:24:17 +08:00
|
|
|
//read metadata file
|
2019-11-05 11:32:55 +08:00
|
|
|
metadata, err := ioutil.ReadFile(path.Join(b.cacheDir, "region_apps_metadata.json"))
|
2018-05-25 18:24:17 +08:00
|
|
|
if err != nil {
|
2019-11-05 11:32:55 +08:00
|
|
|
return fmt.Errorf("error reading file: %v", err)
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
|
|
|
|
metaVersion, err := judgeMetadataVersion(metadata)
|
|
|
|
if err != nil {
|
|
|
|
b.Logger.Info(fmt.Sprintf("Failed to judge the version of metadata"), map[string]string{"step": "backup_builder", "status": "failure"})
|
2018-05-25 18:24:17 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
|
|
|
|
var appSnapshot AppSnapshot
|
|
|
|
var svcSnapshot []*RegionServiceSnapshot
|
|
|
|
if metaVersion == OldMetadata {
|
|
|
|
if err := ffjson.Unmarshal(metadata, &svcSnapshot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
appSnapshot = AppSnapshot{
|
|
|
|
Services: svcSnapshot,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := ffjson.Unmarshal(metadata, &appSnapshot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Logger.Info("读取备份元数据完成", map[string]string{"step": "restore_builder", "status": "running"})
|
2019-11-05 11:32:55 +08:00
|
|
|
logrus.Infof("backup id: %s; successfully read metadata.", b.BackupID)
|
2018-05-25 18:24:17 +08:00
|
|
|
//modify the metadata
|
2019-10-14 17:21:31 +08:00
|
|
|
if err := b.modify(&appSnapshot); err != nil {
|
2018-05-25 18:24:17 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
//restore metadata to db
|
2019-10-14 17:21:31 +08:00
|
|
|
if err := b.restoreMetadata(&appSnapshot); err != nil {
|
2018-05-25 18:24:17 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.Logger.Info("恢复备份元数据完成", map[string]string{"step": "restore_builder", "status": "success"})
|
2019-11-05 11:32:55 +08:00
|
|
|
logrus.Infof("backup id: %s; successfully restore metadata.", b.BackupID)
|
2018-05-25 18:24:17 +08:00
|
|
|
//If the following error occurs, delete the data from the database
|
2019-10-13 19:56:13 +08:00
|
|
|
//restore all app all build version and data
|
2019-10-14 17:21:31 +08:00
|
|
|
if err := b.restoreVersionAndData(backup, &appSnapshot); err != nil {
|
2018-05-25 18:24:17 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
|
2018-05-25 18:24:17 +08:00
|
|
|
//save result
|
2018-05-28 12:18:29 +08:00
|
|
|
b.saveResult("success", "")
|
2019-11-05 11:32:55 +08:00
|
|
|
logrus.Infof("backup id: %s; successfully restore backup.", b.BackupID)
|
2019-10-14 17:21:31 +08:00
|
|
|
b.Logger.Info("恢复成功", map[string]string{"step": "restore_builder", "status": "success"})
|
2018-05-25 18:24:17 +08:00
|
|
|
return nil
|
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
|
2021-08-17 19:40:10 +08:00
|
|
|
func (b *BackupAPPRestore) deleteCache(dir string) error {
|
|
|
|
logrus.Infof("delete cache %s", dir)
|
|
|
|
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// do not delete the metadata file
|
|
|
|
if strings.HasSuffix(path, "console_apps_metadata.json") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return os.RemoveAll(path)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:21:31 +08:00
|
|
|
func (b *BackupAPPRestore) restoreVersionAndData(backup *dbmodel.AppBackup, appSnapshot *AppSnapshot) error {
|
|
|
|
for _, app := range appSnapshot.Services {
|
2018-05-25 18:24:17 +08:00
|
|
|
//backup app image or code slug file
|
|
|
|
b.Logger.Info(fmt.Sprintf("开始恢复应用(%s)运行环境", app.Service.ServiceAlias), map[string]string{"step": "restore_builder", "status": "starting"})
|
|
|
|
for _, version := range app.Versions {
|
|
|
|
if version.DeliveredType == "slug" && version.FinalStatus == "success" {
|
|
|
|
if err := b.downloadSlug(backup, app, version); err != nil {
|
|
|
|
logrus.Errorf("download app slug file error.%s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if version.DeliveredType == "image" && version.FinalStatus == "success" {
|
|
|
|
if err := b.downloadImage(backup, app, version); err != nil {
|
|
|
|
logrus.Errorf("download app image error.%s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
b.Logger.Info(fmt.Sprintf("完成恢复应用(%s)运行环境", app.Service.ServiceAlias), map[string]string{"step": "restore_builder", "status": "running"})
|
2019-10-13 19:56:13 +08:00
|
|
|
|
2018-05-25 18:24:17 +08:00
|
|
|
b.Logger.Info(fmt.Sprintf("开始恢复应用(%s)持久化数据", app.Service.ServiceAlias), map[string]string{"step": "restore_builder", "status": "starting"})
|
|
|
|
//restore app data
|
2020-03-18 16:55:01 +08:00
|
|
|
|
|
|
|
//if all data backup file exist, restore all data directly
|
|
|
|
allDataFilePath := fmt.Sprintf("%s/data_%s/%s.zip", b.cacheDir, b.getOldServiceID(app.ServiceID), "__all_data")
|
|
|
|
allDataRestore := false
|
|
|
|
allTmpDir := fmt.Sprintf("/grdata/tmp/%s", app.ServiceID)
|
|
|
|
if exist, _ := util.FileExists(allDataFilePath); exist {
|
2020-12-02 18:08:35 +08:00
|
|
|
logrus.Infof("unzip all data from %s to %s", allDataFilePath, allTmpDir)
|
2020-03-18 16:55:01 +08:00
|
|
|
if err := util.Unzip(allDataFilePath, allTmpDir); err != nil {
|
|
|
|
logrus.Errorf("unzip all data file failure %s", err.Error())
|
|
|
|
} else {
|
|
|
|
allDataRestore = true
|
|
|
|
}
|
|
|
|
}
|
2018-05-25 18:24:17 +08:00
|
|
|
for _, volume := range app.ServiceVolume {
|
2019-10-13 19:56:13 +08:00
|
|
|
if volume.HostPath == "" {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-18 16:55:01 +08:00
|
|
|
var tmpDir string
|
|
|
|
if !allDataRestore {
|
|
|
|
dstDir := fmt.Sprintf("%s/data_%s/%s.zip", b.cacheDir, b.getOldServiceID(app.ServiceID), strings.Replace(volume.VolumeName, "/", "", -1))
|
|
|
|
tmpDir = fmt.Sprintf("/grdata/tmp/%s_%d", volume.ServiceID, volume.ID)
|
2020-12-02 18:08:35 +08:00
|
|
|
logrus.Infof("unzip %s to %s", dstDir, tmpDir)
|
2020-03-18 16:55:01 +08:00
|
|
|
if err := util.Unzip(dstDir, tmpDir); err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "no such file") {
|
|
|
|
logrus.Errorf("restore service(%s) volume(%s) data error.%s", app.ServiceID, volume.VolumeName, err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//backup data is not exist because dir is empty.
|
|
|
|
//so create host path and continue
|
|
|
|
os.MkdirAll(volume.HostPath, 0777)
|
|
|
|
continue
|
2019-10-13 19:56:13 +08:00
|
|
|
}
|
2020-03-18 16:55:01 +08:00
|
|
|
} else {
|
2020-03-18 18:08:21 +08:00
|
|
|
tmpDir = path.Join(allTmpDir, b.getOldServiceID(app.ServiceID))
|
2019-10-13 19:56:13 +08:00
|
|
|
}
|
2020-02-18 22:41:18 +08:00
|
|
|
|
2019-10-13 19:56:13 +08:00
|
|
|
//if app type is statefulset, change pod hostpath
|
2020-03-05 18:39:38 +08:00
|
|
|
if app.Service.IsState() {
|
2020-12-02 18:08:35 +08:00
|
|
|
logrus.Infof("app type is statefulset, change pod hostpath %s. tmp dir: %s", volume.HostPath, tmpDir)
|
2019-10-13 19:56:13 +08:00
|
|
|
//Next two level directory
|
2020-12-02 18:08:35 +08:00
|
|
|
list, err := util.GetDirList(path.Join(tmpDir, volume.VolumePath), 1)
|
2019-10-13 19:56:13 +08:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("restore statefulset service(%s) volume(%s) data error.%s", app.ServiceID, volume.VolumeName, err.Error())
|
|
|
|
return err
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
2019-10-13 19:56:13 +08:00
|
|
|
for _, path := range list {
|
2020-12-02 18:08:35 +08:00
|
|
|
logrus.Infof("handle path %s", path)
|
2019-10-13 19:56:13 +08:00
|
|
|
newNameTmp := strings.Split(filepath.Base(path), "-")
|
2020-03-18 18:08:21 +08:00
|
|
|
// after version 5.0.4, path name is pod name. eg gr123456-0
|
2020-03-18 16:55:01 +08:00
|
|
|
if len(newNameTmp) == 2 {
|
|
|
|
newNameTmp[0] = b.serviceChange[b.getOldServiceID(app.ServiceID)].ServiceAlias
|
|
|
|
}
|
2020-03-18 18:08:21 +08:00
|
|
|
// before version 5.0.4, path name is pvc name, eg manual16-grcaa708-0
|
2020-03-18 16:55:01 +08:00
|
|
|
if len(newNameTmp) == 3 {
|
|
|
|
newNameTmp[1] = b.serviceChange[b.getOldServiceID(app.ServiceID)].ServiceAlias
|
|
|
|
oldVolumeID, _ := strconv.Atoi(newNameTmp[0][6:])
|
|
|
|
if oldVolumeID > 0 {
|
|
|
|
newNameTmp[0] = fmt.Sprintf("manual%d", b.volumeIDMap[uint(oldVolumeID)])
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 19:56:13 +08:00
|
|
|
newName := strings.Join(newNameTmp, "-")
|
|
|
|
newpath := filepath.Join(util.GetParentDirectory(path), newName)
|
2020-12-02 18:08:35 +08:00
|
|
|
logrus.Infof("rename %s to %s", path, newpath)
|
2019-10-13 19:56:13 +08:00
|
|
|
err := util.Rename(path, newpath)
|
2018-05-30 10:49:17 +08:00
|
|
|
if err != nil {
|
2019-10-13 19:56:13 +08:00
|
|
|
if strings.Contains(err.Error(), "file exists") {
|
|
|
|
if err := util.MergeDir(path, newpath); err != nil {
|
2018-07-12 16:40:39 +08:00
|
|
|
return err
|
2018-07-11 14:13:47 +08:00
|
|
|
}
|
2019-10-13 19:56:13 +08:00
|
|
|
} else {
|
2018-05-30 10:49:17 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 19:56:13 +08:00
|
|
|
if err := os.Chmod(newpath, 0777); err != nil {
|
2018-07-12 16:40:39 +08:00
|
|
|
return err
|
2018-07-11 14:13:47 +08:00
|
|
|
}
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
2019-10-13 19:56:13 +08:00
|
|
|
}
|
2020-03-18 16:55:01 +08:00
|
|
|
if !allDataRestore {
|
2020-12-02 18:08:35 +08:00
|
|
|
logrus.Infof("rename parent directory from %s to %s", tmpDir, util.GetParentDirectory(volume.HostPath))
|
2020-03-18 16:55:01 +08:00
|
|
|
err := util.Rename(tmpDir, util.GetParentDirectory(volume.HostPath))
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "file exists") {
|
|
|
|
if err := util.MergeDir(tmpDir, util.GetParentDirectory(volume.HostPath)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-13 19:56:13 +08:00
|
|
|
return err
|
|
|
|
}
|
2020-03-18 16:55:01 +08:00
|
|
|
}
|
|
|
|
if err := os.Chmod(volume.HostPath, 0777); err != nil {
|
2018-05-25 18:24:17 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 16:55:01 +08:00
|
|
|
}
|
2020-12-02 18:08:35 +08:00
|
|
|
|
2020-03-18 16:55:01 +08:00
|
|
|
if allDataRestore {
|
2020-12-02 18:08:35 +08:00
|
|
|
dst := fmt.Sprintf("/grdata/tenant/%s/service/%s", app.Service.TenantID, app.Service.ServiceID)
|
|
|
|
err := util.Rename(path.Join(allTmpDir, b.getOldServiceID(app.ServiceID)), dst)
|
2020-03-18 16:55:01 +08:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("rename %s to %s failure %s", path.Join(allTmpDir, b.getOldServiceID(app.ServiceID)), fmt.Sprintf("/grdata/tenant/%s/service/%s", app.Service.TenantID, app.Service.ServiceID), err.Error())
|
2019-10-13 19:56:13 +08:00
|
|
|
}
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
b.Logger.Info(fmt.Sprintf("完成恢复应用(%s)持久化数据", app.Service.ServiceAlias), map[string]string{"step": "restore_builder", "status": "running"})
|
2018-05-25 18:24:17 +08:00
|
|
|
//TODO:relation relation volume data?
|
2019-10-14 17:21:31 +08:00
|
|
|
}
|
2019-10-13 19:56:13 +08:00
|
|
|
|
2019-10-14 17:21:31 +08:00
|
|
|
if len(appSnapshot.PluginBuildVersions) == 0 {
|
|
|
|
return nil
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
|
|
|
|
// restore plugin image
|
|
|
|
for _, pb := range appSnapshot.PluginBuildVersions {
|
|
|
|
dstDir := fmt.Sprintf("%s/plugin_%s/image_%s.tar", b.cacheDir, pb.PluginID, pb.DeployVersion)
|
|
|
|
if err := sources.ImageLoad(b.DockerClient, dstDir, b.Logger); err != nil {
|
|
|
|
b.Logger.Error(util.Translation("load image to local hub error"), map[string]string{"step": "restore_builder", "status": "failure"})
|
|
|
|
logrus.Errorf("dst: %s; failed to load plugin image: %v", dstDir, err)
|
|
|
|
return err
|
|
|
|
}
|
2020-03-18 17:37:45 +08:00
|
|
|
imageName := getNewImageName(pb.BuildLocalImage)
|
|
|
|
if imageName != "" {
|
|
|
|
if err := sources.ImagePush(b.DockerClient, imageName, builder.REGISTRYUSER, builder.REGISTRYPASS, b.Logger, 30); err != nil {
|
|
|
|
b.Logger.Error("push plugin image failure", map[string]string{"step": "restore_builder", "status": "failure"})
|
|
|
|
logrus.Errorf("failure push image %s: %v", imageName, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
}
|
|
|
|
b.Logger.Info("完成恢复插件镜像", map[string]string{"step": "restore_builder", "status": "running"})
|
|
|
|
|
2018-05-25 18:24:17 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-12-02 18:08:35 +08:00
|
|
|
|
2018-05-25 18:24:17 +08:00
|
|
|
func (b *BackupAPPRestore) getOldServiceID(new string) string {
|
|
|
|
for k, v := range b.serviceChange {
|
|
|
|
if v.ServiceID == new {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
func (b *BackupAPPRestore) downloadSlug(backup *dbmodel.AppBackup, app *RegionServiceSnapshot, version *dbmodel.VersionInfo) error {
|
2019-11-05 11:32:55 +08:00
|
|
|
dstDir := fmt.Sprintf("%s/app_%s/slug_%s.tgz", b.cacheDir, b.getOldServiceID(app.ServiceID), version.BuildVersion)
|
|
|
|
if err := sources.CopyFileWithProgress(dstDir, version.DeliveredPath, b.Logger); err != nil {
|
|
|
|
b.Logger.Error(util.Translation("down slug file from local dir error"), map[string]string{"step": "restore_builder", "status": "failure"})
|
|
|
|
logrus.Errorf("copy slug file error when backup app, %s", err.Error())
|
|
|
|
return err
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BackupAPPRestore) downloadImage(backup *dbmodel.AppBackup, app *RegionServiceSnapshot, version *dbmodel.VersionInfo) error {
|
2019-11-05 11:32:55 +08:00
|
|
|
dstDir := fmt.Sprintf("%s/app_%s/image_%s.tar", b.cacheDir, b.getOldServiceID(app.ServiceID), version.BuildVersion)
|
|
|
|
if err := sources.ImageLoad(b.DockerClient, dstDir, b.Logger); err != nil {
|
|
|
|
b.Logger.Error(util.Translation("load image to local hub error"), map[string]string{"step": "restore_builder", "status": "failure"})
|
|
|
|
logrus.Errorf("load image to local hub error when restore backup app, %s", err.Error())
|
|
|
|
return err
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
2020-03-18 17:37:45 +08:00
|
|
|
imageName := version.ImageName
|
|
|
|
if imageName == "" {
|
|
|
|
imageName = version.DeliveredPath
|
|
|
|
}
|
|
|
|
newImageName := getNewImageName(imageName)
|
|
|
|
if newImageName != imageName {
|
|
|
|
if err := sources.ImageTag(b.DockerClient, imageName, newImageName, b.Logger, 3); err != nil {
|
|
|
|
b.Logger.Error(util.Translation("change image tag error"), map[string]string{"step": "restore_builder", "status": "failure"})
|
|
|
|
logrus.Errorf("change image tag %s to %s failure, %s", imageName, newImageName, err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
imageName = newImageName
|
|
|
|
}
|
|
|
|
if imageName != "" {
|
|
|
|
if err := sources.ImagePush(b.DockerClient, imageName, builder.REGISTRYUSER, builder.REGISTRYPASS, b.Logger, 30); err != nil {
|
|
|
|
b.Logger.Error(util.Translation("push image to local hub error"), map[string]string{"step": "restore_builder", "status": "failure"})
|
|
|
|
logrus.Errorf("push image to local hub error when restore backup app, %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-05-25 18:24:17 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//if restore error, will clear
|
|
|
|
func (b *BackupAPPRestore) clear() {
|
|
|
|
//clear db
|
2018-05-28 12:18:29 +08:00
|
|
|
manager := db.GetManager()
|
2018-05-28 12:36:41 +08:00
|
|
|
for _, v := range b.serviceChange {
|
|
|
|
serviceID := v.ServiceID
|
|
|
|
manager.TenantServiceDao().DeleteServiceByServiceID(serviceID)
|
|
|
|
manager.TenantServicesPortDao().DELPortsByServiceID(serviceID)
|
|
|
|
manager.ServiceProbeDao().DELServiceProbesByServiceID(serviceID)
|
|
|
|
manager.TenantServiceLBMappingPortDao().DELServiceLBMappingPortByServiceID(serviceID)
|
|
|
|
manager.TenantServiceEnvVarDao().DELServiceEnvsByServiceID(serviceID)
|
|
|
|
manager.TenantServiceLabelDao().DeleteLabelByServiceID(serviceID)
|
|
|
|
manager.TenantServiceMountRelationDao().DELTenantServiceMountRelationByServiceID(serviceID)
|
|
|
|
manager.TenantServicePluginRelationDao().DeleteALLRelationByServiceID(serviceID)
|
|
|
|
manager.TenantServiceRelationDao().DELRelationsByServiceID(serviceID)
|
|
|
|
manager.TenantServiceVolumeDao().DeleteTenantServiceVolumesByServiceID(serviceID)
|
|
|
|
manager.VersionInfoDao().DeleteVersionByServiceID(serviceID)
|
2018-05-28 12:18:29 +08:00
|
|
|
}
|
2018-05-25 18:24:17 +08:00
|
|
|
//clear cache data
|
2018-05-28 12:18:29 +08:00
|
|
|
os.RemoveAll(b.cacheDir)
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
2020-03-18 17:37:45 +08:00
|
|
|
func getNewImageName(imageName string) string {
|
|
|
|
image := parser.ParseImageName(imageName)
|
|
|
|
if image.GetDomain() != builder.REGISTRYDOMAIN {
|
|
|
|
newImageName := strings.Replace(imageName, image.GetDomain(), builder.REGISTRYDOMAIN, 1)
|
|
|
|
return newImageName
|
|
|
|
}
|
|
|
|
return imageName
|
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
func (b *BackupAPPRestore) modify(appSnapshot *AppSnapshot) error {
|
|
|
|
for _, app := range appSnapshot.Services {
|
2018-05-25 18:24:17 +08:00
|
|
|
oldServiceID := app.ServiceID
|
2020-03-16 16:19:08 +08:00
|
|
|
//compatible component type
|
|
|
|
switch app.Service.ExtendMethod {
|
|
|
|
case "state":
|
|
|
|
app.Service.ExtendMethod = dbmodel.ServiceTypeStateMultiple.String()
|
|
|
|
case "stateless":
|
|
|
|
app.Service.ExtendMethod = dbmodel.ServiceTypeStatelessMultiple.String()
|
|
|
|
}
|
2018-06-08 12:59:30 +08:00
|
|
|
//change tenant
|
|
|
|
app.Service.TenantID = b.TenantID
|
|
|
|
for _, port := range app.ServicePort {
|
|
|
|
port.TenantID = b.TenantID
|
|
|
|
}
|
|
|
|
for _, relation := range app.ServiceRelation {
|
|
|
|
relation.TenantID = b.TenantID
|
|
|
|
}
|
|
|
|
for _, env := range app.ServiceEnv {
|
|
|
|
env.TenantID = b.TenantID
|
|
|
|
}
|
|
|
|
for _, smr := range app.ServiceMntRelation {
|
|
|
|
smr.TenantID = b.TenantID
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
2020-03-16 16:19:08 +08:00
|
|
|
|
2018-05-25 18:24:17 +08:00
|
|
|
//change service_id and service_alias
|
|
|
|
newServiceID := util.NewUUID()
|
|
|
|
newServiceAlias := "gr" + newServiceID[26:]
|
|
|
|
app.ServiceID = newServiceID
|
|
|
|
app.Service.ServiceID = newServiceID
|
|
|
|
app.Service.ServiceAlias = newServiceAlias
|
2019-03-05 15:00:39 +08:00
|
|
|
app.Service.ServiceName = newServiceAlias
|
2018-05-25 18:24:17 +08:00
|
|
|
for _, a := range app.ServiceProbe {
|
|
|
|
a.ServiceID = newServiceID
|
|
|
|
}
|
|
|
|
for _, a := range app.ServiceEnv {
|
|
|
|
a.ServiceID = newServiceID
|
|
|
|
}
|
|
|
|
for _, a := range app.ServiceLabel {
|
|
|
|
a.ServiceID = newServiceID
|
|
|
|
}
|
|
|
|
for _, a := range app.ServiceMntRelation {
|
|
|
|
a.ServiceID = newServiceID
|
|
|
|
}
|
|
|
|
for _, a := range app.ServiceRelation {
|
|
|
|
a.ServiceID = newServiceID
|
|
|
|
}
|
|
|
|
for _, a := range app.ServiceVolume {
|
|
|
|
a.ServiceID = newServiceID
|
|
|
|
}
|
2020-04-04 19:48:37 +08:00
|
|
|
for _, a := range app.ServiceConfigFile {
|
|
|
|
a.ServiceID = newServiceID
|
|
|
|
}
|
2018-05-25 18:24:17 +08:00
|
|
|
for _, a := range app.ServicePort {
|
|
|
|
a.ServiceID = newServiceID
|
|
|
|
}
|
|
|
|
for _, a := range app.Versions {
|
2020-03-18 17:37:45 +08:00
|
|
|
if a.DeliveredType == "image" {
|
|
|
|
a.ImageName = getNewImageName(a.ImageName)
|
|
|
|
a.DeliveredPath = getNewImageName(a.DeliveredPath)
|
|
|
|
}
|
2018-05-25 18:24:17 +08:00
|
|
|
a.ServiceID = newServiceID
|
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
|
|
|
|
// plugin
|
|
|
|
for _, a := range app.PluginRelation {
|
|
|
|
a.ServiceID = newServiceID
|
|
|
|
}
|
|
|
|
for _, a := range app.PluginConfigs {
|
|
|
|
a.ServiceID = newServiceID
|
|
|
|
}
|
|
|
|
for _, a := range app.PluginEnvs {
|
|
|
|
a.ServiceID = newServiceID
|
|
|
|
}
|
|
|
|
for _, a := range app.PluginStreamPorts {
|
|
|
|
a.ServiceID = newServiceID
|
|
|
|
}
|
|
|
|
// TODO: change service info in plugin config
|
|
|
|
|
2018-05-25 18:24:17 +08:00
|
|
|
b.serviceChange[oldServiceID] = &Info{
|
|
|
|
ServiceID: newServiceID,
|
|
|
|
ServiceAlias: newServiceAlias,
|
|
|
|
Status: app.ServiceStatus,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//modify relations
|
2019-10-14 17:21:31 +08:00
|
|
|
for _, app := range appSnapshot.Services {
|
2018-05-25 18:24:17 +08:00
|
|
|
for _, a := range app.ServiceMntRelation {
|
2019-02-22 11:54:30 +08:00
|
|
|
info := b.serviceChange[a.DependServiceID]
|
|
|
|
if info != nil {
|
|
|
|
a.DependServiceID = info.ServiceID
|
|
|
|
}
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
|
|
|
for _, a := range app.ServiceRelation {
|
2019-02-22 11:54:30 +08:00
|
|
|
info := b.serviceChange[a.DependServiceID]
|
|
|
|
if info != nil {
|
|
|
|
a.DependServiceID = info.ServiceID
|
|
|
|
}
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
|
|
|
|
// plugin
|
|
|
|
for _, p := range appSnapshot.Plugins {
|
|
|
|
p.TenantID = b.TenantID
|
|
|
|
}
|
|
|
|
|
2018-05-25 18:24:17 +08:00
|
|
|
return nil
|
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
func (b *BackupAPPRestore) restoreMetadata(appSnapshot *AppSnapshot) error {
|
2018-05-25 18:24:17 +08:00
|
|
|
tx := db.GetManager().Begin()
|
2019-06-20 13:59:19 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
logrus.Errorf("Unexpected panic occurred, rollback transaction: %v", r)
|
|
|
|
tx.Rollback()
|
|
|
|
}
|
|
|
|
}()
|
2019-10-14 17:21:31 +08:00
|
|
|
for _, app := range appSnapshot.Services {
|
2018-05-25 18:24:17 +08:00
|
|
|
app.Service.ID = 0
|
|
|
|
if err := db.GetManager().TenantServiceDaoTransactions(tx).AddModel(app.Service); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("create app when restore backup error. %s", err.Error())
|
|
|
|
}
|
|
|
|
for _, a := range app.ServiceProbe {
|
|
|
|
a.ID = 0
|
|
|
|
if err := db.GetManager().ServiceProbeDaoTransactions(tx).AddModel(a); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("create app probe when restore backup error. %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, a := range app.ServiceEnv {
|
|
|
|
a.ID = 0
|
|
|
|
if err := db.GetManager().TenantServiceEnvVarDaoTransactions(tx).AddModel(a); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("create app envs when restore backup error. %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, a := range app.ServiceLabel {
|
|
|
|
a.ID = 0
|
|
|
|
if err := db.GetManager().TenantServiceLabelDaoTransactions(tx).AddModel(a); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("create app labels when restore backup error. %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, a := range app.ServiceMntRelation {
|
|
|
|
a.ID = 0
|
|
|
|
if err := db.GetManager().TenantServiceMountRelationDaoTransactions(tx).AddModel(a); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("create app mount relation when restore backup error. %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, a := range app.ServiceRelation {
|
|
|
|
a.ID = 0
|
|
|
|
if err := db.GetManager().TenantServiceRelationDaoTransactions(tx).AddModel(a); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("create app relation when restore backup error. %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 15:00:39 +08:00
|
|
|
localPath, sharePath := GetVolumeDir()
|
2018-05-25 18:24:17 +08:00
|
|
|
for _, a := range app.ServiceVolume {
|
2020-03-18 16:55:01 +08:00
|
|
|
oldVolumeID := a.ID
|
2018-05-25 18:24:17 +08:00
|
|
|
a.ID = 0
|
2018-05-28 12:36:41 +08:00
|
|
|
switch a.VolumeType {
|
|
|
|
//nfs
|
|
|
|
case dbmodel.ShareFileVolumeType.String():
|
|
|
|
a.HostPath = fmt.Sprintf("%s/tenant/%s/service/%s%s", sharePath, b.TenantID, a.ServiceID, a.VolumePath)
|
|
|
|
//local
|
|
|
|
case dbmodel.LocalVolumeType.String():
|
|
|
|
a.HostPath = fmt.Sprintf("%s/tenant/%s/service/%s%s", localPath, b.TenantID, a.ServiceID, a.VolumePath)
|
2020-03-10 02:01:05 +08:00
|
|
|
case dbmodel.MemoryFSVolumeType.String(), dbmodel.ConfigFileVolumeType.String():
|
|
|
|
logrus.Debugf("simple volume type: %s", a.VolumeType)
|
|
|
|
default:
|
|
|
|
logrus.Warnf("custom volumeType: %s", a.VolumeType)
|
|
|
|
volumeType, err := db.GetManager().VolumeTypeDao().GetVolumeTypeByType(a.VolumeType)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("get volumeType[%s] error : %s, use share-file instead", a.VolumeType, err.Error())
|
|
|
|
}
|
|
|
|
if volumeType == nil {
|
|
|
|
logrus.Warnf("service[%s] volumeType[%s] do not exists, use default volumeType[%s]", a.ServiceID, a.VolumeType, dbmodel.ShareFileVolumeType.String())
|
|
|
|
a.VolumeType = dbmodel.ShareFileVolumeType.String()
|
|
|
|
a.HostPath = fmt.Sprintf("%s/tenant/%s/service/%s%s", sharePath, b.TenantID, a.ServiceID, a.VolumePath)
|
|
|
|
}
|
2018-05-28 12:36:41 +08:00
|
|
|
}
|
2018-05-25 18:24:17 +08:00
|
|
|
if err := db.GetManager().TenantServiceVolumeDaoTransactions(tx).AddModel(a); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("create app volume when restore backup error. %s", err.Error())
|
|
|
|
}
|
2020-03-18 16:55:01 +08:00
|
|
|
b.volumeIDMap[oldVolumeID] = a.ID
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
2020-04-04 19:48:37 +08:00
|
|
|
for _, a := range app.ServiceConfigFile {
|
|
|
|
a.ID = 0
|
|
|
|
if err := db.GetManager().TenantServiceConfigFileDao().AddModel(a); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("create app config file when restore backup errro. %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2018-05-25 18:24:17 +08:00
|
|
|
for _, a := range app.ServicePort {
|
|
|
|
a.ID = 0
|
|
|
|
if err := db.GetManager().TenantServicesPortDaoTransactions(tx).AddModel(a); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("create app ports when restore backup error. %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, a := range app.Versions {
|
|
|
|
a.ID = 0
|
|
|
|
if err := db.GetManager().VersionInfoDaoTransactions(tx).AddModel(a); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("create app versions when restore backup error. %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
// plugin info
|
2019-10-13 19:56:13 +08:00
|
|
|
for _, a := range app.PluginRelation {
|
|
|
|
a.ID = 0
|
|
|
|
if err := db.GetManager().TenantServicePluginRelationDaoTransactions(tx).AddModel(a); err != nil {
|
|
|
|
tx.Rollback()
|
2019-10-14 17:21:31 +08:00
|
|
|
return fmt.Errorf("error creating plugin relation: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, pc := range app.PluginConfigs {
|
|
|
|
pc.ID = 0
|
|
|
|
if err := db.GetManager().TenantPluginVersionConfigDaoTransactions(tx).AddModel(pc); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("error creating plugin config: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, pe := range app.PluginEnvs {
|
|
|
|
pe.ID = 0
|
|
|
|
if err := db.GetManager().TenantPluginVersionENVDaoTransactions(tx).AddModel(pe); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("error creating plugin version env: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, psp := range app.PluginStreamPorts {
|
|
|
|
psp.ID = 0
|
|
|
|
if err := db.GetManager().TenantServicesStreamPluginPortDaoTransactions(tx).AddModel(psp); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("error creating plugin stream port: %v", err)
|
2019-10-13 19:56:13 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
2019-10-14 17:21:31 +08:00
|
|
|
|
|
|
|
for _, p := range appSnapshot.Plugins {
|
|
|
|
p.ID = 0
|
|
|
|
if err := db.GetManager().TenantPluginDaoTransactions(tx).AddModel(p); err != nil {
|
|
|
|
if err == errors.ErrRecordAlreadyExist {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("error creating plugin: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, p := range appSnapshot.PluginBuildVersions {
|
|
|
|
p.ID = 0
|
2020-03-18 17:37:45 +08:00
|
|
|
p.BuildLocalImage = getNewImageName(p.BuildLocalImage)
|
2019-10-14 17:21:31 +08:00
|
|
|
if err := db.GetManager().TenantPluginBuildVersionDaoTransactions(tx).AddModel(p); err != nil {
|
|
|
|
if err == errors.ErrRecordAlreadyExist {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tx.Rollback()
|
|
|
|
return fmt.Errorf("error creating plugin build version: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 18:24:17 +08:00
|
|
|
if err := tx.Commit().Error; err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-28 12:18:29 +08:00
|
|
|
func (b *BackupAPPRestore) downloadFromLocal(backup *dbmodel.AppBackup) error {
|
|
|
|
sourceDir := backup.SourceDir
|
2018-05-25 18:24:17 +08:00
|
|
|
err := util.Unzip(sourceDir, b.cacheDir)
|
|
|
|
if err != nil {
|
|
|
|
b.Logger.Error(util.Translation("unzip metadata file error"), map[string]string{"step": "backup_builder", "status": "failure"})
|
|
|
|
logrus.Errorf("unzip file error when restore backup app , %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2018-06-04 15:42:05 +08:00
|
|
|
dirs, err := util.GetDirNameList(b.cacheDir, 1)
|
2018-06-04 15:18:33 +08:00
|
|
|
if err != nil || len(dirs) < 1 {
|
|
|
|
b.Logger.Error(util.Translation("unzip metadata file error"), map[string]string{"step": "backup_builder", "status": "failure"})
|
|
|
|
return fmt.Errorf("find metadata cache dir error after unzip file")
|
|
|
|
}
|
|
|
|
b.cacheDir = filepath.Join(b.cacheDir, dirs[0])
|
2018-05-25 18:24:17 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:32:55 +08:00
|
|
|
func (b *BackupAPPRestore) downloadFromS3(sourceDir string) error {
|
|
|
|
s3Provider, err := cloudos.Str2S3Provider(b.S3Config.Provider)
|
2018-05-25 18:24:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-05 11:32:55 +08:00
|
|
|
cfg := &cloudos.Config{
|
|
|
|
ProviderType: s3Provider,
|
|
|
|
Endpoint: b.S3Config.Endpoint,
|
|
|
|
AccessKey: b.S3Config.AccessKey,
|
|
|
|
SecretKey: b.S3Config.SecretKey,
|
|
|
|
BucketName: b.S3Config.BucketName,
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
2019-11-05 11:32:55 +08:00
|
|
|
cloudoser, err := cloudos.New(cfg)
|
2018-05-25 18:24:17 +08:00
|
|
|
if err != nil {
|
2019-11-05 11:32:55 +08:00
|
|
|
return fmt.Errorf("error creating cloudoser: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, objectKey := filepath.Split(sourceDir)
|
|
|
|
disDir := path.Join(b.cacheDir, objectKey)
|
|
|
|
logrus.Debugf("object key: %s; file path: %s; start downloading backup file.", objectKey, disDir)
|
|
|
|
if err := cloudoser.GetObject(objectKey, disDir); err != nil {
|
|
|
|
return fmt.Errorf("object key: %s; file path: %s; error downloading file for object storage: %v", objectKey, disDir, err)
|
|
|
|
}
|
|
|
|
logrus.Debugf("successfully downloading backup file: %s", disDir)
|
|
|
|
|
|
|
|
err = util.Unzip(disDir, b.cacheDir)
|
|
|
|
if err != nil {
|
|
|
|
// b.Logger.Error(util.Translation("unzip metadata file error"), map[string]string{"step": "backup_builder", "status": "failure"})
|
|
|
|
logrus.Errorf("error unzipping backup file: %v", err)
|
2018-05-25 18:24:17 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-11-05 11:32:55 +08:00
|
|
|
|
2018-06-04 15:42:05 +08:00
|
|
|
dirs, err := util.GetDirNameList(b.cacheDir, 1)
|
2018-06-04 15:18:33 +08:00
|
|
|
if err != nil || len(dirs) < 1 {
|
2019-11-05 11:32:55 +08:00
|
|
|
// b.Logger.Error(util.Translation("unzip metadata file error"), map[string]string{"step": "backup_builder", "status": "failure"})
|
2018-06-04 15:18:33 +08:00
|
|
|
return fmt.Errorf("find metadata cache dir error after unzip file")
|
|
|
|
}
|
2019-11-05 11:32:55 +08:00
|
|
|
|
2018-06-04 15:18:33 +08:00
|
|
|
b.cacheDir = filepath.Join(b.cacheDir, dirs[0])
|
2018-05-25 18:24:17 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//Stop stop
|
|
|
|
func (b *BackupAPPRestore) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//Name return worker name
|
|
|
|
func (b *BackupAPPRestore) Name() string {
|
|
|
|
return "backup_apps_restore"
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetLogger GetLogger
|
|
|
|
func (b *BackupAPPRestore) GetLogger() event.Logger {
|
|
|
|
return b.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
//ErrorCallBack if run error will callback
|
|
|
|
func (b *BackupAPPRestore) ErrorCallBack(err error) {
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("restore backup group app failure %s", err)
|
|
|
|
b.Logger.Error(util.Translation("restore backup group app failure"), map[string]string{"step": "callback", "status": "failure"})
|
|
|
|
b.clear()
|
2018-05-30 14:41:27 +08:00
|
|
|
b.saveResult("failed", err.Error())
|
2018-05-28 11:14:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//RestoreResult RestoreResult
|
|
|
|
type RestoreResult struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
CreateTime time.Time `json:"create_time"`
|
|
|
|
ServiceChange map[string]*Info `json:"service_change"`
|
|
|
|
BackupID string `json:"backup_id"`
|
|
|
|
RestoreMode string `json:"restore_mode"`
|
|
|
|
EventID string `json:"event_id"`
|
|
|
|
RestoreID string `json:"restore_id"`
|
|
|
|
CacheDir string `json:"cache_dir"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BackupAPPRestore) saveResult(status, message string) {
|
|
|
|
var rr = RestoreResult{
|
|
|
|
Status: status,
|
|
|
|
Message: message,
|
|
|
|
CreateTime: time.Now(),
|
|
|
|
ServiceChange: b.serviceChange,
|
|
|
|
BackupID: b.BackupID,
|
|
|
|
RestoreMode: b.RestoreMode,
|
|
|
|
EventID: b.EventID,
|
|
|
|
RestoreID: b.RestoreID,
|
|
|
|
CacheDir: b.cacheDir,
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|
2018-05-28 11:14:56 +08:00
|
|
|
body, _ := ffjson.Marshal(rr)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
b.etcdcli.Put(ctx, "/rainbond/backup_restore/"+rr.RestoreID, string(body))
|
2018-05-25 18:24:17 +08:00
|
|
|
}
|