Search in sources :

Example 21 with TaskStatus

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

the class BindVolumeTest method test.

@Test
public void test() throws Exception {
    try (final DockerClient docker = getNewDockerClient()) {
        // Start Helios agent, configured to bind host /etc/hostname into container /mnt/hostname
        startDefaultMaster();
        startDefaultAgent(testHost(), "--bind", "/etc/hostname:/mnt/hostname:ro");
        awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
        // Figure out the host kernel version
        final String hostname = docker.info().name();
        // Run a job that cat's /mnt/hostname, which should be the host's name
        final List<String> command = ImmutableList.of("cat", "/mnt/hostname");
        final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, command);
        deployJob(jobId, testHost());
        final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
        final String log;
        try (LogStream logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr())) {
            log = logs.readFully();
        }
        // the kernel version from the host should be in the log
        assertThat(log, containsString(hostname));
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) Matchers.containsString(org.hamcrest.Matchers.containsString) LogStream(com.spotify.docker.client.LogStream) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 22 with TaskStatus

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

the class SupervisorTest method createSupervisor.

private Supervisor createSupervisor(final Job job) {
    final TaskConfig config = TaskConfig.builder().namespace(NAMESPACE).host("AGENT_NAME").job(job).envVars(ENV).build();
    final TaskStatus.Builder taskStatus = TaskStatus.newBuilder().setJob(job).setEnv(ENV).setPorts(PORTS);
    final StatusUpdater statusUpdater = new DefaultStatusUpdater(model, taskStatus);
    final TaskMonitor monitor = new TaskMonitor(job.getId(), FlapController.create(), statusUpdater);
    final TaskRunnerFactory runnerFactory = TaskRunnerFactory.builder().registrar(registrar).config(config).dockerClient(docker).listener(monitor).build();
    return Supervisor.newBuilder().setJob(job).setStatusUpdater(statusUpdater).setDockerClient(docker).setRestartPolicy(retryPolicy).setRunnerFactory(runnerFactory).setMetrics(new NoopSupervisorMetrics()).setMonitor(monitor).build();
}
Also used : NoopSupervisorMetrics(com.spotify.helios.servicescommon.statistics.NoopSupervisorMetrics) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus)

Example 23 with TaskStatus

use of com.spotify.helios.common.descriptors.TaskStatus 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 24 with TaskStatus

use of com.spotify.helios.common.descriptors.TaskStatus 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 25 with TaskStatus

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

the class TemporaryJob method verifyHealthy.

void verifyHealthy() throws AssertionError {
    log.debug("Checking health of {}", job.getImage());
    final JobStatus status = Futures.getUnchecked(client.jobStatus(job.getId()));
    if (status == null) {
        return;
    }
    for (final Map.Entry<String, TaskStatus> entry : status.getTaskStatuses().entrySet()) {
        verifyHealthy(entry.getKey(), entry.getValue());
    }
}
Also used : JobStatus(com.spotify.helios.common.descriptors.JobStatus) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) Map(java.util.Map) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)53 JobId (com.spotify.helios.common.descriptors.JobId)40 Test (org.junit.Test)27 DockerClient (com.spotify.docker.client.DockerClient)18 Deployment (com.spotify.helios.common.descriptors.Deployment)15 Job (com.spotify.helios.common.descriptors.Job)14 HeliosClient (com.spotify.helios.client.HeliosClient)13 LogStream (com.spotify.docker.client.LogStream)10 HostStatus (com.spotify.helios.common.descriptors.HostStatus)10 JobStatus (com.spotify.helios.common.descriptors.JobStatus)8 JobDeployResponse (com.spotify.helios.common.protocol.JobDeployResponse)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)7 PortMapping (com.spotify.helios.common.descriptors.PortMapping)6 Map (java.util.Map)6 ImmutableMap (com.google.common.collect.ImmutableMap)4 TaskStatusEvent (com.spotify.helios.common.descriptors.TaskStatusEvent)4 JobUndeployResponse (com.spotify.helios.common.protocol.JobUndeployResponse)4 IOException (java.io.IOException)4 ExecutionException (java.util.concurrent.ExecutionException)4