From a0d51a61f4ffe483bba9d5fdfbe7a01855fae2be Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Wed, 19 Jun 2019 19:33:23 +0800 Subject: [PATCH] fix storage attach host sync status --- pkg/compute/hostdrivers/kvm.go | 11 +++++++++++ pkg/compute/tasks/host_storage_attach_task.go | 1 + pkg/compute/tasks/host_storage_detach_task.go | 17 +++++++++++++++++ pkg/util/logclient/logclient.go | 1 + 4 files changed, 30 insertions(+) diff --git a/pkg/compute/hostdrivers/kvm.go b/pkg/compute/hostdrivers/kvm.go index 2adbbe3bb8..8e57424800 100644 --- a/pkg/compute/hostdrivers/kvm.go +++ b/pkg/compute/hostdrivers/kvm.go @@ -62,6 +62,17 @@ func (self *SKVMHostDriver) ValidateAttachStorage(host *models.SHost, storage *m pool, _ := storage.StorageConf.GetString("pool") data.Set("mount_point", jsonutils.NewString(fmt.Sprintf("rbd:%s", pool))) } else if utils.IsInStringArray(storage.StorageType, api.SHARED_FILE_STORAGE) { + mountPoint, err := data.GetString("mount_point") + if err != nil { + return httperrors.NewMissingParameterError("mount_point") + } + count, err := models.HoststorageManager.Query().Equals("host_id", host.Id).Equals("mount_point", mountPoint).CountWithError() + if err != nil { + return httperrors.NewInternalServerError("Query host storage error %s", err) + } + if count > 0 { + return httperrors.NewBadRequestError("Host %s already have mount point %s with other storage", host.Name, mountPoint) + } if host.HostStatus != api.HOST_ONLINE { return httperrors.NewInvalidStatusError("Attach nfs storage require host status is online") } diff --git a/pkg/compute/tasks/host_storage_attach_task.go b/pkg/compute/tasks/host_storage_attach_task.go index 0406264c9c..284183d2d5 100644 --- a/pkg/compute/tasks/host_storage_attach_task.go +++ b/pkg/compute/tasks/host_storage_attach_task.go @@ -77,6 +77,7 @@ func (self *HostStorageAttachTask) OnAttachStorageComplete(ctx context.Context, db.OpsLog.LogEvent(storage, db.ACT_ATTACH, "", self.GetUserCred()) logclient.AddActionLogWithContext(ctx, storage, logclient.ACT_ATTACH_HOST, fmt.Sprintf("Attach host %s success", host.Name), self.GetUserCred(), true) + storage.SyncStatusWithHosts() self.SetStageComplete(ctx, nil) } diff --git a/pkg/compute/tasks/host_storage_detach_task.go b/pkg/compute/tasks/host_storage_detach_task.go index bde9df8f2d..47a933bc5f 100644 --- a/pkg/compute/tasks/host_storage_detach_task.go +++ b/pkg/compute/tasks/host_storage_detach_task.go @@ -16,12 +16,14 @@ package tasks import ( "context" + "fmt" "yunion.io/x/jsonutils" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" "yunion.io/x/onecloud/pkg/compute/models" + "yunion.io/x/onecloud/pkg/util/logclient" ) type HostStorageDetachTask struct { @@ -33,6 +35,15 @@ func init() { } func (self *HostStorageDetachTask) taskFail(ctx context.Context, host *models.SHost, reason string) { + var hoststorage = new(models.SHoststorage) + storageId, _ := self.GetParams().GetString("storage_id") + err := models.HoststorageManager.Query().Equals("host_id", host.Id).Equals("storage_id", storageId).First(hoststorage) + if err == nil { + storage := hoststorage.GetStorage() + note := fmt.Sprintf("detach host %s failed: %s", host.Name, reason) + db.OpsLog.LogEvent(storage, db.ACT_DETACH_FAIL, note, self.GetUserCred()) + logclient.AddActionLogWithContext(ctx, storage, logclient.ACT_DETACH_HOST, note, self.GetUserCred(), false) + } self.SetStageFailed(ctx, reason) } @@ -53,7 +64,13 @@ func (self *HostStorageDetachTask) OnInit(ctx context.Context, obj db.IStandalon } func (self *HostStorageDetachTask) OnDetachStorageComplete(ctx context.Context, host *models.SHost, data jsonutils.JSONObject) { + storageId, _ := self.GetParams().GetString("storage_id") + storage := models.StorageManager.FetchStorageById(storageId) + db.OpsLog.LogEvent(storage, db.ACT_DETACH, "", self.GetUserCred()) + logclient.AddActionLogWithContext(ctx, storage, logclient.ACT_DETACH_HOST, + fmt.Sprintf("Detach host %s success", host.Name), self.GetUserCred(), true) self.SetStageComplete(ctx, nil) + storage.SyncStatusWithHosts() } func (self *HostStorageDetachTask) OnDetachStorageCompleteFailed(ctx context.Context, host *models.SHost, reason jsonutils.JSONObject) { diff --git a/pkg/util/logclient/logclient.go b/pkg/util/logclient/logclient.go index 1ccca9bcfc..48960b1142 100644 --- a/pkg/util/logclient/logclient.go +++ b/pkg/util/logclient/logclient.go @@ -141,6 +141,7 @@ const ( ACT_APPLY_SNAPSHOT_POLICY = "绑定快照策略" ACT_CANCEL_SNAPSHOT_POLICY = "取消快照策略" ACT_ATTACH_HOST = "关联宿主机" + ACT_DETACH_HOST = "取消关联宿主机" ACT_IMAGE_SAVE = "上传镜像" ACT_IMAGE_PROBE = "镜像检测"