use of com.cloud.agent.api.HandleConfigDriveIsoAnswer in project cloudstack by apache.
the class ConfigDriveNetworkElement method createConfigDriveIsoOnHostCache.
private boolean createConfigDriveIsoOnHostCache(VirtualMachineProfile profile, Long hostId) throws ResourceUnavailableException {
if (hostId == null) {
throw new ResourceUnavailableException("Config drive iso creation failed, dest host not available", ConfigDriveNetworkElement.class, 0L);
}
LOG.debug("Creating config drive ISO for vm: " + profile.getInstanceName() + " on host: " + hostId);
final String isoFileName = ConfigDrive.configIsoFileName(profile.getInstanceName());
final String isoPath = ConfigDrive.createConfigDrivePath(profile.getInstanceName());
final String isoData = ConfigDriveBuilder.buildConfigDrive(profile.getVmData(), isoFileName, profile.getConfigDriveLabel());
final HandleConfigDriveIsoCommand configDriveIsoCommand = new HandleConfigDriveIsoCommand(isoPath, isoData, null, false, true, true);
final HandleConfigDriveIsoAnswer answer = (HandleConfigDriveIsoAnswer) agentManager.easySend(hostId, configDriveIsoCommand);
if (answer == null) {
throw new CloudRuntimeException("Unable to get an answer to handle config drive creation for vm: " + profile.getInstanceName() + " on host: " + hostId);
}
if (!answer.getResult()) {
throw new ResourceUnavailableException(String.format("Config drive iso creation failed, details: %s", answer.getDetails()), ConfigDriveNetworkElement.class, 0L);
}
profile.setConfigDriveLocation(answer.getConfigDriveLocation());
_userVmDetailsDao.addDetail(profile.getId(), VmDetailConstants.CONFIG_DRIVE_LOCATION, answer.getConfigDriveLocation().toString(), false);
addConfigDriveDisk(profile, null);
return true;
}
use of com.cloud.agent.api.HandleConfigDriveIsoAnswer in project cloudstack by apache.
the class ConfigDriveNetworkElement method createConfigDriveIso.
private boolean createConfigDriveIso(VirtualMachineProfile profile, DeployDestination dest, DiskTO disk) throws ResourceUnavailableException {
DataStore dataStore = getDatastoreForConfigDriveIso(disk, profile, dest);
final Long agentId = findAgentId(profile, dest, dataStore);
if (agentId == null || dataStore == null) {
throw new ResourceUnavailableException("Config drive iso creation failed, agent or datastore not available", ConfigDriveNetworkElement.class, 0L);
}
LOG.debug("Creating config drive ISO for vm: " + profile.getInstanceName());
final String isoFileName = ConfigDrive.configIsoFileName(profile.getInstanceName());
final String isoPath = ConfigDrive.createConfigDrivePath(profile.getInstanceName());
final String isoData = ConfigDriveBuilder.buildConfigDrive(profile.getVmData(), isoFileName, profile.getConfigDriveLabel());
boolean useHostCacheOnUnsupportedPool = VirtualMachineManager.VmConfigDriveUseHostCacheOnUnsupportedPool.valueIn(dest.getDataCenter().getId());
boolean preferHostCache = VirtualMachineManager.VmConfigDriveForceHostCacheUse.valueIn(dest.getDataCenter().getId());
final HandleConfigDriveIsoCommand configDriveIsoCommand = new HandleConfigDriveIsoCommand(isoPath, isoData, dataStore.getTO(), useHostCacheOnUnsupportedPool, preferHostCache, true);
final HandleConfigDriveIsoAnswer answer = (HandleConfigDriveIsoAnswer) agentManager.easySend(agentId, configDriveIsoCommand);
if (!answer.getResult()) {
throw new ResourceUnavailableException(String.format("Config drive iso creation failed, details: %s", answer.getDetails()), ConfigDriveNetworkElement.class, 0L);
}
profile.setConfigDriveLocation(answer.getConfigDriveLocation());
_userVmDetailsDao.addDetail(profile.getId(), VmDetailConstants.CONFIG_DRIVE_LOCATION, answer.getConfigDriveLocation().toString(), false);
addConfigDriveDisk(profile, dataStore);
return true;
}
use of com.cloud.agent.api.HandleConfigDriveIsoAnswer in project cloudstack by apache.
the class ConfigDriveNetworkElement method deleteConfigDriveIso.
private boolean deleteConfigDriveIso(final VirtualMachine vm) throws ResourceUnavailableException {
Long hostId = (vm.getHostId() != null) ? vm.getHostId() : vm.getLastHostId();
Location location = getConfigDriveLocation(vm.getId());
if (location == Location.HOST) {
return deleteConfigDriveIsoOnHostCache(vm, hostId);
}
Long agentId = null;
DataStore dataStore = null;
if (location == Location.SECONDARY) {
dataStore = _dataStoreMgr.getImageStoreWithFreeCapacity(vm.getDataCenterId());
agentId = findAgentIdForImageStore(dataStore);
} else if (location == Location.PRIMARY) {
List<VolumeVO> volumes = _volumeDao.findByInstanceAndType(vm.getId(), Volume.Type.ROOT);
if (volumes != null && volumes.size() > 0) {
dataStore = _dataStoreMgr.getDataStore(volumes.get(0).getPoolId(), DataStoreRole.Primary);
}
agentId = hostId;
}
if (agentId == null || dataStore == null) {
throw new ResourceUnavailableException("Config drive iso deletion failed, agent or datastore not available", ConfigDriveNetworkElement.class, 0L);
}
LOG.debug("Deleting config drive ISO for vm: " + vm.getInstanceName());
final String isoPath = ConfigDrive.createConfigDrivePath(vm.getInstanceName());
final HandleConfigDriveIsoCommand configDriveIsoCommand = new HandleConfigDriveIsoCommand(isoPath, null, dataStore.getTO(), false, false, false);
final HandleConfigDriveIsoAnswer answer = (HandleConfigDriveIsoAnswer) agentManager.easySend(agentId, configDriveIsoCommand);
if (!answer.getResult()) {
LOG.error("Failed to remove config drive for instance: " + vm.getInstanceName());
return false;
}
return true;
}
use of com.cloud.agent.api.HandleConfigDriveIsoAnswer in project cloudstack by apache.
the class LibvirtHandleConfigDriveCommandWrapper method execute.
@Override
public Answer execute(final HandleConfigDriveIsoCommand command, final LibvirtComputingResource libvirtComputingResource) {
String mountPoint = null;
try {
if (command.isCreate()) {
LOG.debug("Creating config drive: " + command.getIsoFile());
NetworkElement.Location location = NetworkElement.Location.PRIMARY;
if (command.isHostCachePreferred()) {
LOG.debug("Using the KVM host for config drive");
mountPoint = libvirtComputingResource.getConfigPath();
location = NetworkElement.Location.HOST;
} else {
final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
KVMStoragePool pool = null;
String poolUuid = null;
Storage.StoragePoolType poolType = null;
DataStoreTO dataStoreTO = command.getDestStore();
if (dataStoreTO != null) {
if (dataStoreTO instanceof PrimaryDataStoreTO) {
PrimaryDataStoreTO primaryDataStoreTO = (PrimaryDataStoreTO) dataStoreTO;
poolType = primaryDataStoreTO.getPoolType();
} else {
poolType = Storage.StoragePoolType.NetworkFilesystem;
}
poolUuid = command.getDestStore().getUuid();
pool = storagePoolMgr.getStoragePool(poolType, poolUuid);
}
if (pool == null || poolType == null) {
return new HandleConfigDriveIsoAnswer(command, "Unable to create config drive, Pool " + (poolUuid != null ? poolUuid : "") + " not found");
}
if (pool.supportsConfigDriveIso()) {
LOG.debug("Using the pool: " + poolUuid + " for config drive");
mountPoint = pool.getLocalPath();
} else if (command.getUseHostCacheOnUnsupportedPool()) {
LOG.debug("Config drive for KVM is not supported for pool type: " + poolType.toString() + ", using the KVM host");
mountPoint = libvirtComputingResource.getConfigPath();
location = NetworkElement.Location.HOST;
} else {
LOG.debug("Config drive for KVM is not supported for pool type: " + poolType.toString());
return new HandleConfigDriveIsoAnswer(command, "Config drive for KVM is not supported for pool type: " + poolType.toString());
}
}
Path isoPath = Paths.get(mountPoint, command.getIsoFile());
File isoFile = new File(mountPoint, command.getIsoFile());
if (command.getIsoData() == null) {
return new HandleConfigDriveIsoAnswer(command, "Invalid config drive ISO data received");
}
if (isoFile.exists()) {
LOG.debug("An old config drive iso already exists");
}
Files.createDirectories(isoPath.getParent());
ConfigDriveBuilder.base64StringToFile(command.getIsoData(), mountPoint, command.getIsoFile());
return new HandleConfigDriveIsoAnswer(command, location);
} else {
LOG.debug("Deleting config drive: " + command.getIsoFile());
Path configDrivePath = null;
if (command.isHostCachePreferred()) {
// Check and delete config drive in host storage if exists
mountPoint = libvirtComputingResource.getConfigPath();
configDrivePath = Paths.get(mountPoint, command.getIsoFile());
Files.deleteIfExists(configDrivePath);
} else {
final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
KVMStoragePool pool = null;
DataStoreTO dataStoreTO = command.getDestStore();
if (dataStoreTO != null) {
if (dataStoreTO instanceof PrimaryDataStoreTO) {
PrimaryDataStoreTO primaryDataStoreTO = (PrimaryDataStoreTO) dataStoreTO;
Storage.StoragePoolType poolType = primaryDataStoreTO.getPoolType();
pool = storagePoolMgr.getStoragePool(poolType, command.getDestStore().getUuid());
} else {
pool = storagePoolMgr.getStoragePool(Storage.StoragePoolType.NetworkFilesystem, command.getDestStore().getUuid());
}
}
if (pool != null && pool.supportsConfigDriveIso()) {
mountPoint = pool.getLocalPath();
configDrivePath = Paths.get(mountPoint, command.getIsoFile());
Files.deleteIfExists(configDrivePath);
}
}
return new HandleConfigDriveIsoAnswer(command);
}
} catch (final IOException e) {
LOG.debug("Failed to handle config drive due to " + e.getMessage(), e);
return new HandleConfigDriveIsoAnswer(command, "Failed due to exception: " + e.getMessage());
} catch (final CloudRuntimeException e) {
LOG.debug("Failed to handle config drive due to " + e.getMessage(), e);
return new HandleConfigDriveIsoAnswer(command, "Failed due to exception: " + e.toString());
}
}
use of com.cloud.agent.api.HandleConfigDriveIsoAnswer in project cloudstack by apache.
the class ConfigDriveNetworkElementTest method testAddPasswordAndUserData.
@Test
@SuppressWarnings("unchecked")
@PrepareForTest({ ConfigDriveBuilder.class, CallContext.class })
public void testAddPasswordAndUserData() throws Exception {
PowerMockito.mockStatic(ConfigDriveBuilder.class);
PowerMockito.mockStatic(CallContext.class);
PowerMockito.when(CallContext.current()).thenReturn(callContextMock);
Mockito.doReturn(Mockito.mock(Account.class)).when(callContextMock).getCallingAccount();
Method method = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("buildConfigDrive")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(Mockito.anyListOf(String[].class), Mockito.anyString(), Mockito.anyString()).thenReturn("content");
final HandleConfigDriveIsoAnswer answer = mock(HandleConfigDriveIsoAnswer.class);
final UserVmDetailVO userVmDetailVO = mock(UserVmDetailVO.class);
when(agentManager.easySend(anyLong(), any(HandleConfigDriveIsoCommand.class))).thenReturn(answer);
when(answer.getResult()).thenReturn(true);
when(answer.getConfigDriveLocation()).thenReturn(NetworkElement.Location.PRIMARY);
when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
when(virtualMachine.getState()).thenReturn(VirtualMachine.State.Stopped);
when(virtualMachine.getUuid()).thenReturn("vm-uuid");
when(userVmDetailVO.getValue()).thenReturn(PUBLIC_KEY);
when(nicp.getIPv4Address()).thenReturn("192.168.111.111");
when(_userVmDetailsDao.findDetail(anyLong(), anyString())).thenReturn(userVmDetailVO);
when(_ipAddressDao.findByAssociatedVmId(VMID)).thenReturn(publicIp);
when(publicIp.getAddress()).thenReturn(new Ip("7.7.7.7"));
when(_hostDao.findById(virtualMachine.getHostId())).thenReturn(hostVO);
when(_hostDao.findById(virtualMachine.getHostId()).getName()).thenReturn("dest-hyp-host-name");
Map<VirtualMachineProfile.Param, Object> parms = Maps.newHashMap();
parms.put(VirtualMachineProfile.Param.VmPassword, PASSWORD);
parms.put(VirtualMachineProfile.Param.VmSshPubKey, PUBLIC_KEY);
VirtualMachineProfile profile = new VirtualMachineProfileImpl(virtualMachine, null, serviceOfferingVO, null, parms);
profile.setConfigDriveLabel("testlabel");
assertTrue(_configDrivesNetworkElement.addPasswordAndUserdata(network, nicp, profile, deployDestination, null));
ArgumentCaptor<HandleConfigDriveIsoCommand> commandCaptor = ArgumentCaptor.forClass(HandleConfigDriveIsoCommand.class);
verify(agentManager, times(1)).easySend(anyLong(), commandCaptor.capture());
HandleConfigDriveIsoCommand createCommand = commandCaptor.getValue();
assertTrue(createCommand.isCreate());
assertTrue(createCommand.getIsoData().length() > 0);
assertTrue(createCommand.getIsoFile().equals(ConfigDrive.createConfigDrivePath(profile.getInstanceName())));
}
Aggregations