com.vmware.vim25.mo.InventoryNavigator Java Examples

The following examples show how to use com.vmware.vim25.mo.InventoryNavigator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VIJavaUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
private static boolean migrateVM(String targetVMName, String newHostName,
    boolean tryAnotherVM, boolean tryAnotherHost) throws Exception {
  ServiceInstance si = new ServiceInstance(new URL(url), username, password,
      true);

  try {
    Folder rootFolder = si.getRootFolder();
    HostSystem newHost = (HostSystem)new InventoryNavigator(rootFolder)
        .searchManagedEntity("HostSystem", newHostName);
    
    return migrateVM(si, rootFolder, newHost, targetVMName, newHostName,
        tryAnotherVM, tryAnotherHost);
  } finally {
    si.getServerConnection().logout();
  }
}
 
Example #2
Source File: VIJavaUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
private static synchronized boolean doMigrateVM(String targetVMName,
    String newHostName) throws Exception {
  ServiceInstance si = new ServiceInstance(new URL(url), username, password,
      true);
  try {
    Folder rootFolder = si.getRootFolder();
    InventoryNavigator in = new InventoryNavigator(rootFolder);
    HostSystem newHost = (HostSystem)in.searchManagedEntity("HostSystem",
        newHostName);
    if (newHost == null) {
      throw new TestException("Could not find host " + newHostName + " as a target host for vMotion.");
    }

    return migrateVM(si, rootFolder, newHost, targetVMName, newHostName);

  } finally {
    si.getServerConnection().logout();
  }
}
 
Example #3
Source File: VIJavaUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public static void HydraTask_migrateNetDownVM() throws Exception {
  SharedMap sMap = VMotionBB.getBB().getSharedMap();

  Boolean bool = (Boolean)sMap.get("connectionDropped");
  if (bool == null || !bool) {
    return;
  }

  initializeParams();
  if (!validateParams()) {
    return;
  }

  ServiceInstance si = new ServiceInstance(new URL(url), username, password,
      true);

  try {
    Folder rootFolder = si.getRootFolder();
    HostSystem newHost = (HostSystem)new InventoryNavigator(rootFolder)
        .searchManagedEntity("HostSystem", hostNames[0]);
    migrateVM(si, rootFolder, newHost, vmNames[0], hostNames[0], false, false);
  } finally {
    si.getServerConnection().logout();
  }
}
 
Example #4
Source File: VIJavaUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
private static boolean migrateVM(String targetVMName, String newHostName,
    boolean tryAnotherVM, boolean tryAnotherHost) throws Exception {
  ServiceInstance si = new ServiceInstance(new URL(url), username, password,
      true);

  try {
    Folder rootFolder = si.getRootFolder();
    HostSystem newHost = (HostSystem)new InventoryNavigator(rootFolder)
        .searchManagedEntity("HostSystem", newHostName);
    
    return migrateVM(si, rootFolder, newHost, targetVMName, newHostName,
        tryAnotherVM, tryAnotherHost);
  } finally {
    si.getServerConnection().logout();
  }
}
 
Example #5
Source File: VIJavaUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
private static synchronized boolean doMigrateVM(String targetVMName,
    String newHostName) throws Exception {
  ServiceInstance si = new ServiceInstance(new URL(url), username, password,
      true);
  try {
    Folder rootFolder = si.getRootFolder();
    InventoryNavigator in = new InventoryNavigator(rootFolder);
    HostSystem newHost = (HostSystem)in.searchManagedEntity("HostSystem",
        newHostName);
    if (newHost == null) {
      throw new TestException("Could not find host " + newHostName + " as a target host for vMotion.");
    }

    return migrateVM(si, rootFolder, newHost, targetVMName, newHostName);

  } finally {
    si.getServerConnection().logout();
  }
}
 
Example #6
Source File: VIJavaUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public static void HydraTask_migrateNetDownVM() throws Exception {
  SharedMap sMap = VMotionBB.getBB().getSharedMap();

  Boolean bool = (Boolean)sMap.get("connectionDropped");
  if (bool == null || !bool) {
    return;
  }

  initializeParams();
  if (!validateParams()) {
    return;
  }

  ServiceInstance si = new ServiceInstance(new URL(url), username, password,
      true);

  try {
    Folder rootFolder = si.getRootFolder();
    HostSystem newHost = (HostSystem)new InventoryNavigator(rootFolder)
        .searchManagedEntity("HostSystem", hostNames[0]);
    migrateVM(si, rootFolder, newHost, vmNames[0], hostNames[0], false, false);
  } finally {
    si.getServerConnection().logout();
  }
}
 
Example #7
Source File: VIJavaUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private static boolean migrateVM(ServiceInstance si, Folder rootFolder,
    HostSystem newHost, String targetVMName, String newHostName,
    boolean tryAnotherVM, boolean tryAnotherHost) throws Exception {

  if (!validateVMNotOnHost(si, rootFolder, newHost, targetVMName, newHostName)) {
    if (!(tryAnotherVM || tryAnotherHost)) {
      Log.getLogWriter().info(
          "vMotion not possible with the available host and [vm]: "
              + newHostName + " [" + targetVMName + "]");
      return false;
    } else {
      if (tryAnotherVM) {
        targetVMName = findVMNotOnHost(newHost);
        if (targetVMName != null) {
          return migrateVM(si, rootFolder, newHost, targetVMName, newHostName);
        }
      }
      if (tryAnotherHost) {
        newHostName = selectHostName(hostNames, newHostName);
        newHost = (HostSystem)new InventoryNavigator(rootFolder)
            .searchManagedEntity("HostSystem", newHostName);
        return migrateVM(si, rootFolder, newHost, targetVMName, newHostName);
      }
      Log.getLogWriter().info(
          "Could not find valid host[vm] pair for vMotion.");
      return false;
    }
  }

  return migrateVM(si, rootFolder, newHost, targetVMName, newHostName);
}
 
Example #8
Source File: VmwareClient.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
/**
 * TODO: メソッドコメントを記述
 *
 * @param rootEntity
 * @return
 */
public ManagedEntity[] searchAll(ManagedEntity rootEntity) {
    InventoryNavigator navigator = new InventoryNavigator(rootEntity);
    ManagedEntity[] entities;
    try {
        entities = navigator.searchManagedEntities(true);
    } catch (RemoteException e) {
        throw new RuntimeException(e);
    }
    return entities;
}
 
Example #9
Source File: VmwareClient.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
/**
 * TODO: メソッドコメントを記述
 *
 * @param <T>
 * @param rootEntity
 * @param type
 * @return
 */
public <T extends ManagedEntity> ManagedEntity[] searchByType(ManagedEntity rootEntity, Class<T> type) {
    InventoryNavigator navigator = new InventoryNavigator(rootEntity);
    ManagedEntity[] entities;
    try {
        entities = navigator.searchManagedEntities(type.getSimpleName());
    } catch (RemoteException e) {
        throw new RuntimeException(e);
    }
    return entities;
}
 
Example #10
Source File: VIJavaUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private static boolean migrateVM(ServiceInstance si, Folder rootFolder,
    HostSystem newHost, String targetVMName, String newHostName,
    boolean tryAnotherVM, boolean tryAnotherHost) throws Exception {

  if (!validateVMNotOnHost(si, rootFolder, newHost, targetVMName, newHostName)) {
    if (!(tryAnotherVM || tryAnotherHost)) {
      Log.getLogWriter().info(
          "vMotion not possible with the available host and [vm]: "
              + newHostName + " [" + targetVMName + "]");
      return false;
    } else {
      if (tryAnotherVM) {
        targetVMName = findVMNotOnHost(newHost);
        if (targetVMName != null) {
          return migrateVM(si, rootFolder, newHost, targetVMName, newHostName);
        }
      }
      if (tryAnotherHost) {
        newHostName = selectHostName(hostNames, newHostName);
        newHost = (HostSystem)new InventoryNavigator(rootFolder)
            .searchManagedEntity("HostSystem", newHostName);
        return migrateVM(si, rootFolder, newHost, targetVMName, newHostName);
      }
      Log.getLogWriter().info(
          "Could not find valid host[vm] pair for vMotion.");
      return false;
    }
  }

  return migrateVM(si, rootFolder, newHost, targetVMName, newHostName);
}
 
Example #11
Source File: VmwareClient.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
/**
 * TODO: メソッドコメントを記述
 *
 * @param rootEntity
 * @param name
 * @return
 */
public ManagedEntity searchByName(ManagedEntity rootEntity, String name) {
    InventoryNavigator navigator = new InventoryNavigator(rootEntity);
    ManagedEntity entity;
    try {
        entity = navigator.searchManagedEntity("ManagedEntity", name);
    } catch (RemoteException e) {
        throw new RuntimeException(e);
    }
    return entity;
}
 
Example #12
Source File: VmwareClient.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
/**
 * TODO: メソッドコメントを記述
 *
 * @param <T>
 * @param rootEntity
 * @param type
 * @param name
 * @return
 */
public <T extends ManagedEntity> T search(ManagedEntity rootEntity, Class<T> type, String name) {
    InventoryNavigator navigator = new InventoryNavigator(rootEntity);
    ManagedEntity entity;
    try {
        entity = navigator.searchManagedEntity(type.getSimpleName(), name);
    } catch (RemoteException e) {
        throw new RuntimeException(e);
    }
    return type.cast(entity);
}
 
Example #13
Source File: VmwareIaasHandler.java    From roboconf-platform with Apache License 2.0 4 votes vote down vote up
@Override
public String createMachine( TargetHandlerParameters parameters ) throws TargetException {

	this.logger.fine( "Creating a new VM @ VMware." );

	// For IaaS, we only expect root instance names to be passed
	if( InstanceHelpers.countInstances( parameters.getScopedInstancePath()) > 1 )
		throw new TargetException( "Only root instances can be passed in arguments." );

	String rootInstanceName = InstanceHelpers.findRootInstancePath( parameters.getScopedInstancePath());

	// Deal with the creation
	try {
		System.setProperty("org.xml.sax.driver","org.apache.xerces.parsers.SAXParser");
		Map<String,String> targetProperties = parameters.getTargetProperties();
		final String machineImageId = targetProperties.get( TEMPLATE );
		final ServiceInstance vmwareServiceInstance = getServiceInstance( targetProperties );

		final ComputeResource vmwareComputeResource = (ComputeResource)(
				new InventoryNavigator( vmwareServiceInstance.getRootFolder())
				.searchManagedEntity("ComputeResource", targetProperties.get( CLUSTER )));

		// Generate the user data first, so that nothing has been done on the IaaS if it fails
		String userData = UserDataHelpers.writeUserDataAsString(
				parameters.getMessagingProperties(),
				parameters.getDomain(),
				parameters.getApplicationName(),
				rootInstanceName );

		VirtualMachine vm = getVirtualMachine( vmwareServiceInstance, machineImageId );
		String vmwareDataCenter = targetProperties.get( DATA_CENTER );
		Folder vmFolder =
				((Datacenter)(new InventoryNavigator( vmwareServiceInstance.getRootFolder())
				.searchManagedEntity("Datacenter", vmwareDataCenter)))
				.getVmFolder();

		this.logger.fine("machineImageId=" + machineImageId);
		if (vm == null || vmFolder == null)
			throw new TargetException("VirtualMachine (= " + vm + " ) or Datacenter path (= " + vmFolder + " ) is NOT correct. Please, double check.");

		VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
		cloneSpec.setLocation(new VirtualMachineRelocateSpec());
		cloneSpec.setPowerOn(false);
		cloneSpec.setTemplate(true);

		VirtualMachineConfigSpec vmSpec = new VirtualMachineConfigSpec();
		vmSpec.setAnnotation( userData );
		cloneSpec.setConfig(vmSpec);

		Task task = vm.cloneVM_Task( vmFolder, rootInstanceName, cloneSpec );
		this.logger.fine("Cloning the template: "+ machineImageId +" ...");
		String status = task.waitForTask();
		if (!status.equals(Task.SUCCESS))
			throw new TargetException("Failure: Virtual Machine cannot be cloned." );

		VirtualMachine vm2 = getVirtualMachine( vmwareServiceInstance, rootInstanceName );
		this.logger.fine("Transforming the clone template to Virtual machine ...");
		vm2.markAsVirtualMachine( vmwareComputeResource.getResourcePool(), null);

		DynamicProperty dprop = new DynamicProperty();
		dprop.setName("guestinfo.userdata");
		dprop.setVal(userData);
		vm2.getGuest().setDynamicProperty(new DynamicProperty[]{dprop});

		task = vm2.powerOnVM_Task(null);
		this.logger.fine("Starting the virtual machine: "+ rootInstanceName +" ...");
		status = task.waitForTask();
		if( ! status.equals( Task.SUCCESS ))
			throw new TargetException("Failure: Virtual Machine cannot be started." );

		return vm2.getName();

	} catch( Exception e ) {
		throw new TargetException( e );
	}
}
 
Example #14
Source File: VMotionTrigger.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void run() {
  int vmToUse = -1;
  int attempts = 0;
  try {
    synchronized (vmList) {
      while (attempts < vmList.size()) {
        lastVMIndex = (lastVMIndex == (vmList.size() - 1)) ? 0
            : ++lastVMIndex;
        vmToUse = lastVMIndex;
        if (!vmsInVMotion[vmToUse]) {
          vmsInVMotion[vmToUse] = Boolean.TRUE;
          lastHostIndex[vmToUse] = (lastHostIndex[vmToUse] == (vmList
              .get(vmToUse).size() - 1)) ? 1
              : ++lastHostIndex[vmToUse];
          break;
        }
        ++attempts;
      }
    }
    if (attempts == vmList.size()) {
      vmToUse = -1; // avoid logic in finally block
      return; // No vm available for vMotion.
    }

    String targetVMName = vmList.get(vmToUse).get(0);
    String newHostName = vmList.get(vmToUse).get(lastHostIndex[vmToUse]);

    Folder rootFolder = si.getRootFolder();
    InventoryNavigator in = new InventoryNavigator(rootFolder);
    HostSystem newHost = (HostSystem)in.searchManagedEntity("HostSystem",
        newHostName);
    if (newHost == null) {
      log(WARNING, "Could not resolve host " + newHostName + ", vMotion to this host cannot be performed.");
      return;
    }

    //dummyVMotion(si, rootFolder, newHost, targetVMName, newHostName);
    migrateVM(si, rootFolder, newHost, targetVMName, newHostName);

  } catch (Exception e) {
    log(WARNING, "Found ", e);
  } finally {
    if (vmToUse != -1) {
      synchronized (vmList) {
        vmsInVMotion[vmToUse] = Boolean.FALSE;
      }
    }
  }
}
 
Example #15
Source File: StandardInventoryNavigatorFactory.java    From chaos-lemur with Apache License 2.0 4 votes vote down vote up
@Override
public InventoryNavigator create() throws IOException {
    ServiceInstance serviceInstance = new ServiceInstance(this.url, this.username, this.password, true);
    return new InventoryNavigator(serviceInstance.getRootFolder());
}
 
Example #16
Source File: VMotionTrigger.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private static boolean migrateVM(ServiceInstance si, Folder rootFolder,
    HostSystem newHost, String targetVMName, String newHostName)
    throws Exception {

  log("Selected host [vm] for vMotion: " + newHostName + " [" + targetVMName
      + "]");
  VirtualMachine vm = (VirtualMachine)new InventoryNavigator(rootFolder)
      .searchManagedEntity("VirtualMachine", targetVMName);
  if (vm == null) {
    log(WARNING, "Could not resolve VM " + targetVMName + ", vMotion of this VM cannot be performed.");
    return false;
  }

  ComputeResource cr = (ComputeResource)newHost.getParent();

  String[] checks = new String[] { "cpu", "software" };
  HostVMotionCompatibility[] vmcs = si.queryVMotionCompatibility(vm,
      new HostSystem[] { newHost }, checks);

  String[] comps = vmcs[0].getCompatibility();
  if (checks.length != comps.length) {
    log(WARNING, "CPU/software NOT compatible, vMotion failed.");
    return false;
  }

  long start = System.currentTimeMillis();
  Task task = vm.migrateVM_Task(cr.getResourcePool(), newHost,
      VirtualMachineMovePriority.highPriority,
      VirtualMachinePowerState.poweredOn);
  if (task.waitForMe() == Task.SUCCESS) {
    long end = System.currentTimeMillis();
    log("vMotion of " + targetVMName + " to " + newHostName
        + " completed in " + (end - start) + "ms. Task result: "
        + task.getTaskInfo().getResult());
    return true;
  } else {
    TaskInfo info = task.getTaskInfo();
    log(WARNING, "vMotion of " + targetVMName + " to " + newHostName
        + " failed. Error details: " + info.getError().getFault());
    return false;
  }
}
 
Example #17
Source File: VMotionTrigger.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void run() {
  int vmToUse = -1;
  int attempts = 0;
  try {
    synchronized (vmList) {
      while (attempts < vmList.size()) {
        lastVMIndex = (lastVMIndex == (vmList.size() - 1)) ? 0
            : ++lastVMIndex;
        vmToUse = lastVMIndex;
        if (!vmsInVMotion[vmToUse]) {
          vmsInVMotion[vmToUse] = Boolean.TRUE;
          lastHostIndex[vmToUse] = (lastHostIndex[vmToUse] == (vmList
              .get(vmToUse).size() - 1)) ? 1
              : ++lastHostIndex[vmToUse];
          break;
        }
        ++attempts;
      }
    }
    if (attempts == vmList.size()) {
      vmToUse = -1; // avoid logic in finally block
      return; // No vm available for vMotion.
    }

    String targetVMName = vmList.get(vmToUse).get(0);
    String newHostName = vmList.get(vmToUse).get(lastHostIndex[vmToUse]);

    Folder rootFolder = si.getRootFolder();
    InventoryNavigator in = new InventoryNavigator(rootFolder);
    HostSystem newHost = (HostSystem)in.searchManagedEntity("HostSystem",
        newHostName);
    if (newHost == null) {
      log(WARNING, "Could not resolve host " + newHostName + ", vMotion to this host cannot be performed.");
      return;
    }

    //dummyVMotion(si, rootFolder, newHost, targetVMName, newHostName);
    migrateVM(si, rootFolder, newHost, targetVMName, newHostName);

  } catch (Exception e) {
    log(WARNING, "Found ", e);
  } finally {
    if (vmToUse != -1) {
      synchronized (vmList) {
        vmsInVMotion[vmToUse] = Boolean.FALSE;
      }
    }
  }
}
 
Example #18
Source File: VMotionTrigger.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private static boolean migrateVM(ServiceInstance si, Folder rootFolder,
    HostSystem newHost, String targetVMName, String newHostName)
    throws Exception {

  log("Selected host [vm] for vMotion: " + newHostName + " [" + targetVMName
      + "]");
  VirtualMachine vm = (VirtualMachine)new InventoryNavigator(rootFolder)
      .searchManagedEntity("VirtualMachine", targetVMName);
  if (vm == null) {
    log(WARNING, "Could not resolve VM " + targetVMName + ", vMotion of this VM cannot be performed.");
    return false;
  }

  ComputeResource cr = (ComputeResource)newHost.getParent();

  String[] checks = new String[] { "cpu", "software" };
  HostVMotionCompatibility[] vmcs = si.queryVMotionCompatibility(vm,
      new HostSystem[] { newHost }, checks);

  String[] comps = vmcs[0].getCompatibility();
  if (checks.length != comps.length) {
    log(WARNING, "CPU/software NOT compatible, vMotion failed.");
    return false;
  }

  long start = System.currentTimeMillis();
  Task task = vm.migrateVM_Task(cr.getResourcePool(), newHost,
      VirtualMachineMovePriority.highPriority,
      VirtualMachinePowerState.poweredOn);
  if (task.waitForMe() == Task.SUCCESS) {
    long end = System.currentTimeMillis();
    log("vMotion of " + targetVMName + " to " + newHostName
        + " completed in " + (end - start) + "ms. Task result: "
        + task.getTaskInfo().getResult());
    return true;
  } else {
    TaskInfo info = task.getTaskInfo();
    log(WARNING, "vMotion of " + targetVMName + " to " + newHostName
        + " failed. Error details: " + info.getError().getFault());
    return false;
  }
}
 
Example #19
Source File: InventoryNavigatorFactory.java    From chaos-lemur with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new {@link InventoryNavigator} on each invocation
 *
 * @return a new {@link InventoryNavigator}
 */
InventoryNavigator create() throws IOException;