From 75701657d0f3f1d2a42d4e5675e0db2e809db04c Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Fri, 7 Dec 2018 21:18:52 +0800 Subject: [PATCH] fix bugs --- pkg/cloudcommon/db/opslog.go | 3 ++ pkg/cloudcommon/db/taskman/interface.go | 2 +- pkg/cloudcommon/db/taskman/tasks.go | 27 +++++++++--------- pkg/compute/models/guests.go | 6 ++++ pkg/compute/models/host_recycle.go | 30 +++++++++++--------- pkg/compute/models/hosts.go | 7 +++++ pkg/compute/tasks/guest_batch_create_task.go | 1 - pkg/compute/tasks/guest_rebuild_root_task.go | 9 +++++- pkg/compute/tasks/schedule.go | 2 +- pkg/util/logclient/logclient.go | 3 ++ 10 files changed, 59 insertions(+), 31 deletions(-) diff --git a/pkg/cloudcommon/db/opslog.go b/pkg/cloudcommon/db/opslog.go index e0efe145b1..70cab84d75 100644 --- a/pkg/cloudcommon/db/opslog.go +++ b/pkg/cloudcommon/db/opslog.go @@ -154,6 +154,9 @@ const ( ACT_RENEW = "renew" ACT_SCHEDULE = "schedule" + + ACT_RECYCLE_PREPAID = "recycle_prepaid" + ACT_UNDO_RECYCLE_PREPAID = "undo_recycle_prepaid" ) type SOpsLogManager struct { diff --git a/pkg/cloudcommon/db/taskman/interface.go b/pkg/cloudcommon/db/taskman/interface.go index e4735c42b1..b61ef9246d 100644 --- a/pkg/cloudcommon/db/taskman/interface.go +++ b/pkg/cloudcommon/db/taskman/interface.go @@ -13,7 +13,7 @@ type ITask interface { GetParams() *jsonutils.JSONDict GetUserCred() mcclient.TokenCredential GetTaskId() string - SetStage(stageName string, data *jsonutils.JSONDict) + SetStage(stageName string, data *jsonutils.JSONDict) error GetTaskRequestHeader() http.Header diff --git a/pkg/cloudcommon/db/taskman/tasks.go b/pkg/cloudcommon/db/taskman/tasks.go index ff9cb2f321..26d5952e28 100644 --- a/pkg/cloudcommon/db/taskman/tasks.go +++ b/pkg/cloudcommon/db/taskman/tasks.go @@ -470,28 +470,28 @@ func (self *STask) SaveRequestContext(data *appctx.AppContextData) { } } -func (self *STask) SaveParams(data *jsonutils.JSONDict) { - self.SetStage("", data) +func (self *STask) SaveParams(data *jsonutils.JSONDict) error { + return self.SetStage("", data) } -func (self *STask) SetStage(stageName string, data *jsonutils.JSONDict) { +func (self *STask) SetStage(stageName string, data *jsonutils.JSONDict) error { _, err := self.GetModelManager().TableSpec().Update(self, func() error { params := jsonutils.NewDict() params.Update(self.Params) if data != nil { params.Update(data) } - stages, _ := params.Get("__stages") - if stages == nil { - stages = jsonutils.NewArray() - params.Add(stages, "__stages") - } - stageList := stages.(*jsonutils.JSONArray) - stageData := jsonutils.NewDict() - stageData.Add(jsonutils.NewString(self.Stage), "name") - stageData.Add(jsonutils.NewTimeString(time.Now()), "complete_at") - stageList.Add(stageData) if len(stageName) > 0 { + stages, _ := params.Get("__stages") + if stages == nil { + stages = jsonutils.NewArray() + params.Add(stages, "__stages") + } + stageList := stages.(*jsonutils.JSONArray) + stageData := jsonutils.NewDict() + stageData.Add(jsonutils.NewString(self.Stage), "name") + stageData.Add(jsonutils.NewTimeString(time.Now()), "complete_at") + stageList.Add(stageData) self.Stage = stageName } self.Params = params @@ -500,6 +500,7 @@ func (self *STask) SetStage(stageName string, data *jsonutils.JSONDict) { if err != nil { log.Errorf("set_stage fail %s", err) } + return err } func (self *STask) GetObjectIdStr() string { diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 198f8c8dd0..37805339b8 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -1195,6 +1195,12 @@ func (self *SGuest) GetExtraDetails(ctx context.Context, userCred mcclient.Token } extra.Add(isGpu, "is_gpu") + if self.IsPrepaidRecycle() { + extra.Add(jsonutils.JSONTrue, "is_prepaid_recycle") + } else { + extra.Add(jsonutils.JSONFalse, "is_prepaid_recycle") + } + return self.moreExtraInfo(extra) } diff --git a/pkg/compute/models/host_recycle.go b/pkg/compute/models/host_recycle.go index 65fef5f7c7..e51d43284e 100644 --- a/pkg/compute/models/host_recycle.go +++ b/pkg/compute/models/host_recycle.go @@ -8,7 +8,6 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/pkg/tristate" - "yunion.io/x/sqlchemy" "strings" "yunion.io/x/onecloud/pkg/cloudcommon/db" @@ -16,6 +15,7 @@ import ( "yunion.io/x/onecloud/pkg/compute/baremetal" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/util/logclient" ) func (self *SHost) GetResourceType() string { @@ -39,9 +39,13 @@ func (self *SGuest) PerformPrepaidRecycle(ctx context.Context, userCred mcclient err := self.doPrepaidRecycle(ctx, userCred) if err != nil { + logclient.AddActionLog(self, logclient.ACT_RECYCLE_PREPAID, self.GetShortDesc(), userCred, false) return nil, httperrors.NewGeneralError(err) } + db.OpsLog.LogEvent(self, db.ACT_RECYCLE_PREPAID, self.GetShortDesc(), userCred) + logclient.AddActionLog(self, logclient.ACT_RECYCLE_PREPAID, self.GetShortDesc(), userCred, true) + return nil, nil } @@ -129,8 +133,6 @@ func (self *SGuest) doPrepaidRecycleNoLock(ctx context.Context, userCred mcclien return err } - log.Infof("save fakeHost success %s", fakeHost.Id) - for i := 0; i < len(guestnics); i += 1 { var nicType string if i == 0 { @@ -166,7 +168,7 @@ func (self *SGuest) doPrepaidRecycleNoLock(ctx context.Context, userCred mcclien } } - diff, err := self.GetModelManager().TableSpec().Update(self, func() error { + _, err = self.GetModelManager().TableSpec().Update(self, func() error { // clear billing information self.BillingType = BILLING_TYPE_POSTPAID self.BillingCycle = "" @@ -177,13 +179,11 @@ func (self *SGuest) doPrepaidRecycleNoLock(ctx context.Context, userCred mcclien }) if err != nil { - log.Errorf("fail to change vm hostId", err) + log.Errorf("clear billing information fail: %s", err) fakeHost.RealDelete(ctx, userCred) return err } - log.Debugf("%s", sqlchemy.UpdateDiffString(diff)) - return nil } @@ -208,9 +208,13 @@ func (self *SGuest) PerformUndoPrepaidRecycle(ctx context.Context, userCred mccl err := doUndoPrepaidRecycle(ctx, userCred, host, self) if err != nil { + logclient.AddActionLog(self, logclient.ACT_UNDO_RECYCLE_PREPAID, self.GetShortDesc(), userCred, false) return nil, httperrors.NewGeneralError(err) } + db.OpsLog.LogEvent(self, db.ACT_UNDO_RECYCLE_PREPAID, self.GetShortDesc(), userCred) + logclient.AddActionLog(self, logclient.ACT_UNDO_RECYCLE_PREPAID, self.GetShortDesc(), userCred, true) + return nil, nil } @@ -239,9 +243,13 @@ func (self *SHost) PerformUndoPrepaidRecycle(ctx context.Context, userCred mccli err := doUndoPrepaidRecycle(ctx, userCred, self, &guests[0]) if err != nil { + logclient.AddActionLog(self, logclient.ACT_UNDO_RECYCLE_PREPAID, self.GetShortDesc(), userCred, false) return nil, httperrors.NewGeneralError(err) } + db.OpsLog.LogEvent(self, db.ACT_UNDO_RECYCLE_PREPAID, self.GetShortDesc(), userCred) + logclient.AddActionLog(self, logclient.ACT_UNDO_RECYCLE_PREPAID, self.GetShortDesc(), userCred, true) + return nil, nil } @@ -281,9 +289,8 @@ func doUndoPrepaidRecycle(ctx context.Context, userCred mcclient.TokenCredential server.HostId = oHost.Id return nil }) - if err != nil { - log.Errorf("fail to recover vm hostId", err) + log.Errorf("fail to recover vm hostId %s", err) return err } @@ -375,11 +382,6 @@ func (host *SHost) SetGuestCreateNetworkAndDiskParams(ctx context.Context, userC } params.Set(fmt.Sprintf("net.%d", netIdx), jsonutils.JSONNull) - /*diskConfs := make([]baremetal.BaremetalStorage, 0) - err := host.StorageInfo.Unmarshal(&diskConfs) - if err != nil { - return nil, err - }*/ for i := 0; i < len(idisks); i += 1 { istorage, err := idisks[i].GetIStorage() if err != nil { diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 5eb6e3867e..675855e386 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -2007,6 +2007,13 @@ func (self *SHost) getMoreDetails(ctx context.Context, extra *jsonutils.JSONDict } extra.Add(jsonutils.NewFloat(memCommitRate), "mem_commit_rate") extra = self.SManagedResourceBase.getExtraDetails(ctx, extra) + + if self.IsPrepaidRecycle() { + extra.Add(jsonutils.JSONTrue, "is_prepaid_recycle") + } else { + extra.Add(jsonutils.JSONFalse, "is_prepaid_recycle") + } + return extra } diff --git a/pkg/compute/tasks/guest_batch_create_task.go b/pkg/compute/tasks/guest_batch_create_task.go index 9c3dc38636..1e13a922ff 100644 --- a/pkg/compute/tasks/guest_batch_create_task.go +++ b/pkg/compute/tasks/guest_batch_create_task.go @@ -69,7 +69,6 @@ func (self *GuestBatchCreateTask) SaveScheduleResult(ctx context.Context, obj IS self.SaveParams(self.Params) } - log.Debugf("%s", self.Params) err = guest.CreateNetworksOnHost(ctx, self.UserCred, host, self.Params, &pendingUsage) self.SetPendingUsage(&pendingUsage) diff --git a/pkg/compute/tasks/guest_rebuild_root_task.go b/pkg/compute/tasks/guest_rebuild_root_task.go index 87ec01cae2..b19ae818b5 100644 --- a/pkg/compute/tasks/guest_rebuild_root_task.go +++ b/pkg/compute/tasks/guest_rebuild_root_task.go @@ -105,6 +105,13 @@ func (self *GuestRebuildRootTask) OnRebuildingDataDisksComplete(ctx context.Cont } } +func (self *GuestRebuildRootTask) OnRebuildingDataDisksCompleteFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) { + db.OpsLog.LogEvent(guest, db.ACT_REBUILD_ROOT_FAIL, data, self.UserCred) + guest.SetStatus(self.UserCred, models.VM_REBUILD_ROOT_FAIL, "OnRebuildingDataDisksCompleteFailed") + logclient.AddActionLog(guest, logclient.ACT_VM_REBUILD, data, self.UserCred, false) + self.SetStageFailed(ctx, data.String()) +} + func (self *GuestRebuildRootTask) OnRebuildAllDisksComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) { imgId, _ := self.Params.GetString("image_id") imginfo, err := models.CachedimageManager.GetImageById(ctx, self.UserCred, imgId, false) @@ -144,7 +151,7 @@ func (self *GuestRebuildRootTask) OnRebuildAllDisksComplete(ctx context.Context, func (self *GuestRebuildRootTask) OnRebuildRootDiskCompleteFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) { db.OpsLog.LogEvent(guest, db.ACT_REBUILD_ROOT_FAIL, data, self.UserCred) - guest.SetStatus(self.UserCred, models.VM_REBUILD_ROOT_FAIL, "") + guest.SetStatus(self.UserCred, models.VM_REBUILD_ROOT_FAIL, "OnRebuildRootDiskCompleteFailed") logclient.AddActionLog(guest, logclient.ACT_VM_REBUILD, data, self.UserCred, false) self.SetStageFailed(ctx, data.String()) } diff --git a/pkg/compute/tasks/schedule.go b/pkg/compute/tasks/schedule.go index f9ca5a8e30..245a64c967 100644 --- a/pkg/compute/tasks/schedule.go +++ b/pkg/compute/tasks/schedule.go @@ -34,7 +34,7 @@ type IScheduleTask interface { GetUserCred() mcclient.TokenCredential GetSchedParams() *jsonutils.JSONDict GetPendingUsage(quota quotas.IQuota) error - SetStage(stageName string, data *jsonutils.JSONDict) + SetStage(stageName string, data *jsonutils.JSONDict) error SetStageFailed(ctx context.Context, reason string) OnStartSchedule(obj IScheduleModel) diff --git a/pkg/util/logclient/logclient.go b/pkg/util/logclient/logclient.go index 6a38b3f460..f17997ff29 100644 --- a/pkg/util/logclient/logclient.go +++ b/pkg/util/logclient/logclient.go @@ -57,6 +57,9 @@ const ( ACT_VM_UNBIND_KEYPAIR = "解绑密钥" ACT_VM_ASSIGNSECGROUP = "关联安全组" ACT_RESET_DISK = "回滚磁盘" + + ACT_RECYCLE_PREPAID = "池化预付费主机" + ACT_UNDO_RECYCLE_PREPAID = "取消池化预付费主机" ) // golang 不支持 const 的string array, http://t.cn/EzAvbw8