private static Map<String, String> parseListOfPairs(final List<String> list, final String fieldName) { return parseListOfPairs(list, fieldName, "="); }
static PortMappingWithName create(final String name, final PortMapping portMapping) { return new PortMappingWithName(name, portMapping); }
public static void watchJobsOnHosts(final PrintStream out, final boolean exact, final List<String> resolvedHosts, final List<JobId> jobIds, final Integer interval, final HeliosClient client) throws InterruptedException, ExecutionException { watchJobsOnHosts(out, exact, resolvedHosts, Sets.newHashSet(jobIds), interval, ImmutableList.of(new TargetAndClient(client))); }
new JobCreateCommand(parse("create")); new JobRemoveCommand(parse("remove")); new JobInspectCommand(parse("inspect")); new JobDeployCommand(parse("deploy")); new JobUndeployCommand(parse("undeploy")); new JobStartCommand(parse("start")); new JobStopCommand(parse("stop")); new JobHistoryCommand(parse("history")); new JobListCommand(parse("jobs")); new JobStatusCommand(parse("status")); new JobWatchCommand(parse("watch")); new HostListCommand(parse("hosts")); new HostRegisterCommand(parse("register")); new HostDeregisterCommand(parse("deregister")); new MasterListCommand(parse("masters")); new DeploymentGroupCreateCommand(parse("create-deployment-group")); new DeploymentGroupRemoveCommand(parse("remove-deployment-group")); new DeploymentGroupListCommand(parse("list-deployment-groups")); new DeploymentGroupInspectCommand(parse("inspect-deployment-group")); new DeploymentGroupStatusCommand(parse("deployment-group-status")); new DeploymentGroupWatchCommand(parse("watch-deployment-group")); new RollingUpdateCommand(parse("rolling-update")); new DeploymentGroupStopCommand(parse("stop-deployment-group")); new VersionCommand(version);
String resolveName(final String host) { // passed FQDN, all set if (allHosts.contains(host)) { return host; } final List<String> matches = findPrefixMatches(host); if (matches.isEmpty()) { return host; // no pfx matches, let it fail upstream } if (matches.size() == 1) { return matches.iterator().next(); } final List<ScoredHost> scored = scoreMatches(matches); final List<ScoredHost> sorted = sortScoredHosts(scored); final List<String> minScoreHosts = findMatchesWithLowestScore(sorted); if (minScoreHosts.size() > 1) { // ambiguous, just return it and let it fail upstream return host; } return minScoreHosts.get(0); }
public List<String> getSortedMatches(final String hostName) { final List<String> matches = findPrefixMatches(hostName); final List<ScoredHost> scored = scoreMatches(matches); final List<ScoredHost> sorted = sortScoredHosts(scored); final ImmutableList.Builder<String> builder = ImmutableList.builder(); for (final ScoredHost host : sorted) { builder.add(host.host); } return builder.build(); }
return 1; clientBuilder.add(new TargetAndClient(target, client)); result = run(options, clients, out, json, stdin); } catch (ExecutionException e) { final Throwable cause = e.getCause(); } finally { for (final TargetAndClient cc : clients) { cc.getClient().close();
+ "---------- [%s UTC]%n", now.toString(formatter)); for (final TargetAndClient cc : clients) { final Optional<Target> target = cc.getTarget(); if (clients.size() > 1) { final String header; showReport(out, exact, prefixes, jobIds, formatter, cc.getClient());
final HostResolver resolver = HostResolver.create(client); final String host = resolver.resolveName(candidateHost); resolvedHosts.add(host); if (!json) { JobWatchCommand.watchJobsOnHosts(out, true, resolvedHosts, ImmutableList.of(jobId), options.getInt(intervalArg.getDest()), client);
@Override int run(final Namespace options, final List<TargetAndClient> clients, final PrintStream out, final boolean json, final BufferedReader stdin) throws ExecutionException, InterruptedException, IOException { final boolean exact = options.getBoolean(exactArg.getDest()); final List<String> prefixes = options.getList(prefixesArg.getDest()); final String jobIdString = options.getString(jobsArg.getDest()); final List<ListenableFuture<Map<JobId, Job>>> jobIdFutures = Lists.newArrayList(); for (final TargetAndClient cc : clients) { jobIdFutures.add(cc.getClient().jobs(jobIdString)); } final Set<JobId> jobIds = Sets.newHashSet(); for (final ListenableFuture<Map<JobId, Job>> future : jobIdFutures) { jobIds.addAll(future.get().keySet()); } watchJobsOnHosts(out, exact, prefixes, jobIds, options.getInt(intervalArg.getDest()), clients); return 0; }
static Map<String, PortMapping> parsePortMappings(final List<String> portSpecs) { final Map<String, PortMapping> explicitPorts = Maps.newHashMap(); for (final String spec : portSpecs) { final PortMappingWithName portMappingWithName = parsePortMapping(spec); final String name = portMappingWithName.name(); if (explicitPorts.containsKey(name)) { throw new IllegalArgumentException("Duplicate port mapping name: " + name); } explicitPorts.put(name, portMappingWithName.portMapping()); } return ImmutableMap.copyOf(explicitPorts); }
private static List<String> escape(final List<String> command) { final List<String> escaped = Lists.newArrayList(); for (final String s : command) { escaped.add(escape(s)); } return escaped; }
private static List<String> quote(final List<String> ss) { final List<String> output = Lists.newArrayList(); for (final String s : ss) { output.add(quote(s)); } return output; } }
private List<ScoredHost> scoreMatches(final List<String> results) { final ImmutableList.Builder<ScoredHost> scored = ImmutableList.builder(); for (final String name : results) { int score = Integer.MAX_VALUE; for (int i = 0; i < searchPath.length; i++) { if (name.endsWith(searchPath[i].toString())) { if (i < score) { score = i; } } } scored.add(new ScoredHost(name, score)); } return scored.build(); } }
static PortMappingWithName parsePortMapping(final String portSpec) { final Matcher matcher = PATTERN.matcher(portSpec); if (!matcher.matches()) { throw new IllegalArgumentException("Bad port mapping: " + portSpec); } final String name = matcher.group("n"); final String ip = matcher.group("ip"); final int internal = Integer.parseInt(matcher.group("i")); final Integer external = nullOrInteger(matcher.group("e")); final String protocol = fromNullable(matcher.group("p")).or(TCP); return PortMappingWithName.create(name, PortMapping.builder() .ip(ip) .internalPort(internal) .externalPort(external) .protocol(protocol) .build()); }
private void showJsonStatuses(PrintStream out, final String hostPattern, final Set<JobId> jobIds, final Map<JobId, JobStatus> statuses) { if (Strings.isNullOrEmpty(hostPattern)) { out.println(Json.asPrettyStringUnchecked(statuses)); return; } final Map<JobId, JobStatus> returnStatuses = Maps.newTreeMap(); showStatusesForHosts(hostPattern, jobIds, statuses, new HostStatusDisplayer() { @Override public void matchedStatus(JobStatus jobStatus, Iterable<String> matchingHosts, Map<String, TaskStatus> taskStatuses) { for (final String host : matchingHosts) { final Map<String, Deployment> deployments = jobStatus.getDeployments(); final Deployment deployment = (deployments == null) ? null : deployments.get(host); if (deployment != null) { returnStatuses.put(jobStatus.getJob().getId(), filterJobStatus(jobStatus, matchingHosts)); } } } }); out.println(Json.asPrettyStringUnchecked(returnStatuses)); }
static HostResolver create(HeliosClient client) throws InterruptedException, ExecutionException { final ResolverConfig currentConfig = ResolverConfig.getCurrentConfig(); final Name[] path; if (currentConfig != null) { final Name[] possiblePath = currentConfig.searchPath(); if (possiblePath != null) { path = possiblePath; } else { path = EMPTY_PATH; } } else { path = EMPTY_PATH; } return new HostResolver(Sets.newHashSet(client.listHosts().get()), path); }
/** * Execute against a cluster at a specific endpoint. */ private boolean run(final Namespace options, final Target target, final PrintStream out, final PrintStream err, final String username, final boolean json, final BufferedReader stdin) throws Exception { final HeliosClient client = Utils.getClient(target, err, username, options); if (client == null) { return false; } try { final int result = run(options, client, out, json, stdin); return result == 0; } catch (ExecutionException e) { final Throwable cause = e.getCause(); // if target is a domain, print message like // "Request timed out to master in ash.spotify.net (http://ash2-helios-a4.ash2.spotify.net)", // otherwise "Request timed out to master http://ash2-helios-a4.ash2.spotify.net:5800" if (cause instanceof TimeoutException) { err.println("Request timed out to master in " + target); } else { throw new RuntimeException(cause); } return false; } finally { client.close(); } }
@Override int run(final Namespace options, final HeliosClient client, final PrintStream out, final boolean json, final BufferedReader stdin) throws ExecutionException, InterruptedException { final String name = options.getString(nameArg.getDest()); final boolean full = options.getBoolean(fullArg.getDest()); return run0(client, out, json, name, full); }
public int run() { try { final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); return parser.getCommand().run(parser.getNamespace(), parser.getTargets(), out, err, parser.getUsername(), parser.getJson(), stdin); } catch (Exception e) { // print entire stack trace in verbose mode, otherwise just the exception message if (parser.getNamespace().getInt("verbose") > 0) { e.printStackTrace(err); } else { err.println(e.getMessage()); } return 1; } }