Search in sources :

Example 1 with JobStatus

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);
}
Also used : JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)

Example 2 with JobStatus

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);
        }
    }
}
Also used : AnsiParagraphBuilder(com.netflix.spinnaker.halyard.cli.ui.v1.AnsiParagraphBuilder) JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) JobExecutor(com.netflix.spinnaker.halyard.core.job.v1.JobExecutor) RemoteAction(com.netflix.spinnaker.halyard.core.RemoteAction) ArrayList(java.util.ArrayList) AnsiStoryBuilder(com.netflix.spinnaker.halyard.cli.ui.v1.AnsiStoryBuilder)

Example 3 with JobStatus

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);
    }
}
Also used : JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) FileOutputStream(java.io.FileOutputStream) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException) DaemonTaskInterrupted(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted) File(java.io.File)

Example 4 with JobStatus

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()));
    }
}
Also used : JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DaemonTaskInterrupted(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted)

Example 5 with JobStatus

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()));
    }
}
Also used : JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) DaemonTaskInterrupted(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted)

Aggregations

JobStatus (com.netflix.spinnaker.halyard.core.job.v1.JobStatus)12 JobRequest (com.netflix.spinnaker.halyard.core.job.v1.JobRequest)11 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)10 JobExecutor (com.netflix.spinnaker.halyard.core.job.v1.JobExecutor)5 DaemonTaskInterrupted (com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted)5 ArrayList (java.util.ArrayList)4 ServiceSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)2 File (java.io.File)2 IOException (java.io.IOException)2 AnsiParagraphBuilder (com.netflix.spinnaker.halyard.cli.ui.v1.AnsiParagraphBuilder)1 AnsiStoryBuilder (com.netflix.spinnaker.halyard.cli.ui.v1.AnsiStoryBuilder)1 KubernetesAccount (com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount)1 RemoteAction (com.netflix.spinnaker.halyard.core.RemoteAction)1 HasServiceSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.HasServiceSettings)1 KubernetesSharedServiceSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.kubernetes.KubernetesSharedServiceSettings)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 URISyntaxException (java.net.URISyntaxException)1