Search in sources :

Example 11 with JobStatus

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

the class GoogleProviderUtils method openSshTunnel.

static URI openSshTunnel(AccountDeploymentDetails<GoogleAccount> details, String instanceName, ServiceSettings service) throws InterruptedException {
    int port = service.getPort();
    String key = Proxy.buildKey(details.getDeploymentName(), instanceName, port);
    Proxy proxy = proxyMap.getOrDefault(key, new Proxy());
    JobExecutor jobExecutor = DaemonTaskHandler.getJobExecutor();
    if (proxy.getJobId() == null || !jobExecutor.jobExists(proxy.getJobId())) {
        String ip = getInstanceIp(details, instanceName);
        String keyFile = getSshKeyFile();
        log.info("Opening port " + port + " against instance " + instanceName);
        boolean connected = false;
        int tries = 0;
        while (!connected && tries < openSshRetries) {
            tries++;
            proxy = openSshTunnel(ip, port, keyFile);
            connected = checkIfProxyIsOpen(proxy);
            if (!connected) {
                if (!jobExecutor.jobExists(proxy.jobId) || jobExecutor.updateJob(proxy.jobId).getState() == JobStatus.State.COMPLETED) {
                    log.warn("SSH tunnel closed prematurely");
                }
                log.info("SSH tunnel never opened, retrying in case the instance hasn't started yet... (" + tries + "/" + openSshRetries + ")");
                closeSshTunnel(proxy);
                DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(10));
            }
        }
        if (!connected) {
            JobStatus status = jobExecutor.updateJob(proxy.getJobId());
            throw new HalException(FATAL, "Unable to connect to instance " + instanceName + ": " + status.getStdErr());
        }
        proxyMap.put(key, proxy);
    }
    try {
        return new URIBuilder().setScheme("http").setHost("localhost").setPort(proxy.getPort()).build();
    } catch (URISyntaxException e) {
        throw new RuntimeException("Failed to build URI for SSH connection", e);
    }
}
Also used : JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) JobExecutor(com.netflix.spinnaker.halyard.core.job.v1.JobExecutor) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) URISyntaxException(java.net.URISyntaxException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 12 with JobStatus

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

the class VaultService method publishSecret.

public void publishSecret(DeploymentConfiguration deploymentConfiguration, String name, String contents) {
    String vaultAddress = deploymentConfiguration.getDeploymentEnvironment().getVault().getAddress();
    String encodedContents = Base64.getEncoder().encodeToString(contents.getBytes());
    String secretName = vaultSecretPrefix + name;
    List<String> command = new ArrayList<>();
    command.add("vault");
    command.add("write");
    command.add("--address");
    command.add(vaultAddress);
    command.add(secretName);
    command.add(encodedContents);
    JobRequest request = new JobRequest().setTokenizedCommand(command).setTimeoutMillis(TimeUnit.SECONDS.toMillis(vaultTimeoutSeconds));
    String id = jobExecutor.startJob(request);
    DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(5));
    JobStatus status = jobExecutor.updateJob(id);
    if (!status.getResult().equals(JobStatus.Result.SUCCESS)) {
        throw new HalException(Problem.Severity.FATAL, "Failed to publish secret " + name + ": " + status.getStdOut() + status.getStdErr());
    }
}
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) ArrayList(java.util.ArrayList)

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