From 0d1a33b495cce61fc79d0ddb248ce6455de3179c Mon Sep 17 00:00:00 2001 From: Zhang Dongliang Date: Sat, 25 Aug 2018 07:59:46 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=BF=E9=87=8C=E4=BA=91=E9=87=8D=E8=A3=85?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E3=80=81=E5=BC=80=E6=9C=BA=E5=85=B3=E6=9C=BA?= =?UTF-8?q?=E3=80=81=E5=90=8C=E6=AD=A5=E7=8A=B6=E6=80=81=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/compute/tasks/guest_rebuild_root_task.go | 16 +++++----- pkg/compute/tasks/guest_start_task.go | 4 +-- pkg/compute/tasks/guest_stop_task.go | 4 +-- pkg/compute/tasks/guest_syncstatus_task.go | 4 +-- pkg/util/logclient/logclient.go | 31 +++++++------------- 5 files changed, 25 insertions(+), 34 deletions(-) diff --git a/pkg/compute/tasks/guest_rebuild_root_task.go b/pkg/compute/tasks/guest_rebuild_root_task.go index 165b8ff213..1bd7c81249 100644 --- a/pkg/compute/tasks/guest_rebuild_root_task.go +++ b/pkg/compute/tasks/guest_rebuild_root_task.go @@ -49,7 +49,7 @@ func (self *GuestRebuildRootTask) StartRebuildRootDisk(ctx context.Context, gues }) if err != nil { self.SetStageFailed(ctx, err.Error()) - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_REBUILD, "", guest, err.Error()) + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_REBUILD, "", guest, err.Error()) return } else { db.OpsLog.LogEvent(gds.Root, db.ACT_UPDATE_STATUS, @@ -66,19 +66,19 @@ func (self *GuestRebuildRootTask) OnRebuildRootDiskComplete(ctx context.Context, imginfo, err := models.CachedimageManager.GetImageById(ctx, self.UserCred, imgId, false) if err != nil { self.SetStageFailed(ctx, err.Error()) - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_REBUILD, "", guest, err.Error()) + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_REBUILD, "", guest, err.Error()) return } osprof, err := osprofile.GetOSProfileFromImageProperties(imginfo.Properties, guest.Hypervisor) if err != nil { self.SetStageFailed(ctx, err.Error()) - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_REBUILD, "", guest, err.Error()) + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_REBUILD, "", guest, err.Error()) return } err = guest.SetMetadata(ctx, "__os_profile__", osprof, self.UserCred) if err != nil { self.SetStageFailed(ctx, err.Error()) - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_REBUILD, "", guest, err.Error()) + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_REBUILD, "", guest, err.Error()) return } if guest.OsType != osprof.OSType { @@ -88,7 +88,7 @@ func (self *GuestRebuildRootTask) OnRebuildRootDiskComplete(ctx context.Context, }) if err != nil { self.SetStageFailed(ctx, err.Error()) - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_REBUILD, "", guest, err.Error()) + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_REBUILD, "", guest, err.Error()) return } } @@ -101,7 +101,7 @@ func (self *GuestRebuildRootTask) OnRebuildRootDiskComplete(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.String(), self.UserCred) guest.SetStatus(self.UserCred, models.VM_REBUILD_ROOT_FAIL, "") - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_REBUILD, "", guest, data.String()) + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_REBUILD, "", guest, data.String()) } func (self *GuestRebuildRootTask) OnSyncStatusComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) { @@ -111,7 +111,7 @@ func (self *GuestRebuildRootTask) OnSyncStatusComplete(ctx context.Context, gues } else { self.SetStageComplete(ctx, nil) } - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_REBUILD, "", guest, "") + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_REBUILD, "", guest, "") } func (self *GuestRebuildRootTask) OnGuestStartComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) { @@ -143,7 +143,7 @@ func (self *KVMGuestRebuildRootTask) OnRebuildRootDiskComplete(ctx context.Conte func (self *KVMGuestRebuildRootTask) OnRebuildRootDiskCompleteFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) { self.SetStageFailed(ctx, data.String()) - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_REBUILD, "", guest, data.String()) + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_REBUILD, "", guest, data.String()) } func (self *KVMGuestRebuildRootTask) OnGuestDeployComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) { diff --git a/pkg/compute/tasks/guest_start_task.go b/pkg/compute/tasks/guest_start_task.go index 3965a51d05..d5af7cadb3 100644 --- a/pkg/compute/tasks/guest_start_task.go +++ b/pkg/compute/tasks/guest_start_task.go @@ -59,7 +59,7 @@ func (self *GuestStartTask) OnStartComplete(ctx context.Context, obj db.IStandal db.OpsLog.LogEvent(guest, db.ACT_START, guest.GetShortDesc(), self.UserCred) self.SetStage("on_guest_syncstatus_after_start", nil) guest.StartSyncstatus(ctx, self.UserCred, self.GetTaskId()) - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_START, "", guest, "") + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_START, "", guest, "") // self.taskComplete(ctx, guest) } @@ -76,7 +76,7 @@ func (self *GuestStartTask) OnStartCompleteFailed(ctx context.Context, obj db.IS func (self *GuestStartTask) onStartGuestFailed(ctx context.Context, guest *models.SGuest, err error) { self.SetStageFailed(ctx, err.Error()) self.OnStartCompleteFailed(ctx, guest, jsonutils.NewString(err.Error())) - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_START, "", guest, err.Error()) + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_START, "", guest, err.Error()) } func (self *GuestStartTask) taskComplete(ctx context.Context, guest *models.SGuest) { diff --git a/pkg/compute/tasks/guest_stop_task.go b/pkg/compute/tasks/guest_stop_task.go index aa0119d549..303c48b2e0 100644 --- a/pkg/compute/tasks/guest_stop_task.go +++ b/pkg/compute/tasks/guest_stop_task.go @@ -58,12 +58,12 @@ func (self *GuestStopTask) OnGuestStopTaskComplete(ctx context.Context, obj db.I if guest.Status == models.VM_READY && guest.DisableDelete.IsFalse() && guest.ShutdownBehavior == models.SHUTDOWN_TERMINATE { guest.StartAutoDeleteGuestTask(ctx, self.UserCred, "") } - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_STOP, "", guest, "") + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_STOP, "", guest, "") } func (self *GuestStopTask) OnStopGuestFail(ctx context.Context, guest *models.SGuest, err error) { guest.SetStatus(self.UserCred, models.VM_STOP_FAILED, err.Error()) db.OpsLog.LogEvent(guest, db.ACT_STOP_FAIL, err.Error(), self.UserCred) self.SetStageFailed(ctx, err.Error()) - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_STOP, "", guest, err.Error()) + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_STOP, "", guest, err.Error()) } diff --git a/pkg/compute/tasks/guest_syncstatus_task.go b/pkg/compute/tasks/guest_syncstatus_task.go index 7b161e9f27..c315525baf 100644 --- a/pkg/compute/tasks/guest_syncstatus_task.go +++ b/pkg/compute/tasks/guest_syncstatus_task.go @@ -58,11 +58,11 @@ func (self *GuestSyncstatusTask) OnGetStatusSucc(ctx context.Context, guest *mod fmt.Println(" \n*\n*\n*\n*\n*\n*\n*\n*\n*\n*\n*\\n*n* * * * * * * * * * * * * ") fmt.Println("* * * * * * * * * * * * * server obj:", guest) fmt.Println(" \n*\n*\n*\n*\n*\n*\n*\n*\n*\n*\n*\\n*n* * * * * * * * * * * * * ") - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_SYNC_STATUS, "", guest, "") + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_SYNC_STATUS, "", guest, "") } func (self *GuestSyncstatusTask) OnGetStatusFail(ctx context.Context, guest *models.SGuest, err error) { guest.SetStatus(self.UserCred, models.VM_UNKNOWN, err.Error()) self.SetStageComplete(ctx, nil) - logclient.AddActionLog(ctx, self.UserCred, logclient.ACT_VM_SYNC_STATUS, "", guest, err.Error()) + logclient.AddActionLog(self.UserCred, logclient.ACT_VM_SYNC_STATUS, "", guest, err.Error()) } diff --git a/pkg/util/logclient/logclient.go b/pkg/util/logclient/logclient.go index b7b52ec23f..658bc2887f 100644 --- a/pkg/util/logclient/logclient.go +++ b/pkg/util/logclient/logclient.go @@ -1,16 +1,12 @@ package logclient import ( - "context" "fmt" - "log" - "net/http" "yunion.io/x/jsonutils" - "yunion.io/x/onecloud/pkg/compute/options" "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/auth" - "yunion.io/x/onecloud/pkg/util/httputils" + "yunion.io/x/onecloud/pkg/mcclient/modules" ) const ( @@ -57,7 +53,7 @@ type IObject interface { Keyword() string } -func AddActionLog(ctx context.Context, userCred mcclient.TokenCredential, action, notes string, obj IObject, e string) { +func AddActionLog(userCred mcclient.TokenCredential, action, notes string, obj IObject, e string) { token := userCred logentry := jsonutils.NewDict() @@ -69,8 +65,11 @@ func AddActionLog(ctx context.Context, userCred mcclient.TokenCredential, action logentry.Add(jsonutils.NewString(token.GetUserName()), "user") logentry.Add(jsonutils.NewString(token.GetTenantId()), "tenant_id") logentry.Add(jsonutils.NewString(token.GetTenantName()), "tenant") - // todo: 正式版去掉下面这行。 - notes = "[region2]" + notes + + // TODO delete following line later + notes = "[a2]" + notes + + s := auth.GetSession(userCred, "", "") if len(e) > 0 { // 失败日志 @@ -83,18 +82,10 @@ func AddActionLog(ctx context.Context, userCred mcclient.TokenCredential, action logentry.Add(jsonutils.NewString(notes), "notes") } - uri, err := auth.GetServiceURL("log", options.Options.Region, "", "") + _, err := modules.Actions.Create(s, logentry) if err != nil { - log.Println("log service not ready", err) - return - } - - uri = fmt.Sprintf("%s/actions", uri) - header := http.Header{} - header.Add("X-Auth-Token", userCred.GetTokenString()) - _, _, err = httputils.JSONRequest(httputils.GetDefaultClient(), ctx, "POST", uri, header, logentry, false) - if err != nil { - log.Println("post log error", err) - return + fmt.Printf("create action log failed %s", err) + } else { + fmt.Println("create action log success") } }