Search in sources :

Example 1 with FATAL

use of com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL in project halyard by spinnaker.

the class HalconfigParser method cleanLocalFiles.

/**
 * Deletes all files in the staging directory that are not referenced in the hal config.
 */
public void cleanLocalFiles(Path stagingDirectoryPath) {
    if (!GlobalApplicationOptions.getInstance().isUseRemoteDaemon()) {
        return;
    }
    Halconfig halconfig = getHalconfig();
    Set<String> referencedFiles = new HashSet<String>();
    Consumer<Node> fileFinder = n -> referencedFiles.addAll(n.localFiles().stream().map(f -> {
        try {
            f.setAccessible(true);
            return (String) f.get(n);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Failed to clean staging directory: " + e.getMessage(), e);
        } finally {
            f.setAccessible(false);
        }
    }).filter(Objects::nonNull).collect(Collectors.toSet()));
    halconfig.recursiveConsume(fileFinder);
    Set<String> existingStagingFiles = ((List<File>) FileUtils.listFiles(stagingDirectoryPath.toFile(), TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE)).stream().map(f -> f.getAbsolutePath()).collect(Collectors.toSet());
    existingStagingFiles.removeAll(referencedFiles);
    try {
        for (String f : existingStagingFiles) {
            FileUtils.forceDelete(new File(f));
        }
    } catch (IOException e) {
        throw new HalException(FATAL, "Failed to clean staging directory: " + e.getMessage(), e);
    }
}
Also used : Autowired(org.springframework.beans.factory.annotation.Autowired) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) DaemonTaskHandler(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskHandler) Yaml(org.yaml.snakeyaml.Yaml) Halconfig(com.netflix.spinnaker.halyard.config.model.v1.node.Halconfig) HashSet(java.util.HashSet) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) ParseConfigException(com.netflix.spinnaker.halyard.config.error.v1.ParseConfigException) Severity(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity) TrueFileFilter(org.apache.commons.io.filefilter.TrueFileFilter) Path(java.nio.file.Path) GlobalApplicationOptions(com.netflix.spinnaker.halyard.core.GlobalApplicationOptions) ScannerException(org.yaml.snakeyaml.scanner.ScannerException) Set(java.util.Set) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) AtomicFileWriter(com.netflix.spinnaker.halyard.core.AtomicFileWriter) Objects(java.util.Objects) Consumer(java.util.function.Consumer) ConfigProblemBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) FATAL(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL) Paths(java.nio.file.Paths) ParserException(org.yaml.snakeyaml.parser.ParserException) Node(com.netflix.spinnaker.halyard.config.model.v1.node.Node) InputStream(java.io.InputStream) Halconfig(com.netflix.spinnaker.halyard.config.model.v1.node.Halconfig) Node(com.netflix.spinnaker.halyard.config.model.v1.node.Node) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException) Objects(java.util.Objects) List(java.util.List) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with FATAL

use of com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL in project halyard by spinnaker.

the class ResponseUnwrapper method formatProblemSet.

private static void formatProblemSet(ProblemSet problemSet) {
    if (problemSet == null || problemSet.isEmpty()) {
        return;
    }
    AnsiSnippet snippet = new AnsiSnippet("\r").setErase(AnsiErase.ERASE_LINE);
    AnsiPrinter.err.print(snippet.toString());
    Map<String, List<Problem>> locationGroup = problemSet.groupByLocation();
    for (Entry<String, List<Problem>> entry : locationGroup.entrySet()) {
        AnsiUi.problemLocation(entry.getKey());
        for (Problem problem : entry.getValue()) {
            Severity severity = problem.getSeverity();
            String message = problem.getMessage();
            String remediation = problem.getRemediation();
            List<String> options = problem.getOptions();
            switch(severity) {
                case FATAL:
                case ERROR:
                    AnsiUi.error(message);
                    break;
                case WARNING:
                    AnsiUi.warning(message);
                    break;
                default:
                    throw new RuntimeException("Unknown severity level " + severity);
            }
            if (remediation != null && !remediation.isEmpty()) {
                AnsiUi.remediation(remediation);
            }
            if (options != null && !options.isEmpty()) {
                AnsiUi.remediation("Options include: ");
                options.forEach(AnsiUi::listRemediation);
            }
            // Newline between errors
            AnsiUi.raw("");
        }
    }
}
Also used : Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) Severity(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity)

Example 3 with FATAL

use of com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL 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 4 with FATAL

use of com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL in project halyard by spinnaker.

the class Node method backupLocalFiles.

public List<String> backupLocalFiles(String outputPath) {
    List<String> files = new ArrayList<>();
    Consumer<Node> fileFinder = n -> files.addAll(n.localFiles().stream().map(f -> {
        try {
            f.setAccessible(true);
            String fPath = (String) f.get(n);
            if (fPath == null) {
                return null;
            }
            File fFile = new File(fPath);
            String fName = fFile.getName();
            // Hash the path to uniquely flatten all files into the output directory
            Path newName = Paths.get(outputPath, Math.abs(fPath.hashCode()) + "-" + fName);
            File parent = newName.toFile().getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            } else if (fFile.getParent().equals(parent.toString())) {
                // Don't move paths that are already in the right folder
                return fPath;
            }
            Files.copy(Paths.get(fPath), newName, REPLACE_EXISTING);
            f.set(n, newName.toString());
            return newName.toString();
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Failed to get local files for node " + n.getNodeName(), e);
        } catch (IOException e) {
            throw new HalException(FATAL, "Failed to backup user file: " + e.getMessage(), e);
        } finally {
            f.setAccessible(false);
        }
    }).filter(Objects::nonNull).collect(Collectors.toList()));
    recursiveConsume(fileFinder);
    return files;
}
Also used : Arrays(java.util.Arrays) Getter(lombok.Getter) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) HashMap(java.util.HashMap) ConfigProblemSetBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder) ArrayList(java.util.ArrayList) REMOVED(com.netflix.spinnaker.halyard.config.model.v1.node.NodeDiff.ChangeType.REMOVED) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) REPLACE_EXISTING(java.nio.file.StandardCopyOption.REPLACE_EXISTING) Method(java.lang.reflect.Method) Path(java.nio.file.Path) GlobalApplicationOptions(com.netflix.spinnaker.halyard.core.GlobalApplicationOptions) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) EDITED(com.netflix.spinnaker.halyard.config.model.v1.node.NodeDiff.ChangeType.EDITED) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) Consumer(java.util.function.Consumer) ADDED(com.netflix.spinnaker.halyard.config.model.v1.node.NodeDiff.ChangeType.ADDED) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) FATAL(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL) Paths(java.nio.file.Paths) CRC32(java.util.zip.CRC32) Path(java.nio.file.Path) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Objects(java.util.Objects) File(java.io.File)

Aggregations

HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)3 FATAL (com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 Paths (java.nio.file.Paths)3 List (java.util.List)3 Map (java.util.Map)3 Objects (java.util.Objects)3 Collectors (java.util.stream.Collectors)3 GlobalApplicationOptions (com.netflix.spinnaker.halyard.core.GlobalApplicationOptions)2 Problem (com.netflix.spinnaker.halyard.core.problem.v1.Problem)2 Severity (com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity)2 DaemonTaskHandler (com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskHandler)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 Compute (com.google.api.services.compute.Compute)1 AccessConfig (com.google.api.services.compute.model.AccessConfig)1