com.vmware.vim25.mo.ServiceInstance Java Examples

The following examples show how to use com.vmware.vim25.mo.ServiceInstance. 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: VmwareIaasHandler.java    From roboconf-platform with Apache License 2.0 6 votes vote down vote up
@Override
public String retrievePublicIpAddress( TargetHandlerParameters parameters, String machineId )
throws TargetException {

	String result = null;
	try {
		ServiceInstance vmwareServiceInstance = VmwareIaasHandler.getServiceInstance( parameters.getTargetProperties());
		String rootInstanceName = InstanceHelpers.findRootInstancePath( parameters.getScopedInstancePath());
		VirtualMachine vm = VmwareIaasHandler.getVirtualMachine( vmwareServiceInstance, rootInstanceName );
		if( vm != null )
			result = vm.getGuest().getIpAddress();

	} catch( Exception e ) {
		throw new TargetException( e );
	}

	return result;
}
 
Example #3
Source File: VmwareIaasHandler.java    From roboconf-platform with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isMachineRunning( TargetHandlerParameters parameters, String machineId )
throws TargetException {

	boolean result;
	try {
		final ServiceInstance vmwareServiceInstance = getServiceInstance( parameters.getTargetProperties());
		VirtualMachine vm = getVirtualMachine( vmwareServiceInstance, machineId );
		result = vm != null;

	} catch( Exception e ) {
		throw new TargetException( e );
	}

	return result;
}
 
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: 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 #8
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 #9
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 #10
Source File: VmwareIaasHandler.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
static ServiceInstance getServiceInstance( Map<String,String> targetProperties )
throws RemoteException, MalformedURLException {

	return new ServiceInstance(
			new URL(targetProperties.get( URL )),
			targetProperties.get( USER ),
			targetProperties.get( PASSWORD ),
			Boolean.parseBoolean(targetProperties.get( IGNORE_CERTIFICATE )));
}
 
Example #11
Source File: VmwareClient.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
/**
 * serviceInstanceを取得します。
 *
 * @return serviceInstance
 */
public ServiceInstance getServiceInstance() {
    if (serviceInstance == null) {
        initialize();
    }
    return serviceInstance;
}
 
Example #12
Source File: meGrabber.java    From vmstats with Apache License 2.0 5 votes vote down vote up
public meGrabber(ServiceInstance si, 
		BlockingQueue<Object> vm_mob_queue,
           BlockingQueue<Object> esx_mob_queue, 
           Hashtable<String, String> appConfig, 
           BlockingQueue<Object> sender) {
	this.vm_mob_queue = vm_mob_queue;
	this.esx_mob_queue = esx_mob_queue;
	this.appConfig = appConfig;
	this.si = si;
       this.sender = sender;
       this.clusterMap = new HashMap<String, String>();
       this.vm_cache = new ArrayList<Object>();
       this.esx_cache = new ArrayList<Object>();
}
 
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: 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 #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: 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 #17
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 #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: 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 #20
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 #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;
  }
}