protected HostStatus awaitHostStatusWithLabels(final HeliosClient client, final String host, final HostStatus.Status status, final Map<String, String> labels) throws Exception { final HostStatus hostStatus = Polling.await(LONG_WAIT_SECONDS, SECONDS, () -> { final HostStatus candidate = getOrNull(client.hostStatus(host)); if (candidate == null || candidate.getStatus() != status // labels are stored in ZK after the host has come up || candidate.getLabels().size() != labels.size()) { return null; } return candidate; }); assertThat("host " + host + " has status=" + status + " with labels=" + hostStatus.getLabels(), hostStatus.getLabels(), is(labels)); return hostStatus; }
/** * Get labels for a set of hosts. * * @param hosts Hostnames for which to get labels. * * @return a map whose keys are hostnames and values are maps of label key-vals. */ private Callable<Map<String, Map<String, String>>> hostLabels(final Set<String> hosts) { return () -> { final ImmutableMap.Builder<String, Map<String, String>> hostLabels = ImmutableMap.builder(); for (final String host : hosts) { final Map<String, HostStatus> status = Json.read( cli("hosts", host, "--json"), new TypeReference<Map<String, HostStatus>>() { }); final HostStatus hostStatus = status.get(host); if (hostStatus == null) { return null; } final Map<String, String> labels = hostStatus.getLabels(); if (labels != null && !labels.isEmpty()) { hostLabels.put(host, labels); } else { return null; } } return hostLabels.build(); }; }
final String hostLabels = Joiner.on(", ").withKeyValueSeparator("=").join(s.getLabels());
@Override public void run(final boolean timeout) throws InterruptedException { final List<String> allHosts = masterModel.listHosts(); final Map<String, Map<String, String>> hostsToLabels = Maps.newHashMap(); // determine all hosts and their labels for (final String host : allHosts) { final HostStatus hostStatus = masterModel.getHostStatus(host); if (hostStatus != null) { hostsToLabels.put(host, hostStatus.getLabels()); } } final HostMatcher hostMatcher = new HostMatcher(hostsToLabels); for (final DeploymentGroup dg : masterModel.getDeploymentGroups().values()) { final List<String> matchingHosts = hostMatcher.getMatchingHosts(dg); try { masterModel.updateDeploymentGroupHosts(dg.getName(), matchingHosts); } catch (Exception e) { log.warn("error processing hosts update for deployment group: {} - {}", dg.getName(), e); } } } }