Search in sources :

Example 16 with HalException

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

the class GoogleProviderUtils method defaultServiceAccount.

static String defaultServiceAccount(AccountDeploymentDetails<GoogleAccount> details) {
    GoogleAccount account = details.getAccount();
    String project = account.getProject();
    Compute compute = getCompute(details);
    try {
        return compute.projects().get(project).execute().getDefaultServiceAccount();
    } catch (IOException e) {
        throw new HalException(FATAL, "Unable to get default compute service account");
    }
}
Also used : GoogleAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount) Compute(com.google.api.services.compute.Compute) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException)

Example 17 with HalException

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

the class GoogleProviderUtils method getInstanceIp.

static String getInstanceIp(AccountDeploymentDetails<GoogleAccount> details, String instanceName) {
    Compute compute = getCompute(details);
    Instance instance = null;
    try {
        instance = compute.instances().get(details.getAccount().getProject(), "us-central1-f", instanceName).execute();
    } catch (IOException e) {
        throw new HalException(FATAL, "Unable to get instance " + instanceName);
    }
    return instance.getNetworkInterfaces().stream().map(i -> i.getAccessConfigs().stream().map(AccessConfig::getNatIP).filter(ip -> !StringUtils.isEmpty(ip)).findFirst()).filter(Optional::isPresent).map(Optional::get).findFirst().orElseThrow(() -> new HalException(FATAL, "No public IP associated with" + instanceName));
}
Also used : Optional(java.util.Optional) Compute(com.google.api.services.compute.Compute) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException)

Example 18 with HalException

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

Example 19 with HalException

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

Example 20 with HalException

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

the class KubectlServiceProvider method getService.

public <S> KubernetesV2Service getService(SpinnakerService.Type type, Class<S> clazz) {
    Field serviceField = getField(type.getCanonicalName() + "service");
    if (serviceField == null) {
        return null;
    }
    serviceField.setAccessible(true);
    try {
        return (KubernetesV2Service) serviceField.get(this);
    } catch (IllegalAccessException e) {
        throw new HalException(Problem.Severity.FATAL, "Can't access service field for " + type + ": " + e.getMessage());
    } finally {
        serviceField.setAccessible(false);
    }
}
Also used : Field(java.lang.reflect.Field) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException)

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