7676import com .cloud .org .Cluster .ClusterType ;
7777import com .cloud .secstorage .CommandExecLogDao ;
7878import com .cloud .server .ConfigurationServer ;
79+ import com .cloud .service .ServiceOfferingVO ;
7980import com .cloud .service .dao .ServiceOfferingDao ;
81+ import com .cloud .storage .GuestOSVO ;
8082import com .cloud .storage .ImageStoreDetailsUtil ;
8183import com .cloud .storage .JavaStorageLayer ;
8284import com .cloud .storage .Storage ;
8587import com .cloud .storage .VMTemplateStoragePoolVO ;
8688import com .cloud .storage .Volume ;
8789import com .cloud .storage .VolumeVO ;
90+ import com .cloud .storage .dao .GuestOSDao ;
8891import com .cloud .storage .dao .VMTemplatePoolDao ;
8992import com .cloud .storage .dao .VolumeDao ;
9093import com .cloud .template .TemplateManager ;
103106import com .cloud .utils .script .Script ;
104107import com .cloud .utils .ssh .SshHelper ;
105108import com .cloud .vm .DomainRouterVO ;
109+ import com .cloud .vm .UserVmVO ;
110+ import com .cloud .vm .VMInstanceVO ;
106111import com .cloud .vm .dao .UserVmCloneSettingDao ;
112+ import com .cloud .vm .dao .UserVmDao ;
107113import com .cloud .vm .dao .VMInstanceDao ;
108114import com .vmware .vim25 .AboutInfo ;
109115import com .vmware .vim25 .ManagedObjectReference ;
126132import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
127133import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
128134import org .apache .cloudstack .utils .identity .ManagementServerNode ;
135+ import org .apache .commons .collections .CollectionUtils ;
129136import org .apache .commons .lang .BooleanUtils ;
130137import org .apache .commons .lang .StringUtils ;
131138import org .apache .log4j .Logger ;
@@ -214,6 +221,10 @@ public class VmwareManagerImpl extends ManagerBase implements VmwareManager, Vmw
214221 private VolumeDao volumeDao ;
215222 @ Inject
216223 private ServiceOfferingDao serviceOfferingDao ;
224+ @ Inject
225+ private GuestOSDao guestOSDao ;
226+ @ Inject
227+ private UserVmDao userVmDao ;
217228
218229 private String _mountParent ;
219230 private StorageLayer _storage ;
@@ -1317,8 +1328,13 @@ public boolean importVmwareVM(String vmInternalName, Long zoneId) {
13171328
13181329 s_logger .debug ("VM " + vmInternalName + " found on datacenter " + datacenterName + ", trying to import it..." );
13191330
1320- importVMAndServiceOffering (configSummary );
1321- importVMVolumes (virtualDisks , zoneId );
1331+ Long guestOsId = importVMGuestOs (configSummary );
1332+ Long serviceOfferingId = importVMServiceOffering (configSummary );
1333+ Long poolId = getPoolId (virtualDisks );
1334+ Long templateId = importVMTemplate (virtualDisks , poolId );
1335+ VMInstanceVO vmInstanceVO = createVMRecord (vmInternalName , poolId , templateId , guestOsId , serviceOfferingId );
1336+
1337+ importVMVolumes (vmInstanceVO , poolId , templateId , guestOsId , virtualDisks , zoneId );
13221338 importVMNetworks (networks , networksWithDetails , nicDevices );
13231339 //Continue importing
13241340 } catch (Exception e ) {
@@ -1327,70 +1343,141 @@ public boolean importVmwareVM(String vmInternalName, Long zoneId) {
13271343 return true ;
13281344 }
13291345
1330- private void importVMAndServiceOffering ( VirtualMachineConfigSummary configSummary ) {
1331- Integer numCpu = configSummary . getNumCpu ();
1332- String internalName = configSummary . getName ( );
1333- Integer memorySizeMB = configSummary . getMemorySizeMB ();
1346+ private void checkBackingInfo ( VirtualDeviceBackingInfo backingInfo ) {
1347+ if (! ( backingInfo instanceof VirtualDiskFlatVer2BackingInfo )) {
1348+ throw new CloudRuntimeException ( "Unsopported backing, expected " + VirtualDiskFlatVer2BackingInfo . class . getSimpleName () );
1349+ }
13341350 }
13351351
1336- private void importVMNetworks (String [] networks , List <NetworkDetails > networksWithDetails , VirtualDevice [] nicDevices ) {
1352+ private boolean isRootDisk (VirtualDisk disk ) {
1353+ VirtualDeviceBackingInfo backing = disk .getBacking ();
1354+ checkBackingInfo (backing );
1355+ VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo ) backing ;
1356+ return info .getFileName ().contains ("ROOT" );
1357+ }
1358+
1359+ private Long getPoolId (VirtualDisk disk ) {
1360+ VirtualDeviceBackingInfo backing = disk .getBacking ();
1361+ checkBackingInfo (backing );
1362+ VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo ) backing ;
1363+ String [] fileNameParts = info .getFileName ().split (" " );
1364+ String datastoreUuid = StringUtils .substringBetween (fileNameParts [0 ], "[" , "]" );
1365+ String poolUuid = UuidUtils .normalize (datastoreUuid );
1366+ StoragePoolVO pool = primaryStorageDao .findByUuid (poolUuid );
1367+ if (pool == null ) {
1368+ throw new CloudRuntimeException ("Couldn't find storage pool " + poolUuid );
1369+ }
1370+ return pool .getId ();
1371+ }
1372+
1373+ private Long getPoolId (List <VirtualDisk > disks ) {
1374+ for (VirtualDisk disk : disks ) {
1375+ if (isRootDisk (disk )) {
1376+ return getPoolId (disk );
1377+ }
1378+ }
1379+ return null ;
1380+ }
1381+
1382+ private String getVolumeNameFromFileName (String fileName ) {
1383+ String [] fileNameParts = fileName .split (" " );
1384+ String volumePath = fileNameParts [1 ];
1385+ return volumePath .split ("/" )[1 ].replaceFirst (".vmdk" , "" );
13371386 }
13381387
1339- private void importVMVolumes (List <VirtualDisk > virtualDisks , Long zoneId ) {
1388+ private String getVolumeName (VirtualDisk disk ) {
1389+ VirtualDeviceBackingInfo backing = disk .getBacking ();
1390+ checkBackingInfo (backing );
1391+ VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo ) backing ;
1392+ return getVolumeNameFromFileName (info .getFileName ());
1393+ }
1394+
1395+ private String getTemplatePath (VirtualDisk rootDisk ) {
1396+ VirtualDeviceBackingInfo backing = rootDisk .getBacking ();
1397+ checkBackingInfo (backing );
1398+ VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo ) backing ;
1399+ VirtualDiskFlatVer2BackingInfo parent = info .getParent ();
1400+ return getVolumeNameFromFileName (parent .getFileName ());
1401+ }
1402+
1403+ private Long importVMTemplate (List <VirtualDisk > virtualDisks , Long poolId ) {
13401404 for (VirtualDisk disk : virtualDisks ) {
1341- Long size = disk .getCapacityInBytes ();
1342- VirtualDeviceBackingInfo backing = disk .getBacking ();
1343- if (backing instanceof VirtualDiskFlatVer2BackingInfo ) {
1344- VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo ) backing ;
1345- String fileName = info .getFileName ();
1346- String [] fileNameParts = fileName .split (" " );
1347- String volumeName = fileNameParts [1 ];
1348- Storage .ProvisioningType provisioningType = null ;
1349- Boolean thinProvisioned = info .isThinProvisioned ();
1350- if (BooleanUtils .isTrue (thinProvisioned )) {
1351- provisioningType = Storage .ProvisioningType .THIN ;
1352- }
1353- VirtualDiskFlatVer2BackingInfo parent = info .getParent ();
1354- Pair <Long , Long > pair = getPoolAndTemplateIds (parent );
1355- VolumeVO volumeVO = null ;
1356- if (volumeName .contains ("ROOT" )) {
1357- String name = volumeName .split ("/" )[1 ].replaceFirst (".vmdk" , "" );
1358- volumeVO = new VolumeVO (Volume .Type .ROOT , name , zoneId , 1L , 2L , 1L ,
1359- provisioningType , size , null , null , null );
1360- volumeVO .setFormat (Storage .ImageFormat .OVA );
1361- volumeVO .setPath (name );
1362- //volumeVO.setInstanceId(null);
1363- //volumeVO.setChainInfo(CHAIN_INFO);
1405+ if (isRootDisk (disk )) {
1406+ String templatePath = getTemplatePath (disk );
1407+ VMTemplateStoragePoolVO templateRef = templateDataStoreDao .findByPoolPath (poolId , templatePath );
1408+ if (templateRef == null ) {
1409+ //TODO: Create template and add ref
1410+ return 0L ;
1411+ } else {
1412+ return templateRef .getTemplateId ();
13641413 }
1365- volumeVO .setPoolId (pair .first ());
1366- volumeVO .setTemplateId (pair .second ());
1367- volumeVO .setState (Volume .State .Ready );
1368- VolumeVO persist = volumeDao .persist (volumeVO );
13691414 }
13701415 }
1416+ return null ;
13711417 }
13721418
1373- private Pair <Long , Long > getPoolAndTemplateIds (VirtualDiskFlatVer2BackingInfo parent ) {
1374- String fileName = parent .getFileName ();
1375- String [] fileNameParts = fileName .split (" " );
1376- if (fileNameParts .length != 2 ) {
1377- throw new CloudRuntimeException ("Error parsing template" );
1378- }
1379- String storagePoolUuid = StringUtils .substringBetween (fileNameParts [0 ], "[" , "]" );
1380- String poolUuid = UuidUtils .fromNoHyphenUuid (storagePoolUuid );
1381- StoragePoolVO storagePoolVO = primaryStorageDao .findByUuid (poolUuid );
1382- if (storagePoolVO == null ) {
1383- throw new CloudRuntimeException ("Could not find primary storage: " + storagePoolUuid );
1384- }
1385- String path = fileNameParts [1 ];
1386- String [] pathParts = path .split ("/" );
1387- String parentUuid = pathParts [0 ];
1388- VMTemplateStoragePoolVO ref = templateDataStoreDao .findByPoolPath (storagePoolVO .getId (), parentUuid );
1389- if (ref == null ) {
1390- //TODO: CREATE TEMPLATE
1391- return new Pair <>(storagePoolVO .getId (), 0L );
1419+ private VMInstanceVO createVMRecord (String vmInternalName , Long poolId , Long templateId , Long guestOsId , Long serviceOfferingId ) {
1420+ long id = userVmDao .getNextInSequence (Long .class , "id" );
1421+ UserVmVO vmInstanceVO = new UserVmVO (id , vmInternalName , vmInternalName , templateId , HypervisorType .VMware ,
1422+ guestOsId , false , false , 1L , 1L , 1L , serviceOfferingId ,
1423+ null , vmInternalName , null );
1424+ return userVmDao .persist (vmInstanceVO );
1425+ }
1426+
1427+ private Long importVMGuestOs (VirtualMachineConfigSummary configSummary ) {
1428+ String guestFullName = configSummary .getGuestFullName ();
1429+ GuestOSVO os = guestOSDao .listByDisplayName (guestFullName );
1430+ if (os == null ) {
1431+ //TODO: Set it to default?
1432+ return 0L ;
1433+ } else {
1434+ return os .getId ();
1435+ }
1436+ }
1437+
1438+ private Long importVMServiceOffering (VirtualMachineConfigSummary configSummary ) {
1439+ Integer numCpu = configSummary .getNumCpu ();
1440+ Integer memorySizeMB = configSummary .getMemorySizeMB ();
1441+ configSummary .getCpuReservation ();
1442+ List <ServiceOfferingVO > offerings = serviceOfferingDao .listPublicByCpuAndMemory (numCpu , memorySizeMB );
1443+ if (CollectionUtils .isEmpty (offerings )) {
1444+ //TODO: Create service offering
1445+ return 0L ;
13921446 } else {
1393- return new Pair <>(storagePoolVO .getId (), ref .getTemplateId ());
1447+ return offerings .get (0 ).getId ();
1448+ }
1449+ }
1450+
1451+ private void importVMNetworks (String [] networks , List <NetworkDetails > networksWithDetails , VirtualDevice [] nicDevices ) {
1452+ }
1453+
1454+ private Storage .ProvisioningType getProvisioningType (VirtualDiskFlatVer2BackingInfo backing ) {
1455+ Boolean thinProvisioned = backing .isThinProvisioned ();
1456+ if (BooleanUtils .isTrue (thinProvisioned )) {
1457+ return Storage .ProvisioningType .THIN ;
1458+ }
1459+ return Storage .ProvisioningType .SPARSE ;
1460+ }
1461+
1462+ private void importVMVolumes (VMInstanceVO vmInstanceVO , Long poolId , Long templateId , Long guestOsId , List <VirtualDisk > virtualDisks , Long zoneId ) {
1463+ for (VirtualDisk disk : virtualDisks ) {
1464+ Long size = disk .getCapacityInBytes ();
1465+ VirtualDeviceBackingInfo backing = disk .getBacking ();
1466+ checkBackingInfo (backing );
1467+ VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo ) backing ;
1468+ String volumeName = getVolumeName (disk );
1469+ Storage .ProvisioningType provisioningType = getProvisioningType (info );
1470+ Volume .Type type = isRootDisk (disk ) ? Volume .Type .ROOT : Volume .Type .DATADISK ;
1471+
1472+ VolumeVO volumeVO = new VolumeVO (type , volumeName , zoneId , 1L , 2L , 1L ,
1473+ provisioningType , size , null , null , null );
1474+ volumeVO .setFormat (Storage .ImageFormat .OVA );
1475+ volumeVO .setPath (volumeName );
1476+ volumeVO .setState (Volume .State .Ready );
1477+ volumeVO .setInstanceId (vmInstanceVO .getId ());
1478+ volumeVO .setPoolId (poolId );
1479+ volumeVO .setTemplateId (templateId );
1480+ volumeDao .persist (volumeVO );
13941481 }
13951482 }
13961483
0 commit comments