diff --git a/pkg/hostman/storageman/backup.go b/pkg/hostman/storageman/backup.go index f54a8ecda6..1783c28d09 100644 --- a/pkg/hostman/storageman/backup.go +++ b/pkg/hostman/storageman/backup.go @@ -201,7 +201,7 @@ func DoInstancePackBackup(ctx context.Context, backupInfo SStoragePackInstanceBa } else { finalPackageFileName = fmt.Sprintf("%s-%d.tar", backupInfo.PackageName, tried) } - exists, err := backupStorage.IsBackupInstanceExists(finalPackageFileName) + exists, _, err := backupStorage.IsBackupInstanceExists(finalPackageFileName) if err != nil { return "", errors.Wrap(err, "IsBackupInstanceExists") } diff --git a/pkg/hostman/storageman/backupstorage/backup_storage.go b/pkg/hostman/storageman/backupstorage/backup_storage.go index cc6472a227..5f6ebdfa72 100644 --- a/pkg/hostman/storageman/backupstorage/backup_storage.go +++ b/pkg/hostman/storageman/backupstorage/backup_storage.go @@ -34,7 +34,7 @@ type IBackupStorage interface { // 删除备份 RemoveBackup(ctx context.Context, backupId string) error // 备份是否存在 - IsBackupExists(backupId string) (bool, error) + IsBackupExists(backupId string) (bool, string, error) // 从指定路径拷贝主机备份文件到备份存储 SaveBackupInstanceFrom(ctx context.Context, srcFilename string, bakcupInstanceId string) error @@ -43,7 +43,7 @@ type IBackupStorage interface { // 删除备份 RemoveBackupInstance(ctx context.Context, backupInstanceId string) error // 备份是否存在 - IsBackupInstanceExists(backupInstanceId string) (bool, error) + IsBackupInstanceExists(backupInstanceId string) (bool, string, error) // ConvertTo(destPath string, format qemuimgfmt.TImageFormat, backupId string) error // ConvertFrom(srcPath string, format qemuimgfmt.TImageFormat, backupId string) (int, error) diff --git a/pkg/hostman/storageman/backupstorage/nfs/nfs.go b/pkg/hostman/storageman/backupstorage/nfs/nfs.go index 69a5b35065..4ffe6fd47a 100644 --- a/pkg/hostman/storageman/backupstorage/nfs/nfs.go +++ b/pkg/hostman/storageman/backupstorage/nfs/nfs.go @@ -194,31 +194,34 @@ func (s *SNFSBackupStorage) removeFile(ctx context.Context, id string, getPathFu return nil } -func (s *SNFSBackupStorage) IsBackupExists(backupId string) (bool, error) { +func (s *SNFSBackupStorage) IsBackupExists(backupId string) (bool, string, error) { return s.isFileExists(backupId, s.getBackupDiskPath) } -func (s *SNFSBackupStorage) IsBackupInstanceExists(backupId string) (bool, error) { +func (s *SNFSBackupStorage) IsBackupInstanceExists(backupId string) (bool, string, error) { return s.isFileExists(backupId, s.getBackupInstancePath) } -func (s *SNFSBackupStorage) isFileExists(id string, getPathFunc func(id string) string) (bool, error) { +func (s *SNFSBackupStorage) isFileExists(id string, getPathFunc func(id string) string) (bool, string, error) { err := s.checkAndMount() if err != nil { - return false, errors.Wrap(err, "unable to checkAndMount") + if errors.Cause(err) == ErrorBackupStorageOffline { + return false, err.Error(), nil + } + return false, "", errors.Wrap(err, "unable to checkAndMount") } defer s.unMount() filename := getPathFunc(id) - return fileutils2.Exists(filename), nil + return fileutils2.Exists(filename), "", nil } func (s *SNFSBackupStorage) IsOnline() (bool, string, error) { err := s.checkAndMount() - if errors.Cause(err) == ErrorBackupStorageOffline { - return false, err.Error(), nil - } if err != nil { + if errors.Cause(err) == ErrorBackupStorageOffline { + return false, err.Error(), nil + } return false, "", err } s.unMount() diff --git a/pkg/hostman/storageman/backupstorage/object/object.go b/pkg/hostman/storageman/backupstorage/object/object.go index 2fc9413e29..c4ca38ea79 100644 --- a/pkg/hostman/storageman/backupstorage/object/object.go +++ b/pkg/hostman/storageman/backupstorage/object/object.go @@ -178,27 +178,27 @@ func (s *SObjectBackupStorage) removeObject(ctx context.Context, id string, getK return nil } -func (s *SObjectBackupStorage) IsBackupExists(backupId string) (bool, error) { +func (s *SObjectBackupStorage) IsBackupExists(backupId string) (bool, string, error) { return s.isObjectExists(backupId, s.getBackupKey) } -func (s *SObjectBackupStorage) IsBackupInstanceExists(backupId string) (bool, error) { +func (s *SObjectBackupStorage) IsBackupInstanceExists(backupId string) (bool, string, error) { return s.isObjectExists(backupId, s.getBackupInstanceKey) } -func (s *SObjectBackupStorage) isObjectExists(id string, getKeyFunc func(string) string) (bool, error) { +func (s *SObjectBackupStorage) isObjectExists(id string, getKeyFunc func(string) string) (bool, string, error) { bucket, err := s.getBucket() if err != nil { - return false, errors.Wrap(err, "getBucket") + return false, "", errors.Wrap(err, "getBucket") } _, err = cloudprovider.GetIObject(bucket, getKeyFunc(id)) if err != nil { if errors.Cause(err) == errors.ErrNotFound { - return false, nil + return false, "", nil } - return false, errors.Wrap(err, "GetIObject") + return false, "", errors.Wrap(err, "GetIObject") } - return true, nil + return true, "", nil } func (s *SObjectBackupStorage) IsOnline() (bool, string, error) { diff --git a/pkg/hostman/storageman/storagehandler/storagehandler.go b/pkg/hostman/storageman/storagehandler/storagehandler.go index 7c7444566a..71eef99bf9 100644 --- a/pkg/hostman/storageman/storagehandler/storagehandler.go +++ b/pkg/hostman/storageman/storagehandler/storagehandler.go @@ -276,7 +276,7 @@ func storageSyncBackup(ctx context.Context, w http.ResponseWriter, r *http.Reque hostutils.Response(ctx, w, err) return } - exist, err := backupStorage.IsBackupExists(backupId) + exist, reason, err := backupStorage.IsBackupExists(backupId) if err != nil { hostutils.Response(ctx, w, err) return @@ -287,11 +287,13 @@ func storageSyncBackup(ctx context.Context, w http.ResponseWriter, r *http.Reque ) if exist { status = compute.BACKUP_EXIST - } else if !exist && err == nil { - status = compute.BACKUP_NOT_EXIST } else { - log.Errorf("fetch snapshot exist failed %s", err) - status = compute.BACKUP_STATUS_UNKNOWN + if len(reason) == 0 { + status = compute.BACKUP_NOT_EXIST + } else { + log.Errorf("fetch snapshot exist failed reason:%s", reason) + status = compute.BACKUP_STATUS_UNKNOWN + } } ret.Set("status", jsonutils.NewString(status)) hostutils.Response(ctx, w, ret)