Search in sources :

Example 1 with SnapshotException

use of org.eclipse.che.api.machine.server.exception.SnapshotException in project che by eclipse.

the class JpaSnapshotDao method replaceSnapshots.

@Override
public List<SnapshotImpl> replaceSnapshots(String workspaceId, String envName, Collection<? extends SnapshotImpl> newSnapshots) throws SnapshotException {
    requireNonNull(workspaceId, "Required non-null workspace id");
    requireNonNull(envName, "Required non-null environment name");
    requireNonNull(newSnapshots, "Required non-null new snapshots");
    try {
        return doReplaceSnapshots(workspaceId, envName, newSnapshots);
    } catch (RuntimeException x) {
        throw new SnapshotException(x.getLocalizedMessage(), x);
    }
}
Also used : SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException)

Example 2 with SnapshotException

use of org.eclipse.che.api.machine.server.exception.SnapshotException in project che by eclipse.

the class WorkspaceRuntimesTest method removesNewlyCreatedSnapshotsWhenFailedToSaveTheirsMetadata.

@Test
public void removesNewlyCreatedSnapshotsWhenFailedToSaveTheirsMetadata() throws Exception {
    WorkspaceImpl workspace = newWorkspace("workspace", "env-name");
    setRuntime(workspace.getId(), WorkspaceStatus.RUNNING, "env-name");
    doThrow(new SnapshotException("test")).when(snapshotDao).replaceSnapshots(any(), any(), any());
    SnapshotImpl snapshot = mock(SnapshotImpl.class);
    when(envEngine.saveSnapshot(any(), any())).thenReturn(snapshot);
    try {
        runtimes.snapshot(workspace.getId());
    } catch (ServerException x) {
        assertEquals(x.getMessage(), "test");
    }
    verify(snapshotDao).replaceSnapshots(any(), any(), snapshotsCaptor.capture());
    verify(envEngine, times(snapshotsCaptor.getValue().size())).removeSnapshot(snapshot);
    verifyEventsSequence(event(workspace.getId(), WorkspaceStatus.RUNNING, WorkspaceStatus.SNAPSHOTTING, EventType.SNAPSHOT_CREATING, null), event(workspace.getId(), WorkspaceStatus.SNAPSHOTTING, WorkspaceStatus.RUNNING, EventType.SNAPSHOT_CREATION_ERROR, "test"));
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ServerException(org.eclipse.che.api.core.ServerException) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException) Test(org.testng.annotations.Test)

Example 3 with SnapshotException

use of org.eclipse.che.api.machine.server.exception.SnapshotException in project che by eclipse.

the class WorkspaceManagerTest method shouldRemoveMachinesSnapshotsEvenSomeRemovalFails.

@Test
public void shouldRemoveMachinesSnapshotsEvenSomeRemovalFails() throws Exception {
    // given
    String testWsId = "testWsId";
    String testNamespace = "testNamespace";
    WorkspaceImpl workspaceMock = mock(WorkspaceImpl.class);
    when(workspaceDao.get(testWsId)).thenReturn(workspaceMock);
    when(workspaceMock.getNamespace()).thenReturn(testNamespace);
    SnapshotImpl.SnapshotBuilder snapshotBuilder = SnapshotImpl.builder().generateId().setEnvName("env").setDev(true).setMachineName("machine1").setWorkspaceId(testWsId).setType("docker").setMachineSource(new MachineSourceImpl("image"));
    SnapshotImpl snapshot1 = snapshotBuilder.build();
    SnapshotImpl snapshot2 = snapshotBuilder.generateId().setDev(false).setMachineName("machine2").build();
    when(snapshotDao.findSnapshots(testWsId)).thenReturn(asList(snapshot1, snapshot2));
    doThrow(new SnapshotException("test")).when(snapshotDao).removeSnapshot(snapshot1.getId());
    // when
    workspaceManager.removeSnapshots(testWsId);
    // then
    captureExecuteCallsAndRunSynchronously();
    verify(runtimes).removeBinaries(singletonList(snapshot2));
    verify(snapshotDao).removeSnapshot(snapshot1.getId());
    verify(snapshotDao).removeSnapshot(snapshot2.getId());
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) Matchers.anyString(org.mockito.Matchers.anyString) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException) Test(org.testng.annotations.Test)

Example 4 with SnapshotException

use of org.eclipse.che.api.machine.server.exception.SnapshotException in project che by eclipse.

the class DockerInstanceProvider method removeInstanceSnapshot.

/**
     * Removes snapshot of the instance in implementation specific way.
     *
     * @param machineSource
     *         contains implementation specific key of the snapshot of the instance that should be removed
     * @throws SnapshotException
     *         if exception occurs on instance snapshot removal
     */
@Override
public void removeInstanceSnapshot(final MachineSource machineSource) throws SnapshotException {
    // use registry API directly because docker doesn't have such API yet
    // https://github.com/docker/docker-registry/issues/45
    final DockerMachineSource dockerMachineSource;
    try {
        dockerMachineSource = new DockerMachineSource(machineSource);
    } catch (MachineException e) {
        throw new SnapshotException(e);
    }
    if (!snapshotUseRegistry) {
        try {
            docker.removeImage(RemoveImageParams.create(dockerMachineSource.getLocation(false)));
        } catch (IOException ignore) {
        }
        return;
    }
    final String registry = dockerMachineSource.getRegistry();
    final String repository = dockerMachineSource.getRepository();
    if (registry == null || repository == null) {
        LOG.error("Failed to remove instance snapshot: invalid machine source: {}", dockerMachineSource);
        throw new SnapshotException("Snapshot removing failed. Snapshot attributes are not valid");
    }
    try {
        URL url = // TODO make possible to use https here
        UriBuilder.fromUri("http://" + registry).path("/v2/{repository}/manifests/{digest}").build(repository, dockerMachineSource.getDigest()).toURL();
        final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        try {
            conn.setConnectTimeout(30 * 1000);
            conn.setRequestMethod("DELETE");
            // TODO add auth header for secured registry
            // conn.setRequestProperty("Authorization", authHeader);
            final int responseCode = conn.getResponseCode();
            if ((responseCode / 100) != 2) {
                InputStream in = conn.getErrorStream();
                if (in == null) {
                    in = conn.getInputStream();
                }
                LOG.error("An error occurred while deleting snapshot with url: {}\nError stream: {}", url, IoUtil.readAndCloseQuietly(in));
                throw new SnapshotException("Internal server error occurs. Can't remove snapshot");
            }
        } finally {
            conn.disconnect();
        }
    } catch (IOException e) {
        LOG.error(e.getLocalizedMessage(), e);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) IOException(java.io.IOException) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException) URL(java.net.URL)

Example 5 with SnapshotException

use of org.eclipse.che.api.machine.server.exception.SnapshotException in project che by eclipse.

the class JpaSnapshotDao method getSnapshot.

@Override
@Transactional
public SnapshotImpl getSnapshot(String workspaceId, String envName, String machineName) throws NotFoundException, SnapshotException {
    requireNonNull(workspaceId, "Required non-null workspace id");
    requireNonNull(envName, "Required non-null environment name");
    requireNonNull(machineName, "Required non-null machine name");
    try {
        return managerProvider.get().createNamedQuery("Snapshot.getByMachine", SnapshotImpl.class).setParameter("workspaceId", workspaceId).setParameter("envName", envName).setParameter("machineName", machineName).getSingleResult();
    } catch (NoResultException x) {
        throw new NotFoundException(format("Snapshot for machine '%s:%s:%s' doesn't exist", workspaceId, envName, machineName));
    } catch (RuntimeException x) {
        throw new SnapshotException(x.getLocalizedMessage(), x);
    }
}
Also used : NotFoundException(org.eclipse.che.api.core.NotFoundException) NoResultException(javax.persistence.NoResultException) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException) Transactional(com.google.inject.persist.Transactional)

Aggregations

SnapshotException (org.eclipse.che.api.machine.server.exception.SnapshotException)6 NotFoundException (org.eclipse.che.api.core.NotFoundException)2 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)2 SnapshotImpl (org.eclipse.che.api.machine.server.model.impl.SnapshotImpl)2 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)2 Test (org.testng.annotations.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 Transactional (com.google.inject.persist.Transactional)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 String.format (java.lang.String.format)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator.comparing (java.util.Comparator.comparing)1 HashSet (java.util.HashSet)1 List (java.util.List)1