@Override public boolean apply(@Nullable AgentStatus agentStatus) { return agentStatus != null && predicate.apply(agentStatus.getInstanceId()); } }
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()); }
public AgentStatus terminateAgent(String agentId) { RemoteAgent agent = null; for (Iterator<Entry<String, RemoteAgent>> iterator = agents.entrySet().iterator(); iterator.hasNext(); ) { Entry<String, RemoteAgent> entry = iterator.next(); if (entry.getValue().status().getAgentId().equals(agentId)) { iterator.remove(); agent = entry.getValue(); break; } } if (agent == null) { return null; } if (!agent.getSlots().isEmpty()) { agents.putIfAbsent(agent.status().getInstanceId(), agent); throw new IllegalStateException("Cannot terminate agent that has slots: " + agentId); } provisioner.terminateAgents(ImmutableList.of(agentId)); return agent.status().changeState(AgentLifecycleState.TERMINATED); }
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 void updateStatus() { URI internalUri = agentStatus.getInternalUri(); if (internalUri != null) { try { Request request = RequestBuilder.prepareGet() .setUri(uriBuilderFrom(internalUri).replacePath("/v1/agent/").build()) .build(); AgentStatusRepresentation agentStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(agentStatusCodec)); agentStatus = agentStatusRepresentation.toAgentStatus(agentStatus.getInstanceId(), agentStatus.getInstanceType()); return; } catch (Exception ignored) { } } // error talking to agent -- mark agent offline if (agentStatus.getState() != PROVISIONING) { agentStatus = agentStatus.changeState(OFFLINE); agentStatus = agentStatus.changeAllSlotsState(SlotLifecycleState.UNKNOWN); } }