Search in sources :

Example 1 with DaemonTaskInterrupted

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

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

use of com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted 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 4 with DaemonTaskInterrupted

use of com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted 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)

Example 5 with DaemonTaskInterrupted

use of com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted in project halyard by spinnaker.

the class GoogleProviderUtils method ensureSshKeysExist.

private static void ensureSshKeysExist() {
    File sshKeyFile = new File(getSshKeyFile());
    if (!sshKeyFile.exists()) {
        if (!sshKeyFile.getParentFile().exists()) {
            sshKeyFile.getParentFile().mkdirs();
        }
        log.info("Generating a new ssh key file...");
        JobExecutor jobExecutor = DaemonTaskHandler.getJobExecutor();
        List<String> command = new ArrayList<>();
        command.add("ssh-keygen");
        // no password
        command.add("-N");
        command.add("");
        // rsa key
        command.add("-t");
        command.add("rsa");
        // path to keyfile
        command.add("-f");
        command.add(getSshKeyFile());
        // username sshing into machine
        command.add("-C");
        command.add("ubuntu");
        JobRequest request = new JobRequest().setTokenizedCommand(command);
        JobStatus status;
        try {
            status = jobExecutor.backoffWait(jobExecutor.startJob(request));
        } catch (InterruptedException e) {
            throw new DaemonTaskInterrupted(e);
        }
        if (status.getResult() == JobStatus.Result.FAILURE) {
            throw new HalException(FATAL, "ssh-keygen failed: " + status.getStdErr());
        }
        try {
            File sshPublicKeyFile = new File(getSshPublicKeyFile());
            String sshKeyContents = IOUtils.toString(new FileInputStream(sshPublicKeyFile));
            if (!sshKeyContents.startsWith("ubuntu:")) {
                sshKeyContents = "ubuntu:" + sshKeyContents;
                FileUtils.writeByteArrayToFile(sshPublicKeyFile, sshKeyContents.getBytes());
            }
        } catch (IOException e) {
            throw new HalException(FATAL, "Cannot reformat ssh key to match google key format expectation: " + e.getMessage(), e);
        }
        command = new ArrayList<>();
        command.add("chmod");
        command.add("400");
        command.add(getSshKeyFile());
        request = new JobRequest().setTokenizedCommand(command);
        try {
            status = jobExecutor.backoffWait(jobExecutor.startJob(request));
        } catch (InterruptedException e) {
            throw new DaemonTaskInterrupted(e);
        }
        if (status.getResult() == JobStatus.Result.FAILURE) {
            throw new HalException(FATAL, "chmod failed: " + status.getStdErr() + status.getStdOut());
        }
    }
}
Also used : HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DaemonTaskInterrupted(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted) FileInputStream(java.io.FileInputStream) 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) File(java.io.File)

Aggregations

DaemonTaskInterrupted (com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted)7 JobRequest (com.netflix.spinnaker.halyard.core.job.v1.JobRequest)6 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)5 JobStatus (com.netflix.spinnaker.halyard.core.job.v1.JobStatus)5 JobExecutor (com.netflix.spinnaker.halyard.core.job.v1.JobExecutor)2 File (java.io.File)2 IOException (java.io.IOException)2 ManagedInstance (com.google.api.services.compute.model.ManagedInstance)1 RunningServiceDetails (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails)1 ServiceSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1