@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(); }
public static Function<SlotStatus, SlotStatusRepresentation> fromSlotStatus(List<SlotStatus> slotStatuses, Repository repository) { return fromSlotStatus(new SlotStatusRepresentationFactory(slotStatuses, repository)); }
return slotStatus.getShortId(); case uuid: return slotStatus.getId().toString(); case machine: return slotStatus.getInstanceId(); case internalHost: return slotStatus.getInternalHost(); case internalIp: return slotStatus.getInternalIp(); case externalHost: return slotStatus.getExternalHost(); case status: return slotStatus.getStatus(); case location: return slotStatus.getLocation(); case shortLocation: return slotStatus.getShortLocation(); case binary: return slotStatus.getBinary(); case shortBinary: return slotStatus.getShortBinary(); case config: return slotStatus.getConfig(); case shortConfig: return slotStatus.getShortConfig(); case expectedStatus: return slotStatus.getExpectedStatus(); case expectedBinary: return slotStatus.getExpectedBinary();
public AgentStatus toAgentStatus(String instanceId, String instanceType) { Builder<SlotStatus> builder = ImmutableList.builder(); for (SlotStatusRepresentation slot : slots) { builder.add(slot.toSlotStatus(instanceId)); } return new AgentStatus(agentId, AgentLifecycleState.ONLINE, instanceId, self, externalUri, location, instanceType, builder.build(), resources); }
@Override public boolean ssh(SlotFilter slotFilter, String command) { URI uri = slotFilter.toUri(uriBuilderFrom(coordinatorUri).replacePath("/v1/slot")); Request request = RequestBuilder.prepareGet() .setUri(uri) .build(); List<SlotStatusRepresentation> slots = client.execute(request, createJsonResponseHandler(SLOTS_CODEC)); if (slots.isEmpty()) { return false; } SlotStatusRepresentation slot = slots.get(0); String host; if (useInternalAddress) { host = slot.getInternalHost(); } else { host = slot.getExternalHost(); } Exec.execRemote(host, slot.getInstallPath(), command); return true; }
public String getInternalIp() { String host = getInternalHost(); if (host == null) { return null; } if ("localhost".equalsIgnoreCase(host)) { return "127.0.0.1"; } try { return InetAddress.getByName(host).getHostAddress(); } catch (UnknownHostException e) { return "unknown"; } }
return new SlotStatusRepresentation(slotStatus.getId(), safeTruncate(slotStatus.getId().toString(), shortIdPrefixSize), slotStatus.getSelf(),
@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); } } }
return new SlotStatusRepresentation(status.getId(), safeTruncate(status.getId().toString(), shortIdPrefixSize), status.getSelf(),
@GET @Produces(MediaType.APPLICATION_JSON) public Response getAllSlots(@Context UriInfo uriInfo) { // build filter List<UUID> uuids = transform(coordinator.getAllSlotStatus(), uuidGetter()); Predicate<SlotStatus> slotFilter = SlotFilterBuilder.build(uriInfo, false, uuids); // select slots List<SlotStatus> slots = coordinator.getAllSlotsStatus(slotFilter); // build response return Response.ok(Iterables.transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository))) .header(GALAXY_SLOTS_VERSION_HEADER, createSlotsVersion(slots)) .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(); }
@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()); } }
@DELETE @Produces(MediaType.APPLICATION_JSON) public Response terminateSlots(@Context UriInfo uriInfo, @HeaderParam(GALAXY_SLOTS_VERSION_HEADER) String expectedSlotsVersion) { // build filter List<UUID> uuids = Lists.transform(coordinator.getAllSlotStatus(), uuidGetter()); Predicate<SlotStatus> slotFilter = SlotFilterBuilder.build(uriInfo, true, uuids); // reset slots expected state List<SlotStatus> result = coordinator.resetExpectedState(slotFilter, expectedSlotsVersion); // build response return Response.ok(transform(result, fromSlotStatus(coordinator.getAllSlotStatus(), repository))) .header(GALAXY_SLOTS_VERSION_HEADER, createSlotsVersion(result)) .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 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()); } }
@DELETE @Produces(MediaType.APPLICATION_JSON) public Response terminateSlots(@Context UriInfo uriInfo, @HeaderParam(GALAXY_SLOTS_VERSION_HEADER) String expectedSlotsVersion) { // build filter List<UUID> uuids = transform(coordinator.getAllSlotStatus(), uuidGetter()); Predicate<SlotStatus> slotFilter = SlotFilterBuilder.build(uriInfo, true, uuids); // terminate slots List<SlotStatus> result = coordinator.terminate(slotFilter, expectedSlotsVersion); // build response return Response.ok(transform(result, fromSlotStatus(coordinator.getAllSlotStatus(), repository))) .header(GALAXY_SLOTS_VERSION_HEADER, createSlotsVersion(result)) .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(); }
@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()); } }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response upgrade(UpgradeVersions upgradeVersions, @Context UriInfo uriInfo, @HeaderParam(GALAXY_SLOTS_VERSION_HEADER) String expectedSlotsVersion) { Preconditions.checkNotNull(upgradeVersions, "upgradeRepresentation must not be null"); // build filter List<UUID> uuids = Lists.transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()); Predicate<SlotStatus> slotFilter = SlotFilterBuilder.build(uriInfo, true, uuids); // upgrade slots List<SlotStatus> results = coordinator.upgrade(slotFilter, upgradeVersions, expectedSlotsVersion); // build response return Response.ok(transform(results, fromSlotStatus(coordinator.getAllSlotStatus(), repository))) .header(GALAXY_SLOTS_VERSION_HEADER, createSlotsVersion(results)) .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; }