@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(AIRSHIP_SLOTS_VERSION_HEADER, createSlotsVersion(slots)) .build(); }
@DELETE @Produces(MediaType.APPLICATION_JSON) public Response terminateSlots(@Context UriInfo uriInfo, @HeaderParam(AIRSHIP_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(AIRSHIP_SLOTS_VERSION_HEADER, createSlotsVersion(result)) .build(); } }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response upgrade(UpgradeVersions upgradeVersions, @Context UriInfo uriInfo, @HeaderParam(AIRSHIP_SLOTS_VERSION_HEADER) String expectedSlotsVersion, @HeaderParam(AIRSHIP_FORCE_HEADER) boolean force) { 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, force); // build response return Response.ok(transform(results, fromSlotStatus(coordinator.getAllSlotStatus(), repository))) .header(AIRSHIP_SLOTS_VERSION_HEADER, createSlotsVersion(results)) .build(); } }
@DELETE @Produces(MediaType.APPLICATION_JSON) public Response terminateSlots(@Context UriInfo uriInfo, @HeaderParam(AIRSHIP_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(AIRSHIP_SLOTS_VERSION_HEADER, createSlotsVersion(result)) .build(); } }
@GET @Path("/agent") @Produces(MediaType.APPLICATION_JSON) public Response getAllAgents(@Context UriInfo uriInfo) { List<SlotStatus> allSlotStatus = coordinator.getAllSlotStatus(); Predicate<AgentStatus> agentPredicate = AgentFilterBuilder.build(uriInfo, transform(coordinator.getAgents(), idGetter()), transform(allSlotStatus, SlotStatus.uuidGetter()), false, repository); List<AgentStatus> agents = coordinator.getAgents(agentPredicate); return Response.ok(transform(agents, fromAgentStatus(coordinator.getAgents(), repository))) .header(AIRSHIP_AGENTS_VERSION_HEADER, createAgentsVersion(agents)) .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; }
@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; }
@Override public List<SlotStatusRepresentation> upgrade(SlotFilter slotFilter, UpgradeVersions upgradeVersions, String expectedSlotsVersion, boolean force) { // build predicate List<UUID> uuids = transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()); Predicate<SlotStatus> slotPredicate = slotFilter.toSlotPredicate(true, uuids); // upgrade slots List<SlotStatus> slots = coordinator.upgrade(slotPredicate, upgradeVersions, expectedSlotsVersion, force); // update to latest state updateServiceInventory(); // build results return transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository)); }
@Override public List<SlotStatusRepresentation> terminate(SlotFilter slotFilter, String expectedSlotsVersion) { // build predicate List<UUID> uuids = transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()); Predicate<SlotStatus> slotPredicate = slotFilter.toSlotPredicate(true, uuids); // terminate slots List<SlotStatus> slots = coordinator.terminate(slotPredicate, expectedSlotsVersion); // update to latest state updateServiceInventory(); // build results return transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository)); }
@Override public List<SlotStatusRepresentation> upgrade(SlotFilter slotFilter, UpgradeVersions upgradeVersions, String expectedSlotsVersion, boolean force) { // build predicate List<UUID> uuids = transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()); Predicate<SlotStatus> slotPredicate = slotFilter.toSlotPredicate(true, uuids); // upgrade slots List<SlotStatus> slots = coordinator.upgrade(slotPredicate, upgradeVersions, expectedSlotsVersion, force); // update to latest state updateServiceInventory(); // build results return transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository)); }
@Override public List<SlotStatusRepresentation> resetExpectedState(SlotFilter slotFilter, String expectedSlotsVersion) { // build predicate List<UUID> uuids = transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()); Predicate<SlotStatus> slotPredicate = slotFilter.toSlotPredicate(true, uuids); // rest slots expected state List<SlotStatus> slots = coordinator.resetExpectedState(slotPredicate, expectedSlotsVersion); // update just in case something changed updateServiceInventory(); // build results return transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository)); }
@Override public List<SlotStatusRepresentation> terminate(SlotFilter slotFilter, String expectedSlotsVersion) { // build predicate List<UUID> uuids = transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()); Predicate<SlotStatus> slotPredicate = slotFilter.toSlotPredicate(true, uuids); // terminate slots List<SlotStatus> slots = coordinator.terminate(slotPredicate, expectedSlotsVersion); // update to latest state updateServiceInventory(); // build results return transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository)); }
@Override public List<SlotStatusRepresentation> resetExpectedState(SlotFilter slotFilter, String expectedSlotsVersion) { // build predicate List<UUID> uuids = transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()); Predicate<SlotStatus> slotPredicate = slotFilter.toSlotPredicate(true, uuids); // rest slots expected state List<SlotStatus> slots = coordinator.resetExpectedState(slotPredicate, expectedSlotsVersion); // update just in case something changed updateServiceInventory(); // build results return transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository)); }
@Override public CommanderResponse<List<SlotStatusRepresentation>> show(SlotFilter slotFilter) { List<SlotStatus> allSlotStatus = coordinator.getAllSlotStatus(); List<UUID> uuids = transform(allSlotStatus, SlotStatus.uuidGetter()); Predicate<SlotStatus> slotPredicate = slotFilter.toSlotPredicate(false, uuids); List<SlotStatus> slots = coordinator.getAllSlotsStatus(slotPredicate); // update just in case something changed updateServiceInventory(); return createCommanderResponse(createSlotsVersion(slots), transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository))); }
@Override public CommanderResponse<List<SlotStatusRepresentation>> show(SlotFilter slotFilter) { List<SlotStatus> allSlotStatus = coordinator.getAllSlotStatus(); List<UUID> uuids = transform(allSlotStatus, SlotStatus.uuidGetter()); Predicate<SlotStatus> slotPredicate = slotFilter.toSlotPredicate(false, uuids); List<SlotStatus> slots = coordinator.getAllSlotsStatus(slotPredicate); // update just in case something changed updateServiceInventory(); return createCommanderResponse(createSlotsVersion(slots), transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository))); }
@Override public List<SlotStatusRepresentation> setState(SlotFilter slotFilter, SlotLifecycleState state, String expectedSlotsVersion) { // build predicate List<UUID> uuids = transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()); Predicate<SlotStatus> slotPredicate = slotFilter.toSlotPredicate(true, uuids); // before changing state (like starting) update just in case something changed updateServiceInventory(); // set slots state List<SlotStatus> slots = coordinator.setState(state, slotPredicate, expectedSlotsVersion); // update to latest state updateServiceInventory(); // build results return transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository)); }
@Override public List<SlotStatusRepresentation> setState(SlotFilter slotFilter, SlotLifecycleState state, String expectedSlotsVersion) { // build predicate List<UUID> uuids = transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()); Predicate<SlotStatus> slotPredicate = slotFilter.toSlotPredicate(true, uuids); // before changing state (like starting) update just in case something changed updateServiceInventory(); // set slots state List<SlotStatus> slots = coordinator.setState(state, slotPredicate, expectedSlotsVersion); // update to latest state updateServiceInventory(); // build results return transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository)); }
@Override public CommanderResponse<List<AgentStatusRepresentation>> showAgents(AgentFilter agentFilter) { Predicate<AgentStatus> agentPredicate = agentFilter.toAgentPredicate( transform(coordinator.getAgents(), idGetter()), transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()), true, repository); List<AgentStatus> agentStatuses = coordinator.getAgents(agentPredicate); // update just in case something changed updateServiceInventory(); return createCommanderResponse(createAgentsVersion(agentStatuses), transform(agentStatuses, fromAgentStatus(coordinator.getAgents(), repository))); }
@Override public CommanderResponse<List<AgentStatusRepresentation>> showAgents(AgentFilter agentFilter) { Predicate<AgentStatus> agentPredicate = agentFilter.toAgentPredicate( transform(coordinator.getAgents(), idGetter()), transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()), true, repository); List<AgentStatus> agentStatuses = coordinator.getAgents(agentPredicate); // update just in case something changed updateServiceInventory(); return createCommanderResponse(createAgentsVersion(agentStatuses), transform(agentStatuses, fromAgentStatus(coordinator.getAgents(), repository))); }
@Override public List<SlotStatusRepresentation> install(AgentFilter agentFilter, int count, Assignment assignment, String expectedAgentsVersion) { // select the target agents Predicate<AgentStatus> agentsPredicate = agentFilter.toAgentPredicate( transform(coordinator.getAgents(), idGetter()), transform(coordinator.getAllSlotStatus(), uuidGetter()), true, repository); List<AgentStatus> agents = coordinator.getAgents(agentsPredicate); // verify the expected status of agents checkAgentsVersion(expectedAgentsVersion, agents); // install the software List<SlotStatus> slots = coordinator.install(agentsPredicate, count, assignment); // update to latest state updateServiceInventory(); // calculate unique prefix size with the new slots included return transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository)); }