System.out.println(
"java ImportLocalOvfVApp https://10.20.140.47/sdk Administrator password 10.17.204.115 E:/Downloads/Nostalgia.ovf NewVM");
return;
}
ServiceInstance si = new ServiceInstance(new URL(args[0]), args[1], args[2], true);
String ovfLocal = args[4];
String hostip = args[3];
String newVmName = args[5];
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());
Folder vmFolder = (Folder) host.getVms()[0].getParent();
OvfCreateImportSpecParams importSpecParams = new OvfCreateImportSpecParams();
importSpecParams.setHostSystem(host.getMOR());
importSpecParams.setLocale("US");
importSpecParams.setEntityName(newVmName);
importSpecParams.setDeploymentOption("");
OvfNetworkMapping networkMapping = new OvfNetworkMapping();
networkMapping.setName("Network 1");
networkMapping.setNetwork(host.getNetworks()[0].getMOR()); // network);
importSpecParams.setNetworkMapping(new OvfNetworkMapping[] { networkMapping });
importSpecParams.setPropertyMapping(null);
String ovfDescriptor = readOvfContent(ovfLocal);
if (ovfDescriptor == null)
{
si.getServerConnection().logout();
return;
}
System.out.println("ovfDesc:" + ovfDescriptor);
ResourcePool rp = ((ComputeResource)host.getParent()).getResourcePool();
OvfCreateImportSpecResult ovfImportResult = si.getOvfManager().createImportSpec(
ovfDescriptor, rp, host.getDatastores()[0], importSpecParams);
if(ovfImportResult==null)
{
si.getServerConnection().logout();
return;
}
long totalBytes = addTotalBytes(ovfImportResult);
System.out.println("Total bytes: " + totalBytes);
HttpNfcLease httpNfcLease = null;
httpNfcLease = rp.importVApp(ovfImportResult.getImportSpec(), vmFolder, host);
// Wait until the HttpNfcLeaseState is ready
HttpNfcLeaseState hls;
for(;;)
{
hls = httpNfcLease.getState();
if(hls == HttpNfcLeaseState.ready || hls == HttpNfcLeaseState.error)
{
break;
}
}
if (hls.equals(HttpNfcLeaseState.ready))
{
System.out.println("HttpNfcLeaseState: ready ");
HttpNfcLeaseInfo httpNfcLeaseInfo = (HttpNfcLeaseInfo) httpNfcLease.getInfo();
printHttpNfcLeaseInfo(httpNfcLeaseInfo);
leaseUpdater = new LeaseProgressUpdater(httpNfcLease, 5000);
leaseUpdater.start();
HttpNfcLeaseDeviceUrl[] deviceUrls = httpNfcLeaseInfo.getDeviceUrl();
long bytesAlreadyWritten = 0;
for (HttpNfcLeaseDeviceUrl deviceUrl : deviceUrls)
{
String deviceKey = deviceUrl.getImportKey();
for (OvfFileItem ovfFileItem : ovfImportResult.getFileItem())
{
if (deviceKey.equals(ovfFileItem.getDeviceId()))
{
System.out.println("Import key==OvfFileItem device id: " + deviceKey);
String absoluteFile = new File(ovfLocal).getParent() + File.separator + ovfFileItem.getPath();
String urlToPost = deviceUrl.getUrl().replace("*", hostip);
uploadVmdkFile(ovfFileItem.isCreate(), absoluteFile, urlToPost, bytesAlreadyWritten, totalBytes);
bytesAlreadyWritten += ovfFileItem.getSize();
System.out.println("Completed uploading the VMDK file:" + absoluteFile);
}
}
}
leaseUpdater.interrupt();
httpNfcLease.httpNfcLeaseProgress(100);
httpNfcLease.httpNfcLeaseComplete();
}
si.getServerConnection().logout();
}