Files
cloudpods/pkg/compute/guestdrivers/esxi.go
T
2019-03-20 14:37:37 +08:00

190 lines
5.7 KiB
Go

package guestdrivers
import (
"context"
"fmt"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/utils"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/compute/options"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/util/billing"
"yunion.io/x/onecloud/pkg/util/httputils"
)
type SESXiGuestDriver struct {
SManagedVirtualizedGuestDriver
}
func init() {
driver := SESXiGuestDriver{}
models.RegisterGuestDriver(&driver)
}
func (self *SESXiGuestDriver) GetHypervisor() string {
return models.HYPERVISOR_ESXI
}
func (self *SESXiGuestDriver) GetDefaultSysDiskBackend() string {
return models.STORAGE_LOCAL
}
func (self *SESXiGuestDriver) GetMinimalSysDiskSizeGb() int {
return options.Options.DefaultDiskSizeMB / 1024
}
// func (self *SESXiGuestDriver) RequestSyncConfigOnHost(ctx context.Context, guest *models.SGuest, host *models.SHost, task taskman.ITask) error {
// task.ScheduleRun(nil)
// return nil
// }
func (self *SESXiGuestDriver) GetMaxSecurityGroupCount() int {
//暂不支持绑定安全组
return 0
}
func (self *SESXiGuestDriver) GetDetachDiskStatus() ([]string, error) {
return []string{models.VM_READY, models.VM_RUNNING}, nil
}
func (self *SESXiGuestDriver) GetAttachDiskStatus() ([]string, error) {
return []string{models.VM_READY, models.VM_RUNNING}, nil
}
func (self *SESXiGuestDriver) GetChangeConfigStatus() ([]string, error) {
return []string{models.VM_READY}, nil
}
func (self *SESXiGuestDriver) CanKeepDetachDisk() bool {
return false
}
// func (self *SESXiGuestDriver) RequestDeleteDetachedDisk(ctx context.Context, disk *models.SDisk, task taskman.ITask, isPurge bool) error {
// err := disk.RealDelete(ctx, task.GetUserCred())
// if err != nil {
// return err
// }
// task.ScheduleRun(nil)
// return nil
// }
func (self *SESXiGuestDriver) RequestGuestHotAddIso(ctx context.Context, guest *models.SGuest, path string, task taskman.ITask) error {
task.ScheduleRun(nil)
return nil
}
func (self *SESXiGuestDriver) GetRebuildRootStatus() ([]string, error) {
return []string{models.VM_READY}, nil
}
func (self *SESXiGuestDriver) GetDeployStatus() ([]string, error) {
return []string{models.VM_READY}, nil
}
func (self *SESXiGuestDriver) ValidateResizeDisk(guest *models.SGuest, disk *models.SDisk, storage *models.SStorage) error {
if !utils.IsInStringArray(guest.Status, []string{models.VM_READY}) {
return fmt.Errorf("Cannot resize disk when guest in status %s", guest.Status)
}
if disk.DiskType == models.DISK_TYPE_SYS {
return fmt.Errorf("Cannot resize system disk")
}
/*if !utils.IsInStringArray(storage.StorageType, []string{models.STORAGE_PUBLIC_CLOUD, models.STORAGE_CLOUD_SSD, models.STORAGE_CLOUD_EFFICIENCY}) {
return fmt.Errorf("Cannot resize %s disk", storage.StorageType)
}*/
return nil
}
func (self *SESXiGuestDriver) RequestDetachDisk(ctx context.Context, guest *models.SGuest, task taskman.ITask) error {
return guest.StartSyncTask(ctx, task.GetUserCred(), false, task.GetTaskId())
}
func (self *SESXiGuestDriver) GetJsonDescAtHost(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, host *models.SHost) jsonutils.JSONObject {
return guest.GetJsonDescAtHypervisor(ctx, host)
}
func (self *SESXiGuestDriver) RequestDeployGuestOnHost(ctx context.Context, guest *models.SGuest, host *models.SHost, task taskman.ITask) error {
config, err := guest.GetDeployConfigOnHost(ctx, task.GetUserCred(), host, task.GetParams())
if err != nil {
log.Errorf("GetDeployConfigOnHost error: %v", err)
return err
}
log.Debugf("RequestDeployGuestOnHost: %s", config)
if !host.IsEsxiAgentReady() {
return fmt.Errorf("No ESXi agent host")
}
diskCat := guest.CategorizeDisks()
if diskCat.Root == nil {
return fmt.Errorf("no root disk???")
}
storage := diskCat.Root.GetStorage()
if storage == nil {
return fmt.Errorf("root disk has no storage???")
}
config.Add(jsonutils.NewString(host.AccessIp), "host_ip")
config.Add(jsonutils.NewString(guest.Id), "guest_id")
accessInfo, err := host.GetCloudaccount().GetVCenterAccessInfo(storage.ExternalId)
if err != nil {
return err
}
config.Add(jsonutils.Marshal(accessInfo), "datastore")
url := "/disks/agent/deploy"
body := jsonutils.NewDict()
body.Add(config, "disk")
header := task.GetTaskRequestHeader()
_, err = host.EsxiRequest(ctx, httputils.POST, url, header, body)
return err
}
func (self *SESXiGuestDriver) OnGuestDeployTaskDataReceived(ctx context.Context, guest *models.SGuest, task taskman.ITask, data jsonutils.JSONObject) error {
if data.Contains("host_ip") {
hostIp, _ := data.GetString("host_ip")
host, err := models.HostManager.GetHostByIp(hostIp)
if err != nil {
log.Errorf("fail to find host with IP %s: %s", hostIp, err)
return err
}
if host.Id != guest.HostId {
models.HostManager.ClearSchedDescCache(host.Id)
models.HostManager.ClearSchedDescCache(guest.HostId)
guest.OnScheduleToHost(ctx, task.GetUserCred(), host.Id)
}
}
return self.SManagedVirtualizedGuestDriver.OnGuestDeployTaskDataReceived(ctx, guest, task, data)
}
func (self *SESXiGuestDriver) AllowReconfigGuest() bool {
return true
}
func (self *SESXiGuestDriver) DoGuestCreateDisksTask(ctx context.Context, guest *models.SGuest, task taskman.ITask) error {
subtask, err := taskman.TaskManager.NewTask(ctx, "ESXiGuestCreateDiskTask", guest, task.GetUserCred(), task.GetParams(), task.GetTaskId(), "", nil)
if err != nil {
return err
}
subtask.ScheduleRun(nil)
return nil
}
func (self *SESXiGuestDriver) RequestRenewInstance(guest *models.SGuest, bc billing.SBillingCycle) (time.Time, error) {
return time.Time{}, nil
}
func (self *SESXiGuestDriver) IsSupportEip() bool {
return false
}