Search in sources :

Example 26 with HalException

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);
    }
}
Also used : HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) RetrofitError(retrofit.RetrofitError) ProblemBuilder(com.netflix.spinnaker.halyard.core.problem.v1.ProblemBuilder)

Example 27 with HalException

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);
}
Also used : DeploymentDetails(com.netflix.spinnaker.halyard.deploy.deployment.v1.DeploymentDetails) Path(java.nio.file.Path) Arrays(java.util.Arrays) Autowired(org.springframework.beans.factory.annotation.Autowired) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) HashMap(java.util.HashMap) ServiceProviderFactory(com.netflix.spinnaker.halyard.deploy.deployment.v1.ServiceProviderFactory) DaemonTaskHandler(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskHandler) ArrayList(java.util.ArrayList) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) Severity(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) Path(java.nio.file.Path) DeploymentDetails(com.netflix.spinnaker.halyard.deploy.deployment.v1.DeploymentDetails) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) DeploymentConfiguration(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration) ConfigParser(com.netflix.spinnaker.halyard.deploy.config.v1.ConfigParser) Collectors(java.util.stream.Collectors) HalconfigDirectoryStructure(com.netflix.spinnaker.halyard.config.config.v1.HalconfigDirectoryStructure) File(java.io.File) ConfigProblemBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) DeploymentService(com.netflix.spinnaker.halyard.config.services.v1.DeploymentService) Profile(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile) Paths(java.nio.file.Paths) Data(lombok.Data) Optional(java.util.Optional) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) SpinnakerServiceProvider(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerServiceProvider) Collections(java.util.Collections) Optional(java.util.Optional) HashMap(java.util.HashMap) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) IOException(java.io.IOException) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) Profile(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile) ConfigProblemBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) DeploymentConfiguration(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration)

Example 28 with HalException

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);
    }
}
Also used : Field(java.lang.reflect.Field) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException)

Example 29 with HalException

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));
}
Also used : Arrays(java.util.Arrays) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) Data(lombok.Data) Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) Optional(java.util.Optional) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) Field(java.lang.reflect.Field) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException)

Example 30 with HalException

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.");
    }
}
Also used : HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HashMap(java.util.HashMap) Map(java.util.Map)

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