Search in sources :

Example 6 with HandleConfigDriveIsoAnswer

use of com.cloud.agent.api.HandleConfigDriveIsoAnswer in project cloudstack by apache.

the class NfsSecondaryStorageResource method execute.

private Answer execute(HandleConfigDriveIsoCommand cmd) {
    if (cmd.isCreate()) {
        if (cmd.getIsoData() == null) {
            return new HandleConfigDriveIsoAnswer(cmd, "Invalid config drive ISO data");
        }
        String nfsMountPoint = getRootDir(cmd.getDestStore().getUrl(), _nfsVersion);
        File isoFile = new File(nfsMountPoint, cmd.getIsoFile());
        if (isoFile.exists()) {
            s_logger.debug("config drive iso already exists");
        }
        Path tempDir = null;
        try {
            tempDir = java.nio.file.Files.createTempDirectory(ConfigDrive.CONFIGDRIVEDIR);
            File tmpIsoFile = ConfigDriveBuilder.base64StringToFile(cmd.getIsoData(), tempDir.toAbsolutePath().toString(), cmd.getIsoFile());
            copyLocalToNfs(tmpIsoFile, new File(cmd.getIsoFile()), cmd.getDestStore());
        } catch (IOException | ConfigurationException e) {
            return new HandleConfigDriveIsoAnswer(cmd, "Failed due to exception: " + e.getMessage());
        } finally {
            try {
                if (tempDir != null) {
                    FileUtils.deleteDirectory(tempDir.toFile());
                }
            } catch (IOException ioe) {
                s_logger.warn("Failed to delete ConfigDrive temporary directory: " + tempDir.toString(), ioe);
            }
        }
        return new HandleConfigDriveIsoAnswer(cmd, NetworkElement.Location.SECONDARY, "Successfully saved config drive at secondary storage");
    } else {
        DataStoreTO dstore = cmd.getDestStore();
        if (dstore instanceof NfsTO) {
            NfsTO nfs = (NfsTO) dstore;
            String relativeTemplatePath = new File(cmd.getIsoFile()).getPath();
            String nfsMountPoint = getRootDir(nfs.getUrl(), _nfsVersion);
            File tmpltPath = new File(nfsMountPoint, relativeTemplatePath);
            try {
                Files.deleteIfExists(tmpltPath.toPath());
            } catch (IOException e) {
                return new HandleConfigDriveIsoAnswer(cmd, e);
            }
            return new HandleConfigDriveIsoAnswer(cmd);
        } else {
            return new HandleConfigDriveIsoAnswer(cmd, "Not implemented yet");
        }
    }
}
Also used : Path(java.nio.file.Path) HandleConfigDriveIsoAnswer(com.cloud.agent.api.HandleConfigDriveIsoAnswer) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) ConfigurationException(javax.naming.ConfigurationException) IOException(java.io.IOException) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) NfsTO(com.cloud.agent.api.to.NfsTO)

Example 7 with HandleConfigDriveIsoAnswer

use of com.cloud.agent.api.HandleConfigDriveIsoAnswer in project cloudstack by apache.

the class ConfigDriveNetworkElement method deleteConfigDriveIsoOnHostCache.

private boolean deleteConfigDriveIsoOnHostCache(final VirtualMachine vm, final Long hostId) throws ResourceUnavailableException {
    if (hostId == null) {
        throw new ResourceUnavailableException("Config drive iso deletion failed, host not available", ConfigDriveNetworkElement.class, 0L);
    }
    LOG.debug("Deleting config drive ISO for vm: " + vm.getInstanceName() + " on host: " + hostId);
    final String isoPath = ConfigDrive.createConfigDrivePath(vm.getInstanceName());
    final HandleConfigDriveIsoCommand configDriveIsoCommand = new HandleConfigDriveIsoCommand(isoPath, null, null, false, true, false);
    final HandleConfigDriveIsoAnswer answer = (HandleConfigDriveIsoAnswer) agentManager.easySend(hostId, configDriveIsoCommand);
    if (answer == null) {
        throw new CloudRuntimeException("Unable to get an answer to handle config drive deletion for vm: " + vm.getInstanceName() + " on host: " + hostId);
    }
    if (!answer.getResult()) {
        LOG.error("Failed to remove config drive for instance: " + vm.getInstanceName());
        return false;
    }
    return true;
}
Also used : HandleConfigDriveIsoAnswer(com.cloud.agent.api.HandleConfigDriveIsoAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) HandleConfigDriveIsoCommand(com.cloud.agent.api.HandleConfigDriveIsoCommand)

Example 8 with HandleConfigDriveIsoAnswer

use of com.cloud.agent.api.HandleConfigDriveIsoAnswer in project cloudstack by apache.

the class ConfigDriveNetworkElementTest method testExpunge.

@Test
@SuppressWarnings("unchecked")
public void testExpunge() throws NoTransitionException, NoSuchFieldException, IllegalAccessException {
    final StateMachine2<VirtualMachine.State, VirtualMachine.Event, VirtualMachine> stateMachine = VirtualMachine.State.getStateMachine();
    final Field listenersField = StateMachine2.class.getDeclaredField("_listeners");
    listenersField.setAccessible(true);
    List<StateListener<VirtualMachine.State, VirtualMachine.Event, VirtualMachine>> listeners = (List<StateListener<VirtualMachine.State, VirtualMachine.Event, VirtualMachine>>) listenersField.get(stateMachine);
    listeners.clear();
    _configDrivesNetworkElement.start();
    when(virtualMachine.getState()).thenReturn(VirtualMachine.State.Stopped);
    when(_vmInstanceDao.updateState(VirtualMachine.State.Stopped, VirtualMachine.Event.ExpungeOperation, VirtualMachine.State.Expunging, virtualMachine, null)).thenReturn(true);
    final HandleConfigDriveIsoAnswer answer = mock(HandleConfigDriveIsoAnswer.class);
    when(agentManager.easySend(anyLong(), any(HandleConfigDriveIsoCommand.class))).thenReturn(answer);
    when(answer.getResult()).thenReturn(true);
    stateMachine.transitTo(virtualMachine, VirtualMachine.Event.ExpungeOperation, null, _vmInstanceDao);
    ArgumentCaptor<HandleConfigDriveIsoCommand> commandCaptor = ArgumentCaptor.forClass(HandleConfigDriveIsoCommand.class);
    verify(agentManager, times(1)).easySend(anyLong(), commandCaptor.capture());
    HandleConfigDriveIsoCommand deleteCommand = commandCaptor.getValue();
    assertThat(deleteCommand.isCreate(), is(false));
}
Also used : Field(java.lang.reflect.Field) HandleConfigDriveIsoAnswer(com.cloud.agent.api.HandleConfigDriveIsoAnswer) HandleConfigDriveIsoCommand(com.cloud.agent.api.HandleConfigDriveIsoCommand) StateListener(com.cloud.utils.fsm.StateListener) List(java.util.List) VirtualMachine(com.cloud.vm.VirtualMachine) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

HandleConfigDriveIsoAnswer (com.cloud.agent.api.HandleConfigDriveIsoAnswer)8 HandleConfigDriveIsoCommand (com.cloud.agent.api.HandleConfigDriveIsoCommand)6 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)2 File (java.io.File)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 List (java.util.List)2 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 NfsTO (com.cloud.agent.api.to.NfsTO)1 KVMStoragePool (com.cloud.hypervisor.kvm.storage.KVMStoragePool)1 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)1 NetworkElement (com.cloud.network.element.NetworkElement)1 Storage (com.cloud.storage.Storage)1 Account (com.cloud.user.Account)1 StateListener (com.cloud.utils.fsm.StateListener)1 Ip (com.cloud.utils.net.Ip)1