RunnerService extends Service { @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) public RunnerEnvironmentTree getRunnerEnvironments(@ApiParam(value = "Workspace ID", required = true) @PathParam("ws-id") String workspace, @ApiParam(value = "Project name") @Description("project name") @QueryParam("project") String project) { final DtoFactory dtoFactory = DtoFactory.getInstance(); final RunnerEnvironmentTree root = dtoFactory.createDto(RunnerEnvironmentTree.class).withDisplayName("system"); final List<RemoteRunnerServer> registerRunnerServers = runQueue.getRegisterRunnerServers(); for (Iterator<RemoteRunnerServer> itr = registerRunnerServers.iterator(); itr.hasNext(); ) { final RemoteRunnerServer runnerServer = itr.next(); if (!runnerServer.isAvailable()) { LOG.error("Runner server {} becomes unavailable", runnerServer.getBaseUrl()); itr.remove(); } } for (RemoteRunnerServer runnerServer : registerRunnerServers) { final String assignedWorkspace; final String assignedProject; try { assignedWorkspace = runnerServer.getAssignedWorkspace(); assignedProject = runnerServer.getAssignedProject(); } catch (RunnerException e) { LOG.error(e.getMessage(), e); continue; } if (((assignedWorkspace != null && assignedWorkspace.equals(workspace)) || assignedWorkspace == null) && ((assignedProject != null && assignedProject.equals(project)) || assignedProject == null)) { final List<RunnerDescriptor> runners; try { runners = runnerServer.getRunnerDescriptors(); } catch (RunnerException e) { LOG.error(e.getMessage(), e); continue; } for (RunnerDescriptor runnerDescriptor : runners) { for (RunnerEnvironment runnerEnvironment : runnerDescriptor.getEnvironments()) { RunnerEnvironmentTree node = root; for (String s : runnerDescriptor.getName().split("/")) { RunnerEnvironmentTree child = node.getNode(s); if (child == null) { child = dtoFactory.createDto(RunnerEnvironmentTree.class).withDisplayName(s); node.addNode(child); } node = child; } final String unique = new EnvironmentId(EnvironmentId.Scope.system, runnerDescriptor.getName(), runnerEnvironment.getId()) .toString(); final String envId = runnerEnvironment.getId(); final RunnerEnvironmentLeaf environment = node.getEnvironment(envId); if (environment == null) { node.addLeaf(dtoFactory.createDto(RunnerEnvironmentLeaf.class) .withDisplayName(envId) .withEnvironment(dtoFactory.clone(runnerEnvironment).withId(unique))); } } } } } return root; } @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 testGetRunnerEnvironments() throws Exception { List<RemoteRunnerServer> servers = new ArrayList<>(1); RemoteRunnerServer server1 = mock(RemoteRunnerServer.class); doReturn(true).when(server1).isAvailable(); servers.add(server1); List<RunnerDescriptor> runners1 = new ArrayList<>(1); RunnerDescriptor runner1 = dto(RunnerDescriptor.class).withName("java/web"); runner1.getEnvironments().add(dto(RunnerEnvironment.class).withId("tomcat7")); runner1.getEnvironments().add(dto(RunnerEnvironment.class).withId("jboss7")); runners1.add(runner1); RemoteRunnerServer server2 = mock(RemoteRunnerServer.class); doReturn(true).when(server2).isAvailable(); servers.add(server2); List<RunnerDescriptor> runners2 = new ArrayList<>(1); RunnerDescriptor runner2 = dto(RunnerDescriptor.class).withName("java/web"); runner2.getEnvironments().add(dto(RunnerEnvironment.class).withId("tomcat7")); runner2.getEnvironments().add(dto(RunnerEnvironment.class).withId("jboss7")); runners2.add(runner2); doReturn(runners1).when(server1).getRunnerDescriptors(); doReturn(runners2).when(server2).getRunnerDescriptors(); doReturn(servers).when(runQueue).getRegisterRunnerServers(); RunnerEnvironmentTree system = service.getRunnerEnvironments(null, null); assertEquals(system.getDisplayName(), "system"); assertEquals(system.getLeaves().size(), 0); List<RunnerEnvironmentTree> nodes = system.getNodes(); assertEquals(nodes.size(), 1); RunnerEnvironmentTree java = system.getNode("java"); assertNotNull(java); assertEquals(java.getDisplayName(), "java"); nodes = java.getNodes(); assertEquals(nodes.size(), 1); assertEquals(java.getLeaves().size(), 0); RunnerEnvironmentTree web = java.getNode("web"); assertNotNull(web); assertEquals(web.getNodes().size(), 0); assertEquals(web.getLeaves().size(), 2); RunnerEnvironmentLeaf tomcat7 = web.getEnvironment("tomcat7"); assertNotNull(tomcat7); RunnerEnvironmentLeaf jboss7 = web.getEnvironment("jboss7"); assertNotNull(jboss7); RunnerEnvironment tomcat7Environment = tomcat7.getEnvironment(); RunnerEnvironment jboss7Environment = jboss7.getEnvironment(); assertNotNull(tomcat7Environment); assertNotNull(jboss7Environment); assertEquals(tomcat7Environment.getId(), "system:/java/web/tomcat7"); assertEquals(jboss7Environment.getId(), "system:/java/web/jboss7"); }
@Test public void testGetRunnerEnvironmentsIfOneServerUnavailable() throws Exception { List<RemoteRunnerServer> servers = new ArrayList<>(2); RemoteRunnerServer server1 = mock(RemoteRunnerServer.class); doReturn(true).when(server1).isAvailable(); servers.add(server1); List<RunnerDescriptor> runners = new ArrayList<>(1); RunnerDescriptor runner1 = dto(RunnerDescriptor.class).withName("java/web"); runner1.getEnvironments().add(dto(RunnerEnvironment.class).withId("tomcat7")); runner1.getEnvironments().add(dto(RunnerEnvironment.class).withId("jboss7")); runners.add(runner1); doReturn(runners).when(server1).getRunnerDescriptors(); RemoteRunnerServer server2 = mock(RemoteRunnerServer.class); doReturn(true).when(server2).isAvailable(); doThrow(new RunnerException("Connection refused")).when(server2).getRunnerDescriptors(); servers.add(server2); doReturn(servers).when(runQueue).getRegisterRunnerServers(); RunnerEnvironmentTree system = service.getRunnerEnvironments(null, null); assertEquals(system.getDisplayName(), "system"); assertEquals(system.getLeaves().size(), 0); List<RunnerEnvironmentTree> nodes = system.getNodes(); assertEquals(nodes.size(), 1); RunnerEnvironmentTree java = system.getNode("java"); assertNotNull(java); assertEquals(java.getDisplayName(), "java"); nodes = java.getNodes(); assertEquals(nodes.size(), 1); assertEquals(java.getLeaves().size(), 0); RunnerEnvironmentTree web = java.getNode("web"); assertNotNull(web); assertEquals(web.getNodes().size(), 0); assertEquals(web.getLeaves().size(), 2); RunnerEnvironmentLeaf tomcat7 = web.getEnvironment("tomcat7"); assertNotNull(tomcat7); RunnerEnvironmentLeaf jboss7 = web.getEnvironment("jboss7"); assertNotNull(jboss7); RunnerEnvironment tomcat7Environment = tomcat7.getEnvironment(); RunnerEnvironment jboss7Environment = jboss7.getEnvironment(); assertNotNull(tomcat7Environment); assertNotNull(jboss7Environment); assertEquals(tomcat7Environment.getId(), "system:/java/web/tomcat7"); assertEquals(jboss7Environment.getId(), "system:/java/web/jboss7"); }
@Test public void testFilerEnvironmentsByWorkspaceGetAll() throws Exception { createRunners(); RunnerEnvironmentTree system = service.getRunnerEnvironments(null, null); assertEquals(system.getDisplayName(), "system"); assertEquals(system.getLeaves().size(), 0); List<RunnerEnvironmentTree> nodes = system.getNodes(); assertEquals(nodes.size(), 1); RunnerEnvironmentTree java = system.getNode("java"); assertNotNull(java); RunnerEnvironmentTree web = java.getNode("web"); assertNotNull(web); assertEquals(web.getNodes().size(), 0); assertEquals(web.getLeaves().size(), 4); RunnerEnvironmentLeaf tomcat7 = web.getEnvironment("tomcat7"); assertNotNull(tomcat7); RunnerEnvironmentLeaf jboss7 = web.getEnvironment("jboss7"); assertNotNull(jboss7); RunnerEnvironmentLeaf _tomcat7 = web.getEnvironment("my_tomcat7"); assertNotNull(_tomcat7); RunnerEnvironmentLeaf _jboss7 = web.getEnvironment("my_jboss7"); assertNotNull(_jboss7); }
@Test public void testFilerEnvironmentsByWorkspaceRestricted() throws Exception { List<RemoteRunnerServer> servers = createRunners(); doReturn("my").when(servers.get(0)).getAssignedWorkspace(); RunnerEnvironmentTree system = service.getRunnerEnvironments(null, null); assertEquals(system.getDisplayName(), "system"); assertEquals(system.getLeaves().size(), 0); List<RunnerEnvironmentTree> nodes = system.getNodes(); assertEquals(nodes.size(), 1); RunnerEnvironmentTree java = system.getNode("java"); assertNotNull(java); RunnerEnvironmentTree web = java.getNode("web"); assertNotNull(web); assertEquals(web.getNodes().size(), 0); assertEquals(web.getLeaves().size(), 2); RunnerEnvironmentLeaf tomcat7 = web.getEnvironment("tomcat7"); assertNotNull(tomcat7); RunnerEnvironmentLeaf jboss7 = web.getEnvironment("jboss7"); assertNotNull(jboss7); }