System.out.println("java ExportOvfToLocal <SdkUrl> <username> <password> <VappOrVmName> <hostip> <VirtualMachine|VirtualApp> <localDir>");
System.out.println("java ExportOvfToLocal https://10.20.152.74/sdk root password NewVM1 10.20.152.74 VirtualMachine C:\\Temp\\ovf\\");
return;
}
ServiceInstance si = new ServiceInstance(new URL(args[0]), args[1], args[2], true);
String vAppOrVmName = args[3];
String hostip = args[4];
String entityType = args[5];
String targetDir = args[6];
HostSystem host = (HostSystem) si.getSearchIndex().findByIp(null, hostip, false);
System.out.println("Host Name : " + host.getName());
System.out.println("Network : " + host.getNetworks()[0].getName());
System.out.println("Datastore : " + host.getDatastores()[0].getName());
InventoryNavigator iv = new InventoryNavigator(si.getRootFolder());
HttpNfcLease hnLease = null;
ManagedEntity me = null;
if (entityType.equals("VirtualApp"))
{
me = iv.searchManagedEntity("VirtualApp", vAppOrVmName);
hnLease = ((VirtualApp)me).exportVApp();
}
else
{
me = iv.searchManagedEntity("VirtualMachine", vAppOrVmName);
hnLease = ((VirtualMachine)me).exportVm();
}
// Wait until the HttpNfcLeaseState is ready
HttpNfcLeaseState hls;
for(;;)
{
hls = hnLease.getState();
if(hls == HttpNfcLeaseState.ready)
{
break;
}
if(hls == HttpNfcLeaseState.error)
{
si.getServerConnection().logout();
return;
}
}
System.out.println("HttpNfcLeaseState: ready ");
HttpNfcLeaseInfo httpNfcLeaseInfo = hnLease.getInfo();
httpNfcLeaseInfo.setLeaseTimeout(300*1000*1000);
printHttpNfcLeaseInfo(httpNfcLeaseInfo);
//Note: the diskCapacityInByte could be many time bigger than
//the total size of VMDK files downloaded.
//As a result, the progress calculated could be much less than reality.
long diskCapacityInByte = (httpNfcLeaseInfo.getTotalDiskCapacityInKB()) * 1024;
leaseProgUpdater = new LeaseProgressUpdater(hnLease, 5000);
leaseProgUpdater.start();
long alredyWrittenBytes = 0;
HttpNfcLeaseDeviceUrl[] deviceUrls = httpNfcLeaseInfo.getDeviceUrl();
if (deviceUrls != null)
{
OvfFile[] ovfFiles = new OvfFile[deviceUrls.length];
System.out.println("Downloading Files:");
for (int i = 0; i < deviceUrls.length; i++)
{
String deviceId = deviceUrls[i].getKey();
String deviceUrlStr = deviceUrls[i].getUrl();
String diskFileName = deviceUrlStr.substring(deviceUrlStr.lastIndexOf("/") + 1);
String diskUrlStr = deviceUrlStr.replace("*", hostip);
String diskLocalPath = targetDir + diskFileName;
System.out.println("File Name: " + diskFileName);
System.out.println("VMDK URL: " + diskUrlStr);
String cookie = si.getServerConnection().getVimService().getWsc().getCookie();
long lengthOfDiskFile = writeVMDKFile(diskLocalPath, diskUrlStr, cookie, alredyWrittenBytes, diskCapacityInByte);
alredyWrittenBytes += lengthOfDiskFile;
OvfFile ovfFile = new OvfFile();
ovfFile.setPath(diskFileName);
ovfFile.setDeviceId(deviceId);
ovfFile.setSize(lengthOfDiskFile);
ovfFiles[i] = ovfFile;
}
OvfCreateDescriptorParams ovfDescParams = new OvfCreateDescriptorParams();
ovfDescParams.setOvfFiles(ovfFiles);
OvfCreateDescriptorResult ovfCreateDescriptorResult =
si.getOvfManager().createDescriptor(me, ovfDescParams);
String ovfPath = targetDir + vAppOrVmName + ".ovf";
FileWriter out = new FileWriter(ovfPath);
out.write(ovfCreateDescriptorResult.getOvfDescriptor());
out.close();
System.out.println("OVF Desriptor Written to file: " + ovfPath);
}
System.out.println("Completed Downloading the files");
leaseProgUpdater.interrupt();
hnLease.httpNfcLeaseProgress(100);
hnLease.httpNfcLeaseComplete();
si.getServerConnection().logout();
}