public FakeVirtualMachine addVM(String name, boolean isRunning, VirtualMachineCloneSpec spec){ final FakeVirtualMachine vm = new FakeVirtualMachine(name, name.contains("template"), isRunning); putVM(name, vm); if (spec != null && spec.getLocation()!= null && VirtualMachineRelocateDiskMoveOptions.createNewChildDiskBacking.name().equals(spec.getLocation().getDiskMoveType())){ //((FakeVirtualMachine)vm).set } if (spec != null && spec.getConfig() != null) { final OptionValue[] extraConfig = spec.getConfig().getExtraConfig(); if (extraConfig != null) { for (OptionValue optionValue : extraConfig) { vm.addCustomParam(optionValue.getKey(), String.valueOf(optionValue.getValue())); } } } return vm; }
public Pair<String, Integer> getVncPort(String hostNetworkName) throws Exception { HostMO hostMo = getRunningHost(); VmwareHypervisorHostNetworkSummary summary = hostMo.getHyperHostNetworkSummary(hostNetworkName); VirtualMachineConfigInfo configInfo = getConfigInfo(); List<OptionValue> values = configInfo.getExtraConfig(); if (values != null) { for (OptionValue option : values) { if (option.getKey().equals("RemoteDisplay.vnc.port")) { String value = (String)option.getValue(); if (value != null) { return new Pair<String, Integer>(summary.getHostIp(), Integer.parseInt(value)); } } } } return new Pair<String, Integer>(summary.getHostIp(), 0); }
/** * Stores a timestamp into a VM's extraConfig on provisioning. * Currently used for resource cleanup only. */ private void recordTimestamp(List<OptionValue> extraConfig) { if (extraConfig == null) { return; } OptionValue ov = new OptionValue(); ov.setKey(EXTRA_CONFIG_CREATED); ov.setValue(Long.toString(System.currentTimeMillis())); extraConfig.add(ov); }
private void recordSnapshotLimit(List<OptionValue> extraConfig, String snapshotLimitValue) { if (snapshotLimitValue != null && !snapshotLimitValue.isEmpty()) { if (extraConfig == null) { extraConfig = new ArrayList<>(); } extraConfig.add(populateSnapshotLimitValue(snapshotLimitValue)); } }
public static String getOVFParamValue(VirtualMachineConfigSpec config) { String paramVal = ""; List<OptionValue> options = config.getExtraConfig(); for (OptionValue option : options) { if (OVA_OPTION_KEY_BOOTDISK.equalsIgnoreCase(option.getKey())) { paramVal = (String)option.getValue(); break; } } return paramVal; }
public boolean setVncConfigInfo(boolean enableVnc, String vncPassword, int vncPort, String keyboard) throws Exception { VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); OptionValue[] vncOptions = VmwareHelper.composeVncOptions(null, enableVnc, vncPassword, vncPort, keyboard); vmConfigSpec.getExtraConfig().addAll(Arrays.asList(vncOptions)); ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, vmConfigSpec); boolean result = _context.getVimClient().waitForTask(morTask); if (result) { _context.waitForTaskProgressDone(morTask); return true; } else { s_logger.error("VMware reconfigVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask)); } return false; }
public HashMap<String, Integer> getVmVncPortsOnCluster() throws Exception { ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] {"name", "config.extraConfig[\"RemoteDisplay.vnc.port\"]"}); HashMap<String, Integer> portInfo = new HashMap<String, Integer>(); if (ocs != null && ocs.length > 0) { for (ObjectContent oc : ocs) { List<DynamicProperty> objProps = oc.getPropSet(); if (objProps != null) { String name = null; String value = null; for (DynamicProperty objProp : objProps) { if (objProp.getName().equals("name")) { name = (String)objProp.getVal(); } else { OptionValue optValue = (OptionValue)objProp.getVal(); value = (String)optValue.getValue(); } } if (name != null && value != null) { portInfo.put(name, Integer.parseInt(value)); } } } } return portInfo; }
public static OptionValue[] composeVncOptions(OptionValue[] optionsToMerge, boolean enableVnc, String vncPassword, int vncPort, String keyboardLayout) { int numOptions = 3; boolean needKeyboardSetup = false; if (keyboardLayout != null && !keyboardLayout.isEmpty()) { numOptions++; needKeyboardSetup = true; } if (optionsToMerge != null) numOptions += optionsToMerge.length; OptionValue[] options = new OptionValue[numOptions]; int i = 0; if (optionsToMerge != null) { for (int j = 0; j < optionsToMerge.length; j++) options[i++] = optionsToMerge[j]; } options[i] = new OptionValue(); options[i].setKey("RemoteDisplay.vnc.enabled"); options[i++].setValue(enableVnc ? "true" : "false"); options[i] = new OptionValue(); options[i].setKey("RemoteDisplay.vnc.password"); options[i++].setValue(vncPassword); options[i] = new OptionValue(); options[i].setKey("RemoteDisplay.vnc.port"); options[i++].setValue("" + vncPort); if (needKeyboardSetup) { options[i] = new OptionValue(); options[i].setKey("RemoteDisplay.vnc.keymap"); options[i++].setValue(keyboardLayout); } return options; }
private static void configCustomExtraOption(List<OptionValue> extraOptions, VirtualMachineTO vmSpec) { // we no longer to validation anymore for (Map.Entry<String, String> entry : vmSpec.getDetails().entrySet()) { OptionValue newVal = new OptionValue(); newVal.setKey(entry.getKey()); newVal.setValue(entry.getValue()); extraOptions.add(newVal); } }
private OptionValue populateSnapshotLimitValue(String snapshotLimitValue) { OptionValue ov = new OptionValue(); ov.setKey(SNAPSHOT_LIMIT_CONFIG_STRING); ov.setValue(snapshotLimitValue); return ov; }
public HashMap<String, Integer> getVmVncPortsOnHost() throws Exception { int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME); if (key == 0) { s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!"); } ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] {"name", "config.extraConfig[\"RemoteDisplay.vnc.port\"]", "value[" + key + "]"}); HashMap<String, Integer> portInfo = new HashMap<String, Integer>(); if (ocs != null && ocs.length > 0) { for (ObjectContent oc : ocs) { List<DynamicProperty> objProps = oc.getPropSet(); if (objProps != null) { String vmName = null; String value = null; String vmInternalCSName = null; for (DynamicProperty objProp : objProps) { if (objProp.getName().equals("name")) { vmName = (String)objProp.getVal(); } else if (objProp.getName().startsWith("value[")) { if (objProp.getVal() != null) vmInternalCSName = ((CustomFieldStringValue)objProp.getVal()).getValue(); } else { OptionValue optValue = (OptionValue)objProp.getVal(); value = (String)optValue.getValue(); } } if (vmInternalCSName != null && isUserVMInternalCSName(vmInternalCSName)) vmName = vmInternalCSName; if (vmName != null && value != null) { portInfo.put(vmName, Integer.parseInt(value)); } } } } return portInfo; }
protected OptionValue[] configureVnc(OptionValue[] optionsToMerge, VmwareHypervisorHost hyperHost, String vmName, String vncPassword, String keyboardLayout) throws Exception { VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName); VmwareManager mgr = hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); if (!mgr.beginExclusiveOperation(600)) throw new Exception("Unable to begin exclusive operation, lock time out"); try { int maxVncPorts = 64; int vncPort = 0; Random random = new Random(); HostMO vmOwnerHost = vmMo.getRunningHost(); ManagedObjectReference morParent = vmOwnerHost.getParentMor(); HashMap<String, Integer> portInfo; if (morParent.getType().equalsIgnoreCase("ClusterComputeResource")) { ClusterMO clusterMo = new ClusterMO(vmOwnerHost.getContext(), morParent); portInfo = clusterMo.getVmVncPortsOnCluster(); } else { portInfo = vmOwnerHost.getVmVncPortsOnHost(); } // allocate first at 5900 - 5964 range Collection<Integer> existingPorts = portInfo.values(); int val = random.nextInt(maxVncPorts); int startVal = val; do { if (!existingPorts.contains(5900 + val)) { vncPort = 5900 + val; break; } val = (++val) % maxVncPorts; } while (val != startVal); if (vncPort == 0) { s_logger.info("we've run out of range for ports between 5900-5964 for the cluster, we will try port range at 59000-60000"); Pair<Integer, Integer> additionalRange = mgr.getAddiionalVncPortRange(); maxVncPorts = additionalRange.second(); val = random.nextInt(maxVncPorts); startVal = val; do { if (!existingPorts.contains(additionalRange.first() + val)) { vncPort = additionalRange.first() + val; break; } val = (++val) % maxVncPorts; } while (val != startVal); } if (vncPort == 0) { throw new Exception("Unable to find an available VNC port on host"); } if (s_logger.isInfoEnabled()) { s_logger.info("Configure VNC port for VM " + vmName + ", port: " + vncPort + ", host: " + vmOwnerHost.getHyperHostName()); } return VmwareHelper.composeVncOptions(optionsToMerge, true, vncPassword, vncPort, keyboardLayout); } finally { try { mgr.endExclusiveOperation(); } catch (Throwable e) { assert (false); s_logger.error("Unexpected exception ", e); } } }
List<OptionValue> getSetting();
List<OptionValue> queryOptions(String name) throws InvalidName, RuntimeFault, RemoteException;
void updateOptions(List<OptionValue> changedValue) throws InvalidName, RuntimeFault, RemoteException;