Search in sources :

Example 1 with JobId

use of com.spotify.helios.common.descriptors.JobId in project helios by spotify.

the class JobInspectCommand method runWithJobId.

@Override
protected int runWithJobId(final Namespace options, final HeliosClient client, final PrintStream out, final boolean json, final JobId jobId, final BufferedReader stdin) throws ExecutionException, InterruptedException {
    final Map<JobId, Job> jobs = client.jobs(jobId.toString()).get();
    if (jobs.size() == 0) {
        out.printf("Unknown job: %s%n", jobId);
        return 1;
    }
    final Job job = Iterables.getOnlyElement(jobs.values());
    if (json) {
        out.println(Json.asPrettyStringUnchecked(job));
    } else {
        out.printf("Id: %s%n", job.getId());
        out.printf("Image: %s%n", job.getImage());
        out.printf("Created: %s%n", job.getCreated() == null ? "" : DATE_FORMATTER.format(new Date(job.getCreated())));
        out.printf("Expires: %s%n", job.getExpires() == null ? "never" : DATE_FORMATTER.format(job.getExpires()));
        out.printf("Hostname: %s%n", job.getHostname());
        out.printf("Command: %s%n", quote(job.getCommand()));
        printMap(out, "Env:   ", QUOTE, job.getEnv());
        out.printf("Health check: %s%n", formatHealthCheck(job.getHealthCheck()));
        out.printf("Grace period (seconds): %s%n", job.getGracePeriod());
        out.printf("Time to wait before kill (seconds): %s%n", job.getSecondsToWaitBeforeKill());
        printMap(out, "Metadata: ", QUOTE, job.getMetadata());
        printMap(out, "Ports: ", FORMAT_PORTMAPPING, job.getPorts());
        printMap(out, "Reg: ", FORMAT_SERVICE_PORTS, job.getRegistration());
        out.printf("Security options: %s%n", job.getSecurityOpt());
        out.printf("Network mode: %s%n", job.getNetworkMode());
        out.printf("Token: %s%n", job.getToken());
        printVolumes(out, job.getVolumes());
        out.printf("Add capabilities: %s%n", Joiner.on(", ").join(job.getAddCapabilities()));
        out.printf("Drop capabilities: %s%n", Joiner.on(", ").join(job.getDropCapabilities()));
    }
    return 0;
}
Also used : Job(com.spotify.helios.common.descriptors.Job) JobId(com.spotify.helios.common.descriptors.JobId) Date(java.util.Date)

Example 2 with JobId

use of com.spotify.helios.common.descriptors.JobId in project helios by spotify.

the class RollingUpdateCommand method runWithJobId.

@Override
protected int runWithJobId(final Namespace options, final HeliosClient client, final PrintStream out, final boolean json, final JobId jobId, final BufferedReader stdin) throws ExecutionException, InterruptedException, IOException {
    final String name = options.getString(nameArg.getDest());
    final long timeout = options.getLong(timeoutArg.getDest());
    final int parallelism = options.getInt(parallelismArg.getDest());
    final boolean async = options.getBoolean(asyncArg.getDest());
    final long rolloutTimeout = options.getLong(rolloutTimeoutArg.getDest());
    final boolean migrate = options.getBoolean(migrateArg.getDest());
    final boolean overlap = options.getBoolean(overlapArg.getDest());
    final String token = options.getString(tokenArg.getDest());
    checkArgument(timeout > 0, "Timeout must be greater than 0");
    checkArgument(parallelism > 0, "Parallelism must be greater than 0");
    checkArgument(rolloutTimeout > 0, "Rollout timeout must be greater than 0");
    final long startTime = timeSupplier.get();
    final RolloutOptions rolloutOptions = RolloutOptions.newBuilder().setTimeout(timeout).setParallelism(parallelism).setMigrate(migrate).setOverlap(overlap).setToken(token).build();
    final RollingUpdateResponse response = client.rollingUpdate(name, jobId, rolloutOptions).get();
    if (response.getStatus() != RollingUpdateResponse.Status.OK) {
        if (!json) {
            out.println("Failed: " + response);
        } else {
            out.println(response.toJsonString());
        }
        return 1;
    }
    if (!json) {
        out.println(format("Rolling update%s started: %s -> %s " + "(parallelism=%d, timeout=%d, overlap=%b, token=%s)%s", async ? " (async)" : "", name, jobId.toShortString(), parallelism, timeout, overlap, token, async ? "" : "\n"));
    }
    final Map<String, Object> jsonOutput = Maps.newHashMap();
    jsonOutput.put("parallelism", parallelism);
    jsonOutput.put("timeout", timeout);
    jsonOutput.put("overlap", overlap);
    jsonOutput.put("token", token);
    if (async) {
        if (json) {
            jsonOutput.put("status", response.getStatus());
            out.println(Json.asStringUnchecked(jsonOutput));
        }
        return 0;
    }
    String error = "";
    boolean failed = false;
    boolean timedOut = false;
    final Set<String> reported = Sets.newHashSet();
    while (true) {
        final DeploymentGroupStatusResponse status = client.deploymentGroupStatus(name).get();
        if (status == null) {
            failed = true;
            error = "Failed to fetch deployment-group status";
            break;
        }
        if (!jobId.equals(status.getDeploymentGroup().getJobId())) {
            // Another rolling-update was started, overriding this one -- exit
            failed = true;
            error = "Deployment-group job id changed during rolling-update";
            break;
        }
        if (!json) {
            for (final DeploymentGroupStatusResponse.HostStatus hostStatus : status.getHostStatuses()) {
                final JobId hostJobId = hostStatus.getJobId();
                final String host = hostStatus.getHost();
                final TaskStatus.State state = hostStatus.getState();
                final boolean done = hostJobId != null && hostJobId.equals(jobId) && state == TaskStatus.State.RUNNING;
                if (done && reported.add(host)) {
                    out.println(format("%s -> %s (%d/%d)", host, state, reported.size(), status.getHostStatuses().size()));
                }
            }
        }
        if (status.getStatus() != DeploymentGroupStatusResponse.Status.ROLLING_OUT) {
            if (status.getStatus() == DeploymentGroupStatusResponse.Status.FAILED) {
                failed = true;
                error = status.getError();
            }
            break;
        }
        if (timeSupplier.get() - startTime > TimeUnit.MINUTES.toMillis(rolloutTimeout)) {
            // Rollout timed out
            timedOut = true;
            break;
        }
        sleepFunction.sleep(POLL_INTERVAL_MILLIS);
    }
    final double duration = (timeSupplier.get() - startTime) / 1000.0;
    if (json) {
        if (failed) {
            jsonOutput.put("status", "FAILED");
            jsonOutput.put("error", error);
        } else if (timedOut) {
            jsonOutput.put("status", "TIMEOUT");
        } else {
            jsonOutput.put("status", "DONE");
        }
        jsonOutput.put("duration", duration);
        out.println(Json.asStringUnchecked(jsonOutput));
    } else {
        out.println();
        if (failed) {
            out.println(format("Failed: %s", error));
        } else if (timedOut) {
            out.println("Timed out! (rolling-update still in progress)");
        } else {
            out.println("Done.");
        }
        out.println(format("Duration: %.2f s", duration));
    }
    return (failed || timedOut) ? 1 : 0;
}
Also used : RolloutOptions(com.spotify.helios.common.descriptors.RolloutOptions) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) DeploymentGroupStatusResponse(com.spotify.helios.common.protocol.DeploymentGroupStatusResponse) RollingUpdateResponse(com.spotify.helios.common.protocol.RollingUpdateResponse) JobId(com.spotify.helios.common.descriptors.JobId)

Example 3 with JobId

use of com.spotify.helios.common.descriptors.JobId in project helios by spotify.

the class DeploymentGroupStatusCommand method run0.

static int run0(final HeliosClient client, final PrintStream out, final boolean json, final String name, final boolean full) throws ExecutionException, InterruptedException {
    final DeploymentGroupStatusResponse status = client.deploymentGroupStatus(name).get();
    if (status == null) {
        if (json) {
            final Map<String, Object> output = Maps.newHashMap();
            output.put("status", "DEPLOYMENT_GROUP_NOT_FOUND");
            out.print(Json.asStringUnchecked(output));
        } else {
            out.printf("Unknown deployment group: %s%n", name);
        }
        return 1;
    }
    if (json) {
        out.println(Json.asPrettyStringUnchecked(status));
    } else {
        final JobId jobId = status.getDeploymentGroup().getJobId();
        final String error = status.getError();
        final List<HostSelector> hostSelectors = status.getDeploymentGroup().getHostSelectors();
        out.printf("Name: %s%n", name);
        out.printf("Job Id: %s%n", full ? jobId : (jobId == null ? null : jobId.toShortString()));
        out.printf("Status: %s%n", status.getStatus());
        out.printf("Host selectors:%n");
        for (final HostSelector hostSelector : hostSelectors) {
            out.printf("  %s%n", hostSelector.toPrettyString());
        }
        if (!Strings.isNullOrEmpty(error)) {
            out.printf("Error: %s%n", error);
        }
        out.printf("%n");
        printTable(out, jobId, status.getHostStatuses(), full);
    }
    return 0;
}
Also used : HostSelector(com.spotify.helios.common.descriptors.HostSelector) DeploymentGroupStatusResponse(com.spotify.helios.common.protocol.DeploymentGroupStatusResponse) JobId(com.spotify.helios.common.descriptors.JobId)

Example 4 with JobId

use of com.spotify.helios.common.descriptors.JobId 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;
    }
    if (quiet) {
        final List<String> sortedHosts = natural().sortedCopy(hosts);
        if (json) {
            out.println(Json.asPrettyStringUnchecked(sortedHosts));
        } else {
            for (final String host : sortedHosts) {
                out.println(formatHostname(full, host));
            }
        }
    } else {
        final Map<String, HostStatus> statuses = new TreeMap<>(client.hostStatuses(hosts, queryParams).get());
        if (json) {
            out.println(Json.asPrettyStringUnchecked(statuses));
        } 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) TreeMap(java.util.TreeMap) 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) TreeMap(java.util.TreeMap) HostInfo(com.spotify.helios.common.descriptors.HostInfo) JobId(com.spotify.helios.common.descriptors.JobId)

Example 5 with JobId

use of com.spotify.helios.common.descriptors.JobId 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 = 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)

Aggregations

JobId (com.spotify.helios.common.descriptors.JobId)132 Test (org.junit.Test)78 Job (com.spotify.helios.common.descriptors.Job)47 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)44 HeliosClient (com.spotify.helios.client.HeliosClient)35 Deployment (com.spotify.helios.common.descriptors.Deployment)32 Matchers.containsString (org.hamcrest.Matchers.containsString)26 JobStatus (com.spotify.helios.common.descriptors.JobStatus)22 DockerClient (com.spotify.docker.client.DockerClient)19 JobDeployResponse (com.spotify.helios.common.protocol.JobDeployResponse)17 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)15 HostStatus (com.spotify.helios.common.descriptors.HostStatus)13 IOException (java.io.IOException)12 Map (java.util.Map)11 LogStream (com.spotify.docker.client.LogStream)10 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)10 KeeperException (org.apache.zookeeper.KeeperException)9 TaskStatusEvent (com.spotify.helios.common.descriptors.TaskStatusEvent)8 AgentMain (com.spotify.helios.agent.AgentMain)7 PortMapping (com.spotify.helios.common.descriptors.PortMapping)7