Exemplo n.º 1
0
Arquivo: attach.go Projeto: vmware/vic
func toggle(handle *exec.Handle, connected bool) (*exec.Handle, error) {
	// get the virtual device list
	devices := object.VirtualDeviceList(handle.Config.Hardware.Device)

	// select the virtual serial ports
	serials := devices.SelectByBackingInfo((*types.VirtualSerialPortURIBackingInfo)(nil))
	if len(serials) == 0 {
		return nil, fmt.Errorf("Unable to bind a device with desired backing")
	}
	if len(serials) > 1 {
		return nil, fmt.Errorf("Multiple matches found with desired backing")
	}
	serial := serials[0]

	ip, err := lookupVCHIP()
	if err != nil {
		return nil, err
	}

	log.Debugf("Found a device with desired backing: %#v", serial)

	c := serial.GetVirtualDevice().Connectable
	b := serial.GetVirtualDevice().Backing.(*types.VirtualSerialPortURIBackingInfo)

	serviceURI := fmt.Sprintf("tcp://%s:%d", ip, constants.SerialOverLANPort)

	if b.ServiceURI == serviceURI && c.Connected == connected {
		log.Debugf("Already in the desired state (connected: %t, serviceURI: %s)", connected, serviceURI)
		return handle, nil
	}

	// set the values
	log.Debugf("Setting Connected to %t", connected)
	c.Connected = connected
	if connected && handle.ExecConfig.Sessions[handle.ExecConfig.ID].Attach {
		log.Debugf("Setting the start connected state to %t", connected)
		c.StartConnected = handle.ExecConfig.Sessions[handle.ExecConfig.ID].Attach
	}

	log.Debugf("Setting ServiceURI to %s", serviceURI)
	b.ServiceURI = serviceURI

	config := &types.VirtualDeviceConfigSpec{
		Device:    serial,
		Operation: types.VirtualDeviceConfigSpecOperationEdit,
	}
	handle.Spec.DeviceChange = append(handle.Spec.DeviceChange, config)

	// iterate over Sessions and set their RunBlock property to connected
	// if attach happens before start then this property will be persist in the vmx
	// if attash happens after start then this propery will be thrown away by commit (one simply cannot change ExtraConfig if the vm is powered on)
	for _, session := range handle.ExecConfig.Sessions {
		session.RunBlock = connected
	}

	return handle, nil
}
Exemplo n.º 2
0
func NewVirtualMachine(spec *types.VirtualMachineConfigSpec) (*VirtualMachine, types.BaseMethodFault) {
	vm := &VirtualMachine{}

	if spec.Name == "" {
		return nil, &types.InvalidVmConfig{Property: "configSpec.name"}
	}

	if spec.Files == nil || spec.Files.VmPathName == "" {
		return nil, &types.InvalidVmConfig{Property: "configSpec.files.vmPathName"}
	}

	vm.Config = &types.VirtualMachineConfigInfo{}
	vm.Summary.Guest = &types.VirtualMachineGuestSummary{}
	vm.Summary.Storage = &types.VirtualMachineStorageSummary{}

	// Add the default devices
	devices, _ := object.VirtualDeviceList(esx.VirtualDevice).ConfigSpec(types.VirtualDeviceConfigSpecOperationAdd)

	if !strings.HasSuffix(spec.Files.VmPathName, ".vmx") {
		spec.Files.VmPathName = path.Join(spec.Files.VmPathName, spec.Name+".vmx")
	}

	dsPath := path.Dir(spec.Files.VmPathName)

	defaults := types.VirtualMachineConfigSpec{
		NumCPUs:           1,
		NumCoresPerSocket: 1,
		MemoryMB:          32,
		Uuid:              uuid.New().String(),
		Version:           "vmx-11",
		Files: &types.VirtualMachineFileInfo{
			SnapshotDirectory: dsPath,
			SuspendDirectory:  dsPath,
			LogDirectory:      dsPath,
		},
		DeviceChange: devices,
	}

	err := vm.configure(&defaults)
	if err != nil {
		return nil, err
	}

	vm.Runtime.PowerState = types.VirtualMachinePowerStatePoweredOff
	vm.Summary.Runtime = vm.Runtime

	err = vm.configure(spec)
	if err != nil {
		return nil, err
	}

	return vm, nil
}
Exemplo n.º 3
0
func (vm *VirtualMachine) configureDevices(spec *types.VirtualMachineConfigSpec) types.BaseMethodFault {
	devices := object.VirtualDeviceList(vm.Config.Hardware.Device)

	for i, change := range spec.DeviceChange {
		dspec := change.GetVirtualDeviceConfigSpec()
		device := dspec.Device.GetVirtualDevice()
		invalid := &types.InvalidDeviceSpec{DeviceIndex: int32(i)}

		switch dspec.Operation {
		case types.VirtualDeviceConfigSpecOperationAdd:
			if devices.FindByKey(device.Key) != nil {
				return invalid
			}
			devices = append(devices, device)
		}
	}

	vm.Config.Hardware.Device = []types.BaseVirtualDevice(devices)

	return nil
}
Exemplo n.º 4
0
func (d *Dispatcher) switchISO(filePath string) ([]types.BaseVirtualDeviceConfigSpec, error) {
	defer trace.End(trace.Begin(filePath))

	var devices object.VirtualDeviceList
	var err error

	log.Infof("Switching appliance iso to %s", filePath)
	devices, err = d.appliance.Device(d.ctx)
	if err != nil {
		log.Errorf("Failed to get vm devices for appliance: %s", err)
		return nil, err
	}
	// find the single cdrom
	cd, err := devices.FindCdrom("")
	if err != nil {
		log.Errorf("Failed to get CD rom device from appliance: %s", err)
		return nil, err
	}

	oldApplianceISO := cd.Backing.(*types.VirtualCdromIsoBackingInfo).FileName
	if oldApplianceISO == filePath {
		log.Debugf("Target file name %q is same to old one, no need to change.")
		return nil, nil
	}
	cd = devices.InsertIso(cd, filePath)
	changedDevices := object.VirtualDeviceList([]types.BaseVirtualDevice{cd})

	deviceChange, err := changedDevices.ConfigSpec(types.VirtualDeviceConfigSpecOperationEdit)
	if err != nil {
		log.Errorf("Failed to create config spec for appliance: %s", err)
		return nil, err
	}

	d.oldApplianceISO = oldApplianceISO
	return deviceChange, nil
}