Search in sources :

Example 1 with Table

use of com.spotify.helios.cli.Table in project helios by spotify.

the class DeploymentGroupStatusCommand method printTable.

private static void printTable(final PrintStream out, final JobId jobId, final List<DeploymentGroupStatusResponse.HostStatus> hosts, final boolean full) {
    final Table table = table(out);
    table.row("HOST", "UP-TO-DATE", "JOB", "STATE");
    for (final DeploymentGroupStatusResponse.HostStatus hostStatus : hosts) {
        final String displayHostName = formatHostname(full, hostStatus.getHost());
        final boolean upToDate = hostStatus.getJobId() != null && hostStatus.getJobId().equals(jobId);
        final String job;
        if (hostStatus.getJobId() == null) {
            job = "-";
        } else if (full) {
            job = hostStatus.getJobId().toString();
        } else {
            job = hostStatus.getJobId().toShortString();
        }
        final String state = hostStatus.getState() != null ? hostStatus.getState().toString() : "-";
        table.row(displayHostName, upToDate ? "X" : "", job, state);
    }
    table.print();
}
Also used : Table(com.spotify.helios.cli.Table) DeploymentGroupStatusResponse(com.spotify.helios.common.protocol.DeploymentGroupStatusResponse)

Example 2 with Table

use of com.spotify.helios.cli.Table in project helios by spotify.

the class HostListCommand method run.

@Override
int run(final Namespace options, final HeliosClient client, final PrintStream out, final boolean json, final BufferedReader stdin) throws ExecutionException, InterruptedException {
    final String pattern = options.getString(patternArg.getDest());
    final List<String> selectorArgValue = options.getList(hostSelectorsArg.getDest());
    final Set<String> selectors = ImmutableSet.copyOf(selectorArgValue);
    final List<String> hosts;
    if (pattern.isEmpty() && selectors.isEmpty()) {
        hosts = client.listHosts().get();
    } else if (!pattern.isEmpty() && selectors.isEmpty()) {
        hosts = client.listHosts(pattern).get();
    } else if (pattern.isEmpty() && !selectors.isEmpty()) {
        hosts = client.listHosts(selectors).get();
    } else {
        hosts = client.listHosts(pattern, selectors).get();
    }
    final Map<String, String> queryParams = Maps.newHashMap();
    final String statusFilter = options.getString(statusArg.getDest());
    if (!isNullOrEmpty(statusFilter)) {
        try {
            HostStatus.Status.valueOf(statusFilter);
            queryParams.put("status", statusFilter);
        } catch (IllegalArgumentException ignored) {
            throw new IllegalArgumentException("Invalid status. Valid statuses are: " + statusChoicesString);
        }
    }
    final boolean full = options.getBoolean(fullArg.getDest());
    final boolean quiet = options.getBoolean(quietArg.getDest());
    if (hosts.isEmpty()) {
        if (json) {
            out.println("{ }");
        } else if (!quiet && !isNullOrEmpty(pattern)) {
            out.printf("host pattern %s matched no hosts%n", pattern);
        }
        return 1;
    }
    final List<String> sortedHosts = natural().sortedCopy(hosts);
    if (quiet) {
        if (json) {
            out.println(Json.asPrettyStringUnchecked(sortedHosts));
        } else {
            for (final String host : sortedHosts) {
                out.println(formatHostname(full, host));
            }
        }
    } else {
        final Map<String, HostStatus> statuses = client.hostStatuses(hosts, queryParams).get();
        if (json) {
            final Map<String, HostStatus> sorted = Maps.newTreeMap();
            sorted.putAll(statuses);
            out.println(Json.asPrettyStringUnchecked(sorted));
        } else {
            final Table table = table(out);
            table.row("HOST", "STATUS", "DEPLOYED", "RUNNING", "CPUS", "MEM", "LOAD AVG", "MEM USAGE", "OS", "HELIOS", "DOCKER", "LABELS");
            for (final Map.Entry<String, HostStatus> e : statuses.entrySet()) {
                final String host = e.getKey();
                final HostStatus s = e.getValue();
                if (s == null) {
                    continue;
                }
                final Set<TaskStatus> runningDeployedJobs = Sets.newHashSet();
                for (final JobId jobId : s.getJobs().keySet()) {
                    final TaskStatus taskStatus = s.getStatuses().get(jobId);
                    if (taskStatus == null) {
                        continue;
                    }
                    if (taskStatus.getState() == TaskStatus.State.RUNNING) {
                        runningDeployedJobs.add(taskStatus);
                    }
                }
                final HostInfo hi = s.getHostInfo();
                final String memUsage;
                final String cpus;
                final String mem;
                final String loadAvg;
                final String os;
                final String docker;
                if (hi != null) {
                    final long free = hi.getMemoryFreeBytes();
                    final long total = hi.getMemoryTotalBytes();
                    memUsage = format("%.2f", (float) (total - free) / total);
                    cpus = String.valueOf(hi.getCpus());
                    mem = hi.getMemoryTotalBytes() / (1024 * 1024 * 1024) + " gb";
                    loadAvg = format("%.2f", hi.getLoadAvg());
                    os = hi.getOsName() + " " + hi.getOsVersion();
                    final DockerVersion dv = hi.getDockerVersion();
                    docker = (dv != null) ? format("%s (%s)", dv.getVersion(), dv.getApiVersion()) : "";
                } else {
                    memUsage = cpus = mem = loadAvg = os = docker = "";
                }
                final String version;
                if (s.getAgentInfo() != null) {
                    version = Optional.fromNullable(s.getAgentInfo().getVersion()).or("");
                } else {
                    version = "";
                }
                String status = s.getStatus() == UP ? "Up" : "Down";
                if (s.getAgentInfo() != null) {
                    final long startTime = s.getAgentInfo().getStartTime();
                    final long upTime = s.getAgentInfo().getUptime();
                    if (s.getStatus() == UP) {
                        status += " " + humanDuration(currentTimeMillis() - startTime);
                    } else {
                        status += " " + humanDuration(currentTimeMillis() - startTime - upTime);
                    }
                }
                final String hostLabels = Joiner.on(", ").withKeyValueSeparator("=").join(s.getLabels());
                table.row(formatHostname(full, host), status, s.getJobs().size(), runningDeployedJobs.size(), cpus, mem, loadAvg, memUsage, os, version, docker, hostLabels);
            }
            table.print();
        }
    }
    return 0;
}
Also used : Table(com.spotify.helios.cli.Table) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) DockerVersion(com.spotify.helios.common.descriptors.DockerVersion) HostStatus(com.spotify.helios.common.descriptors.HostStatus) Map(java.util.Map) HostInfo(com.spotify.helios.common.descriptors.HostInfo) JobId(com.spotify.helios.common.descriptors.JobId)

Example 3 with Table

use of com.spotify.helios.cli.Table in project helios by spotify.

the class JobHistoryCommand method run.

@Override
int run(final Namespace options, final HeliosClient client, final PrintStream out, final boolean json, final BufferedReader stdin) throws ExecutionException, InterruptedException {
    final String jobIdString = options.getString(jobIdArg.getDest());
    final Map<JobId, Job> jobs = client.jobs(jobIdString).get();
    if (jobs.size() == 0) {
        out.printf("Unknown job: %s%n", jobIdString);
        return 1;
    } else if (jobs.size() > 1) {
        out.printf("Ambiguous job id: %s%n", jobIdString);
        return 1;
    }
    final JobId jobId = getLast(jobs.keySet());
    final TaskStatusEvents result = client.jobHistory(jobId).get();
    if (json) {
        out.println(Json.asPrettyStringUnchecked(result));
        return 0;
    }
    final Table table = table(out);
    table.row("HOST", "TIMESTAMP", "STATE", "THROTTLED", "CONTAINERID");
    final List<TaskStatusEvent> events = result.getEvents();
    final DateTimeFormatter format = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.SSS");
    for (final TaskStatusEvent event : events) {
        final String host = checkNotNull(event.getHost());
        final long timestamp = checkNotNull(event.getTimestamp());
        final TaskStatus status = checkNotNull(event.getStatus());
        final State state = checkNotNull(status.getState());
        String containerId = status.getContainerId();
        containerId = containerId == null ? "<none>" : containerId;
        table.row(host, format.print(timestamp), state, status.getThrottled(), containerId);
    }
    table.print();
    return 0;
}
Also used : TaskStatusEvent(com.spotify.helios.common.descriptors.TaskStatusEvent) Table(com.spotify.helios.cli.Table) State(com.spotify.helios.common.descriptors.TaskStatus.State) TaskStatusEvents(com.spotify.helios.common.protocol.TaskStatusEvents) Job(com.spotify.helios.common.descriptors.Job) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) JobId(com.spotify.helios.common.descriptors.JobId)

Example 4 with Table

use of com.spotify.helios.cli.Table in project helios by spotify.

the class JobListCommand method run.

@Override
int run(final Namespace options, final HeliosClient client, final PrintStream out, final boolean json, final BufferedReader stdin) throws ExecutionException, InterruptedException {
    final boolean full = options.getBoolean(fullArg.getDest());
    final boolean quiet = options.getBoolean(quietArg.getDest());
    final String pattern = options.getString(patternArg.getDest());
    final boolean deployed = options.getBoolean(deployedArg.getDest());
    final Map<JobId, Job> jobs;
    if (pattern == null) {
        jobs = client.jobs().get();
    } else {
        jobs = client.jobs(pattern).get();
    }
    if (!Strings.isNullOrEmpty(pattern) && jobs.isEmpty()) {
        if (json) {
            out.println(Json.asPrettyStringUnchecked(jobs));
        } else if (!quiet) {
            out.printf("job pattern %s matched no jobs%n", pattern);
        }
        return 1;
    }
    final Map<JobId, JobStatus> jobStatuses = getJobStatuses(client, jobs, deployed);
    final Set<JobId> sortedJobIds = Sets.newTreeSet(jobStatuses.keySet());
    if (json) {
        if (quiet) {
            out.println(Json.asPrettyStringUnchecked(sortedJobIds));
        } else {
            final Map<JobId, Job> filteredJobs = Maps.newHashMap();
            for (final Entry<JobId, Job> entry : jobs.entrySet()) {
                if (jobStatuses.containsKey(entry.getKey())) {
                    filteredJobs.put(entry.getKey(), entry.getValue());
                }
            }
            out.println(Json.asPrettyStringUnchecked(filteredJobs));
        }
    } else {
        if (quiet) {
            for (final JobId jobId : sortedJobIds) {
                out.println(jobId);
            }
        } else {
            final Table table = table(out);
            table.row("JOB ID", "NAME", "VERSION", "HOSTS", "COMMAND", "ENVIRONMENT");
            for (final JobId jobId : sortedJobIds) {
                final Job job = jobs.get(jobId);
                final String command = on(' ').join(escape(job.getCommand()));
                final String env = Joiner.on(" ").withKeyValueSeparator("=").join(job.getEnv());
                final JobStatus status = jobStatuses.get(jobId);
                table.row(full ? jobId : jobId.toShortString(), jobId.getName(), jobId.getVersion(), status != null ? status.getDeployments().keySet().size() : 0, command, env);
            }
            table.print();
        }
    }
    return 0;
}
Also used : JobStatus(com.spotify.helios.common.descriptors.JobStatus) Table(com.spotify.helios.cli.Table) Job(com.spotify.helios.common.descriptors.Job) JobId(com.spotify.helios.common.descriptors.JobId)

Aggregations

Table (com.spotify.helios.cli.Table)4 JobId (com.spotify.helios.common.descriptors.JobId)3 Job (com.spotify.helios.common.descriptors.Job)2 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)2 DockerVersion (com.spotify.helios.common.descriptors.DockerVersion)1 HostInfo (com.spotify.helios.common.descriptors.HostInfo)1 HostStatus (com.spotify.helios.common.descriptors.HostStatus)1 JobStatus (com.spotify.helios.common.descriptors.JobStatus)1 State (com.spotify.helios.common.descriptors.TaskStatus.State)1 TaskStatusEvent (com.spotify.helios.common.descriptors.TaskStatusEvent)1 DeploymentGroupStatusResponse (com.spotify.helios.common.protocol.DeploymentGroupStatusResponse)1 TaskStatusEvents (com.spotify.helios.common.protocol.TaskStatusEvents)1 Map (java.util.Map)1 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)1