use of com.spotify.helios.common.protocol.JobUndeployResponse in project helios by spotify.
the class JobUndeployCommand 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 boolean all = options.getBoolean(allArg.getDest());
final boolean yes = options.getBoolean(yesArg.getDest());
final boolean force = options.getBoolean(forceArg.getDest());
final List<String> hosts;
if (force) {
log.warn("If you are using '--force' to skip the interactive prompt, " + "note that we have deprecated it. Please use '--yes'.");
}
if (all) {
final JobStatus status = client.jobStatus(jobId).get();
hosts = ImmutableList.copyOf(status.getDeployments().keySet());
if (hosts.isEmpty()) {
out.printf("%s is not currently deployed on any hosts.", jobId);
return 0;
}
if (!yes && !force) {
out.printf("This will undeploy %s from %s%n", jobId, hosts);
final boolean confirmed = Utils.userConfirmed(out, stdin);
if (!confirmed) {
return 1;
}
}
} else {
hosts = options.getList(hostsArg.getDest());
if (hosts.isEmpty()) {
out.println("Please either specify a list of hosts or use the -a/--all flag.");
return 1;
}
}
if (!json) {
out.printf("Undeploying %s from %s%n", jobId, hosts);
}
int code = 0;
final HostResolver resolver = HostResolver.create(client);
for (final String candidateHost : hosts) {
final String host = resolver.resolveName(candidateHost);
if (!json) {
out.printf("%s: ", host);
}
final String token = options.getString(tokenArg.getDest());
final JobUndeployResponse response = client.undeploy(jobId, host, token).get();
if (response.getStatus() == JobUndeployResponse.Status.OK) {
if (!json) {
out.println("done");
} else {
out.print(response.toJsonString());
}
} else {
if (!json) {
out.println("failed: " + response);
} else {
out.print(response.toJsonString());
}
code = -1;
}
}
return code;
}
use of com.spotify.helios.common.protocol.JobUndeployResponse in project helios by spotify.
the class Jobs method undeploy.
/**
* Undeploy the job from all specified hosts, and delete the job. Any failures will be ignored,
* and we will keep trying each host. A list of errors encountered along the way will be returned
* to the caller.
*
* @param client the HeliosClient to use
* @param job the job to undeploy and delete
* @param hosts the hosts to undeploy from
* @param errors errors encountered during the undeploy will be added to this list
*
* @return the list of errors
*/
static List<AssertionError> undeploy(final HeliosClient client, final Job job, final List<String> hosts, final List<AssertionError> errors) {
final JobId id = job.getId();
for (final String host : hosts) {
log.info("Undeploying {} from {}", getJobDescription(job), host);
final JobUndeployResponse response;
try {
response = get(client.undeploy(id, host));
if (response.getStatus() != JobUndeployResponse.Status.OK && response.getStatus() != JobUndeployResponse.Status.JOB_NOT_FOUND) {
errors.add(new AssertionError(format("Failed to undeploy job %s - %s", id, response)));
}
} catch (InterruptedException | ExecutionException | TimeoutException e) {
errors.add(new AssertionError(e));
}
}
try {
log.debug("Deleting job {}", id);
final JobDeleteResponse response = get(client.deleteJob(id));
if (response.getStatus() != JobDeleteResponse.Status.OK && response.getStatus() != JobDeleteResponse.Status.JOB_NOT_FOUND) {
errors.add(new AssertionError(format("Failed to delete job %s - %s", id.toString(), response.toString())));
}
} catch (InterruptedException | ExecutionException | TimeoutException e) {
errors.add(new AssertionError(e));
}
return errors;
}
use of com.spotify.helios.common.protocol.JobUndeployResponse in project helios by spotify.
the class JobUndeployCommand method runWithJob.
@Override
protected int runWithJob(final Namespace options, final HeliosClient client, final PrintStream out, final boolean json, final Job job, final BufferedReader stdin) throws ExecutionException, InterruptedException, IOException {
final JobId jobId = job.getId();
final boolean all = options.getBoolean(allArg.getDest());
final boolean yes = options.getBoolean(yesArg.getDest());
final List<String> hosts;
if (all) {
final JobStatus status = client.jobStatus(jobId).get();
hosts = ImmutableList.copyOf(status.getDeployments().keySet());
if (hosts.isEmpty()) {
out.printf("%s is not currently deployed on any hosts.", jobId);
return 0;
}
if (!yes) {
out.printf("This will undeploy %s from %s%n", jobId, hosts);
final boolean confirmed = Utils.userConfirmed(out, stdin);
if (!confirmed) {
return 1;
}
}
} else {
hosts = options.getList(hostsArg.getDest());
if (hosts.isEmpty()) {
out.println("Please either specify a list of hosts or use the -a/--all flag.");
return 1;
}
}
if (!json) {
out.printf("Undeploying %s from %s%n", jobId, hosts);
}
int code = 0;
final HostResolver resolver = HostResolver.create(client);
for (final String candidateHost : hosts) {
final String host = resolver.resolveName(candidateHost);
if (!json) {
out.printf("%s: ", host);
}
final String token = options.getString(tokenArg.getDest());
final JobUndeployResponse response = client.undeploy(jobId, host, token).get();
if (response.getStatus() == JobUndeployResponse.Status.OK) {
if (!json) {
out.println("done");
} else {
out.print(response.toJsonString());
}
} else {
if (!json) {
out.println("failed: " + response);
} else {
out.print(response.toJsonString());
}
code = -1;
}
}
return code;
}
use of com.spotify.helios.common.protocol.JobUndeployResponse in project helios by spotify.
the class HeliosSoloDeploymentTest method testUndeployLeftoverJobs.
@Test
public void testUndeployLeftoverJobs() throws Exception {
final HeliosSoloDeployment solo = buildHeliosSoloDeployment();
final ListenableFuture<List<String>> hostsFuture = Futures.<List<String>>immediateFuture(ImmutableList.of(HOST1, HOST2));
when(heliosClient.listHosts()).thenReturn(hostsFuture);
// These futures represent HostStatuses when the job is still deployed
final ListenableFuture<HostStatus> statusFuture11 = Futures.immediateFuture(HostStatus.newBuilder().setStatus(Status.UP).setStatuses(ImmutableMap.of(JOB_ID1, TASK_STATUS1)).setJobs(ImmutableMap.of(JOB_ID1, Deployment.of(JOB_ID1, Goal.START))).build());
final ListenableFuture<HostStatus> statusFuture21 = Futures.immediateFuture(HostStatus.newBuilder().setStatus(Status.UP).setStatuses(ImmutableMap.of(JOB_ID2, TASK_STATUS2)).setJobs(ImmutableMap.of(JOB_ID2, Deployment.of(JOB_ID2, Goal.START))).build());
// These futures represent HostStatuses when the job is undeployed
final ListenableFuture<HostStatus> statusFuture12 = Futures.immediateFuture(HostStatus.newBuilder().setStatus(Status.UP).setStatuses(Collections.<JobId, TaskStatus>emptyMap()).setJobs(ImmutableMap.of(JOB_ID1, Deployment.of(JOB_ID1, Goal.START))).build());
final ListenableFuture<HostStatus> statusFuture22 = Futures.immediateFuture(HostStatus.newBuilder().setStatus(Status.UP).setStatuses(Collections.<JobId, TaskStatus>emptyMap()).setJobs(ImmutableMap.of(JOB_ID2, Deployment.of(JOB_ID2, Goal.START))).build());
// noinspection unchecked
when(heliosClient.hostStatus(HOST1)).thenReturn(statusFuture11);
// noinspection unchecked
when(heliosClient.hostStatus(HOST2)).thenReturn(statusFuture21);
final ListenableFuture<JobUndeployResponse> undeployFuture1 = Futures.immediateFuture(new JobUndeployResponse(JobUndeployResponse.Status.OK, HOST1, JOB_ID1));
final ListenableFuture<JobUndeployResponse> undeployFuture2 = Futures.immediateFuture(new JobUndeployResponse(JobUndeployResponse.Status.OK, HOST2, JOB_ID2));
// when undeploy is called, respond correctly & patch the mock to return
// the undeployed HostStatus
when(heliosClient.undeploy(JOB_ID1, HOST1)).thenAnswer(new Answer<ListenableFuture<JobUndeployResponse>>() {
@Override
public ListenableFuture<JobUndeployResponse> answer(final InvocationOnMock invocation) throws Throwable {
when(heliosClient.hostStatus(HOST1)).thenReturn(statusFuture12);
return undeployFuture1;
}
});
when(heliosClient.undeploy(JOB_ID2, HOST2)).thenAnswer(new Answer<ListenableFuture<JobUndeployResponse>>() {
@Override
public ListenableFuture<JobUndeployResponse> answer(final InvocationOnMock invocation) throws Throwable {
when(heliosClient.hostStatus(HOST1)).thenReturn(statusFuture22);
return undeployFuture2;
}
});
solo.undeployLeftoverJobs();
verify(heliosClient).undeploy(JOB_ID1, HOST1);
verify(heliosClient).undeploy(JOB_ID2, HOST2);
}
use of com.spotify.helios.common.protocol.JobUndeployResponse in project helios by spotify.
the class HeliosIT method test.
@Test
public void test() throws Exception {
final CreateJobResponse create = cli(CreateJobResponse.class, "create", "test:1", "spotify/busybox:latest");
assertThat(create.getStatus(), equalTo(CreateJobResponse.Status.OK));
final JobDeployResponse deploy = cli(JobDeployResponse.class, "deploy", "test:1", TEST_HOST);
assertThat(deploy.getStatus(), equalTo(JobDeployResponse.Status.OK));
final JobUndeployResponse undeploy = cli(JobUndeployResponse.class, "undeploy", "--yes", "test:1", "-a");
assertThat(undeploy.getStatus(), equalTo(JobUndeployResponse.Status.OK));
final JobDeleteResponse delete = cli(JobDeleteResponse.class, "remove", "--yes", "test:1");
assertThat(delete.getStatus(), equalTo(JobDeleteResponse.Status.OK));
}
Aggregations