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);
}
}
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"));
}
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());
}
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);
}
}
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);
}
}
Aggregations