Skip to content

Commit

Permalink
fix(region,host): slvm snapshot delete and instance snapshot purge
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyaoqi committed Jan 24, 2025
1 parent 266732b commit 1f8c86d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
11 changes: 11 additions & 0 deletions cmd/climc/shell/compute/instance_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ func init() {
printBatchResults(ret, modules.InstanceSnapshots.GetColumns(s))
return nil
})
type InstanceSnapshotPurgeOptions struct {
ID string `help:"Delete snapshot id"`
}
R(&InstanceSnapshotPurgeOptions{}, "instance-snapshot-purge", "Purge snapshots", func(s *mcclient.ClientSession, args *InstanceSnapshotPurgeOptions) error {
result, err := modules.InstanceSnapshots.PerformAction(s, args.ID, "purge", nil)
if err != nil {
return err
}
printObject(result)
return nil
})

type InstanceSnapshotShowOptions struct {
ID string `help:"ID or Name of snapshot"`
Expand Down
23 changes: 23 additions & 0 deletions pkg/compute/models/instance_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,29 @@ func (self *SInstanceSnapshot) StartInstanceSnapshotDeleteTask(
return nil
}

func (self *SInstanceSnapshot) PerformPurge(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
snapshots, err := self.GetSnapshots()
if err != nil {
return nil, err
}
for i := range snapshots {
snapshotId := snapshots[i].Id
isjp := new(SInstanceSnapshotJoint)
err = InstanceSnapshotJointManager.Query().
Equals("instance_snapshot_id", self.Id).Equals("snapshot_id", snapshotId).First(isjp)
err = isjp.Delete(ctx, userCred)
if err != nil {
return nil, errors.Wrapf(err, "delete instance snapshot joint: %s", snapshotId)
}
_, err = snapshots[i].PerformPurge(ctx, userCred, query, data)
if err != nil {
return nil, errors.Wrapf(err, "delete snapshot: %s", snapshotId)
}
}
err = self.RealDelete(ctx, userCred)
return nil, err
}

func (self *SInstanceSnapshot) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error {
return db.DeleteModel(ctx, userCred, self)
}
Expand Down
20 changes: 13 additions & 7 deletions pkg/hostman/storageman/disk_slvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,13 @@ func (d *SSLVMDisk) DeleteSnapshot(snapshotId, convertSnapshot string, blockStre
if err != nil {
return errors.Wrap(err, "LVActive")
}
convertSnapshotPath := d.GetSnapshotPath(convertSnapshot)
err = lvmutils.LVActive(convertSnapshotPath, false, d.Storage.Lvmlockd())
if err != nil {
return errors.Wrap(err, "LVActive convert snapshot")
var convertSnapshotPath string
if len(convertSnapshot) > 0 {
convertSnapshotPath = d.GetSnapshotPath(convertSnapshot)
err = lvmutils.LVActive(convertSnapshotPath, false, d.Storage.Lvmlockd())
if err != nil {
return errors.Wrap(err, "LVActive convert snapshot")
}
}

err = d.SLVMDisk.DeleteSnapshot(snapshotId, convertSnapshot, blockStream, encryptInfo)
Expand All @@ -319,9 +322,12 @@ func (d *SSLVMDisk) DeleteSnapshot(snapshotId, convertSnapshot string, blockStre
if e != nil {
log.Errorf("failed active with share mode: %s", e)
}
e = lvmutils.LVActive(convertSnapshotPath, d.Storage.Lvmlockd(), false)
if e != nil {
log.Errorf("failed active convert snapshot %s with share mode: %s", convertSnapshotPath, e)
if len(convertSnapshot) > 0 {
e = lvmutils.LVActive(convertSnapshotPath, d.Storage.Lvmlockd(), false)
if e != nil {
log.Errorf("failed active convert snapshot %s with share mode: %s", convertSnapshotPath, e)
}
}

return err
}

0 comments on commit 1f8c86d

Please sign in to comment.