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;
}
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;
}
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);
}
}
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;
});
}
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;
}
Aggregations