Merge pull request #8265 from rainzm/esxi/template_hack

sync and use of ESXi template
This commit is contained in:
yunion-ci-robot
2020-10-15 13:13:30 +08:00
committed by GitHub
11 changed files with 73 additions and 18 deletions
+6
View File
@@ -187,6 +187,12 @@ type NetworkCreateInput struct {
// 是否加入自动分配地址池
IsAutoAlloc *bool `json:"is_auto_alloc"`
// VlanId
VlanId *int `json:"vlan_id"`
// deprecated
Vlan *int `json:"vlan" yunion-deprecated-by:"vlan_id"`
}
type NetworkDetails struct {
+4
View File
@@ -87,6 +87,10 @@ func alterNameValidator(model IModel, name string) error {
return nil
}
func GenerateAlertName(model IModel, hint string) (string, error) {
return GenerateName2(model.GetModelManager(), nil, hint, model, 1)
}
func GenerateName(manager IModelManager, ownerId mcclient.IIdentityProvider, hint string) (string, error) {
return GenerateName2(manager, ownerId, hint, nil, 1)
}
+6 -1
View File
@@ -189,8 +189,13 @@ func (self *SESXiGuestDriver) GetJsonDescAtHost(ctx context.Context, userCred mc
var hostIp string
storageCacheHost, err := storageCaches[0].GetHost()
if err != nil {
log.Errorf("fail to GetHost of storageCache %s", storageCaches[0].Id)
log.Errorf("unable to GetHost of storageCache %s: %v", storageCaches[0].Id, err)
hostIp = storageCaches[0].ExternalId
} else if storageCacheHost == nil {
log.Errorf("unable to GetHost of storageCache %s: result is nil", storageCaches[0].Id)
hostIp = storageCaches[0].ExternalId
} else {
hostIp = storageCacheHost.AccessIp
}
hostIp = storageCacheHost.AccessIp
imageInfo := SEsxiImageInfo{
+5 -1
View File
@@ -472,7 +472,11 @@ func (self *SCachedimage) canDeleteLastCache() bool {
func (self *SCachedimage) syncWithCloudImage(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, image cloudprovider.ICloudImage, managerId string) error {
diff, err := db.UpdateWithLock(ctx, self, func() error {
self.Name = image.GetName()
newName, err := db.GenerateAlertName(self, image.GetName())
if err != nil {
return errors.Wrap(err, "GenerateAlertName")
}
self.Name = newName
self.Size = image.GetSizeByte()
self.ExternalId = image.GetGlobalId()
self.ImageType = image.GetImageType()
+14
View File
@@ -1333,6 +1333,20 @@ func (manager *SNetworkManager) ValidateCreateData(ctx context.Context, userCred
return input, httperrors.NewInputParameterError("Invalid server_type: %s", input.ServerType)
}
{
defaultVlanId := 1
if input.VlanId == nil {
input.VlanId = &defaultVlanId
} else if *input.VlanId < 1 {
input.VlanId = &defaultVlanId
}
if *input.VlanId > 4095 {
return input, httperrors.NewInputParameterError("valid vlan id")
}
}
{
if len(input.IfnameHint) == 0 {
input.IfnameHint = input.Name
+2 -3
View File
@@ -310,10 +310,9 @@ func (as *SAgentStorage) AgentDeployGuest(ctx context.Context, data interface{})
}
array := jsonutils.NewArray()
diskArray, _ := desc.GetArray("disks")
for idx, d := range disks {
for _, d := range disks {
disk := d.(*esxi.SVirtualDisk)
diskId, _ := diskArray[idx].GetString("disk_id")
diskId := disk.GetId()
diskDict := jsonutils.NewDict()
diskDict.Add(jsonutils.NewString(diskId), "disk_id")
diskDict.Add(jsonutils.NewString(disk.GetGlobalId()), "uuid")
+1 -1
View File
@@ -287,7 +287,7 @@ func (dc *SDatacenter) fetchFakeTemplateVMs(movms []mo.VirtualMachine, regex str
objs := make([]types.ManagedObjectReference, 0)
for i := range movms {
name := movms[i].Name
if tNameRegex.MatchString(name) {
if tNameRegex != nil && tNameRegex.MatchString(name) {
objs = append(objs, movms[i].Reference())
}
}
+9 -9
View File
@@ -1008,15 +1008,15 @@ func (host *SHost) CloneVM(ctx context.Context, from *SVirtualMachine, ds *SData
log.Infof("resize system disk: %dGB => %dGB", from.vdisks[0].GetDiskSizeMB()/1024, vdisk.CapacityInKB/1024/1024)
}
// remove extra disk
for i := 1; i < len(from.vdisks); i++ {
dev := from.vdisks[i].dev
spec := &types.VirtualDeviceConfigSpec{}
spec.Operation = types.VirtualDeviceConfigSpecOperationRemove
spec.Device = dev
spec.FileOperation = types.VirtualDeviceConfigSpecFileOperationDestroy
deviceChange = append(deviceChange, spec)
log.Debugf("remove disk, index: %d", i)
}
// for i := 1; i < len(from.vdisks); i++ {
// dev := from.vdisks[i].dev
// spec := &types.VirtualDeviceConfigSpec{}
// spec.Operation = types.VirtualDeviceConfigSpecOperationRemove
// spec.Device = dev
// spec.FileOperation = types.VirtualDeviceConfigSpecFileOperationDestroy
// deviceChange = append(deviceChange, spec)
// log.Debugf("remove disk, index: %d", i)
// }
}
dc, err := host.GetDatacenter()
+5 -2
View File
@@ -351,8 +351,8 @@ func (cli *SESXiClient) scanAllMObjects(props []string, dst interface{}) error {
func (cli *SESXiClient) SearchTemplateVM(id string) (*SVirtualMachine, error) {
filter := property.Filter{}
filter["config.template"] = true
filter["summary.config.uuid"] = id
uuid := toTemplateUuid(id)
filter["summary.config.uuid"] = uuid
var movms []mo.VirtualMachine
err := cli.scanMObjectsWithFilter(cli.client.ServiceContent.RootFolder, VIRTUAL_MACHINE_PROPS, &movms, filter)
if err != nil {
@@ -362,6 +362,9 @@ func (cli *SESXiClient) SearchTemplateVM(id string) (*SVirtualMachine, error) {
return nil, errors.ErrNotFound
}
vm := NewVirtualMachine(cli, &movms[0], nil)
if !vm.IsTemplate() {
return nil, errors.ErrNotFound
}
dc, err := vm.fetchDatacenter()
if err != nil {
return nil, errors.Wrap(err, "fetchDatacenter")
+3
View File
@@ -100,6 +100,9 @@ func (self *SDatastoreImageCache) getTempalteVMs() ([]*SVirtualMachine, error) {
}
func (self *SDatastoreImageCache) getFakeTempateVMs() ([]*SVirtualMachine, error) {
if tempalteNameRegex == nil {
return nil, nil
}
return self.datastore.FetchFakeTempateVMs("")
}
+18 -1
View File
@@ -16,6 +16,8 @@ package esxi
import (
"context"
"fmt"
"strings"
"time"
"yunion.io/x/jsonutils"
@@ -42,8 +44,23 @@ func NewVMTemplate(vm *SVirtualMachine, cache *SDatastoreImageCache) *SVMTemplat
}
}
const splitStr = "/"
func toTemplateUuid(templateId string) string {
ids := strings.Split(templateId, splitStr)
if len(ids) == 1 {
return ids[0]
}
return ids[1]
}
func toTemplateId(providerId string, templateUuid string) string {
return fmt.Sprintf("%s%s%s", providerId, splitStr, templateUuid)
}
func (t *SVMTemplate) GetId() string {
return t.uuid
providerId := t.vm.manager.cpcfg.Id
return toTemplateId(providerId, t.uuid)
}
func (t *SVMTemplate) UEFI() bool {