Search in sources :

Example 36 with Job

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

the class JobStatusCommand 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(jobArg.getDest());
    final String hostPattern = options.getString(hostArg.getDest());
    final boolean full = options.getBoolean(fullArg.getDest());
    final Map<JobId, Job> jobs;
    if (Strings.isNullOrEmpty(jobIdString)) {
        jobs = client.jobs().get();
    } else {
        jobs = client.jobs(jobIdString).get();
    }
    if (jobs == null) {
        out.printf("The specified Helios master either returned an error or job id matcher " + "\"%s\" matched no jobs%n", jobIdString);
        return 1;
    }
    final Set<JobId> jobIds = jobs.keySet();
    if (!Strings.isNullOrEmpty(jobIdString) && jobIds.isEmpty()) {
        if (json) {
            out.println("{ }");
        } else {
            out.printf("job id matcher \"%s\" matched no jobs%n", jobIdString);
        }
        return 1;
    }
    final Map<JobId, JobStatus> statuses = Maps.newTreeMap();
    statuses.putAll(client.jobStatuses(jobIds).get());
    if (json) {
        showJsonStatuses(out, hostPattern, jobIds, statuses);
        return 0;
    }
    final JobStatusTable table = jobStatusTable(out, full);
    final boolean noHostMatchedEver = showStatusesForHosts(hostPattern, jobIds, statuses, new HostStatusDisplayer() {

        @Override
        public void matchedStatus(JobStatus jobStatus, Iterable<String> matchingHosts, Map<String, TaskStatus> taskStatuses) {
            displayTask(full, table, jobStatus.getJob().getId(), jobStatus, taskStatuses, matchingHosts);
        }
    });
    if (noHostMatchedEver) {
        String domainsSwitchString = "";
        final List<String> domains = options.get("domains");
        if (domains.size() > 0) {
            domainsSwitchString = "-d " + Joiner.on(",").join(domains);
        }
        out.printf("There are no jobs deployed to hosts with the host pattern '%s'%n" + "Run 'helios %s hosts %s' to check your host exists and is up.%n", hostPattern, domainsSwitchString, hostPattern);
        return 1;
    }
    table.print();
    return 0;
}
Also used : JobStatusTable(com.spotify.helios.cli.JobStatusTable) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobStatus(com.spotify.helios.common.descriptors.JobStatus) Job(com.spotify.helios.common.descriptors.Job) JobId(com.spotify.helios.common.descriptors.JobId)

Example 37 with Job

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

the class TemporaryJobs method jobWithConfig.

public TemporaryJobBuilder jobWithConfig(final String configFile) throws IOException {
    checkNotNull(configFile);
    final Path configPath = Paths.get(configFile);
    final File file = configPath.toFile();
    if (!file.exists() || !file.isFile() || !file.canRead()) {
        throw new IllegalArgumentException("Cannot read file " + file);
    }
    final byte[] bytes = Files.readAllBytes(configPath);
    final String config = new String(bytes, UTF_8);
    final Job job = Json.read(config, Job.class);
    return this.job(job.toBuilder());
}
Also used : Path(java.nio.file.Path) Job(com.spotify.helios.common.descriptors.Job) File(java.io.File)

Example 38 with Job

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

the class WildcardJobCommand method run.

@Override
int run(final Namespace options, final HeliosClient client, final PrintStream out, final boolean json, final BufferedReader stdin) throws ExecutionException, InterruptedException, IOException {
    final String jobIdString = options.getString(jobArg.getDest());
    final Map<JobId, Job> jobs = client.jobs(jobIdString).get();
    if (jobs.size() == 0) {
        if (!json) {
            out.printf("Unknown job: %s%n", jobIdString);
        } else {
            final JobDeployResponse jobDeployResponse = new JobDeployResponse(JobDeployResponse.Status.JOB_NOT_FOUND, null, null);
            out.print(jobDeployResponse.toJsonString());
        }
        return 1;
    } else if (jobs.size() > 1) {
        if (!json) {
            out.printf("Ambiguous job reference: %s%n", jobIdString);
        } else {
            final JobDeployResponse jobDeployResponse = new JobDeployResponse(JobDeployResponse.Status.AMBIGUOUS_JOB_REFERENCE, null, null);
            out.print(jobDeployResponse.toJsonString());
        }
        return 1;
    }
    final JobId jobId = Iterables.getOnlyElement(jobs.keySet());
    return runWithJobId(options, client, out, json, jobId, stdin);
}
Also used : Job(com.spotify.helios.common.descriptors.Job) JobDeployResponse(com.spotify.helios.common.protocol.JobDeployResponse) JobId(com.spotify.helios.common.descriptors.JobId)

Example 39 with Job

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

Example 40 with Job

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

the class ZooKeeperMasterModel method updateDeployment.

/**
   * Used to update the existing deployment of a job.
   */
@Override
public void updateDeployment(final String host, final Deployment deployment, final String token) throws HostNotFoundException, JobNotDeployedException, TokenVerificationException {
    log.info("updating deployment {}: {}", deployment, host);
    final ZooKeeperClient client = provider.get("updateDeployment");
    final JobId jobId = deployment.getJobId();
    final Job job = getJob(client, jobId);
    final Deployment existingDeployment = getDeployment(host, jobId);
    if (job == null) {
        throw new JobNotDeployedException(host, jobId);
    }
    verifyToken(token, job);
    assertHostExists(client, host);
    assertTaskExists(client, host, deployment.getJobId());
    final String path = Paths.configHostJob(host, jobId);
    final Task task = new Task(job, deployment.getGoal(), existingDeployment.getDeployerUser(), existingDeployment.getDeployerMaster(), existingDeployment.getDeploymentGroupName());
    try {
        client.setData(path, task.toJsonBytes());
    } catch (Exception e) {
        throw new HeliosRuntimeException("updating deployment " + deployment + " on host " + host + " failed", e);
    }
}
Also used : Task(com.spotify.helios.common.descriptors.Task) RolloutTask(com.spotify.helios.common.descriptors.RolloutTask) ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) Deployment(com.spotify.helios.common.descriptors.Deployment) Job(com.spotify.helios.common.descriptors.Job) JobId(com.spotify.helios.common.descriptors.JobId) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) NotEmptyException(org.apache.zookeeper.KeeperException.NotEmptyException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) BadVersionException(org.apache.zookeeper.KeeperException.BadVersionException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException)

Aggregations

Job (com.spotify.helios.common.descriptors.Job)79 Test (org.junit.Test)57 JobId (com.spotify.helios.common.descriptors.JobId)38 HeliosClient (com.spotify.helios.client.HeliosClient)25 Deployment (com.spotify.helios.common.descriptors.Deployment)21 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)16 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)15 JobStatus (com.spotify.helios.common.descriptors.JobStatus)12 JobDeployResponse (com.spotify.helios.common.protocol.JobDeployResponse)11 ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)10 KeeperException (org.apache.zookeeper.KeeperException)10 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)10 PortMapping (com.spotify.helios.common.descriptors.PortMapping)9 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)8 ServiceEndpoint (com.spotify.helios.common.descriptors.ServiceEndpoint)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 DockerClient (com.spotify.docker.client.DockerClient)7 ZooKeeperOperation (com.spotify.helios.servicescommon.coordination.ZooKeeperOperation)7 IOException (java.io.IOException)7 ServicePorts (com.spotify.helios.common.descriptors.ServicePorts)6