Search in sources :

Example 1 with JobExecutor

use of com.netflix.spinnaker.halyard.core.job.v1.JobExecutor 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 2 with JobExecutor

use of com.netflix.spinnaker.halyard.core.job.v1.JobExecutor 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 3 with JobExecutor

use of com.netflix.spinnaker.halyard.core.job.v1.JobExecutor in project halyard by spinnaker.

the class KubernetesV1ProviderUtils method kubectlDeleteNamespaceCommand.

static void kubectlDeleteNamespaceCommand(JobExecutor jobExecutor, AccountDeploymentDetails<KubernetesAccount> details, String namespace) {
    List<String> command = kubectlAccountCommand(details);
    command.add("delete");
    command.add("namespace");
    command.add(namespace);
    JobRequest request = new JobRequest().setTokenizedCommand(command);
    try {
        jobExecutor.backoffWait(jobExecutor.startJob(request));
    } catch (InterruptedException e) {
        throw new DaemonTaskInterrupted(e);
    }
}
Also used : JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) DaemonTaskInterrupted(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted)

Example 4 with JobExecutor

use of com.netflix.spinnaker.halyard.core.job.v1.JobExecutor in project halyard by spinnaker.

the class GoogleProviderUtils method closeSshTunnel.

private static void closeSshTunnel(Proxy proxy) {
    JobExecutor jobExecutor = DaemonTaskHandler.getJobExecutor();
    jobExecutor.cancelJob(proxy.getJobId());
}
Also used : JobExecutor(com.netflix.spinnaker.halyard.core.job.v1.JobExecutor)

Example 5 with JobExecutor

use of com.netflix.spinnaker.halyard.core.job.v1.JobExecutor in project halyard by spinnaker.

the class GoogleProviderUtils method openSshTunnel.

private static Proxy openSshTunnel(String ip, int port, String keyFile) throws InterruptedException {
    JobExecutor jobExecutor = DaemonTaskHandler.getJobExecutor();
    List<String> command = new ArrayList<>();
    // Make sure we don't have an entry for this host already (GCP recycles IPs).
    command.add("ssh-keygen");
    command.add("-R");
    command.add(ip);
    JobRequest request = new JobRequest().setTokenizedCommand(command);
    JobStatus status = jobExecutor.backoffWait(jobExecutor.startJob(request));
    if (status.getResult() != JobStatus.Result.SUCCESS) {
        if (status.getStdErr().contains("No such file")) {
            log.info("No ssh known_hosts file exists yet");
        } else {
            throw new HalException(FATAL, "Unable to remove old host entry " + status.getStdErr());
        }
    }
    int localPort = SocketUtils.findAvailableTcpPort();
    command.clear();
    command.add("ssh");
    command.add("ubuntu@" + ip);
    command.add("-o");
    command.add("StrictHostKeyChecking=no");
    command.add("-i");
    command.add(keyFile);
    command.add("-N");
    command.add("-L");
    command.add(String.format("%d:localhost:%d", localPort, port));
    request = new JobRequest().setTokenizedCommand(command);
    String jobId = jobExecutor.startJob(request);
    status = jobExecutor.updateJob(jobId);
    while (status == null) {
        DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(1));
        status = jobExecutor.updateJob(jobId);
    }
    return new Proxy().setJobId(jobId).setPort(localPort);
}
Also used : 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) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArrayList(java.util.ArrayList)

Aggregations

JobRequest (com.netflix.spinnaker.halyard.core.job.v1.JobRequest)7 JobStatus (com.netflix.spinnaker.halyard.core.job.v1.JobStatus)7 JobExecutor (com.netflix.spinnaker.halyard.core.job.v1.JobExecutor)6 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)5 DaemonTaskInterrupted (com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted)4 ArrayList (java.util.ArrayList)3 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 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 URISyntaxException (java.net.URISyntaxException)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1