RunnerService extends Service { protected List<ApplicationProcessDescriptor> getRunningProcesses(String workspaceId, String project, String accountId) throws ForbiddenException, ServerException { if (project != null && !project.startsWith("/")) { project = '/' + project; } final List<ApplicationProcessDescriptor> processes = new LinkedList<>(); final User user = EnvironmentContext.getCurrent().getUser(); if (user == null) { return processes; } final String userId = user.getId(); if (accountId != null) { Optional<Member> owner = accountDao.getMembers(accountId) .stream() .filter(member -> userId.equals(member.getUserId()) && member.getRoles().contains("account/owner")) .findAny(); if (!owner.isPresent()) { throw new ForbiddenException("You are not an owner of the specified account"); } } for (RunQueueTask task : runQueue.getTasks()) { if (workspaceId != null && !task.getRequest().getWorkspace().equals(workspaceId)) { continue; } if (project != null && !task.getRequest().getProject().equals(project)) { continue; } if (!isRelatedToAccountOrUser(task, accountId, userId)) { continue; } try { processes.add(task.getDescriptor()); } catch (NotFoundException ignored) { } catch (RunnerException e) { LOG.error(e.getMessage(), e); } } return processes; } @Inject  RunnerService(RunQueue runQueue,
                         WorkspaceDao workspaceDao,
                         HttpJsonRequestFactory requestFactory,
                         AccountDao accountDao); @ApiOperation(value = "Run project", notes = "Run selected project", response = ApplicationProcessDescriptor.class, position = 1) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GenerateLink(rel = Constants.LINK_REL_RUN) @Path("/{ws-id}/run") @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) ApplicationProcessDescriptor run(@ApiParam(value = "Workspace ID", required = true)
                                            @PathParam("ws-id") String workspace,
                                            @ApiParam(value = "Project name", required = true)
                                            @Required @Description("project name") @QueryParam("project") String project,
                                            @ApiParam(value = "Run options")
                                            @Description("run options") RunOptions options); @ApiOperation(value = "Get run status", notes = "Get status of a selected run process", response = ApplicationProcessDescriptor.class, position = 2) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GET @Path("/{ws-id}/status/{id}") @Produces(MediaType.APPLICATION_JSON) ApplicationProcessDescriptor getStatus(@ApiParam(value = "Workspace ID", required = true)
                                                  @PathParam("ws-id") String workspace,
                                                  @ApiParam(value = "Run ID", required = true)
                                                  @PathParam("id") Long id); @ApiOperation(value = "Get all user running processes", notes = "Get info on all running processes in the scope of account id and project name", response = ApplicationProcessDescriptor.class, responseContainer = "List", position = 3) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GET @Path("/processes") @Produces(MediaType.APPLICATION_JSON) List<ApplicationProcessDescriptor> getAllRunningProcesses(@ApiParam(value = "Project name")
                                                                     @Description("project name")
                                                                     @QueryParam("project") String project,
                                                                     @ApiParam(value = "Account Id")
                                                                     @Description("account id")
                                                                     @QueryParam("account") String accountId); @ApiOperation(value = "Get run processes for a given workspace", notes = "Get info on all running processes for a given workspace", response = ApplicationProcessDescriptor.class, responseContainer = "List", position = 4) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GET @Path("/{ws-id}/processes") @Produces(MediaType.APPLICATION_JSON) List<ApplicationProcessDescriptor> getWorkspaceRunningProcesses(@ApiParam(value = "Workspace ID", required = true)
                                                                           @PathParam("ws-id") String workspace,
                                                                           @ApiParam(value = "Project name")
                                                                           @Description("project name")
                                                                           @QueryParam("project") String project); @ApiOperation(value = "Stop run process", notes = "Stop running process", response = ApplicationProcessDescriptor.class, position = 5) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Internal Server Error")}) @POST @Path("/{ws-id}/stop/{id}") @Produces(MediaType.APPLICATION_JSON) ApplicationProcessDescriptor stop(@ApiParam(value = "Workspace ID", required = true)
                                             @PathParam("ws-id") String workspace,
                                             @ApiParam(value = "Run ID", required = true)
                                             @PathParam("id") Long id); @ApiOperation(value = "Get logs", notes = "Get logs from a running application", position = 6) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GET @Path("/{ws-id}/logs/{id}") @Produces(MediaType.TEXT_PLAIN) void getLogs(@ApiParam(value = "Workspace ID", required = true)
                        @PathParam("ws-id") String workspace,
                        @ApiParam(value = "Run ID", required = true)
                        @PathParam("id") Long id, @Context HttpServletResponse httpServletResponse); @ApiOperation(value = "Get available RAM resources", notes = "Get RAM resources of a workspace: used and free RAM", response = ResourcesDescriptor.class, position = 7) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GET @Path("/{ws-id}/resources") @Produces(MediaType.APPLICATION_JSON) ResourcesDescriptor getResources(@ApiParam(value = "Workspace ID", required = true)
                                            @PathParam("ws-id") String workspace); @ApiOperation(value = "Get available runner environments", notes = "Get available runner environments", response = RunnerEnvironmentTree.class, position = 8) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GenerateLink(rel = Constants.LINK_REL_AVAILABLE_RUNNERS) @GET @Path("/{ws-id}/available") @Produces(MediaType.APPLICATION_JSON) RunnerEnvironmentTree getRunnerEnvironments(@ApiParam(value = "Workspace ID", required = true)
                                                       @PathParam("ws-id") String workspace,
                                                       @ApiParam(value = "Project name")
                                                       @Description("project name") @QueryParam("project") String project); @ApiOperation(value = "Get runtime recipe", notes = "Get content of a Dockerfile used to 'cook' runtime environment", position = 9) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GET @Path("/{ws-id}/recipe/{id}") void getRecipeFile(@ApiParam(value = "Workspace ID", required = true)
                              @PathParam("ws-id") String workspace,
                              @ApiParam(value = "Run ID", required = true)
                              @PathParam("id") Long id,
                              @Context HttpServletResponse httpServletResponse); @ApiOperation(value = "Get recipe", notes = "Get content of a Dockerfile", position = 10) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Not found"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GenerateLink(rel = Constants.LINK_REL_GET_RECIPE) @GET @Path("/{ws-id}/recipe") @Produces(MediaType.TEXT_PLAIN) String getRecipe(@QueryParam("id") String id);  }
@Test public void testGetRunningProcesses() throws Exception { User user = new UserImpl("florent", "id-2314", "token-2323", Collections.<String>emptyList(), false); User dummy = new UserImpl("dummy", "id-2315", "token-2324", Collections.<String>emptyList(), false); EnvironmentContext context = EnvironmentContext.getCurrent(); context.setUser(user); String workspaceFlorent = "workspaceFlorent"; String workspaceCodenvy = "workspaceCodenvy"; String projectA = "/projectA"; String projectFlorentA = projectA; String projectFlorentB = "projectB"; String projectCodenvyA = projectA; List<RunQueueTask> tasks = new ArrayList<>(4); tasks.add(createTask(user, workspaceFlorent, projectFlorentA)); tasks.add(createTask(user, workspaceFlorent, projectFlorentB)); tasks.add(createTask(user, workspaceCodenvy, projectCodenvyA)); tasks.add(createTask(dummy, "dummyWorkspace", "dummyProject")); doReturn(tasks).when(runQueue).getTasks(); List<ApplicationProcessDescriptor> processes = service.getRunningProcesses(null, null, null); assertEquals(processes.size(), 3); processes = service.getRunningProcesses(workspaceCodenvy, null, null); assertEquals(processes.size(), 1); ApplicationProcessDescriptor process = processes.get(0); assertEquals(process.getWorkspace(), workspaceCodenvy); assertEquals(process.getProject(), projectCodenvyA); processes = service.getRunningProcesses(workspaceCodenvy, projectA, null); assertEquals(processes.size(), 1); processes = service.getRunningProcesses(null, projectA, null); assertEquals(processes.size(), 2); processes = service.getRunningProcesses("dummy", null, null); assertEquals(processes.size(), 0); processes = service.getRunningProcesses(null, "project", null); assertEquals(processes.size(), 0); processes = service.getRunningProcesses("dummy", "project", null); assertEquals(processes.size(), 0); }
@Test public void shouldGetRunningProcessWithAccount() throws Exception { User user = new UserImpl("User", "id-2314", "token-2323", Collections.<String>emptyList(), false); EnvironmentContext context = EnvironmentContext.getCurrent(); context.setUser(user); Workspace workspace = mock(Workspace.class); Workspace dummyWorkspace = mock(Workspace.class); Member member = mock(Member.class); List<RunQueueTask> tasks = Collections.singletonList(createTask(user, "workspaceId", "project")); doReturn(tasks).when(runQueue).getTasks(); doReturn(Collections.singletonList(member)).when(accountDao).getMembers("accountId"); doReturn("id-2314").when(member).getUserId(); doReturn(Collections.singletonList("account/owner")).when(member).getRoles(); doReturn("accountId").when(workspace).getAccountId(); doReturn("dummyAccountId").when(dummyWorkspace).getAccountId(); doReturn(workspace).when(workspaceDao).getById("workspaceId"); doReturn(dummyWorkspace).when(workspaceDao).getById("dummyWorkspace"); List<ApplicationProcessDescriptor> processes = service.getRunningProcesses(null, null, "accountId"); assertEquals(processes.size(), 1); }
@Test(expectedExceptions = ForbiddenException.class) public void shouldThrowForbiddenExceptionWhenIsNotAnAccountOwner() throws Exception { User user = new UserImpl("User", "id-2314", "token-2323", Collections.<String>emptyList(), false); EnvironmentContext context = EnvironmentContext.getCurrent(); context.setUser(user); Member member = mock(Member.class); List<RunQueueTask> tasks = Collections.singletonList(createTask(user, "workspaceId", "project")); doReturn(tasks).when(runQueue).getTasks(); doReturn(Collections.singletonList(member)).when(accountDao).getMembers("accountId"); doReturn("id-2314").when(member).getUserId(); doReturn(Collections.singletonList("account/member")).when(member).getRoles(); service.getRunningProcesses(null, null, "accountId"); }