use of com.netflix.spinnaker.halyard.core.job.v1.JobStatus in project halyard by spinnaker.
the class KubernetesV1DistributedService method connectToInstance.
@Override
default <S> S connectToInstance(AccountDeploymentDetails<KubernetesAccount> details, SpinnakerRuntimeSettings runtimeSettings, SpinnakerService<S> sidecar, String instanceId) {
ServiceSettings settings = runtimeSettings.getServiceSettings(sidecar);
String namespace = getNamespace(settings);
int localPort = SocketUtils.findAvailableTcpPort();
int targetPort = settings.getPort();
List<String> command = KubernetesV1ProviderUtils.kubectlPortForwardCommand(details, namespace, instanceId, targetPort, localPort);
JobRequest request = new JobRequest().setTokenizedCommand(command);
String jobId = getJobExecutor().startJob(request);
// Wait for the proxy to spin up.
DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(5));
JobStatus status = getJobExecutor().updateJob(jobId);
// This should be a long-running job.
if (status.getState() == JobStatus.State.COMPLETED) {
throw new HalException(Problem.Severity.FATAL, "Unable to establish a proxy against " + getServiceName() + ":\n" + status.getStdOut() + "\n" + status.getStdErr());
}
return getServiceInterfaceFactory().createService(settings.getScheme() + "://localhost:" + localPort, sidecar);
}
use of com.netflix.spinnaker.halyard.core.job.v1.JobStatus in project halyard by spinnaker.
the class AbstractRemoteActionCommand method runRemoteAction.
protected void runRemoteAction(OperationHandler<RemoteAction> operation) {
RemoteAction action = operation.get();
String scriptPath = action.getScriptPath();
if (StringUtils.isEmpty(scriptPath)) {
return;
}
boolean shouldRun;
if (autoRun == null) {
shouldRun = action.isAutoRun();
} else {
shouldRun = action.isAutoRun() && autoRun;
}
if (!shouldRun) {
AnsiStoryBuilder storyBuilder = new AnsiStoryBuilder();
AnsiParagraphBuilder paragraphBuilder = storyBuilder.addParagraph();
paragraphBuilder.addSnippet(action.getScriptDescription());
storyBuilder.addNewline();
paragraphBuilder = storyBuilder.addParagraph();
paragraphBuilder.addSnippet("Please run the following script:");
storyBuilder.addNewline();
paragraphBuilder = storyBuilder.addParagraph();
paragraphBuilder.addSnippet(action.getScriptPath()).addStyle(AnsiStyle.UNDERLINE);
AnsiUi.raw(storyBuilder.toString());
} else {
List<String> command = new ArrayList<>();
command.add(scriptPath);
JobRequest request = new JobRequest().setTokenizedCommand(command);
JobExecutor executor = getJobExecutor();
String jobId = executor.startJobFromStandardStreams(request);
JobStatus status = null;
try {
status = executor.backoffWait(jobId);
} catch (InterruptedException e) {
AnsiUi.failure("Interrupted.");
System.exit(1);
}
if (status.getResult() != JobStatus.Result.SUCCESS) {
AnsiUi.error("Error encountered running script. See above output for more details.");
System.exit(1);
}
}
}
use of com.netflix.spinnaker.halyard.core.job.v1.JobStatus in project halyard by spinnaker.
the class KubernetesV1ProviderUtils method storeInstanceLogs.
static void storeInstanceLogs(JobExecutor jobExecutor, AccountDeploymentDetails<KubernetesAccount> details, String namespace, String instanceName, String containerName, File outputFile) {
List<String> command = kubectlAccountCommand(details);
command.add("--namespace");
command.add(namespace);
command.add("logs");
command.add(instanceName);
command.add(containerName);
JobRequest request = new JobRequest().setTokenizedCommand(command);
JobStatus status;
try {
status = jobExecutor.backoffWait(jobExecutor.startJob(request));
} catch (InterruptedException e) {
throw new DaemonTaskInterrupted(e);
}
try {
IOUtils.write(status.getStdOut().getBytes(), new FileOutputStream(new File(outputFile, containerName)));
} catch (IOException e) {
throw new HalException(Severity.FATAL, "Unable to store logs: " + e.getMessage(), e);
}
}
use of com.netflix.spinnaker.halyard.core.job.v1.JobStatus in project halyard by spinnaker.
the class KubernetesV2Utils method apply.
public static void apply(KubernetesAccount account, String manifest) {
manifest = prettify(manifest);
List<String> command = kubectlPrefix(account);
command.add("apply");
command.add("-f");
// read from stdin
command.add("-");
JobRequest request = new JobRequest().setTokenizedCommand(command);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
String jobId = DaemonTaskHandler.getJobExecutor().startJob(request, System.getenv(), new ByteArrayInputStream(manifest.getBytes()), stdout, stderr);
JobStatus status;
try {
status = DaemonTaskHandler.getJobExecutor().backoffWait(jobId);
} catch (InterruptedException e) {
throw new DaemonTaskInterrupted(e);
}
if (status.getState() != JobStatus.State.COMPLETED) {
throw new HalException(Problem.Severity.FATAL, String.join("\n", "Unterminated deployment of manifest:", manifest, stderr.toString(), stdout.toString()));
}
if (status.getResult() != JobStatus.Result.SUCCESS) {
throw new HalException(Problem.Severity.FATAL, String.join("\n", "Failed to deploy manifest:", manifest, stderr.toString(), stdout.toString()));
}
}
use of com.netflix.spinnaker.halyard.core.job.v1.JobStatus in project halyard by spinnaker.
the class KubernetesV2Utils method exists.
private static boolean exists(KubernetesAccount account, String namespace, String kind, String name) {
log.info("Checking for " + kind + "/" + name);
List<String> command = kubectlPrefix(account);
if (StringUtils.isNotEmpty(namespace)) {
command.add("-n");
command.add(namespace);
}
command.add("get");
command.add(kind);
command.add(name);
JobRequest request = new JobRequest().setTokenizedCommand(command);
String jobId = DaemonTaskHandler.getJobExecutor().startJob(request);
JobStatus status;
try {
status = DaemonTaskHandler.getJobExecutor().backoffWait(jobId);
} catch (InterruptedException e) {
throw new DaemonTaskInterrupted(e);
}
if (status.getState() != JobStatus.State.COMPLETED) {
throw new HalException(Problem.Severity.FATAL, String.join("\n", "Unterminated check for " + kind + "/" + name + " in " + namespace, status.getStdErr(), status.getStdOut()));
}
if (status.getResult() == JobStatus.Result.SUCCESS) {
return true;
} else if (status.getStdErr().contains("NotFound")) {
return false;
} else {
throw new HalException(Problem.Severity.FATAL, String.join("\n", "Failed check for " + kind + "/" + name + " in " + namespace, status.getStdErr(), status.getStdOut()));
}
}
Aggregations