Search in sources :

Example 31 with Halconfig

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

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

the class ArtifactProviderController method setArtifactProvider.

@RequestMapping(value = "/{providerName:.+}", method = RequestMethod.PUT)
DaemonTask<Halconfig, Void> setArtifactProvider(@PathVariable String deploymentName, @PathVariable String providerName, @RequestParam(required = false, defaultValue = DefaultControllerValues.validate) boolean validate, @RequestParam(required = false, defaultValue = DefaultControllerValues.severity) Severity severity, @RequestBody Object rawArtifactProvider) {
    ArtifactProvider provider = objectMapper.convertValue(rawArtifactProvider, Artifacts.translateArtifactProviderType(providerName));
    UpdateRequestBuilder builder = new UpdateRequestBuilder();
    Path configPath = halconfigDirectoryStructure.getConfigPath(deploymentName);
    builder.setStage(() -> provider.stageLocalFiles(configPath));
    builder.setUpdate(() -> providerService.setArtifactProvider(deploymentName, provider));
    builder.setSeverity(severity);
    Supplier<ProblemSet> doValidate = ProblemSet::new;
    if (validate) {
        doValidate = () -> providerService.validateArtifactProvider(deploymentName, providerName);
    }
    builder.setValidate(doValidate);
    builder.setRevert(() -> halconfigParser.undoChanges());
    builder.setSave(() -> halconfigParser.saveConfig());
    builder.setClean(() -> halconfigParser.cleanLocalFiles(configPath));
    return DaemonTaskHandler.submitTask(builder::build, "Edit the " + providerName + " provider");
}
Also used : Path(java.nio.file.Path) ArtifactProvider(com.netflix.spinnaker.halyard.config.model.v1.node.ArtifactProvider) UpdateRequestBuilder(com.netflix.spinnaker.halyard.core.DaemonResponse.UpdateRequestBuilder) ProblemSet(com.netflix.spinnaker.halyard.core.problem.v1.ProblemSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 33 with Halconfig

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

the class BakeryController method addBaseImage.

@RequestMapping(value = "/defaults/baseImage/", method = RequestMethod.POST)
DaemonTask<Halconfig, Void> addBaseImage(@PathVariable String deploymentName, @PathVariable String providerName, @RequestParam(required = false, defaultValue = DefaultControllerValues.validate) boolean validate, @RequestParam(required = false, defaultValue = DefaultControllerValues.severity) Severity severity, @RequestBody Object rawBaseImage) {
    BaseImage baseImage = objectMapper.convertValue(rawBaseImage, Providers.translateBaseImageType(providerName));
    UpdateRequestBuilder builder = new UpdateRequestBuilder();
    Path configPath = halconfigDirectoryStructure.getConfigPath(deploymentName);
    builder.setStage(() -> baseImage.stageLocalFiles(configPath));
    builder.setSeverity(severity);
    // TODO(lwander): Would be good to indicate when an added base image id conflicts with an existing base image id.
    builder.setUpdate(() -> bakeryService.addBaseImage(deploymentName, providerName, baseImage));
    Supplier<ProblemSet> doValidate = ProblemSet::new;
    if (validate) {
        doValidate = () -> bakeryService.validateBaseImage(deploymentName, providerName, baseImage.getBaseImage().getId());
    }
    builder.setValidate(doValidate);
    builder.setRevert(() -> halconfigParser.undoChanges());
    builder.setSave(() -> halconfigParser.saveConfig());
    builder.setClean(() -> halconfigParser.cleanLocalFiles(configPath));
    return DaemonTaskHandler.submitTask(builder::build, "Add " + baseImage.getNodeName() + " base image");
}
Also used : Path(java.nio.file.Path) UpdateRequestBuilder(com.netflix.spinnaker.halyard.core.DaemonResponse.UpdateRequestBuilder) ProblemSet(com.netflix.spinnaker.halyard.core.problem.v1.ProblemSet) BaseImage(com.netflix.spinnaker.halyard.config.model.v1.node.BaseImage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 34 with Halconfig

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

the class PersistentStorageController method setPersistentStore.

@RequestMapping(value = "/{persistentStoreType:.+}", method = RequestMethod.PUT)
DaemonTask<Halconfig, Void> setPersistentStore(@PathVariable String deploymentName, @PathVariable String persistentStoreType, @RequestParam(required = false, defaultValue = DefaultControllerValues.validate) boolean validate, @RequestParam(required = false, defaultValue = DefaultControllerValues.severity) Severity severity, @RequestBody Object rawPersistentStore) {
    PersistentStore persistentStore = objectMapper.convertValue(rawPersistentStore, PersistentStorage.translatePersistentStoreType(persistentStoreType));
    UpdateRequestBuilder builder = new UpdateRequestBuilder();
    Path configPath = halconfigDirectoryStructure.getConfigPath(deploymentName);
    builder.setStage(() -> persistentStore.stageLocalFiles(configPath));
    builder.setUpdate(() -> persistentStorageService.setPersistentStore(deploymentName, persistentStore));
    builder.setSeverity(severity);
    Supplier<ProblemSet> doValidate = ProblemSet::new;
    if (validate) {
        doValidate = () -> persistentStorageService.validatePersistentStore(deploymentName, persistentStoreType);
    }
    builder.setValidate(doValidate);
    builder.setRevert(() -> halconfigParser.undoChanges());
    builder.setSave(() -> halconfigParser.saveConfig());
    builder.setClean(() -> halconfigParser.cleanLocalFiles(configPath));
    return DaemonTaskHandler.submitTask(builder::build, "Edit persistent store");
}
Also used : Path(java.nio.file.Path) UpdateRequestBuilder(com.netflix.spinnaker.halyard.core.DaemonResponse.UpdateRequestBuilder) PersistentStore(com.netflix.spinnaker.halyard.config.model.v1.node.PersistentStore) ProblemSet(com.netflix.spinnaker.halyard.core.problem.v1.ProblemSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 35 with Halconfig

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

the class PubsubController method setPubsub.

@RequestMapping(value = "/{pubsubName:.+}", method = RequestMethod.PUT)
DaemonTask<Halconfig, Void> setPubsub(@PathVariable String deploymentName, @PathVariable String pubsubName, @RequestParam(required = false, defaultValue = DefaultControllerValues.validate) boolean validate, @RequestParam(required = false, defaultValue = DefaultControllerValues.severity) Severity severity, @RequestBody Object rawPubsub) {
    Pubsub pubsub = objectMapper.convertValue(rawPubsub, Pubsubs.translatePubsubType(pubsubName));
    UpdateRequestBuilder builder = new UpdateRequestBuilder();
    Path configPath = halconfigDirectoryStructure.getConfigPath(deploymentName);
    builder.setStage(() -> pubsub.stageLocalFiles(configPath));
    builder.setUpdate(() -> pubsubService.setPubsub(deploymentName, pubsub));
    builder.setSeverity(severity);
    Supplier<ProblemSet> doValidate = ProblemSet::new;
    if (validate) {
        doValidate = () -> pubsubService.validatePubsub(deploymentName, pubsubName);
    }
    builder.setValidate(doValidate);
    builder.setRevert(() -> halconfigParser.undoChanges());
    builder.setSave(() -> halconfigParser.saveConfig());
    builder.setClean(() -> halconfigParser.cleanLocalFiles(configPath));
    return DaemonTaskHandler.submitTask(builder::build, "Edit the " + pubsubName + " pubsub");
}
Also used : Path(java.nio.file.Path) Pubsub(com.netflix.spinnaker.halyard.config.model.v1.node.Pubsub) UpdateRequestBuilder(com.netflix.spinnaker.halyard.core.DaemonResponse.UpdateRequestBuilder) ProblemSet(com.netflix.spinnaker.halyard.core.problem.v1.ProblemSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RequestMapping (org.springframework.web.bind.annotation.RequestMapping)36 ProblemSet (com.netflix.spinnaker.halyard.core.problem.v1.ProblemSet)35 UpdateRequestBuilder (com.netflix.spinnaker.halyard.core.DaemonResponse.UpdateRequestBuilder)33 Path (java.nio.file.Path)33 ConfigProblemBuilder (com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder)11 Halconfig (com.netflix.spinnaker.halyard.config.model.v1.node.Halconfig)10 DeploymentConfiguration (com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration)9 ConfigNotFoundException (com.netflix.spinnaker.halyard.config.error.v1.ConfigNotFoundException)7 IllegalConfigException (com.netflix.spinnaker.halyard.config.error.v1.IllegalConfigException)6 NodeFilter (com.netflix.spinnaker.halyard.config.model.v1.node.NodeFilter)5 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)5 IOException (java.io.IOException)5 Node (com.netflix.spinnaker.halyard.config.model.v1.node.Node)4 AbstractCanaryAccount (com.netflix.spinnaker.halyard.config.model.v1.canary.AbstractCanaryAccount)3 Account (com.netflix.spinnaker.halyard.config.model.v1.node.Account)3 List (java.util.List)3 ParseConfigException (com.netflix.spinnaker.halyard.config.error.v1.ParseConfigException)2 ArtifactAccount (com.netflix.spinnaker.halyard.config.model.v1.node.ArtifactAccount)2 ArtifactProvider (com.netflix.spinnaker.halyard.config.model.v1.node.ArtifactProvider)2 BaseImage (com.netflix.spinnaker.halyard.config.model.v1.node.BaseImage)2