From ed3deb02bc04bc253d7da12910754ef78267bc23 Mon Sep 17 00:00:00 2001 From: TangBin Date: Tue, 18 Dec 2018 16:48:40 +0800 Subject: [PATCH] disk rollback bugfix --- pkg/cloudprovider/resources.go | 2 +- pkg/compute/hostdrivers/managedvirtual.go | 6 ++++-- pkg/compute/tasks/disk_reset_task.go | 9 ++++++++- pkg/util/aliyun/disk.go | 4 ++-- pkg/util/aws/disk.go | 19 ++++++++++--------- pkg/util/azure/classic_disk.go | 4 ++-- pkg/util/azure/disk.go | 4 ++-- pkg/util/esxi/vdisk.go | 4 ++-- pkg/util/qcloud/disk.go | 4 ++-- pkg/util/qcloud/localdisk.go | 4 ++-- 10 files changed, 35 insertions(+), 25 deletions(-) diff --git a/pkg/cloudprovider/resources.go b/pkg/cloudprovider/resources.go index 26430467f3..6ac7f3c28d 100644 --- a/pkg/cloudprovider/resources.go +++ b/pkg/cloudprovider/resources.go @@ -300,7 +300,7 @@ type ICloudDisk interface { GetISnapshots() ([]ICloudSnapshot, error) Resize(ctx context.Context, newSizeMB int64) error - Reset(ctx context.Context, snapshotId string) error + Reset(ctx context.Context, snapshotId string) (string, error) Rebuild(ctx context.Context) error } diff --git a/pkg/compute/hostdrivers/managedvirtual.go b/pkg/compute/hostdrivers/managedvirtual.go index f1179e1bd2..3865e401aa 100644 --- a/pkg/compute/hostdrivers/managedvirtual.go +++ b/pkg/compute/hostdrivers/managedvirtual.go @@ -282,8 +282,10 @@ func (self *SManagedVirtualizationHostDriver) RequestResetDisk(ctx context.Conte return err } taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - err := iDisk.Reset(ctx, snapshotId) - return nil, err + exteranlDiskId, err := iDisk.Reset(ctx, snapshotId) + data := jsonutils.NewDict() + data.Set("exteranl_disk_id", jsonutils.NewString(exteranlDiskId)) + return data, err }) return nil } diff --git a/pkg/compute/tasks/disk_reset_task.go b/pkg/compute/tasks/disk_reset_task.go index 25d296f5d7..ae6f7da2b4 100644 --- a/pkg/compute/tasks/disk_reset_task.go +++ b/pkg/compute/tasks/disk_reset_task.go @@ -28,6 +28,10 @@ func (self *DiskResetTask) TaskFailed(ctx context.Context, disk *models.SDisk, r } func (self *DiskResetTask) TaskCompleted(ctx context.Context, disk *models.SDisk, data *jsonutils.JSONDict) { + // data不能为空指针,否则会导致AddActionLog抛空指针异常 + if data == nil { + data = jsonutils.NewDict() + } logclient.AddActionLog(disk, logclient.ACT_RESET_DISK, data, self.UserCred, true) self.SetStageComplete(ctx, data) } @@ -81,9 +85,12 @@ func (self *DiskResetTask) OnRequestResetDisk(ctx context.Context, disk *models. snapshotId, _ := self.Params.GetString("snapshot_id") iSnapshot, _ := models.SnapshotManager.FetchById(snapshotId) snapshot := iSnapshot.(*models.SSnapshot) - if disk.DiskSize != snapshot.Size { + + externalId, _ := data.GetString("exteranl_disk_id") + if disk.DiskSize != snapshot.Size || (len(externalId) > 0 && externalId != disk.GetExternalId() ){ _, err := models.DiskManager.TableSpec().Update(disk, func() error { disk.DiskSize = snapshot.Size + disk.ExternalId = externalId return nil }) if err != nil { diff --git a/pkg/util/aliyun/disk.go b/pkg/util/aliyun/disk.go index e5bd5c23c5..390900c1c0 100644 --- a/pkg/util/aliyun/disk.go +++ b/pkg/util/aliyun/disk.go @@ -368,8 +368,8 @@ func (self *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) { return isnapshots, nil } -func (self *SDisk) Reset(ctx context.Context, snapshotId string) error { - return self.storage.zone.region.resetDisk(self.DiskId, snapshotId) +func (self *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) { + return "", self.storage.zone.region.resetDisk(self.DiskId, snapshotId) } func (self *SDisk) GetBillingType() string { diff --git a/pkg/util/aws/disk.go b/pkg/util/aws/disk.go index baba49a543..c2e232c403 100644 --- a/pkg/util/aws/disk.go +++ b/pkg/util/aws/disk.go @@ -223,7 +223,7 @@ func (self *SDisk) Resize(ctx context.Context, newSizeMb int64) error { return self.storage.zone.region.resizeDisk(self.DiskId, newSizeMb) } -func (self *SDisk) Reset(ctx context.Context, snapshotId string) error { +func (self *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) { return self.storage.zone.region.resetDisk(self.DiskId, snapshotId) } @@ -395,12 +395,12 @@ func (self *SRegion) resizeDisk(diskId string, sizeMb int64) error { return err } -func (self *SRegion) resetDisk(diskId, snapshotId string) error { +func (self *SRegion) resetDisk(diskId, snapshotId string) (string, error) { // 这里实际是回滚快照 disk, err := self.GetDisk(diskId) if err != nil { log.Debugf("resetDisk %s:%s", diskId, err.Error()) - return err + return "", err } params := &ec2.CreateVolumeInput{} @@ -415,7 +415,7 @@ func (self *SRegion) resetDisk(diskId, snapshotId string) error { ret, err := self.ec2Client.CreateVolume(params) if err != nil { log.Debugf("resetDisk %s: %s", params.String(), err.Error()) - return err + return "", err } // detach disk @@ -423,24 +423,24 @@ func (self *SRegion) resetDisk(diskId, snapshotId string) error { err := self.DetachDisk(disk.InstanceId, diskId) if err != nil { log.Debugf("resetDisk %s %s: %s", disk.InstanceId, diskId, err.Error()) - return err + return "", err } err = self.ec2Client.WaitUntilVolumeAvailable(&ec2.DescribeVolumesInput{VolumeIds: []*string{&diskId}}) if err != nil { log.Debugf("resetDisk :%s", err.Error()) - return err + return "", err } } err = self.AttachDisk(disk.InstanceId, *ret.VolumeId, disk.Device) if err != nil { log.Debugf("resetDisk %s %s %s: %s", disk.InstanceId, *ret.VolumeId, disk.Device, err.Error()) - return err + return "", err } // 绑定成功后删除原磁盘 - return self.DeleteDisk(diskId) + return StrVal(ret.VolumeId), self.DeleteDisk(diskId) } func (self *SRegion) CreateDisk(zoneId string, category string, name string, sizeGb int, snapshotId string, desc string) (string, error) { @@ -477,5 +477,6 @@ func (disk *SDisk) GetAccessPath() string { } func (self *SDisk) Rebuild(ctx context.Context) error { - return self.storage.zone.region.resetDisk(self.DiskId, "") + _, err := self.storage.zone.region.resetDisk(self.DiskId, "") + return err } diff --git a/pkg/util/azure/classic_disk.go b/pkg/util/azure/classic_disk.go index 922e7ab84d..581d0f1e87 100644 --- a/pkg/util/azure/classic_disk.go +++ b/pkg/util/azure/classic_disk.go @@ -208,8 +208,8 @@ func (self *SClassicDisk) Refresh() error { return nil } -func (self *SClassicDisk) Reset(ctx context.Context, snapshotId string) error { - return cloudprovider.ErrNotSupported +func (self *SClassicDisk) Reset(ctx context.Context, snapshotId string) (string, error) { + return "", cloudprovider.ErrNotSupported } func (self *SClassicDisk) Resize(ctx context.Context, sizeMb int64) error { diff --git a/pkg/util/azure/disk.go b/pkg/util/azure/disk.go index 886d476b98..9eec81b073 100644 --- a/pkg/util/azure/disk.go +++ b/pkg/util/azure/disk.go @@ -315,8 +315,8 @@ func (region *SRegion) GetSnapShots(diskId string) ([]SSnapshot, error) { return result, nil } -func (self *SDisk) Reset(ctx context.Context, snapshotId string) error { - return self.storage.zone.region.resetDisk(self.ID, snapshotId) +func (self *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) { + return "", self.storage.zone.region.resetDisk(self.ID, snapshotId) } func (self *SRegion) resetDisk(diskId, snapshotId string) error { diff --git a/pkg/util/esxi/vdisk.go b/pkg/util/esxi/vdisk.go index 6b03977cd5..e177b57c7e 100644 --- a/pkg/util/esxi/vdisk.go +++ b/pkg/util/esxi/vdisk.go @@ -229,8 +229,8 @@ func (disk *SVirtualDisk) Resize(ctx context.Context, newSizeMb int64) error { return err } -func (disk *SVirtualDisk) Reset(ctx context.Context, snapshotId string) error { - return cloudprovider.ErrNotImplemented +func (disk *SVirtualDisk) Reset(ctx context.Context, snapshotId string) (string, error) { + return "", cloudprovider.ErrNotImplemented } func (disk *SVirtualDisk) GetBillingType() string { diff --git a/pkg/util/qcloud/disk.go b/pkg/util/qcloud/disk.go index cf29ec44dd..ac9d7c2fb1 100644 --- a/pkg/util/qcloud/disk.go +++ b/pkg/util/qcloud/disk.go @@ -344,8 +344,8 @@ func (self *SRegion) ResetDisk(diskId, snapshotId string) error { return nil } -func (self *SDisk) Reset(ctx context.Context, snapshotId string) error { - return self.storage.zone.region.ResetDisk(self.DiskId, snapshotId) +func (self *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) { + return "", self.storage.zone.region.ResetDisk(self.DiskId, snapshotId) } func (self *SRegion) CreateDisk(zoneId string, category string, name string, sizeGb int, desc string) (string, error) { diff --git a/pkg/util/qcloud/localdisk.go b/pkg/util/qcloud/localdisk.go index dd8d45b501..723ca61952 100644 --- a/pkg/util/qcloud/localdisk.go +++ b/pkg/util/qcloud/localdisk.go @@ -87,8 +87,8 @@ func (self *SLocalDisk) Refresh() error { return nil } -func (self *SLocalDisk) Reset(ctx context.Context, snapshotId string) error { - return cloudprovider.ErrNotSupported +func (self *SLocalDisk) Reset(ctx context.Context, snapshotId string) (string, error) { + return "", cloudprovider.ErrNotSupported } func (self *SLocalDisk) GetTemplateId() string {