use of org.eclipse.che.api.machine.server.spi.Instance in project che by eclipse.
the class CheEnvironmentEngine method startMachine.
/**
* Starts machine in running environment.
*
* @param workspaceId
* ID of workspace that owns environment in which machine should be started
* @param machineConfig
* configuration of machine that should be started
* @return running machine
* @throws EnvironmentNotRunningException
* if environment is not running
* @throws NotFoundException
* if provider of machine implementation is not found
* @throws ConflictException
* if machine with the same name already exists in the environment
* @throws ServerException
* if any other error occurs
*/
public Instance startMachine(String workspaceId, MachineConfig machineConfig, List<String> agents) throws ServerException, NotFoundException, ConflictException, EnvironmentException {
MachineConfig machineConfigCopy = new MachineConfigImpl(machineConfig);
EnvironmentHolder environmentHolder;
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.readLock(workspaceId)) {
environmentHolder = environments.get(workspaceId);
if (environmentHolder == null || environmentHolder.status != EnvStatus.RUNNING) {
throw new EnvironmentNotRunningException(format("Environment '%s' is not running", workspaceId));
}
for (Instance machine : environmentHolder.machines) {
if (machine.getConfig().getName().equals(machineConfigCopy.getName())) {
throw new ConflictException(format("Machine with name '%s' already exists in environment of workspace '%s'", machineConfigCopy.getName(), workspaceId));
}
}
}
final String creator = EnvironmentContext.getCurrent().getSubject().getUserId();
final String namespace = EnvironmentContext.getCurrent().getSubject().getUserName();
MachineImpl machine = MachineImpl.builder().setConfig(machineConfig).setWorkspaceId(workspaceId).setStatus(MachineStatus.CREATING).setEnvName(environmentHolder.name).setOwner(creator).build();
MachineStarter machineStarter;
if ("docker".equals(machineConfig.getType())) {
// needed to reuse startInstance method and
// create machine instances by different implementation-specific providers
CheServiceImpl service = machineConfigToService(machineConfig);
normalize(namespace, workspaceId, machineConfig.getName(), service);
machine.setId(service.getId());
machineStarter = (machineLogger, machineSource) -> {
CheServiceImpl serviceWithNormalizedSource = normalizeServiceSource(service, machineSource);
normalize(namespace, workspaceId, machineConfig.getName(), serviceWithNormalizedSource);
infrastructureProvisioner.provision(new ExtendedMachineImpl().withAgents(agents), serviceWithNormalizedSource);
return machineProvider.startService(namespace, workspaceId, environmentHolder.name, machineConfig.getName(), machineConfig.isDev(), environmentHolder.networkId, serviceWithNormalizedSource, machineLogger);
};
} else {
try {
InstanceProvider provider = machineInstanceProviders.getProvider(machineConfig.getType());
machine.setId(generateMachineId());
addAgentsProvidedServers(machine, agents);
machineStarter = (machineLogger, machineSource) -> {
Machine machineWithNormalizedSource = normalizeMachineSource(machine, machineSource);
return provider.createInstance(machineWithNormalizedSource, machineLogger);
};
} catch (NotFoundException e) {
throw new NotFoundException(format("Provider of machine type '%s' not found", machineConfig.getType()));
}
}
return startInstance(false, environmentHolder.logger, machine, machineStarter);
}
use of org.eclipse.che.api.machine.server.spi.Instance in project che by eclipse.
the class CheEnvironmentEngine method stop.
/**
* Stops running environment of specified workspace.
*
* @param workspaceId
* ID of workspace that owns environment
* @throws EnvironmentNotRunningException
* when environment is not running
* @throws ServerException
* if other error occurs
*/
public void stop(String workspaceId) throws EnvironmentNotRunningException, ServerException {
List<Instance> machinesCopy;
EnvironmentHolder environmentHolder;
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.readLock(workspaceId)) {
environmentHolder = environments.get(workspaceId);
if (environmentHolder == null || environmentHolder.status != EnvStatus.RUNNING) {
throw new EnvironmentNotRunningException(format("Stop of not running environment of workspace with ID '%s' is not allowed.", workspaceId));
}
List<Instance> machines = environmentHolder.machines;
if (machines != null && !machines.isEmpty()) {
machinesCopy = new ArrayList<>(machines);
} else {
machinesCopy = emptyList();
}
}
// long operation - perform out of lock
destroyEnvironment(environmentHolder.networkId, machinesCopy);
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(workspaceId)) {
environments.remove(workspaceId);
}
}
use of org.eclipse.che.api.machine.server.spi.Instance in project che by eclipse.
the class CheEnvironmentEngine method stopMachine.
/**
* Stops machine in running environment.
*
* @param workspaceId
* ID of workspace of environment that owns machine
* @param machineId
* ID of machine that should be stopped
* @throws NotFoundException
* if machine in not found in environment
* @throws EnvironmentNotRunningException
* if environment is not running
* @throws ConflictException
* if stop of dev machine is requested
* @throws ServerException
* if other error occurs
*/
public void stopMachine(String workspaceId, String machineId) throws NotFoundException, ServerException, ConflictException {
Instance targetMachine = null;
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(workspaceId)) {
EnvironmentHolder environmentHolder = environments.get(workspaceId);
if (environmentHolder == null || environmentHolder.status != EnvStatus.RUNNING) {
throw new EnvironmentNotRunningException(format("Environment '%s' is not running", workspaceId));
}
for (Instance machine : environmentHolder.machines) {
if (machine.getId().equals(machineId)) {
if (machine.getConfig().isDev()) {
throw new ConflictException("Stop of dev machine is not allowed. Please, stop whole environment");
}
targetMachine = machine;
break;
}
}
environmentHolder.machines.remove(targetMachine);
}
if (targetMachine == null) {
throw new NotFoundException(format("Machine with ID '%s' is not found in environment of workspace '%s'", machineId, workspaceId));
}
// out of lock to prevent blocking by potentially long-running method
destroyMachine(targetMachine);
}
use of org.eclipse.che.api.machine.server.spi.Instance in project che by eclipse.
the class WorkspaceRuntimes method startMachine.
/**
* Starts machine in running workspace.
*
* @param workspaceId
* ID of workspace that owns machine
* @param machineConfig
* config of machine that should be started
* @return running machine
* @throws ConflictException
* if environment is not running or conflicting machine already exists in the environment
* @throws ConflictException
* if environment was stopped during start of machine
* @throws ServerException
* if any other error occurs
*/
public Instance startMachine(String workspaceId, MachineConfig machineConfig) throws ServerException, ConflictException, NotFoundException, EnvironmentException {
try (@SuppressWarnings("unused") Unlocker u = locks.readLock(workspaceId)) {
getRunningState(workspaceId);
}
// Copy constructor makes deep copy of objects graph
// which means that original values won't affect the values in used further in this class
MachineConfigImpl machineConfigCopy = new MachineConfigImpl(machineConfig);
List<String> agents = Collections.singletonList("org.eclipse.che.terminal");
Instance instance = envEngine.startMachine(workspaceId, machineConfigCopy, agents);
launchAgents(instance, agents);
try (@SuppressWarnings("unused") Unlocker u = locks.writeLock(workspaceId)) {
checkIsNotTerminated("start the machine");
RuntimeState workspaceState = states.get(workspaceId);
if (workspaceState == null || workspaceState.status != RUNNING) {
try {
envEngine.stopMachine(workspaceId, instance.getId());
} catch (NotFoundException | ServerException | ConflictException e) {
LOG.error(e.getLocalizedMessage(), e);
}
throw new ConflictException(format("Environment of workspace '%s' was stopped during start of machine", workspaceId));
}
}
return instance;
}
use of org.eclipse.che.api.machine.server.spi.Instance in project che by eclipse.
the class CheEnvironmentEngine method saveSnapshot.
/**
* Saves machine into snapshot.
*
* @param workspaceId
* ID of workspace that owns environment
* @param machineId
* ID of machine to save
* @return snapshot
* @throws EnvironmentNotRunningException
* if environment of machine is not running
* @throws NotFoundException
* if machine is not running
* @throws ServerException
* if another error occurs
*/
public SnapshotImpl saveSnapshot(String workspaceId, String machineId) throws ServerException, NotFoundException {
EnvironmentHolder environmentHolder;
SnapshotImpl snapshot = null;
Instance instance = null;
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.readLock(workspaceId)) {
environmentHolder = environments.get(workspaceId);
if (environmentHolder == null || environmentHolder.status != EnvStatus.RUNNING) {
throw new EnvironmentNotRunningException(format("Environment '%s' is not running", workspaceId));
}
for (Instance machine : environmentHolder.machines) {
if (machine.getId().equals(machineId)) {
instance = machine;
snapshot = SnapshotImpl.builder().generateId().setType(machine.getConfig().getType()).setWorkspaceId(machine.getWorkspaceId()).setDescription(machine.getEnvName()).setDev(machine.getConfig().isDev()).setEnvName(machine.getEnvName()).setMachineName(machine.getConfig().getName()).useCurrentCreationDate().build();
}
}
}
if (instance == null) {
throw new NotFoundException(format("Machine with id '%s' is not found in environment of workspace '%s'", machineId, workspaceId));
}
try {
MachineSource machineSource = instance.saveToSnapshot();
snapshot.setMachineSource(new MachineSourceImpl(machineSource));
return snapshot;
} catch (ServerException e) {
try {
instance.getLogger().writeLine("Snapshot storing failed. " + e.getLocalizedMessage());
} catch (IOException ignore) {
}
throw e;
}
}
Aggregations