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;
}
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());
}
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);
}
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;
}
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);
}
}
Aggregations