[REV] update some code for backup app feature

This commit is contained in:
barnett 2018-05-23 12:17:34 +08:00
parent 3e129850c3
commit d15da40683
5 changed files with 33 additions and 7 deletions

View File

@ -23,6 +23,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/goodrain/rainbond/event"
@ -115,6 +116,9 @@ func (h *BackupHandle) NewBackup(b Backup) (*dbmodel.AppBackup, *util.APIHandleE
//snapshot the app metadata of region and write
if err := h.snapshot(b.Body.ServiceIDs, sourceDir); err != nil {
os.RemoveAll(sourceDir)
if strings.HasPrefix(err.Error(), "Statefulset app must be closed") {
return nil, util.CreateAPIHandleError(401, fmt.Errorf("snapshot group apps error,%s", err))
}
return nil, util.CreateAPIHandleError(500, fmt.Errorf("snapshot group apps error,%s", err))
}
logger.Info(core_util.Translation("write region level metadata success"), map[string]string{"step": "back-api"})
@ -201,15 +205,15 @@ func (h *BackupHandle) snapshot(ids []string, sourceDir string) error {
status := h.statusCli.GetStatus(id)
serviceType, err := db.GetManager().TenantServiceLabelDao().GetTenantServiceTypeLabel(id)
if err != nil {
return fmt.Errorf("Get service deploy type error,%s", err)
return fmt.Errorf("Get service deploy type error,%s", err.Error())
}
if status != client.CLOSED && serviceType.LabelValue == core_util.StatefulServiceType {
return fmt.Errorf("Statefulset app must be closed before backup,%s", err)
return fmt.Errorf("Statefulset app must be closed before backup,%s", err.Error())
}
data.ServiceStatus = status
service, err := db.GetManager().TenantServiceDao().GetServiceByID(id)
if err != nil {
return fmt.Errorf("Get service(%s) error %s", id, err)
return fmt.Errorf("Get service(%s) error %s", id, err.Error())
}
data.Service = service
serviceProbes, err := db.GetManager().ServiceProbeDao().GetServiceProbes(id)

View File

@ -158,6 +158,7 @@ func (i *ImageBuildItem) StorageVersionInfo(imageURL string) error {
}
version.DeliveredType = "image"
version.DeliveredPath = imageURL
version.ImageName = imageURL
if err := db.GetManager().VersionInfoDao().UpdateModel(version); err != nil {
return err
}

View File

@ -384,6 +384,9 @@ func (i *SourceCodeBuildItem) UpdateVersionInfo(vi *dbmodel.VersionInfo) error {
}
if vi.DeliveredPath != "" {
version.DeliveredPath = vi.DeliveredPath
if vi.DeliveredType == "image" {
version.ImageName = vi.DeliveredPath
}
}
if vi.FinalStatus != "" {
version.FinalStatus = vi.FinalStatus

View File

@ -26,6 +26,7 @@ import (
"time"
"github.com/goodrain/rainbond/builder/sources"
"github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/util"
"github.com/Sirupsen/logrus"
@ -179,6 +180,9 @@ func (b *BackupAPPNew) Run(timeout time.Duration) error {
b.BackupSize += util.GetFileSize(fmt.Sprintf("/grdata/tmp/%s_%s.zip", b.GroupID, b.Version))
os.Remove(fmt.Sprintf("/grdata/tmp/%s_%s.zip", b.GroupID, b.Version))
}
if err := b.updateBackupStatu("success"); err != nil {
return err
}
return nil
}
func (b *BackupAPPNew) uploadSlug(app *RegionServiceSnapshot, version *dbmodel.VersionInfo) error {
@ -214,11 +218,11 @@ func (b *BackupAPPNew) uploadImage(app *RegionServiceSnapshot, version *dbmodel.
if err != nil {
return fmt.Errorf("create backup image error %s", err)
}
info, err := sources.ImagePull(b.DockerClient, version.ImageName, types.ImagePullOptions{}, b.Logger, 10)
info, err := sources.ImagePull(b.DockerClient, version.DeliveredPath, types.ImagePullOptions{}, b.Logger, 10)
if err != nil {
return fmt.Errorf("pull image when backup error %s", err)
}
if err := sources.ImageTag(b.DockerClient, version.ImageName, backupImage, b.Logger, 1); err != nil {
if err := sources.ImageTag(b.DockerClient, version.DeliveredPath, backupImage, b.Logger, 1); err != nil {
return fmt.Errorf("change image tag when backup error %s", err)
}
if b.ImageInfo.IsTrust {
@ -235,7 +239,7 @@ func (b *BackupAPPNew) uploadImage(app *RegionServiceSnapshot, version *dbmodel.
b.BackupSize += info.Size
} else {
dstDir := fmt.Sprintf("%s/app_%s/image_%s.tar", b.SourceDir, app.ServiceID, version.BuildVersion)
if err := sources.ImageSave(b.DockerClient, version.ImageName, dstDir, b.Logger); err != nil {
if err := sources.ImageSave(b.DockerClient, version.DeliveredPath, dstDir, b.Logger); err != nil {
b.Logger.Error(util.Translation("save image to local dir error"), map[string]string{"step": "backup_builder", "status": "failure"})
logrus.Errorf("save image to local dir error when backup app, %s", err.Error())
return err
@ -264,5 +268,17 @@ func (b *BackupAPPNew) ErrorCallBack(err error) {
if err != nil {
logrus.Errorf("backup group app failure %s", err)
b.Logger.Error(util.Translation("backup group app failure"), map[string]string{"step": "callback", "status": "failure"})
b.updateBackupStatu("failed")
}
}
func (b *BackupAPPNew) updateBackupStatu(status string) error {
backupstatus, err := db.GetManager().AppBackupDao().GetAppBackup(b.BackupID)
if err != nil {
logrus.Errorf("update backup group app history failure %s", err)
return err
}
backupstatus.Status = status
backupstatus.BuckupSize = int(b.BackupSize)
return db.GetManager().AppBackupDao().UpdateModel(backupstatus)
}

View File

@ -72,6 +72,7 @@ func (a *AppBackupDaoImpl) AddModel(mo model.Interface) error {
if err := a.DB.Create(app).Error; err != nil {
return err
}
return nil
}
return fmt.Errorf("backup info exist with id %s", app.BackupID)
}
@ -91,7 +92,8 @@ func (a *AppBackupDaoImpl) UpdateModel(mo model.Interface) error {
//CheckHistory CheckHistory
func (a *AppBackupDaoImpl) CheckHistory(groupID, version string) bool {
var app model.AppBackup
return a.DB.Where("(group_id = ? and status =?) or version=? ", groupID, "starting", version).Find(&app).RecordNotFound()
exist := a.DB.Where("(group_id = ? and status =?) or version=? ", groupID, "starting", version).Find(&app).RecordNotFound()
return !exist
}
//GetAppBackups GetAppBackups