| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package hostdrivers
- import (
- "context"
- "fmt"
- "yunion.io/x/cloudmux/pkg/cloudprovider"
- "yunion.io/x/cloudmux/pkg/multicloud/esxi/vcenter"
- "yunion.io/x/jsonutils"
- "yunion.io/x/log"
- "yunion.io/x/pkg/errors"
- "yunion.io/x/pkg/util/httputils"
- 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/mcclient"
- )
- type SESXiHostDriver struct {
- SManagedVirtualizationHostDriver
- }
- func init() {
- driver := SESXiHostDriver{}
- models.RegisterHostDriver(&driver)
- }
- func (self *SESXiHostDriver) GetHostType() string {
- return api.HOST_TYPE_ESXI
- }
- func (self *SESXiHostDriver) GetHypervisor() string {
- return api.HYPERVISOR_ESXI
- }
- func (self *SESXiHostDriver) GetProvider() string {
- return api.CLOUD_PROVIDER_ONECLOUD
- }
- func (self *SESXiHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb int) error {
- return nil
- }
- func (self *SESXiHostDriver) RequestRemoteUpdateDisk(ctx context.Context, userCred mcclient.TokenCredential, storage *models.SStorage, disk *models.SDisk, replaceTags bool) error {
- return nil
- }
- func (self *SESXiHostDriver) CheckAndSetCacheImage(ctx context.Context, userCred mcclient.TokenCredential, host *models.SHost, storageCache *models.SStoragecache, task taskman.ITask) error {
- params := task.GetParams()
- imageId, err := params.GetString("image_id")
- if err != nil {
- return err
- }
- isForce := jsonutils.QueryBoolean(params, "is_force", false)
- obj, err := models.CachedimageManager.FetchById(imageId)
- if err != nil {
- return err
- }
- cacheImage := obj.(*models.SCachedimage)
- var srcHostCacheImage *models.SStoragecachedimage
- // Check if storageCache has this cacheImage.
- // If no, choose source storage cache
- // else, use it
- hostCacheImage := models.StoragecachedimageManager.GetStoragecachedimage(storageCache.GetId(), cacheImage.GetId())
- if hostCacheImage == nil {
- zone, _ := host.GetZone()
- srcHostCacheImage, err = cacheImage.ChooseSourceStoragecacheInRange(api.HOST_TYPE_ESXI, []string{host.Id},
- []interface{}{zone, host.GetCloudprovider()})
- if err != nil {
- return err
- }
- }
- type contentStruct struct {
- ImageId string
- HostId string
- HostIp string
- SrcHostIp string
- SrcPath string
- SrcDatastore vcenter.SVCenterAccessInfo
- Datastore vcenter.SVCenterAccessInfo
- Format string
- IsForce bool
- StoragecacheId string
- ImageType string
- ImageExternalId string
- StorageCacheHostIp string
- }
- content := contentStruct{}
- content.ImageId = imageId
- content.HostId = host.Id
- content.HostIp = host.AccessIp
- // format force VMDK
- format := cacheImage.GetFormat()
- if format == "qcow2" {
- format = "vmdk"
- }
- content.Format = format
- content.ImageType = cacheImage.ImageType
- content.ImageExternalId = cacheImage.ExternalId
- storage := host.GetStorageByFilePath(storageCache.Path)
- if storage == nil {
- msg := fmt.Sprintf("fail to find storage for storageCache %s", storageCache.Path)
- log.Errorf("%s", msg)
- return errors.Error(msg)
- }
- accessInfo, err := host.GetCloudaccount().GetVCenterAccessInfo(storage.ExternalId)
- if err != nil {
- return err
- }
- content.Datastore = accessInfo
- if cloudprovider.TImageType(cacheImage.ImageType) == cloudprovider.ImageTypeSystem {
- var host *models.SHost
- if srcHostCacheImage != nil {
- host, err = srcHostCacheImage.GetHost()
- if err != nil {
- return errors.Wrap(err, "srcHostCacheImage.GetHost")
- }
- } else {
- host, err = storageCache.GetMasterHost()
- if err != nil {
- return errors.Wrap(err, "StorageCache.GetHost")
- }
- }
- content.StorageCacheHostIp = host.AccessIp
- } else if srcHostCacheImage != nil {
- err = srcHostCacheImage.AddDownloadRefcount()
- if err != nil {
- return err
- }
- srcHost, err := srcHostCacheImage.GetHost()
- if err != nil {
- return err
- }
- content.SrcHostIp = srcHost.AccessIp
- content.SrcPath = srcHostCacheImage.Path
- srcStorageCache := srcHostCacheImage.GetStoragecache()
- if srcStorageCache == nil {
- return errors.Wrap(errors.ErrNotFound, "StorageCacheImage.GetStoragecaceh")
- }
- srcStorage := srcHost.GetStorageByFilePath(srcStorageCache.Path)
- accessInfo, err := srcHost.GetCloudaccount().GetVCenterAccessInfo(srcStorage.ExternalId)
- if err != nil {
- return err
- }
- content.SrcDatastore = accessInfo
- }
- if !host.IsEsxiAgentReady() {
- return fmt.Errorf("fail to find valid ESXi agent")
- }
- url := "/disks/image_cache"
- if isForce {
- content.IsForce = true
- }
- content.StoragecacheId = storageCache.Id
- body := jsonutils.NewDict()
- body.Add(jsonutils.Marshal(&content), "disk")
- header := task.GetTaskRequestHeader()
- _, err = host.EsxiRequest(ctx, httputils.POST, url, header, body)
- if err != nil {
- return err
- }
- return nil
- }
- func (self *SESXiHostDriver) RequestAllocateDiskOnStorage(ctx context.Context, userCred mcclient.TokenCredential, host *models.SHost, storage *models.SStorage, disk *models.SDisk, task taskman.ITask, input api.DiskAllocateInput) error {
- if !host.IsEsxiAgentReady() {
- return fmt.Errorf("fail to find valid ESXi agent")
- }
- type specStruct struct {
- Datastore vcenter.SVCenterAccessInfo
- }
- input.HostIp = host.AccessIp
- input.Format = "vmdk"
- var err error
- input.Datastore, err = host.GetCloudaccount().GetVCenterAccessInfo(storage.ExternalId)
- if err != nil {
- return err
- }
- body := jsonutils.NewDict()
- body.Add(jsonutils.Marshal(&input), "disk")
- url := fmt.Sprintf("/disks/agent/create/%s", disk.Id)
- header := task.GetTaskRequestHeader()
- _, err = host.EsxiRequest(ctx, httputils.POST, url, header, body)
- return err
- }
- func (self *SESXiHostDriver) RequestPrepareSaveDiskOnHost(ctx context.Context, host *models.SHost, disk *models.SDisk, imageId string, task taskman.ITask) error {
- if !host.IsEsxiAgentReady() {
- return fmt.Errorf("fail to find valid ESXi agent")
- }
- guests := disk.GetGuests()
- if len(guests) == 0 {
- return fmt.Errorf("No VM associate with this disk")
- }
- if len(guests) > 1 {
- return fmt.Errorf("The disk is attached to multiple guests")
- }
- guest := guests[0]
- if guest.HostId != host.Id {
- return fmt.Errorf("The only guest is not on the host????")
- }
- type specStruct struct {
- Vm vcenter.SVCenterAccessInfo
- Disk vcenter.SVCenterAccessInfo
- HostIp string
- ImageId string
- }
- spec := specStruct{}
- spec.HostIp = host.AccessIp
- spec.ImageId = imageId
- account := host.GetCloudaccount()
- accessInfo, err := account.GetVCenterAccessInfo(guest.ExternalId)
- if err != nil {
- return err
- }
- spec.Vm = accessInfo
- accessInfo, err = account.GetVCenterAccessInfo(disk.ExternalId)
- if err != nil {
- return err
- }
- spec.Disk = accessInfo
- body := jsonutils.NewDict()
- body.Add(jsonutils.Marshal(&spec), "disk")
- url := fmt.Sprintf("/disks/agent/save-prepare/%s", disk.Id)
- header := task.GetTaskRequestHeader()
- _, err = host.EsxiRequest(ctx, httputils.POST, url, header, body)
- return err
- }
- func (self *SESXiHostDriver) RequestResizeDiskOnHost(ctx context.Context, host *models.SHost, storage *models.SStorage, disk *models.SDisk, sizeMb int64, task taskman.ITask) error {
- guest := disk.GetGuest()
- if guest == nil {
- return fmt.Errorf("unable to find guest has disk %s", disk.GetId())
- }
- iVm, err := guest.GetIVM(ctx)
- if err != nil {
- return errors.Wrapf(err, "GetIVM")
- }
- if iVm.GetStatus() == api.VM_RUNNING {
- taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
- disks, err := iVm.GetIDisks()
- if err != nil {
- return nil, errors.Wrapf(err, "GetIDisk")
- }
- for i := range disks {
- if disks[i].GetGlobalId() == disk.ExternalId {
- err = disks[i].Resize(ctx, sizeMb)
- if err != nil {
- return nil, errors.Wrapf(err, "Resize")
- }
- return jsonutils.Marshal(map[string]int64{"disk_size": sizeMb}), nil
- }
- }
- return nil, errors.Wrapf(cloudprovider.ErrNotFound, "disk %s", disk.Name)
- })
- return nil
- }
- spec := struct {
- HostInfo vcenter.SVCenterAccessInfo
- VMId string
- DiskId string
- SizeMb int64
- }{}
- account := host.GetCloudaccount()
- accessInfo, err := account.GetVCenterAccessInfo(host.ExternalId)
- if err != nil {
- return err
- }
- spec.HostInfo = accessInfo
- spec.DiskId = disk.GetExternalId()
- spec.VMId = guest.GetExternalId()
- spec.SizeMb = sizeMb
- body := jsonutils.NewDict()
- body.Add(jsonutils.Marshal(spec), "disk")
- url := fmt.Sprintf("/disks/agent/resize/%s", disk.Id)
- header := task.GetTaskRequestHeader()
- _, err = host.EsxiRequest(ctx, httputils.POST, url, header, body)
- return err
- }
- func (self *SESXiHostDriver) RequestSaveUploadImageOnHost(ctx context.Context, host *models.SHost, disk *models.SDisk, imageId string, task taskman.ITask, data jsonutils.JSONObject) error {
- imagePath, _ := data.GetString("backup")
- if len(imagePath) == 0 {
- return fmt.Errorf("missing parameter backup")
- }
- // agentId, _ := data.GetString("agent_id")
- // if len(agentId) == 0 {
- // return fmt.Errorf("missing parameter agent_id")
- // }
- // agent := models.HostManager.FetchHostById(agentId)
- // if agent == nil {
- // return fmt.Errorf("cannot find host with id %s", agentId)
- // }
- storage, _ := disk.GetStorage()
- type specStruct struct {
- ImagePath string
- ImageId string
- StorageId string
- StoragecacheId string
- Compress bool `json:",allowfalse"`
- }
- spec := specStruct{}
- spec.ImageId = imageId
- spec.ImagePath = imagePath
- spec.StorageId = storage.Id
- spec.StoragecacheId = storage.StoragecacheId
- spec.Compress = false
- body := jsonutils.NewDict()
- body.Add(jsonutils.Marshal(&spec), "disk")
- url := "/disks/agent/upload"
- header := task.GetTaskRequestHeader()
- _, err := host.EsxiRequest(ctx, httputils.POST, url, header, body)
- return err
- }
|