public static void checkAgentVersion(AgentStatus agentStatus, String expectedAgentStatus) { Preconditions.checkNotNull(agentStatus, "agentStatus is null"); if (expectedAgentStatus == null) { return; } if (!expectedAgentStatus.equals(agentStatus.getVersion())) { throw new VersionConflictException(GALAXY_AGENT_VERSION_HEADER, agentStatus.getVersion()); } }
@GET @Produces(MediaType.APPLICATION_JSON) public Response getAllSlotsStatus() { AgentStatus agentStatus = agent.getAgentStatus(); AgentStatusRepresentation agentStatusRepresentation = AgentStatusRepresentation.from(agentStatus); return Response.ok(agentStatusRepresentation) .header(GALAXY_AGENT_VERSION_HEADER, agentStatus.getVersion()) .build(); } }
public static String createAgentsVersion(Iterable<AgentStatus> agents) { Preconditions.checkNotNull(agents, "agents is null"); // canonicalize agent order Map<String, String> agentVersions = new TreeMap<String, String>(); for (AgentStatus agent : agents) { agentVersions.put(agent.getAgentId() + agent.getInstanceId(), agent.getVersion()); } return DigestUtils.md5Hex(agentVersions.values().toString()); }
@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(); }
@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(); } }
.header(GALAXY_AGENT_VERSION_HEADER, agent.getAgentStatus().getVersion()) .header(GALAXY_SLOT_VERSION_HEADER, status.getVersion()) .build();
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()); } }
@Override public SlotStatus install(Installation installation) { Preconditions.checkNotNull(installation, "installation is null"); URI internalUri = agentStatus.getInternalUri(); Preconditions.checkState(internalUri != null, "agent is down"); try { Request request = RequestBuilder.preparePost() .setUri(uriBuilderFrom(internalUri).replacePath("/v1/agent/slot/").build()) .setHeader(CONTENT_TYPE, APPLICATION_JSON) .setHeader(GALAXY_AGENT_VERSION_HEADER, status().getVersion()) .setBodyGenerator(jsonBodyGenerator(installationCodec, InstallationRepresentation.from(installation))) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.CREATED.getStatusCode())); SlotStatus slotStatus = slotStatusRepresentation.toSlotStatus(agentStatus.getInstanceId()); agentStatus = agentStatus.changeSlotStatus(slotStatus); return slotStatus; } catch (Exception e) { throw Throwables.propagate(e); } } }
@Override public SlotStatus terminate() { try { Request request = RequestBuilder.prepareDelete() .setUri(slotStatus.getSelf()) .setHeader(GALAXY_AGENT_VERSION_HEADER, agent.status().getVersion()) .setHeader(GALAXY_SLOT_VERSION_HEADER, slotStatus.getVersion()) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.OK.getStatusCode())); updateStatus(slotStatusRepresentation.toSlotStatus(slotStatus.getInstanceId())); return slotStatus; } catch (Exception e) { log.error(e); return setErrorStatus(e.getMessage()); } }
@Override public SlotStatus stop() { try { Request request = RequestBuilder.preparePut() .setUri(uriBuilderFrom(slotStatus.getSelf()).appendPath("lifecycle").build()) .setHeader(GALAXY_AGENT_VERSION_HEADER, agent.status().getVersion()) .setHeader(GALAXY_SLOT_VERSION_HEADER, slotStatus.getVersion()) .setBodyGenerator(createStaticBodyGenerator("stopped", UTF_8)) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.OK.getStatusCode())); updateStatus(slotStatusRepresentation.toSlotStatus(slotStatus.getInstanceId())); return slotStatus; } catch (Exception e) { log.error(e); return setErrorStatus(e.getMessage()); } } }
@Override public SlotStatus start() { try { Request request = RequestBuilder.preparePut() .setUri(uriBuilderFrom(slotStatus.getSelf()).appendPath("lifecycle").build()) .setHeader(GALAXY_AGENT_VERSION_HEADER, agent.status().getVersion()) .setHeader(GALAXY_SLOT_VERSION_HEADER, slotStatus.getVersion()) .setBodyGenerator(createStaticBodyGenerator("running", UTF_8)) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.OK.getStatusCode())); updateStatus(slotStatusRepresentation.toSlotStatus(slotStatus.getInstanceId())); return slotStatus; } catch (Exception e) { log.error(e); return setErrorStatus(e.getMessage()); } }
@Override public SlotStatus restart() { try { Request request = RequestBuilder.preparePut() .setUri(uriBuilderFrom(slotStatus.getSelf()).appendPath("lifecycle").build()) .setHeader(GALAXY_AGENT_VERSION_HEADER, agent.status().getVersion()) .setHeader(GALAXY_SLOT_VERSION_HEADER, slotStatus.getVersion()) .setBodyGenerator(createStaticBodyGenerator("restarting", UTF_8)) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.OK.getStatusCode())); updateStatus(slotStatusRepresentation.toSlotStatus(slotStatus.getInstanceId())); return slotStatus; } catch (Exception e) { log.error(e); return setErrorStatus(e.getMessage()); } }
@Override public SlotStatus assign(Installation installation) { try { Request request = RequestBuilder.preparePut() .setUri(uriBuilderFrom(slotStatus.getSelf()).appendPath("assignment").build()) .setHeader(CONTENT_TYPE, APPLICATION_JSON) .setHeader(GALAXY_AGENT_VERSION_HEADER, agent.status().getVersion()) .setHeader(GALAXY_SLOT_VERSION_HEADER, slotStatus.getVersion()) .setBodyGenerator(jsonBodyGenerator(installationCodec, InstallationRepresentation.from(installation))) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.OK.getStatusCode())); updateStatus(slotStatusRepresentation.toSlotStatus(slotStatus.getInstanceId())); return slotStatus; } catch (Exception e) { log.error(e); return setErrorStatus(e.getMessage()); } }