Search in sources :

Example 11 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException 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 12 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.

the class KubernetesV2Utils method createSecret.

public static String createSecret(KubernetesAccount account, String namespace, String name, List<SecretMountPair> files) {
    Map<String, String> contentMap = new HashMap<>();
    for (SecretMountPair pair : files) {
        String contents;
        try {
            contents = new String(Base64.getEncoder().encode(IOUtils.toByteArray(new FileInputStream(pair.getContents()))));
        } catch (IOException e) {
            throw new HalException(Problem.Severity.FATAL, "Failed to read required config file: " + pair.getContents().getAbsolutePath() + ": " + e.getMessage(), e);
        }
        contentMap.put(pair.getName(), contents);
    }
    name = name + "-" + Math.abs(contentMap.hashCode());
    TemplatedResource secret = new JinjaJarResource("/kubernetes/manifests/secret.yml");
    Map<String, Object> bindings = new HashMap<>();
    bindings.put("files", contentMap);
    bindings.put("name", name);
    bindings.put("namespace", namespace);
    secret.extendBindings(bindings);
    apply(account, secret.toString());
    return name;
}
Also used : HashMap(java.util.HashMap) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) JinjaJarResource(com.netflix.spinnaker.halyard.core.resource.v1.JinjaJarResource) TemplatedResource(com.netflix.spinnaker.halyard.core.resource.v1.TemplatedResource) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 13 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException 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 14 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.

the class KubernetesV1RedisService method connectToPrimaryService.

@Override
public Jedis connectToPrimaryService(AccountDeploymentDetails<KubernetesAccount> details, SpinnakerRuntimeSettings runtimeSettings) {
    ServiceSettings settings = runtimeSettings.getServiceSettings(this);
    List<String> command = Arrays.stream(connectCommand(details, runtimeSettings).split(" ")).collect(Collectors.toList());
    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 Redis:\n" + status.getStdOut() + "\n" + status.getStdErr());
    }
    return new Jedis("localhost", settings.getPort());
}
Also used : JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) Jedis(redis.clients.jedis.Jedis) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) HasServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.HasServiceSettings) KubernetesSharedServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.kubernetes.KubernetesSharedServiceSettings) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)

Example 15 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.

the class GoogleProviderUtils method getCompute.

static Compute getCompute(AccountDeploymentDetails<GoogleAccount> details) {
    ConfigProblemSetBuilder problemSetBuilder = new ConfigProblemSetBuilder(null);
    GoogleNamedAccountCredentials credentials = details.getAccount().getNamedAccountCredentials("", problemSetBuilder);
    if (credentials == null) {
        throw new HalException(problemSetBuilder.build().getProblems());
    }
    return credentials.getCompute();
}
Also used : ConfigProblemSetBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) GoogleNamedAccountCredentials(com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials)

Aggregations

HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)88 IOException (java.io.IOException)37 ConfigProblemBuilder (com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder)17 ServiceSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)16 ArrayList (java.util.ArrayList)15 FileInputStream (java.io.FileInputStream)14 File (java.io.File)12 HashMap (java.util.HashMap)12 JobStatus (com.netflix.spinnaker.halyard.core.job.v1.JobStatus)11 RunningServiceDetails (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails)11 Map (java.util.Map)11 JobRequest (com.netflix.spinnaker.halyard.core.job.v1.JobRequest)10 Field (java.lang.reflect.Field)9 SpinnakerRuntimeSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings)8 Path (java.nio.file.Path)8 List (java.util.List)7 Compute (com.google.api.services.compute.Compute)6 Problem (com.netflix.spinnaker.halyard.core.problem.v1.Problem)6 Paths (java.nio.file.Paths)6 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)5