fix(region,host): slvm snapshot delete and instance snapshot purge (#22022)

This commit is contained in:
wanyaoqi
2025-01-26 19:38:11 +08:00
committed by GitHub
parent 2bb4511567
commit 10dd582a83
3 changed files with 47 additions and 7 deletions

View File

@@ -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"`

View File

@@ -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)
}

View File

@@ -304,10 +304,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)
@@ -316,9 +319,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
}