@GET @Produces(MediaType.APPLICATION_JSON) public Response getAllSlotsStatus(@Context UriInfo uriInfo) { List<SlotStatusRepresentation> representations = Lists.newArrayList(); for (Slot slot : agent.getAllSlots()) { SlotStatus slotStatus = slot.status(); representations.add(SlotStatusRepresentation.from(slotStatus)); } return Response.ok(representations) .header(GALAXY_AGENT_VERSION_HEADER, agent.getAgentStatus().getVersion()) .build(); }
@Path("{slotId}") @GET @Produces(MediaType.APPLICATION_JSON) public Response getSlotStatus(@PathParam("slotId") UUID slotId, @Context UriInfo uriInfo) { Preconditions.checkNotNull(slotId, "slotId must not be null"); Slot slot = agent.getSlot(slotId); if (slot == null) { return Response.status(Response.Status.NOT_FOUND).build(); } SlotStatus slotStatus = slot.status(); return Response.ok(SlotStatusRepresentation.from(slotStatus)) .header(GALAXY_AGENT_VERSION_HEADER, agent.getAgentStatus().getVersion()) .header(GALAXY_SLOT_VERSION_HEADER, slotStatus.getVersion()) .build(); }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response installSlot(@HeaderParam(GALAXY_AGENT_VERSION_HEADER) String agentVersion, InstallationRepresentation installation, @Context UriInfo uriInfo) { Preconditions.checkNotNull(installation, "installation must not be null"); checkAgentVersion(agent.getAgentStatus(), agentVersion); SlotStatus slotStatus = agent.install(installation.toInstallation()); return Response .created(getSelfUri(slotStatus.getId(), uriInfo.getBaseUri())) .entity(SlotStatusRepresentation.from(slotStatus)) .header(GALAXY_AGENT_VERSION_HEADER, agent.getAgentStatus().getVersion()) .header(GALAXY_SLOT_VERSION_HEADER, slotStatus.getVersion()) .build(); }
@Override public boolean ssh(SlotFilter slotFilter, String command) { // build predicate List<UUID> uuids = transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()); Predicate<SlotStatus> slotPredicate = slotFilter.toSlotPredicate(true, uuids); // find the matching slots List<SlotStatus> slots = newArrayList(coordinator.getAllSlotsStatus(slotPredicate)); // update just in case something changed updateServiceInventory(); if (slots.isEmpty()) { return false; } // execute the command against one of the slots Collections.shuffle(slots); Exec.execLocal(SlotStatusRepresentation.from(slots.get(0)), command); return true; }
@Path("{slotId}") @DELETE public Response terminateSlot(@HeaderParam(GALAXY_AGENT_VERSION_HEADER) String agentVersion, @HeaderParam(GALAXY_SLOT_VERSION_HEADER) String slotVersion, @PathParam("slotId") UUID slotId) { Preconditions.checkNotNull(slotId, "slotId must not be null"); Slot slot = agent.getSlot(slotId); if (slot == null) { return Response.status(Response.Status.NOT_FOUND).build(); } checkAgentVersion(agent.getAgentStatus(), agentVersion); checkSlotVersion(slot.status(), slotVersion); SlotStatus slotStatus = agent.terminateSlot(slotId); if (slotStatus == null) { return Response.status(Response.Status.NOT_FOUND).build(); } return Response.ok(SlotStatusRepresentation.from(slotStatus)) .header(GALAXY_AGENT_VERSION_HEADER, agent.getAgentStatus().getVersion()) .header(GALAXY_SLOT_VERSION_HEADER, slotStatus.getVersion()) .build(); }
@PUT @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response assign(@HeaderParam(GALAXY_AGENT_VERSION_HEADER) String agentVersion, @HeaderParam(GALAXY_SLOT_VERSION_HEADER) String slotVersion, @PathParam("slotId") UUID slotId, InstallationRepresentation installation) { Preconditions.checkNotNull(slotId, "slotId must not be null"); Preconditions.checkNotNull(installation, "installation must not be null"); Slot slot = agent.getSlot(slotId); if (slot == null) { return Response.status(Response.Status.NOT_FOUND).build(); } checkAgentVersion(agent.getAgentStatus(), agentVersion); checkSlotVersion(slot.status(), slotVersion); SlotStatus status = slot.assign(installation.toInstallation()); return Response.ok(SlotStatusRepresentation.from(status)) .header(GALAXY_AGENT_VERSION_HEADER, agent.getAgentStatus().getVersion()) .header(GALAXY_SLOT_VERSION_HEADER, status.getVersion()) .build(); } }
return Response.status(Response.Status.BAD_REQUEST).build(); return Response.ok(SlotStatusRepresentation.from(status)) .header(GALAXY_AGENT_VERSION_HEADER, agent.getAgentStatus().getVersion()) .header(GALAXY_SLOT_VERSION_HEADER, status.getVersion())
public AgentStatusRepresentation create(AgentStatus status) { Builder<SlotStatusRepresentation> builder = ImmutableList.builder(); for (SlotStatus slot : status.getSlotStatuses()) { builder.add(SlotStatusRepresentation.from(slot, Integer.MAX_VALUE, repository)); } return new AgentStatusRepresentation( status.getAgentId(), safeTruncate(status.getAgentId(), shortIdPrefixSize), status.getInstanceId(), status.getState(), status.getInternalUri(), status.getExternalUri(), status.getLocation(), trimLeadingSegments(status.getLocation(), '/', commonLocationParts), status.getInstanceType(), builder.build(), status.getResources(), status.getVersion()); } }