diff --git a/pkg/compute/hostdrivers/aliyun.go b/pkg/compute/hostdrivers/aliyun.go index f6e0ded615..fc6dfea21a 100644 --- a/pkg/compute/hostdrivers/aliyun.go +++ b/pkg/compute/hostdrivers/aliyun.go @@ -17,12 +17,10 @@ package hostdrivers import ( "fmt" - "yunion.io/x/jsonutils" "yunion.io/x/pkg/utils" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" - "yunion.io/x/onecloud/pkg/httperrors" ) type SAliyunHostDriver struct { @@ -42,10 +40,6 @@ func (self *SAliyunHostDriver) GetHypervisor() string { return api.HYPERVISOR_ALIYUN } -func (self *SAliyunHostDriver) ValidateAttachStorage(host *models.SHost, storage *models.SStorage, data *jsonutils.JSONDict) error { - return httperrors.NewUnsupportOperationError("Not support attach storage for %s host", self.GetHostType()) -} - func (self *SAliyunHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb int) error { if utils.IsInStringArray(storage.StorageType, []string{api.STORAGE_CLOUD_EFFICIENCY, api.STORAGE_CLOUD_SSD, api.STORAGE_CLOUD_ESSD}) { if sizeGb < 20 || sizeGb > 32768 { diff --git a/pkg/compute/hostdrivers/aws.go b/pkg/compute/hostdrivers/aws.go index 0a86acbe88..3f2c673d3a 100644 --- a/pkg/compute/hostdrivers/aws.go +++ b/pkg/compute/hostdrivers/aws.go @@ -17,12 +17,10 @@ package hostdrivers import ( "fmt" - "yunion.io/x/jsonutils" "yunion.io/x/pkg/utils" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" - "yunion.io/x/onecloud/pkg/httperrors" ) type SAwsHostDriver struct { @@ -42,10 +40,6 @@ func (self *SAwsHostDriver) GetHypervisor() string { return api.HYPERVISOR_AWS } -func (self *SAwsHostDriver) ValidateAttachStorage(host *models.SHost, storage *models.SStorage, data *jsonutils.JSONDict) error { - return httperrors.NewUnsupportOperationError("Not support attach storage for %s host", self.GetHostType()) -} - func (self *SAwsHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb int) error { if storage.StorageType == api.STORAGE_GP2_SSD { if sizeGb < 1 || sizeGb > 16384 { diff --git a/pkg/compute/hostdrivers/azure.go b/pkg/compute/hostdrivers/azure.go index b5a032ae88..2b6eeb4814 100644 --- a/pkg/compute/hostdrivers/azure.go +++ b/pkg/compute/hostdrivers/azure.go @@ -45,10 +45,6 @@ func (self *SAzureHostDriver) GetHypervisor() string { return api.HYPERVISOR_AZURE } -func (self *SAzureHostDriver) ValidateAttachStorage(host *models.SHost, storage *models.SStorage, data *jsonutils.JSONDict) error { - return httperrors.NewUnsupportOperationError("Not support attach storage for %s host", self.GetHostType()) -} - func (self *SAzureHostDriver) ValidateUpdateDisk(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { if data.Contains("name") { return nil, httperrors.NewInputParameterError("cannot support change azure disk name") diff --git a/pkg/compute/hostdrivers/baremetal.go b/pkg/compute/hostdrivers/baremetal.go index 922fe1c3be..bf90461b22 100644 --- a/pkg/compute/hostdrivers/baremetal.go +++ b/pkg/compute/hostdrivers/baremetal.go @@ -23,7 +23,6 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" "yunion.io/x/onecloud/pkg/compute/models" - "yunion.io/x/onecloud/pkg/httperrors" ) type SBaremetalHostDriver struct { @@ -43,10 +42,6 @@ func (self *SBaremetalHostDriver) GetHypervisor() string { return api.HYPERVISOR_BAREMETAL } -func (self *SBaremetalHostDriver) ValidateAttachStorage(host *models.SHost, storage *models.SStorage, data *jsonutils.JSONDict) error { - return httperrors.NewUnsupportOperationError("Not support attach storage for %s host", self.GetHostType()) -} - func (self *SBaremetalHostDriver) CheckAndSetCacheImage(ctx context.Context, host *models.SHost, storageCache *models.SStoragecache, task taskman.ITask) error { return fmt.Errorf("not supported") } diff --git a/pkg/compute/hostdrivers/base.go b/pkg/compute/hostdrivers/base.go index 8a24e55f1e..93cd5e337a 100644 --- a/pkg/compute/hostdrivers/base.go +++ b/pkg/compute/hostdrivers/base.go @@ -38,7 +38,7 @@ func (self *SBaseHostDriver) ValidateUpdateDisk(ctx context.Context, userCred mc return data, nil } -func (self *SBaseHostDriver) ValidateAttachStorage(host *models.SHost, storage *models.SStorage, data *jsonutils.JSONDict) error { +func (self *SBaseHostDriver) ValidateAttachStorage(ctx context.Context, userCred mcclient.TokenCredential, host *models.SHost, storage *models.SStorage, data *jsonutils.JSONDict) error { return httperrors.NewNotImplementedError("Not Implement ValidateAttachStorage") } diff --git a/pkg/compute/hostdrivers/esxi.go b/pkg/compute/hostdrivers/esxi.go index 2b8bab52cc..7a65dff63f 100644 --- a/pkg/compute/hostdrivers/esxi.go +++ b/pkg/compute/hostdrivers/esxi.go @@ -25,7 +25,6 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" "yunion.io/x/onecloud/pkg/compute/models" - "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/util/httputils" ) @@ -50,10 +49,6 @@ func (self *SESXiHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb i return nil } -func (self *SESXiHostDriver) ValidateAttachStorage(host *models.SHost, storage *models.SStorage, data *jsonutils.JSONDict) error { - return httperrors.NewUnsupportOperationError("Not support attach storage for %s host", self.GetHostType()) -} - func (self *SESXiHostDriver) CheckAndSetCacheImage(ctx context.Context, host *models.SHost, storageCache *models.SStoragecache, task taskman.ITask) error { params := task.GetParams() imageId, err := params.GetString("image_id") diff --git a/pkg/compute/hostdrivers/huawei.go b/pkg/compute/hostdrivers/huawei.go index fac851e111..d6856a7cca 100644 --- a/pkg/compute/hostdrivers/huawei.go +++ b/pkg/compute/hostdrivers/huawei.go @@ -17,11 +17,8 @@ package hostdrivers import ( "fmt" - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" - "yunion.io/x/onecloud/pkg/httperrors" ) type SHuaweiHostDriver struct { @@ -41,10 +38,6 @@ func (self *SHuaweiHostDriver) GetHypervisor() string { return api.HYPERVISOR_HUAWEI } -func (self *SHuaweiHostDriver) ValidateAttachStorage(host *models.SHost, storage *models.SStorage, data *jsonutils.JSONDict) error { - return httperrors.NewUnsupportOperationError("Not support attach storage for %s host", self.GetHostType()) -} - // 系统盘必须至少40G func (self *SHuaweiHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb int) error { switch storage.StorageType { diff --git a/pkg/compute/hostdrivers/kvm.go b/pkg/compute/hostdrivers/kvm.go index 955661a332..528974d86a 100644 --- a/pkg/compute/hostdrivers/kvm.go +++ b/pkg/compute/hostdrivers/kvm.go @@ -17,6 +17,7 @@ package hostdrivers import ( "context" "fmt" + "net/http" "net/url" "yunion.io/x/jsonutils" @@ -51,7 +52,7 @@ func (self *SKVMHostDriver) GetHypervisor() string { return api.HYPERVISOR_KVM } -func (self *SKVMHostDriver) ValidateAttachStorage(host *models.SHost, storage *models.SStorage, data *jsonutils.JSONDict) error { +func (self *SKVMHostDriver) ValidateAttachStorage(ctx context.Context, userCred mcclient.TokenCredential, host *models.SHost, storage *models.SStorage, data *jsonutils.JSONDict) error { if !utils.IsInStringArray(storage.StorageType, append([]string{api.STORAGE_LOCAL}, api.SHARED_STORAGE...)) { return httperrors.NewUnsupportOperationError("Unsupport attach %s storage for %s host", storage.StorageType, host.HostType) } @@ -76,6 +77,21 @@ func (self *SKVMHostDriver) ValidateAttachStorage(host *models.SHost, storage *m if host.HostStatus != api.HOST_ONLINE { return httperrors.NewInvalidStatusError("Attach nfs storage require host status is online") } + if storage.StorageType == api.STORAGE_GPFS { + header := http.Header{} + header.Set(mcclient.AUTH_TOKEN, userCred.GetTokenString()) + header.Set(mcclient.REGION_VERSION, "v2") + params := jsonutils.NewDict() + params.Set("mount_point", jsonutils.NewString(mountPoint)) + urlStr := fmt.Sprintf("%s/storages/is-mount-point?%s", host.ManagerUri, params.QueryString()) + _, res, err := httputils.JSONRequest(httputils.GetDefaultClient(), ctx, "GET", urlStr, header, nil, false) + if err != nil { + return err + } + if !jsonutils.QueryBoolean(res, "is_mount_point", false) { + return httperrors.NewBadRequestError("%s is not mount point %s", mountPoint, res) + } + } } return nil } diff --git a/pkg/compute/hostdrivers/openstack.go b/pkg/compute/hostdrivers/openstack.go index 9a7f322f9c..b5321677b6 100644 --- a/pkg/compute/hostdrivers/openstack.go +++ b/pkg/compute/hostdrivers/openstack.go @@ -15,11 +15,8 @@ package hostdrivers import ( - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" - "yunion.io/x/onecloud/pkg/httperrors" ) type SOpenStackHostDriver struct { @@ -39,10 +36,6 @@ func (self *SOpenStackHostDriver) GetHypervisor() string { return api.HYPERVISOR_OPENSTACK } -func (self *SOpenStackHostDriver) ValidateAttachStorage(host *models.SHost, storage *models.SStorage, data *jsonutils.JSONDict) error { - return httperrors.NewUnsupportOperationError("Not support attach storage for %s host", self.GetHostType()) -} - func (self *SOpenStackHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb int) error { return nil } diff --git a/pkg/compute/hostdrivers/qcloud.go b/pkg/compute/hostdrivers/qcloud.go index bc7d9d5153..cf269fff3e 100644 --- a/pkg/compute/hostdrivers/qcloud.go +++ b/pkg/compute/hostdrivers/qcloud.go @@ -18,8 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" "yunion.io/x/onecloud/pkg/compute/models" @@ -43,10 +41,6 @@ func (self *SQcloudHostDriver) GetHypervisor() string { return api.HYPERVISOR_OPENSTACK } -func (self *SQcloudHostDriver) ValidateAttachStorage(host *models.SHost, storage *models.SStorage, data *jsonutils.JSONDict) error { - return httperrors.NewUnsupportOperationError("Not support attach storage for %s host", self.GetHostType()) -} - func (self *SQcloudHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb int) error { if sizeGb%10 != 0 { return fmt.Errorf("The disk size must be a multiple of 10Gb") diff --git a/pkg/compute/hostdrivers/zstack.go b/pkg/compute/hostdrivers/zstack.go index 2a4bb69bb3..736facf7ae 100644 --- a/pkg/compute/hostdrivers/zstack.go +++ b/pkg/compute/hostdrivers/zstack.go @@ -15,11 +15,8 @@ package hostdrivers import ( - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" - "yunion.io/x/onecloud/pkg/httperrors" ) type SZStackHostDriver struct { @@ -39,10 +36,6 @@ func (self *SZStackHostDriver) GetHypervisor() string { return api.HYPERVISOR_ZSTACK } -func (self *SZStackHostDriver) ValidateAttachStorage(host *models.SHost, storage *models.SStorage, data *jsonutils.JSONDict) error { - return httperrors.NewUnsupportOperationError("Not support attach storage for %s host", self.GetHostType()) -} - func (self *SZStackHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb int) error { return nil } diff --git a/pkg/compute/models/hostdrivers.go b/pkg/compute/models/hostdrivers.go index f7daf64713..66821ed010 100644 --- a/pkg/compute/models/hostdrivers.go +++ b/pkg/compute/models/hostdrivers.go @@ -60,7 +60,7 @@ type IHostDriver interface { IsReachStoragecacheCapacityLimit(host *SHost, cachedImages []SCachedimage) bool GetStoragecacheQuota(host *SHost) int - ValidateAttachStorage(host *SHost, storage *SStorage, data *jsonutils.JSONDict) error + ValidateAttachStorage(ctx context.Context, userCred mcclient.TokenCredential, host *SHost, storage *SStorage, data *jsonutils.JSONDict) error RequestAttachStorage(ctx context.Context, hoststorage *SHoststorage, host *SHost, storage *SStorage, task taskman.ITask) error RequestDetachStorage(ctx context.Context, host *SHost, storage *SStorage, task taskman.ITask) error } diff --git a/pkg/compute/models/hoststorages.go b/pkg/compute/models/hoststorages.go index badc60f7cb..1dff80dc8c 100644 --- a/pkg/compute/models/hoststorages.go +++ b/pkg/compute/models/hoststorages.go @@ -131,7 +131,7 @@ func (manager *SHoststorageManager) ValidateCreateData(ctx context.Context, user } host := hostTmp.(*SHost) - if err := host.GetHostDriver().ValidateAttachStorage(host, storage, data); err != nil { + if err := host.GetHostDriver().ValidateAttachStorage(ctx, userCred, host, storage, data); err != nil { return nil, err } diff --git a/pkg/hostman/storageman/storagehandler/storagehandler.go b/pkg/hostman/storageman/storagehandler/storagehandler.go index f2e6f7a7dd..239617e1be 100644 --- a/pkg/hostman/storageman/storagehandler/storagehandler.go +++ b/pkg/hostman/storageman/storagehandler/storagehandler.go @@ -28,6 +28,7 @@ import ( "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient/auth" "yunion.io/x/onecloud/pkg/mcclient/modules" + "yunion.io/x/onecloud/pkg/util/procutils" ) var ( @@ -49,6 +50,27 @@ func AddStorageHandler(prefix string, app *appsrv.Application) { app.AddHandler("POST", fmt.Sprintf("%s/%s//delete-snapshots", prefix, keyWords), auth.Authenticate(storageDeleteSnapshots)) + app.AddHandler("GET", + fmt.Sprintf("%s/%s/is-mount-point", prefix, keyWords), + auth.Authenticate(storageVerifyMountPoint)) + } +} + +func storageVerifyMountPoint(ctx context.Context, w http.ResponseWriter, r *http.Request) { + _, query, _ := appsrv.FetchEnv(ctx, w, r) + mountPoint, err := query.GetString("mount_point") + if err != nil { + hostutils.Response(ctx, w, httperrors.NewMissingParameterError("mount_point")) + return + } + output, err := procutils.NewCommand("mountpoint", mountPoint).Run() + if err == nil { + appsrv.SendStruct(w, map[string]interface{}{"is_mount_point": true}) + } else { + appsrv.SendStruct(w, map[string]interface{}{ + "is_mount_point": false, + "error": string(output), + }) } }