Merge pull request #3391 from wanyaoqi/bugfix/wyq/instance-snapshot-fix

instance snapshot fix
This commit is contained in:
yunion-ci-robot
2019-10-28 21:38:10 +08:00
committed by GitHub
4 changed files with 49 additions and 8 deletions
@@ -49,3 +49,11 @@ func (manager *SInstanceSnapshotJointManager) CreateJoint(instanceSnapshotId, sn
instanceSnapshotJoint.DiskIndex = diskIndex
return manager.TableSpec().Insert(instanceSnapshotJoint)
}
func (manager *SInstanceSnapshotJointManager) IsSubSnapshot(snapshotId string) (bool, error) {
count, err := manager.Query().Equals("snapshot_id", snapshotId).CountWithError()
if err != nil {
return false, err
}
return count > 0, nil
}
+25 -6
View File
@@ -67,21 +67,40 @@ func (self *SInstanceSnapshot) AllowUpdateItem(ctx context.Context, userCred mcc
func (self *SInstanceSnapshot) GetCustomizeColumns(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) *jsonutils.JSONDict {
extra := self.SVirtualResourceBase.GetCustomizeColumns(ctx, userCred, query)
extra = self.getMoreDetails(extra)
extra = self.getMoreDetails(userCred, extra)
return extra
}
func (self *SInstanceSnapshot) getMoreDetails(extra *jsonutils.JSONDict) *jsonutils.JSONDict {
func (self *SInstanceSnapshot) getMoreDetails(userCred mcclient.TokenCredential, extra *jsonutils.JSONDict) *jsonutils.JSONDict {
if guest := GuestManager.FetchGuestById(self.GuestId); guest != nil {
extra.Set("guest_status", jsonutils.NewString(guest.Status))
extra.Set("guest_name", jsonutils.NewString(guest.Name))
extra.Set("guest", jsonutils.NewString(guest.Name))
}
var osType, storageType string
snapshots, _ := self.GetSnapshots()
snapshotsDesc := jsonutils.NewDict()
snapshotsDesc := jsonutils.NewArray()
for i := 0; i < len(snapshots); i++ {
snapshotsDesc.Set(snapshots[i].Id, jsonutils.NewString(snapshots[i].Name))
if snapshots[i].DiskType == compute.DISK_TYPE_SYS {
osType = snapshots[i].OsType
}
if len(snapshots[i].StorageId) > 0 && len(storageType) == 0 {
storage := snapshots[i].GetStorage()
storageType = storage.StorageType
}
jsonDict := jsonutils.Marshal(&snapshots[i]).(*jsonutils.JSONDict)
metaFields := db.GetDetailFields(SnapshotManager, userCred)
jsonDict = jsonDict.CopyIncludes(metaFields...)
snapshotsDesc.Add(jsonDict)
}
extra.Set("snapshots", snapshotsDesc)
if len(osType) > 0 {
properties := jsonutils.NewDict()
properties.Set("os_type", jsonutils.NewString(osType))
extra.Set("properties", properties)
}
if len(storageType) > 0 {
extra.Set("storage_type", jsonutils.NewString(storageType))
}
return extra
}
@@ -90,7 +109,7 @@ func (self *SInstanceSnapshot) GetExtraDetails(ctx context.Context, userCred mcc
if err != nil {
return nil, err
}
extra = self.getMoreDetails(extra)
extra = self.getMoreDetails(userCred, extra)
return extra, nil
}
func (self *SInstanceSnapshot) StartCreateInstanceSnapshotTask(
+4 -1
View File
@@ -60,6 +60,7 @@ type SSnapshot struct {
OutOfChain bool `nullable:"false" default:"false" list:"admin" create:"optional"`
FakeDeleted bool `nullable:"false" default:"false"`
DiskType string `width:"32" charset:"ascii" nullable:"true" list:"user" create:"optional"`
OsType string `width:"32" charset:"ascii" nullable:"true" list:"user" create:"optional"`
// create disk from snapshot, snapshot as disk backing file
RefCount int `nullable:"false" default:"0" list:"user"`
@@ -216,10 +217,12 @@ func (self *SSnapshot) getMoreDetails(extra *jsonutils.JSONDict) *jsonutils.JSON
}
extra.Add(jsonutils.NewString(disk.Name), "disk_name")
}
if t, _ := InstanceSnapshotJointManager.IsSubSnapshot(self.Id); t {
extra.Set("is_sub_snapshot", jsonutils.JSONTrue)
}
info := self.getCloudProviderInfo()
extra.Update(jsonutils.Marshal(&info))
return extra
}
+12 -1
View File
@@ -16,10 +16,12 @@ package tasks
import (
"context"
"fmt"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/apis/compute"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
@@ -72,11 +74,20 @@ func (self *GuestDiskSnapshotTask) OnDiskSnapshotComplete(ctx context.Context, g
log.Infof("OnDiskSnapshotComplete called with data no location")
return
}
db.Update(snapshot, func() error {
var osType string
if snapshot.DiskType == compute.DISK_TYPE_SYS {
osType = guest.GetOS()
}
_, err = db.Update(snapshot, func() error {
snapshot.Location = location
snapshot.Status = api.SNAPSHOT_READY
snapshot.OsType = osType
return nil
})
if err != nil {
self.TaskFailed(ctx, guest, fmt.Sprintf("update sanpshot failed: %s", err))
return
}
guest.SetStatus(self.UserCred, api.VM_SNAPSHOT_SUCC, "")
self.TaskComplete(ctx, guest, nil)