com.vmware.vim25.mo.Folder Java Examples

The following examples show how to use com.vmware.vim25.mo.Folder. 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
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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
Source File: VmwareMachineProcess.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
protected String selectDatastore(VmwareProcessClient vmwareProcessClient, VmwareInstance vmwareInstance) {
    // データストアフォルダ内のデータストアのうち、アクセス可能で空き容量が最も大きいものを用いる
    Datastore datastore = null;
    long freeSpace = 0L;

    // ComputeResourceごとのフォルダがあれば、その中のデータストアを用いる
    String datastoreFolderName = vmwareInstance.getComputeResource() + "-storage";
    Folder datastoreFolder = vmwareProcessClient.getVmwareClient().search(Folder.class, datastoreFolderName);
    if (datastoreFolder == null) {
        // ComputeResourceごとのフォルダがなければ、"storage"フォルダの中のデータストアを用いる
        datastoreFolder = vmwareProcessClient.getVmwareClient().search(Folder.class, "storage");
    }

    if (datastoreFolder != null) {
        ManagedEntity[] entities = vmwareProcessClient.getVmwareClient().searchByType(datastoreFolder,
                Datastore.class);
        for (ManagedEntity entity : entities) {
            Datastore datastore2 = Datastore.class.cast(entity);
            DatastoreSummary summary2 = datastore2.getSummary();

            if (summary2.isAccessible() && freeSpace < summary2.getFreeSpace()) {
                datastore = datastore2;
                freeSpace = summary2.getFreeSpace();
            }
        }
    }

    if (datastore == null) {
        // 利用可能なデータストアがない場合
        throw new AutoException("EPROCESS-000528", vmwareInstance.getComputeResource());
    }

    return datastore.getName();
}
 
Example #8
Source File: VmwareApiConnectorTest.java    From teamcity-vmware-plugin with Apache License 2.0 5 votes vote down vote up
private <T extends ManagedEntity> T createEntity(Class<T> type, @Nullable ManagedEntity parent, String val, String name){
  final ManagedObjectReference mor = new ManagedObjectReference();
  mor.setType(type.getSimpleName());
  mor.setVal(val);
  if (type==Folder.class){
    return (T)new Folder(null, mor){
      @Override
      public ManagedEntity getParent() {
        return parent;
      }

      @Override
      public String getName() {
        return name;
      }
    };
  }
  if (type==ResourcePool.class){
    return (T)new ResourcePool(null, mor){
      @Override
      public ManagedEntity getParent() {
        return parent;
      }

      @Override
      public String getName() {
        return name;
      }
    };
  }
  throw new IllegalArgumentException("can't create instance of type " + type.getSimpleName());
}
 
Example #9
Source File: VIJavaUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private static boolean validateVMNotOnHost(ServiceInstance si,
    Folder rootFolder, HostSystem newHost, String vmName, String hostName)
    throws Exception {
  VirtualMachine[] vms = newHost.getVms();
  for (VirtualMachine vmac : vms) {
    if (vmac.getName().equals(vmName)) {
      Log.getLogWriter().info(
          vmName + " is already running on target host " + hostName
              + ". Selecting another pair...");
      return false;
    }
  }
  return true;
}
 
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: FakeFolder.java    From teamcity-vmware-plugin with Apache License 2.0 5 votes vote down vote up
public <T extends ManagedEntity> void setParent(final String parentName, Class<T> parentType) {
  if (parentType.isAssignableFrom(FakeDatacenter.class)){
    setParent(FakeModel.instance().getDatacenter(parentName));
  } else if (parentType.isAssignableFrom(Folder.class)){
    setParent(FakeModel.instance().getFolder(parentName));
  }
  if (myParent == null) {
    Assert.fail(String.format("Unable to set parent %s of type %s", parentName, parentType.getSimpleName()));
  }
}
 
Example #12
Source File: VMotionTrigger.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private static void dummyVMotion(ServiceInstance si, Folder rootFolder,
    HostSystem newHost, String targetVMName, String newHostName) {
  log("Selected host [vm] for vMotion: " + newHostName + " [" + targetVMName
      + "]");
  try {Thread.sleep(120000);} catch(InterruptedException ir) {}
  log("vMotion of " + targetVMName + " to " + newHostName
      + " completed");
}
 
Example #13
Source File: VIJavaUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private static boolean validateVMNotOnHost(ServiceInstance si,
    Folder rootFolder, HostSystem newHost, String vmName, String hostName)
    throws Exception {
  VirtualMachine[] vms = newHost.getVms();
  for (VirtualMachine vmac : vms) {
    if (vmac.getName().equals(vmName)) {
      Log.getLogWriter().info(
          vmName + " is already running on target host " + hostName
              + ". Selecting another pair...");
      return false;
    }
  }
  return true;
}
 
Example #14
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 #15
Source File: VMotionTrigger.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private static void dummyVMotion(ServiceInstance si, Folder rootFolder,
    HostSystem newHost, String targetVMName, String newHostName) {
  log("Selected host [vm] for vMotion: " + newHostName + " [" + targetVMName
      + "]");
  try {Thread.sleep(120000);} catch(InterruptedException ir) {}
  log("vMotion of " + targetVMName + " to " + newHostName
      + " completed");
}
 
Example #16
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 #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: FolderBean.java    From teamcity-vmware-plugin with Apache License 2.0 4 votes vote down vote up
@Used("Tests")
public FolderBean(final Folder folder){
  this(folder.getMOR(), folder.getName(), null, folder.getChildType(), folder.getParent().getMOR(), "dc");
}
 
Example #19
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 #20
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 #21
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;
  }
}