Search in sources :

Example 11 with Node

use of com.netflix.spinnaker.halyard.config.model.v1.node.Node in project halyard by spinnaker.

the class ValidateService method recursiveValidate.

private void recursiveValidate(ConfigProblemSetBuilder psBuilder, Node node, NodeFilter filter) {
    int runCount = validatorCollection.runAllValidators(psBuilder, node);
    log.info("Ran " + runCount + " validators for node \"" + node.getNodeName() + "\" with class \"" + node.getClass().getSimpleName() + "\"");
    NodeIterator children = node.getChildren();
    Node recurse = children.getNext(filter);
    while (recurse != null) {
        recursiveValidate(psBuilder, recurse, filter);
        recurse = children.getNext(filter);
    }
}
Also used : NodeIterator(com.netflix.spinnaker.halyard.config.model.v1.node.NodeIterator) Node(com.netflix.spinnaker.halyard.config.model.v1.node.Node)

Example 12 with Node

use of com.netflix.spinnaker.halyard.config.model.v1.node.Node in project halyard by spinnaker.

the class FieldValidator method validateFieldForSpinnakerVersion.

private void validateFieldForSpinnakerVersion(ConfigProblemSetBuilder p, Node n) {
    DeploymentConfiguration deploymentConfiguration = n.parentOfType(DeploymentConfiguration.class);
    String spinnakerVersion = deploymentConfiguration.getVersion();
    if (spinnakerVersion == null) {
        return;
    }
    Class clazz = n.getClass();
    while (clazz != Object.class) {
        Class finalClazz = clazz;
        Arrays.stream(clazz.getDeclaredFields()).forEach(field -> {
            ValidForSpinnakerVersion annotation = field.getDeclaredAnnotation(ValidForSpinnakerVersion.class);
            try {
                field.setAccessible(true);
                Object v = field.get(n);
                boolean fieldNotValid = v != null && annotation != null && Versions.lessThan(spinnakerVersion, annotation.lowerBound());
                // If the field was set to false, it's assumed it's not enabling a restricted feature
                if (fieldNotValid && (v instanceof Boolean) && !((Boolean) v)) {
                    fieldNotValid = false;
                }
                if (fieldNotValid) {
                    p.addProblem(Problem.Severity.WARNING, "Field " + finalClazz.getSimpleName() + "." + field.getName() + " not supported for Spinnaker version " + spinnakerVersion + ": " + annotation.message()).setRemediation("Use at least " + annotation.lowerBound() + " (It may not have been released yet).");
                }
            } catch (NumberFormatException e) {
                log.info("Nightly builds do not contain version information.");
            } catch (IllegalAccessException e) {
                log.warn("Error validating field " + finalClazz.getSimpleName() + "." + field.getName() + ": ", e);
            }
        });
        clazz = clazz.getSuperclass();
    }
}
Also used : DeploymentConfiguration(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration) ValidForSpinnakerVersion(com.netflix.spinnaker.halyard.config.model.v1.node.ValidForSpinnakerVersion)

Example 13 with Node

use of com.netflix.spinnaker.halyard.config.model.v1.node.Node 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

Node (com.netflix.spinnaker.halyard.config.model.v1.node.Node)7 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)5 List (java.util.List)5 DeploymentConfiguration (com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration)4 ConfigProblemSetBuilder (com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 NodeIterator (com.netflix.spinnaker.halyard.config.model.v1.node.NodeIterator)3 GlobalApplicationOptions (com.netflix.spinnaker.halyard.core.GlobalApplicationOptions)3 FATAL (com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL)3 File (java.io.File)3 IOException (java.io.IOException)3 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ADDED (com.netflix.spinnaker.halyard.config.model.v1.node.NodeDiff.ChangeType.ADDED)2 EDITED (com.netflix.spinnaker.halyard.config.model.v1.node.NodeDiff.ChangeType.EDITED)2 REMOVED (com.netflix.spinnaker.halyard.config.model.v1.node.NodeDiff.ChangeType.REMOVED)2 NodeFilter (com.netflix.spinnaker.halyard.config.model.v1.node.NodeFilter)2 Validator (com.netflix.spinnaker.halyard.config.model.v1.node.Validator)2