Search in sources :

Example 56 with HalException

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

the class GoogleDistributedService method stageProfiles.

@Override
default List<ConfigSource> stageProfiles(AccountDeploymentDetails<GoogleAccount> details, ResolvedConfiguration resolvedConfiguration) {
    String deploymentName = details.getDeploymentName();
    SpinnakerRuntimeSettings runtimeSettings = resolvedConfiguration.getRuntimeSettings();
    SpinnakerService thisService = getService();
    ServiceSettings thisServiceSettings = resolvedConfiguration.getServiceSettings(thisService);
    Map<String, String> env = new HashMap<>();
    Integer version = getRunningServiceDetails(details, runtimeSettings).getLatestEnabledVersion();
    if (version == null) {
        version = 0;
    } else {
        version++;
    }
    List<ConfigSource> configSources = new ArrayList<>();
    String stagingPath = getSpinnakerStagingPath(deploymentName);
    GoogleVaultServerService vaultService = getVaultServerService();
    VaultServerService.Vault vault = vaultService.connectToPrimaryService(details, runtimeSettings);
    for (SidecarService sidecarService : getSidecars(runtimeSettings)) {
        for (Profile profile : sidecarService.getSidecarProfiles(resolvedConfiguration, thisService)) {
            if (profile == null) {
                throw new HalException(Problem.Severity.FATAL, "Service " + sidecarService.getService().getCanonicalName() + " is required but was not supplied for deployment.");
            }
            String secretName = secretName(profile.getName(), version);
            String mountPoint = Paths.get(profile.getOutputFile()).toString();
            Path stagedFile = Paths.get(profile.getStagedFile(stagingPath));
            VaultConfigMount vaultConfigMount = VaultConfigMount.fromLocalFile(stagedFile.toFile(), mountPoint);
            secretName = vaultService.writeVaultConfig(deploymentName, vault, secretName, vaultConfigMount);
            configSources.add(new ConfigSource().setId(secretName).setMountPath(mountPoint));
        }
    }
    Map<String, Profile> serviceProfiles = resolvedConfiguration.getProfilesForService(thisService.getType());
    Set<String> requiredFiles = new HashSet<>();
    for (Map.Entry<String, Profile> entry : serviceProfiles.entrySet()) {
        Profile profile = entry.getValue();
        requiredFiles.addAll(profile.getRequiredFiles());
        env.putAll(profile.getEnv());
        String mountPoint = profile.getOutputFile();
        String secretName = secretName("profile-" + profile.getName(), version);
        Path stagedFile = Paths.get(profile.getStagedFile(stagingPath));
        VaultConfigMount vaultConfigMount = VaultConfigMount.fromLocalFile(stagedFile.toFile(), mountPoint);
        secretName = vaultService.writeVaultConfig(deploymentName, vault, secretName, vaultConfigMount);
        configSources.add(new ConfigSource().setId(secretName).setMountPath(mountPoint));
    }
    for (String file : requiredFiles) {
        String mountPoint = Paths.get(file).toString();
        String secretName = secretName("dependencies-" + file, version);
        VaultConfigMount vaultConfigMount = VaultConfigMount.fromLocalFile(Paths.get(file).toFile(), mountPoint);
        secretName = vaultService.writeVaultConfig(deploymentName, vault, secretName, vaultConfigMount);
        configSources.add(new ConfigSource().setId(secretName).setMountPath(mountPoint));
    }
    env.putAll(thisServiceSettings.getEnv());
    String envSourceFile = env.entrySet().stream().reduce("", (s, e) -> String.format("%s\n%s=%s", s, e.getKey(), e.getValue()), (s1, s2) -> String.join("\n", s1, s2));
    String mountPoint = getEnvFile();
    String secretName = secretName("env", version);
    VaultConfigMount vaultConfigMount = VaultConfigMount.fromString(envSourceFile, mountPoint);
    secretName = vaultService.writeVaultConfig(deploymentName, vault, secretName, vaultConfigMount);
    configSources.add(new ConfigSource().setId(secretName).setMountPath(mountPoint));
    return configSources;
}
Also used : Path(java.nio.file.Path) SidecarService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.SidecarService) HashMap(java.util.HashMap) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) ArrayList(java.util.ArrayList) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) Profile(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile) ConfigSource(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ConfigSource) Map(java.util.Map) HashMap(java.util.HashMap) VaultServerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.VaultServerService) VaultConfigMount(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.VaultConfigMount) HashSet(java.util.HashSet)

Example 57 with HalException

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

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

the class GoogleProviderUtils method ensureSpinnakerNetworkExists.

static String ensureSpinnakerNetworkExists(AccountDeploymentDetails<GoogleAccount> details) {
    String networkName = getNetworkName();
    String project = details.getAccount().getProject();
    Compute compute = getCompute(details);
    boolean exists = true;
    try {
        compute.networks().get(project, networkName).execute();
    } catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == 404) {
            exists = false;
        } else {
            throw new HalException(FATAL, "Google error encountered retrieving network: " + e.getMessage(), e);
        }
    } catch (IOException e) {
        throw new HalException(FATAL, "Failed to check if spinnaker network exists: " + e.getMessage(), e);
    }
    if (!exists) {
        String networkUrl;
        Network network = new Network().setAutoCreateSubnetworks(true).setName(networkName).setDescription("Spinnaker network auto-created by Halyard");
        try {
            DaemonTaskHandler.message("Creating a spinnaker network...");
            Operation operation = compute.networks().insert(project, network).execute();
            networkUrl = operation.getTargetLink();
            GoogleProviderUtils.waitOnGlobalOperation(compute, project, operation);
        } catch (IOException e) {
            throw new HalException(FATAL, "Failed to create Spinnaker network: " + e.getMessage(), e);
        }
        Firewall.Allowed allowSsh = new Firewall.Allowed().setPorts(Collections.singletonList("22")).setIPProtocol("tcp");
        Firewall firewallSsh = new Firewall().setNetwork(networkUrl).setAllowed(Collections.singletonList(allowSsh)).setName(networkName + "-allow-ssh").setSourceRanges(Collections.singletonList("0.0.0.0/0"));
        Firewall.Allowed allowInternalTcp = new Firewall.Allowed().setPorts(Collections.singletonList("1-65535")).setIPProtocol("tcp");
        Firewall.Allowed allowInternalUdp = new Firewall.Allowed().setPorts(Collections.singletonList("1-65535")).setIPProtocol("udp");
        Firewall.Allowed allowInternalIcmp = new Firewall.Allowed().setIPProtocol("icmp");
        List<Firewall.Allowed> allowInteral = new ArrayList<>();
        allowInteral.add(allowInternalTcp);
        allowInteral.add(allowInternalUdp);
        allowInteral.add(allowInternalIcmp);
        Firewall firewallInternal = new Firewall().setNetwork(networkUrl).setAllowed(allowInteral).setName(networkName + "-allow-internal").setSourceRanges(Collections.singletonList("10.0.0.0/8"));
        try {
            DaemonTaskHandler.message("Adding firewall rules...");
            compute.firewalls().insert(project, firewallSsh).execute();
            compute.firewalls().insert(project, firewallInternal).execute();
        } catch (IOException e) {
            throw new HalException(FATAL, "Failed to create Firewall rule network: " + e.getMessage(), e);
        }
    }
    return String.format("projects/%s/global/networks/%s", project, networkName);
}
Also used : HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Compute(com.google.api.services.compute.Compute)

Example 59 with HalException

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

the class BakeServiceProvider method getBakeService.

public <S> BakeService<S> getBakeService(SpinnakerService.Type type, Class<S> clazz) {
    Field serviceField = getField(type.getCanonicalName() + "service");
    if (serviceField == null) {
        return null;
    }
    serviceField.setAccessible(true);
    try {
        return (BakeService<S>) 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)

Example 60 with HalException

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

the class DistributedServiceProvider method getDeployableService.

public <S> DistributedService<S, T> getDeployableService(SpinnakerService.Type type, Class<S> clazz) {
    Field serviceField = getField(type.getCanonicalName() + "service");
    if (serviceField == null) {
        return null;
    }
    serviceField.setAccessible(true);
    try {
        return (DistributedService<S, T>) 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