Search in sources :

Example 31 with ServiceSettings

use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings in project halyard by spinnaker.

the class GoogleDistributedService method getRunningServiceDetails.

@Override
default RunningServiceDetails getRunningServiceDetails(AccountDeploymentDetails<GoogleAccount> details, SpinnakerRuntimeSettings runtimeSettings) {
    ServiceSettings settings = runtimeSettings.getServiceSettings(getService());
    RunningServiceDetails result = new RunningServiceDetails();
    // All GCE load balancing is done via consul
    result.setLoadBalancer(new RunningServiceDetails.LoadBalancer().setExists(true));
    Compute compute = GoogleProviderUtils.getCompute(details);
    GoogleAccount account = details.getAccount();
    List<InstanceGroupManager> migs;
    try {
        migs = compute.instanceGroupManagers().list(account.getProject(), settings.getLocation()).execute().getItems();
        if (migs == null) {
            migs = Collections.emptyList();
        }
    } catch (IOException e) {
        throw new HalException(FATAL, "Failed to load MIGS: " + e.getMessage(), e);
    }
    boolean consulEnabled = getSidecars(runtimeSettings).stream().anyMatch(s -> s.getService().getType().equals(SpinnakerService.Type.CONSUL_CLIENT));
    Set<String> healthyConsulInstances = consulEnabled ? getConsulServerService().connectToPrimaryService(details, runtimeSettings).serviceHealth(getService().getCanonicalName(), true).stream().map(s -> s != null && s.getNode() != null ? s.getNode().getNodeName() : null).filter(Objects::nonNull).collect(Collectors.toSet()) : new HashSet<>();
    String serviceName = getService().getServiceName();
    migs = migs.stream().filter(ig -> ig.getName().startsWith(serviceName + "-v")).collect(Collectors.toList());
    Map<Integer, List<RunningServiceDetails.Instance>> instances = migs.stream().reduce(new HashMap<>(), (map, mig) -> {
        Names names = Names.parseName(mig.getName());
        Integer version = names.getSequence();
        List<RunningServiceDetails.Instance> computeInstances;
        try {
            List<ManagedInstance> managedInstances = compute.instanceGroupManagers().listManagedInstances(account.getProject(), settings.getLocation(), mig.getName()).execute().getManagedInstances();
            if (managedInstances == null) {
                managedInstances = new ArrayList<>();
            }
            computeInstances = managedInstances.stream().map(i -> {
                String instanceUrl = i.getInstance();
                String instanceStatus = i.getInstanceStatus();
                boolean running = instanceStatus != null && instanceStatus.equalsIgnoreCase("running");
                String instanceName = instanceUrl.substring(instanceUrl.lastIndexOf('/') + 1, instanceUrl.length());
                return new RunningServiceDetails.Instance().setId(instanceName).setLocation(settings.getLocation()).setRunning(running).setHealthy(!consulEnabled || healthyConsulInstances.contains(instanceName));
            }).collect(Collectors.toList());
        } catch (IOException e) {
            throw new HalException(FATAL, "Failed to load target pools for " + serviceName, e);
        }
        map.put(version, computeInstances);
        return map;
    }, (m1, m2) -> {
        m1.putAll(m2);
        return m1;
    });
    result.setInstances(instances);
    return result;
}
Also used : HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ManagedInstance(com.google.api.services.compute.model.ManagedInstance) ServiceAccount(com.google.api.services.compute.model.ServiceAccount) DaemonTaskHandler(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskHandler) AttachedDisk(com.google.api.services.compute.model.AttachedDisk) SidecarService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.SidecarService) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Names(com.netflix.frigga.Names) Map(java.util.Map) Provider(com.netflix.spinnaker.halyard.config.model.v1.node.Provider) URI(java.net.URI) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) Path(java.nio.file.Path) InstanceTemplate(com.google.api.services.compute.model.InstanceTemplate) ServiceInterfaceFactory(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceInterfaceFactory) VaultConnectionDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.VaultConnectionDetails) URIBuilder(org.apache.http.client.utils.URIBuilder) Set(java.util.Set) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) Operation(com.google.api.services.compute.model.Operation) Collectors(java.util.stream.Collectors) AttachedDiskInitializeParams(com.google.api.services.compute.model.AttachedDiskInitializeParams) Objects(java.util.Objects) List(java.util.List) InstanceProperties(com.google.api.services.compute.model.InstanceProperties) VaultConfigMount(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.VaultConfigMount) FATAL(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL) GoogleDiskType(com.netflix.spinnaker.clouddriver.google.model.GoogleDiskType) Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) DaemonTaskInterrupted(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) Compute(com.google.api.services.compute.Compute) Metadata(com.google.api.services.compute.model.Metadata) VaultConfigMountSet(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.VaultConfigMountSet) HashMap(java.util.HashMap) ArtifactService(com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AccountDeploymentDetails(com.netflix.spinnaker.halyard.deploy.deployment.v1.AccountDeploymentDetails) AccessConfig(com.google.api.services.compute.model.AccessConfig) GoogleAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount) ResolvedConfiguration(com.netflix.spinnaker.halyard.deploy.services.v1.GenerateService.ResolvedConfiguration) RandomStringUtils(org.apache.commons.lang.RandomStringUtils) InstanceGroupManager(com.google.api.services.compute.model.InstanceGroupManager) IOException(java.io.IOException) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) VaultServerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.VaultServerService) TimeUnit(java.util.concurrent.TimeUnit) GCEUtil(com.netflix.spinnaker.clouddriver.google.deploy.GCEUtil) Profile(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile) Paths(java.nio.file.Paths) DistributedService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.DistributedService) ConfigSource(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ConfigSource) Collections(java.util.Collections) GoogleAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount) ManagedInstance(com.google.api.services.compute.model.ManagedInstance) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) Names(com.netflix.frigga.Names) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) List(java.util.List) ArrayList(java.util.ArrayList) InstanceGroupManager(com.google.api.services.compute.model.InstanceGroupManager) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) IOException(java.io.IOException) Compute(com.google.api.services.compute.Compute) Objects(java.util.Objects) ManagedInstance(com.google.api.services.compute.model.ManagedInstance)

Example 32 with ServiceSettings

use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings 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 33 with ServiceSettings

use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings in project halyard by spinnaker.

the class SpinnakerRuntimeSettings method getServiceSettings.

private ServiceSettings getServiceSettings(String name) {
    Field serviceField = getServiceField(name);
    serviceField.setAccessible(true);
    try {
        return (ServiceSettings) serviceField.get(services);
    } catch (IllegalAccessException e) {
        throw new HalException(Problem.Severity.FATAL, "Can't access service field for " + name + ": " + e.getMessage());
    } finally {
        serviceField.setAccessible(false);
    }
}
Also used : Field(java.lang.reflect.Field) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)

Example 34 with ServiceSettings

use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings in project halyard by spinnaker.

the class SpinnakerRuntimeSettings method getAllServiceSettings.

@JsonIgnore
public Map<SpinnakerService.Type, ServiceSettings> getAllServiceSettings() {
    return Arrays.stream(Services.class.getDeclaredFields()).reduce(new HashMap<>(), (map, field) -> {
        if (!ServiceSettings.class.isAssignableFrom(field.getType())) {
            return map;
        }
        SpinnakerService.Type type = SpinnakerService.Type.fromCanonicalName(field.getName());
        ServiceSettings settings;
        try {
            settings = (ServiceSettings) field.get(services);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        if (settings != null) {
            map.put(type, settings);
        }
        return map;
    }, (map1, map2) -> {
        map1.putAll(map2);
        return map1;
    });
}
Also used : ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore)

Example 35 with ServiceSettings

use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings in project halyard by spinnaker.

the class ConsulClientService method getProfiles.

@Override
public List<Profile> getProfiles(DeploymentConfiguration deploymentConfiguration, SpinnakerRuntimeSettings endpoints) {
    List<Profile> result = new ArrayList<>();
    for (Map.Entry<Type, ServiceSettings> entry : endpoints.getAllServiceSettings().entrySet()) {
        ServiceSettings settings = entry.getValue();
        Type type = entry.getKey();
        if (!settings.getSidecar() && settings.getEnabled()) {
            String serviceName = type.getCanonicalName();
            String profileName = consulClientService(serviceName);
            String profilePath = Paths.get(CLIENT_OUTPUT_PATH, serviceName + ".json").toString();
            ProfileFactory factory = consulServiceProfileFactoryBuilder.build(type, settings);
            result.add(factory.getProfile(profileName, profilePath, deploymentConfiguration, endpoints));
        }
    }
    String profileName = clientProfileName;
    String profilePath = Paths.get(CLIENT_OUTPUT_PATH, profileName.split("/")[1]).toString();
    result.add(consulClientProfileFactory.getProfile(profileName, profilePath, deploymentConfiguration, endpoints));
    return result;
}
Also used : ProfileFactory(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.ProfileFactory) ConsulClientProfileFactory(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.consul.ConsulClientProfileFactory) Profile(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile)

Aggregations

ServiceSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)35 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)18 ArrayList (java.util.ArrayList)18 RunningServiceDetails (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails)14 HashMap (java.util.HashMap)14 SpinnakerRuntimeSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings)12 Map (java.util.Map)12 List (java.util.List)10 Profile (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile)9 ConfigSource (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ConfigSource)9 HasServiceSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.HasServiceSettings)8 SpinnakerService (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService)8 HashSet (java.util.HashSet)7 Names (com.netflix.frigga.Names)6 DistributedService (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.DistributedService)6 Collectors (java.util.stream.Collectors)6 KubernetesImageDescription (com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesImageDescription)5 KubernetesAccount (com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount)5 AccountDeploymentDetails (com.netflix.spinnaker.halyard.deploy.deployment.v1.AccountDeploymentDetails)5 ArtifactService (com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService)5