@Override public List<ComputeResource> getComputeResources(Long platformNo) { List<ComputeResource> computeResources = new ArrayList<ComputeResource>(); computeResources.add(new ComputeResource(null, null) { @Override public String getName() { return "Cluster1"; } }); computeResources.add(new ComputeResource(null, null) { @Override public String getName() { return "Cluster2"; } }); return computeResources; }
private void loadData() { VmwareDescribeService vmwareDescribeService = BeanContext.getBean(VmwareDescribeService.class); Long platformNo = platform.getPlatform().getPlatformNo(); // キーペア情報を取得 List<VmwareKeyPair> vmwareKeyPairs = vmwareDescribeService.getKeyPairs(ViewContext.getUserNo(), platformNo); this.vmwareKeyPairs = vmwareKeyPairs; // クラスタ情報を取得 List<ComputeResource> computeResources = vmwareDescribeService.getComputeResources(platformNo); List<String> clusters = new ArrayList<String>(); for (ComputeResource computeResource : computeResources) { clusters.add(computeResource.getName()); } this.clusters = clusters; }
/** * TODO: メソッドコメントを記述 * * @param vmwareProcessClient * @param networkNo * @param instanceNo */ public void addNetwork(VmwareProcessClient vmwareProcessClient, Long networkNo, Long instanceNo) { VmwareNetwork vmwareNetwork = vmwareNetworkDao.read(networkNo); // HostSystemを取得 VmwareClient vmwareClient = vmwareProcessClient.getVmwareClient(); ManagedEntity[] hostSystems; if (instanceNo == null) { hostSystems = vmwareClient.searchByType(HostSystem.class); } else { VmwareInstance vmwareInstance = vmwareInstanceDao.read(instanceNo); ComputeResource computeResource = vmwareClient.search(ComputeResource.class, vmwareInstance.getComputeResource()); hostSystems = vmwareClient.searchByType(computeResource, HostSystem.class); } // ネットワークを追加 for (ManagedEntity entity : hostSystems) { HostSystem hostSystem = HostSystem.class.cast(entity); vmwareProcessClient.addNetwork(hostSystem.getName(), vmwareNetwork.getNetworkName(), vmwareNetwork.getVlanId(), vmwareNetwork.getVswitchName()); } }
/** * TODO: メソッドコメントを記述 * * @param vmwareProcessClient * @param networkNo * @param instanceNo */ public void removeNetwork(VmwareProcessClient vmwareProcessClient, Long networkNo, Long instanceNo) { VmwareNetwork vmwareNetwork = vmwareNetworkDao.read(networkNo); // HostSystemを取得 VmwareClient vmwareClient = vmwareProcessClient.getVmwareClient(); ManagedEntity[] hostSystems; if (instanceNo == null) { hostSystems = vmwareClient.searchByType(HostSystem.class); } else { VmwareInstance vmwareInstance = vmwareInstanceDao.read(instanceNo); ComputeResource computeResource = vmwareClient.search(ComputeResource.class, vmwareInstance.getComputeResource()); hostSystems = vmwareClient.searchByType(computeResource, HostSystem.class); } // ネットワークを除去 for (ManagedEntity entity : hostSystems) { HostSystem hostSystem = HostSystem.class.cast(entity); vmwareProcessClient.removeNetwork(hostSystem.getName(), vmwareNetwork.getNetworkName()); } }
public DistributedVirtualPortgroupInfo getDVPortgroupInfo(VirtualMachine machine, String networkName) { HostSystem host = new HostSystem(machine.getServerConnection(), machine.getRuntime().getHost()); ComputeResource computeResource = (ComputeResource) host.getParent(); EnvironmentBrowser envBrowser = computeResource.getEnvironmentBrowser(); DistributedVirtualPortgroupInfo dvPortgroupInfo = null; try { ConfigTarget configTarget = envBrowser.queryConfigTarget(host); // 分散ポートグループの場合 if (configTarget.getDistributedVirtualPortgroup() != null) { // 分散ポートグループ情報を取得 dvPortgroupInfo = findDVPortgroupInfo(configTarget.getDistributedVirtualPortgroup(), networkName); } } catch (RemoteException e) { throw new RuntimeException(e); } return dvPortgroupInfo; }
private PlatformVmwareResponse getVmwareDetail(Long userNo, Long platformNo) { PlatformVmwareResponse response = new PlatformVmwareResponse(); // キー名 List<VmwareKeyPair> keyPairs = vmwareDescribeService.getKeyPairs(userNo, platformNo); for (VmwareKeyPair keyPair : keyPairs) { response.getKeyNames().add(keyPair.getKeyName()); } // ComputeResource List<ComputeResource> computeResources = vmwareDescribeService.getComputeResources(platformNo); for (ComputeResource computeResource : computeResources) { response.getComputeResources().add(computeResource.getName()); } return response; }
/** * {@inheritDoc} */ @Override public List<ComputeResource> getComputeResources(Long platformNo) { PlatformVmware platformVmware = platformVmwareDao.read(platformNo); VmwareClientFactory factory = new VmwareClientFactory(); factory.setUrl(platformVmware.getUrl()); factory.setUsername(platformVmware.getUsername()); factory.setPassword(platformVmware.getPassword()); factory.setDatacenterName(platformVmware.getDatacenter()); factory.setIgnoreCert(true); VmwareClient vmwareClient = factory.createVmwareClient(); List<ComputeResource> computeResources = new ArrayList<ComputeResource>(); ManagedEntity[] entities = vmwareClient.searchByType(ComputeResource.class); for (ManagedEntity entity : entities) { ComputeResource computeResource = ComputeResource.class.cast(entity); if (StringUtils.isNotEmpty(platformVmware.getComputeResource()) && !StringUtils.equals(computeResource.getName(), platformVmware.getComputeResource())) { continue; } computeResources.add(computeResource); } // ソート Collections.sort(computeResources, Comparators.COMPARATOR_COMPUTE_RESOURCE); return computeResources; }
protected VirtualMachineCloneSpec createCloneSpec(String computeResourceName, String resourcePoolName, String datastoreName) { VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec(); // ComputeResource ComputeResource computeResource = vmwareClient.search(ComputeResource.class, computeResourceName); if (computeResource == null) { // ComputeResourceが見つからない場合 throw new AutoException("EPROCESS-000503", computeResourceName); } // ResourcePool if (StringUtils.isEmpty(resourcePoolName)) { resourcePoolName = "Resources"; } ResourcePool resourcePool = vmwareClient.search(computeResource, ResourcePool.class, resourcePoolName); if (resourcePool == null) { // ResourcePoolが見つからない場合 throw new AutoException("EPROCESS-000504", resourcePoolName); } relocateSpec.setPool(resourcePool.getMOR()); // Datastore if (StringUtils.isNotEmpty(datastoreName)) { // データストアが指定されている場合 Datastore datastore = vmwareClient.search(Datastore.class, datastoreName); if (datastore == null) { // データストアが見つからない場合 throw new AutoException("EPROCESS-000505", datastoreName); } relocateSpec.setDatastore(datastore.getMOR()); } VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec(); cloneSpec.setLocation(relocateSpec); cloneSpec.setPowerOn(false); cloneSpec.setTemplate(false); return cloneSpec; }
private boolean checkComputeResource(Long platformNo, String computeResourceName) { List<ComputeResource> computeResources = vmwareDescribeService.getComputeResources(platformNo); for (ComputeResource computeResource : computeResources) { if (StringUtils.equals(computeResourceName, computeResource.getName())) { return true; } } return false; }
private void findHostsInFolder(Folder hostsFolder, List<HostSystem> hostsList) throws IOException, Exception { for (ManagedEntity e : hostsFolder.getChildEntity()) { if (e instanceof HostSystem) { hostsList.add((HostSystem)e); } // This is a cluster resource. Delve deeper to // find more hosts. if (e instanceof ComputeResource) { ComputeResource cr = (ComputeResource) e; if (e instanceof ClusterComputeResource) { ClusterComputeResource cluster = (ClusterComputeResource) e; if ((contrailClusterName != null) && (cluster.getName().equals(contrailClusterName) != true)) { continue; } } for(HostSystem host : cr.getHosts()) { hostsList.add((HostSystem)host); } } if (e instanceof Folder) { findHostsInFolder((Folder)e, hostsList); } } }
/** Create a new virtual network adapter on the VM * Your MAC address should start with 00:50:56 */ public void createNetworkAdapter(VirtualNetworkAdapterType type, String networkName, String macAddress, boolean wakeOnLan, boolean startConnected) throws InvalidProperty, RuntimeFault, RemoteException, InterruptedException { VirtualMachinePowerState powerState = vm.getRuntime().getPowerState(); String vmVerStr = vm.getConfig().getVersion(); int vmVer = Integer.parseInt(vmVerStr.substring(vmVerStr.length()-2)); if((powerState == VirtualMachinePowerState.suspended) || (powerState == VirtualMachinePowerState.suspended && vmVer < 7)) { throw new InvalidPowerState(); } HostSystem host = new HostSystem(vm.getServerConnection(), vm.getRuntime().getHost()); ComputeResource cr = (ComputeResource) host.getParent(); EnvironmentBrowser envBrowser = cr.getEnvironmentBrowser(); ConfigTarget configTarget = envBrowser.queryConfigTarget(host); VirtualMachineConfigOption vmCfgOpt = envBrowser.queryConfigOption(null, host); type = validateNicType(vmCfgOpt.getGuestOSDescriptor(), vm.getConfig().getGuestId(), type); VirtualDeviceConfigSpec nicSpec = createNicSpec(type, networkName, macAddress, wakeOnLan, startConnected, configTarget); VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); vmConfigSpec.setDeviceChange(new VirtualDeviceConfigSpec []{nicSpec}); Task task = vm.reconfigVM_Task(vmConfigSpec); task.waitForTask(200, 100); }
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; } }
/** * {@inheritDoc} */ @Override public Long createVmwareInstance(Long farmNo, String instanceName, Long platformNo, String comment, Long imageNo, String instanceType) { // インスタンスの作成 Long instanceNo = createInstance(farmNo, instanceName, platformNo, comment, imageNo); // 引数チェック if (instanceType == null || instanceType.length() == 0) { throw new AutoApplicationException("ECOMMON-000003", "instanceType"); } // プラットフォームのチェック Platform platform = platformDao.read(platformNo); if (PCCConstant.PLATFORM_TYPE_VMWARE.equals(platform.getPlatformType()) == false) { throw new AutoApplicationException("ESERVICE-000408", instanceName); } PlatformVmware platformVmware = platformVmwareDao.read(platformNo); // VMwareインスタンスの作成 VmwareInstance vmwareInstance = new VmwareInstance(); vmwareInstance.setInstanceNo(instanceNo); // 仮想マシン名 Farm farm = farmDao.read(farmNo); String machineName = farm.getFarmName() + "_" + instanceName; vmwareInstance.setMachineName(machineName); // InstanceType vmwareInstance.setInstanceType(instanceType); // クラスタ、ホスト String computeResource = platformVmware.getComputeResource(); if (StringUtils.isEmpty(computeResource)) { List<ComputeResource> computeResources = vmwareDescribeService.getComputeResources(platformNo); computeResource = computeResources.get(0).getName(); } vmwareInstance.setComputeResource(computeResource); // リソースプール // TODO: リソースプールの選択をどうする? String resourcePool = null; vmwareInstance.setResourcePool(resourcePool); // データストア(この時点ではデータストアを決めない) String datastore = null; vmwareInstance.setDatastore(datastore); // キーペア List<VmwareKeyPair> vmwareKeyPairs = vmwareKeyPairDao.readByUserNoAndPlatformNo(farm.getUserNo(), platformNo); Long keyPairNo = vmwareKeyPairs.get(0).getKeyNo(); vmwareInstance.setKeyPairNo(keyPairNo); // RootSize ImageVmware imageVmware = imageVmwareDao.read(imageNo); if (imageVmware.getRootSize() != null) { vmwareInstance.setRootSize(imageVmware.getRootSize()); } vmwareInstanceDao.create(vmwareInstance); // イベントログ出力 eventLogger.log(EventLogLevel.INFO, farmNo, farm.getFarmName(), null, null, instanceNo, instanceName, "InstanceCreate", instanceType, platformNo, new Object[] { platform.getPlatformName() }); // フック処理の実行 processHook.execute("post-create-instance", farm.getUserNo(), farm.getFarmNo(), instanceNo); return instanceNo; }
@Override public int compare(ComputeResource o1, ComputeResource o2) { return o1.getName().compareTo(o2.getName()); }
public static void main(String[] args) throws Exception { if(args.length!=5) { System.out.println("Usage: java MigrateVM <url> " + "<username> <password> <vmname> <newhost>"); System.exit(0); } String vmname = args[3]; String newHostName = args[4]; ServiceInstance si = new ServiceInstance( new URL(args[0]), args[1], args[2], true); Folder rootFolder = si.getRootFolder(); VirtualMachine vm = (VirtualMachine) new InventoryNavigator( rootFolder).searchManagedEntity( "VirtualMachine", vmname); HostSystem newHost = (HostSystem) new InventoryNavigator( rootFolder).searchManagedEntity( "HostSystem", newHostName); 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) { System.out.println("CPU/software NOT compatible. Exit."); si.getServerConnection().logout(); return; } Task task = vm.migrateVM_Task(cr.getResourcePool(), newHost, VirtualMachineMovePriority.highPriority, VirtualMachinePowerState.poweredOn); if(task.waitForMe()==Task.SUCCESS) { System.out.println("VMotioned!"); } else { System.out.println("VMotion failed!"); TaskInfo info = task.getTaskInfo(); System.out.println(info.getError().getFault()); } si.getServerConnection().logout(); }
@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 ); } }
public List<ComputeResource> getComputeResources(Long platformNo);