예제 #1
0
파일: vm.go 프로젝트: vmware/vic
func (vm *VirtualMachine) registerVM(ctx context.Context, path, name string,
	vapp, pool, host *types.ManagedObjectReference, vmfolder *object.Folder) (*object.Task, error) {
	log.Debugf("Register VM %s", name)

	if vapp == nil {
		var hostObject *object.HostSystem
		if host != nil {
			hostObject = object.NewHostSystem(vm.Vim25(), *host)
		}
		poolObject := object.NewResourcePool(vm.Vim25(), *pool)
		return vmfolder.RegisterVM(ctx, path, name, false, poolObject, hostObject)
	}

	req := types.RegisterChildVM_Task{
		This: vapp.Reference(),
		Path: path,
		Host: host,
	}

	if name != "" {
		req.Name = name
	}

	res, err := methods.RegisterChildVM_Task(ctx, vm.Vim25(), &req)
	if err != nil {
		return nil, err
	}

	return object.NewTask(vm.Vim25(), res.Returnval), nil
}
예제 #2
0
파일: vsphere_vm.go 프로젝트: dnaeon/gru
// newVm creates a new virtual machine.
func (vm *VirtualMachine) newVm(f *object.Folder, p *object.ResourcePool, ds *object.Datastore, h *object.HostSystem) error {
	if vm.Hardware == nil {
		return errors.New("Missing hardware configuration")
	}

	Logf("%s creating virtual machine\n", vm.ID())

	spec := types.VirtualMachineConfigSpec{
		Name:              vm.Name,
		Version:           vm.Hardware.Version,
		GuestId:           vm.GuestID,
		Annotation:        vm.Annotation,
		NumCPUs:           vm.Hardware.Cpu,
		NumCoresPerSocket: vm.Hardware.Cores,
		MemoryMB:          vm.Hardware.Memory,
		MaxMksConnections: vm.MaxMksConnections,
		Files: &types.VirtualMachineFileInfo{
			VmPathName: ds.Path(vm.Name),
		},
	}

	task, err := f.CreateVM(vm.ctx, spec, p, h)
	if err != nil {
		return err
	}

	return task.Wait(vm.ctx)
}
func deleteFolder(client *govmomi.Client, f *folder) error {
	dc, err := getDatacenter(client, f.datacenter)
	if err != nil {
		return err
	}
	var folder *object.Folder
	currentPath := f.path

	finder := find.NewFinder(client.Client, true)
	finder = finder.SetDatacenter(dc)
	si := object.NewSearchIndex(client.Client)

	folderRef, err := si.FindByInventoryPath(
		context.TODO(), fmt.Sprintf("%v/vm/%v", f.datacenter, f.path))

	if err != nil {
		return fmt.Errorf("[ERROR] Could not locate folder %s: %v", f.path, err)
	} else {
		folder = folderRef.(*object.Folder)
	}

	log.Printf("[INFO] Deleting empty sub-folders of existing path: %s", f.existingPath)
	for currentPath != f.existingPath {
		log.Printf("[INFO] Deleting folder: %s", currentPath)
		children, err := folder.Children(context.TODO())
		if err != nil {
			return err
		}

		if len(children) > 0 {
			return fmt.Errorf("Folder %s is non-empty and will not be deleted", currentPath)
		} else {
			log.Printf("[DEBUG] current folder: %#v", folder)
			currentPath = path.Dir(currentPath)
			if currentPath == "." {
				currentPath = ""
			}
			log.Printf("[INFO] parent path of %s is calculated as %s", f.path, currentPath)
			task, err := folder.Destroy(context.TODO())
			if err != nil {
				return err
			}
			err = task.Wait(context.TODO())
			if err != nil {
				return err
			}
			folderRef, err = si.FindByInventoryPath(
				context.TODO(), fmt.Sprintf("%v/vm/%v", f.datacenter, currentPath))

			if err != nil {
				return err
			} else if folderRef != nil {
				folder = folderRef.(*object.Folder)
			}
		}
	}
	return nil
}
예제 #4
0
파일: create.go 프로젝트: kristinn/govmomi
func (cmd *create) Run(f *flag.FlagSet) error {
	ctx := context.TODO()
	var parent *object.Folder

	if f.NArg() != 1 {
		return flag.ErrHelp
	}

	name := f.Arg(0)

	if cmd.parent == "" {
		dc, err := cmd.Datacenter()
		if err != nil {
			return err
		}

		folders, err := dc.Folders(ctx)
		if err != nil {
			return err
		}

		parent = folders.NetworkFolder
	} else {
		finder, err := cmd.Finder()
		if err != nil {
			return err
		}

		parent, err = finder.Folder(ctx, cmd.parent)
		if err != nil {
			return err
		}
	}

	cmd.configSpec.Name = name

	task, err := parent.CreateDVS(ctx, cmd.DVSCreateSpec)
	if err != nil {
		return err
	}

	logger := cmd.ProgressLogger(fmt.Sprintf("adding %s to folder %s... ", name, parent.InventoryPath))
	defer logger.Wait()

	_, err = task.WaitForResult(ctx, logger)
	return err
}
예제 #5
0
func (f *Finder) findSwitchesInFolder(ctx context.Context, folder *object.Folder) []object.DistributedVirtualSwitch {
	var switches []object.DistributedVirtualSwitch
	if children, err := folder.Children(ctx); err == nil {
		for _, j := range children {
			if j.Reference().Type == "VmwareDistributedVirtualSwitch" {
				switches = append(switches, *object.NewDistributedVirtualSwitch(f.client, j.Reference()))
			}
			if j.Reference().Type == "Folder" {
				switches2 := f.findSwitchesInFolder(ctx, object.NewFolder(f.client, j.Reference()))
				for _, k := range switches2 {
					switches = append(switches, k)
				}
			}
		}
	} else {
		fmt.Printf("Error getting switches! %v\n", err)
	}
	return switches
}
예제 #6
0
파일: create.go 프로젝트: kristinn/govmomi
func (cmd *create) Run(f *flag.FlagSet) error {
	ctx := context.TODO()
	var parent *object.Folder

	if f.NArg() != 1 {
		return flag.ErrHelp
	}

	if cmd.parent == "" {
		dc, err := cmd.Datacenter()
		if err != nil {
			return err
		}

		folders, err := dc.Folders(ctx)
		if err != nil {
			return err
		}

		parent = folders.HostFolder
	} else {
		finder, err := cmd.Finder()
		if err != nil {
			return err
		}

		parent, err = finder.Folder(ctx, cmd.parent)
		if err != nil {
			return err
		}
	}

	_, err := parent.CreateCluster(ctx, f.Arg(0), cmd.ClusterConfigSpecEx)
	if err != nil {
		return err
	}

	return nil
}
예제 #7
0
파일: vsphere_vm.go 프로젝트: dnaeon/gru
// cloneVm creates the virtual machine using a template.
func (vm *VirtualMachine) cloneVm(f *object.Folder, p *object.ResourcePool, ds *object.Datastore, h *object.HostSystem) error {
	Logf("%s cloning virtual machine from %s\n", vm.ID(), vm.TemplateConfig.Use)

	obj, err := vm.finder.VirtualMachine(vm.ctx, vm.TemplateConfig.Use)
	if err != nil {
		return err
	}

	folderRef := f.Reference()
	datastoreRef := ds.Reference()
	poolRef := p.Reference()

	var hostRef *types.ManagedObjectReference
	if h != nil {
		ref := h.Reference()
		hostRef = &ref
	}

	spec := types.VirtualMachineCloneSpec{
		Location: types.VirtualMachineRelocateSpec{
			Folder:    &folderRef,
			Datastore: &datastoreRef,
			Pool:      &poolRef,
			Host:      hostRef,
		},
		Template: vm.TemplateConfig.MarkAsTemplate,
		PowerOn:  vm.TemplateConfig.PowerOn,
	}

	task, err := obj.Clone(vm.ctx, f, vm.Name, spec)
	if err != nil {
		return err
	}

	return task.Wait(vm.ctx)
}
예제 #8
0
파일: add.go 프로젝트: vmware/vic
func (cmd *add) Add(ctx context.Context, parent *object.Folder) error {
	spec := cmd.Spec(parent.Client())

	req := types.AddStandaloneHost_Task{
		This:         parent.Reference(),
		Spec:         spec,
		AddConnected: cmd.connect,
	}

	res, err := methods.AddStandaloneHost_Task(ctx, parent.Client(), &req)
	if err != nil {
		return err
	}

	logger := cmd.ProgressLogger(fmt.Sprintf("adding %s to folder %s... ", spec.HostName, parent.InventoryPath))
	defer logger.Wait()

	task := object.NewTask(parent.Client(), res.Returnval)
	_, err = task.WaitForResult(ctx, logger)
	return err
}
예제 #9
0
파일: folder_test.go 프로젝트: jak-atx/vic
func addStandaloneHost(folder *object.Folder, spec types.HostConnectSpec) (*object.Task, error) {
	// TODO: add govmomi wrapper
	req := types.AddStandaloneHost_Task{
		This:         folder.Reference(),
		Spec:         spec,
		AddConnected: true,
	}

	res, err := methods.AddStandaloneHost_Task(context.TODO(), folder.Client(), &req)
	if err != nil {
		return nil, err
	}

	task := object.NewTask(folder.Client(), res.Returnval)
	return task, nil
}
예제 #10
0
파일: add.go 프로젝트: MerlinDMC/machine
func (cmd *add) Run(f *flag.FlagSet) error {
	var ctx = context.Background()
	var parent *object.Folder

	client, err := cmd.Client()
	if err != nil {
		return err
	}

	if cmd.parent == "" {
		dc, err := cmd.Datacenter()
		if err != nil {
			return err
		}

		folders, err := dc.Folders(ctx)
		if err != nil {
			return err
		}

		parent = folders.HostFolder
	} else {
		finder, err := cmd.Finder()
		if err != nil {
			return err
		}

		mo, err := finder.ManagedObjectList(ctx, cmd.parent)
		if err != nil {
			return err
		}

		if len(mo) == 0 {
			return errors.New("parent does not resolve to object")
		}

		if len(mo) > 1 {
			return errors.New("parent resolves to more than one object")
		}

		ref := mo[0].Object.Reference()
		if ref.Type != "Folder" {
			return errors.New("parent does not resolve to folder")
		}

		parent = object.NewFolder(client, ref)
	}

	req := types.AddStandaloneHost_Task{
		This: parent.Reference(),
		Spec: types.HostConnectSpec{
			HostName:      cmd.host,
			UserName:      cmd.username,
			Password:      cmd.password,
			SslThumbprint: cmd.fingerprint,
		},
		AddConnected: cmd.connect,
	}

	res, err := methods.AddStandaloneHost_Task(ctx, client, &req)
	if err != nil {
		return err
	}

	task := object.NewTask(client, res.Returnval)
	_, err = task.WaitForResult(ctx, nil)
	if err != nil {
		f, ok := err.(types.HasFault)
		if !ok {
			return err
		}

		switch fault := f.Fault().(type) {
		case *types.SSLVerifyFault:
			// Add fingerprint to error message
			return fmt.Errorf("%s Fingerprint is %s.", err.Error(), fault.Thumbprint)
		default:
			return err
		}
	}

	return nil
}