use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.
the class OrcaRunner method monitor.
private void monitor(Supplier<Pipeline> getPipeline) {
String status;
Pipeline pipeline;
Set<String> loggedTasks = new HashSet<>();
try {
pipeline = getPipeline.get();
status = pipeline.getStatus();
while (status.equalsIgnoreCase("running") || status.equalsIgnoreCase("not_started")) {
logPipelineOutput(pipeline, loggedTasks);
DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(10));
pipeline = getPipeline.get();
status = pipeline.getStatus();
}
} catch (RetrofitError e) {
throw new HalException(new ProblemBuilder(Problem.Severity.FATAL, "Failed to monitor task: " + e.getMessage()).build());
}
logPipelineOutput(pipeline, loggedTasks);
if (status.equalsIgnoreCase("terminal")) {
Problem problem = findExecutionError(pipeline);
throw new HalException(problem);
}
}
use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.
the class GenerateService method generateConfig.
/**
* Generate config for a given deployment.
*
* This involves a few steps:
*
* 1. Clear out old config generated in a prior run.
* 2. Generate configuration using the halconfig as the source of truth, while collecting files needed by
* the deployment.
*
* @param deploymentName is the deployment whose config to generate
* @param services is the list of services to generate configs for
* @return a mapping from components to the profile's required local files.
*/
public ResolvedConfiguration generateConfig(String deploymentName, List<SpinnakerService.Type> services) {
DaemonTaskHandler.newStage("Generating all Spinnaker profile files and endpoints");
log.info("Generating config from \"" + halconfigPath + "\" with deploymentName \"" + deploymentName + "\"");
File spinnakerStaging = halconfigDirectoryStructure.getStagingPath(deploymentName).toFile();
DeploymentConfiguration deploymentConfiguration = deploymentService.getDeploymentConfiguration(deploymentName);
DaemonTaskHandler.message("Building service endpoints");
SpinnakerServiceProvider<DeploymentDetails> serviceProvider = serviceProviderFactory.create(deploymentConfiguration);
SpinnakerRuntimeSettings runtimeSettings = serviceProvider.buildRuntimeSettings(deploymentConfiguration);
// Step 1.
try {
FileUtils.deleteDirectory(spinnakerStaging);
} catch (IOException e) {
throw new HalException(new ConfigProblemBuilder(Severity.FATAL, "Unable to clear old spinnaker config: " + e.getMessage() + ".").build());
}
Path userProfilePath = halconfigDirectoryStructure.getUserProfilePath(deploymentName);
List<String> userProfileNames = aggregateProfilesInPath(userProfilePath.toString(), "");
// Step 2.
Map<SpinnakerService.Type, Map<String, Profile>> serviceProfiles = new HashMap<>();
for (SpinnakerService service : serviceProvider.getServices()) {
boolean isDesiredService = services.stream().filter(s -> s.equals(service.getType())).count() > 0;
if (!isDesiredService) {
continue;
}
ServiceSettings settings = runtimeSettings.getServiceSettings(service);
if (settings == null || !settings.getEnabled()) {
continue;
}
List<Profile> profiles = service.getProfiles(deploymentConfiguration, runtimeSettings);
String pluralModifier = profiles.size() == 1 ? "" : "s";
String profileMessage = "Generated " + profiles.size() + " profile" + pluralModifier;
Map<String, Profile> outputProfiles = processProfiles(spinnakerStaging, profiles);
List<Profile> customProfiles = userProfileNames.stream().map(s -> (Optional<Profile>) service.customProfile(deploymentConfiguration, runtimeSettings, Paths.get(userProfilePath.toString(), s), s)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
pluralModifier = customProfiles.size() == 1 ? "" : "s";
profileMessage += " and discovered " + customProfiles.size() + " custom profile" + pluralModifier + " for " + service.getCanonicalName();
DaemonTaskHandler.message(profileMessage);
mergeProfilesAndPreserveProperties(outputProfiles, processProfiles(spinnakerStaging, customProfiles));
serviceProfiles.put(service.getType(), outputProfiles);
}
return new ResolvedConfiguration().setServiceProfiles(serviceProfiles).setRuntimeSettings(runtimeSettings);
}
use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.
the class SpinnakerRuntimeSettings method setServiceSettings.
public void setServiceSettings(SpinnakerService.Type type, ServiceSettings settings) {
Field serviceField = getServiceField(type.getCanonicalName());
serviceField.setAccessible(true);
try {
serviceField.set(services, settings);
} catch (IllegalAccessException e) {
throw new HalException(Problem.Severity.FATAL, "Can't access service field for " + type.toString() + ": " + e.getMessage());
} finally {
serviceField.setAccessible(false);
}
}
use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.
the class SpinnakerRuntimeSettings method getServiceField.
private Field getServiceField(String name) {
String reducedName = name.replace("-", "").replace("_", "");
Optional<Field> matchingField = Arrays.stream(Services.class.getDeclaredFields()).filter(f -> f.getName().equalsIgnoreCase(reducedName)).findFirst();
return matchingField.orElseThrow(() -> new HalException(Problem.Severity.FATAL, "Unknown service " + reducedName));
}
use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.
the class KubernetesV2ClouddriverProfileFactory method processKubernetesAccount.
private void processKubernetesAccount(KubernetesAccount account) {
if (account.usesServiceAccount()) {
return;
}
String kubeconfigFile = account.getKubeconfigFile();
String context = account.getContext();
FileInputStream is = null;
try {
is = new FileInputStream(kubeconfigFile);
} catch (FileNotFoundException e) {
throw new IllegalStateException("No kubeconfig file '" + kubeconfigFile + "' found, but validation passed: " + e.getMessage(), e);
}
Object obj = yamlParser.load(is);
Map<String, Object> parsedKubeconfig = objectMapper.convertValue(obj, new TypeReference<Map<String, Object>>() {
});
if (StringUtils.isEmpty(context)) {
context = (String) parsedKubeconfig.get("current-context");
}
if (StringUtils.isEmpty(context)) {
throw new HalException(Problem.Severity.FATAL, "No context specified in kubernetes account " + account.getName() + " and no 'current-context' in " + kubeconfigFile);
}
final String finalContext = context;
String user = (String) ((List<Map<String, Object>>) parsedKubeconfig.getOrDefault("contexts", new ArrayList<>())).stream().filter(c -> c.getOrDefault("name", "").equals(finalContext)).findFirst().map(m -> ((Map<String, Object>) m.getOrDefault("context", new HashMap<>())).get("user")).orElse("");
if (StringUtils.isEmpty(user)) {
return;
}
Map<String, Object> userProperties = (Map<String, Object>) ((List<Map<String, Object>>) parsedKubeconfig.getOrDefault("users", new ArrayList<>())).stream().filter(c -> c.getOrDefault("name", "").equals(user)).findFirst().map(u -> u.get("user")).orElse(null);
Map<String, Object> authProvider = (Map<String, Object>) userProperties.get("auth-provider");
if (authProvider == null || !authProvider.getOrDefault("name", "").equals("gcp")) {
return;
}
Map<String, String> authProviderConfig = (Map<String, String>) authProvider.get("config");
if (authProviderConfig == null) {
return;
}
authProviderConfig.put("cmd-path", "gcloud");
try {
yamlParser.dump(parsedKubeconfig, new FileWriter(kubeconfigFile));
} catch (IOException e) {
throw new HalException(Problem.Severity.FATAL, "Unable to write the kubeconfig file to the staging area. This may be a user permissions issue.");
}
}
Aggregations