use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.
the class ContentPackService method getUninstallDetails.
private ContentPackUninstallDetails getUninstallDetails(ContentPackV1 contentPack, ContentPackInstallation installation) {
final Entity rootEntity = EntityV1.createRoot(contentPack);
final ImmutableMap<String, ValueReference> parameters = installation.parameters();
final ImmutableGraph<Entity> dependencyGraph = buildEntityGraph(rootEntity, contentPack.entities(), parameters);
final Traverser<Entity> entityTraverser = Traverser.forGraph(dependencyGraph);
final Iterable<Entity> entitiesInOrder = entityTraverser.breadthFirst(rootEntity);
final Set<NativeEntityDescriptor> nativeEntityDescriptors = new HashSet<>();
entitiesInOrder.forEach((entity -> {
if (entity.equals(rootEntity)) {
return;
}
final Optional<NativeEntityDescriptor> nativeEntityDescriptorOptional = installation.entities().stream().filter(descriptor -> entity.id().equals(descriptor.contentPackEntityId())).findFirst();
if (nativeEntityDescriptorOptional.isPresent()) {
NativeEntityDescriptor nativeEntityDescriptor = nativeEntityDescriptorOptional.get();
if (contentPackInstallationPersistenceService.countInstallationOfEntityById(nativeEntityDescriptor.id()) <= 1) {
nativeEntityDescriptors.add(nativeEntityDescriptor);
}
}
}));
return ContentPackUninstallDetails.create(nativeEntityDescriptors);
}
use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.
the class ContentPackPersistenceService method loadAllLatest.
public Set<ContentPack> loadAllLatest() {
final Set<ContentPack> allContentPacks = loadAll();
final ImmutableMultimap.Builder<ModelId, ContentPack> byIdBuilder = ImmutableMultimap.builder();
for (ContentPack contentPack : allContentPacks) {
byIdBuilder.put(contentPack.id(), contentPack);
}
final ImmutableMultimap<ModelId, ContentPack> contentPacksById = byIdBuilder.build();
final ImmutableSet.Builder<ContentPack> latestContentPacks = ImmutableSet.builderWithExpectedSize(contentPacksById.keySet().size());
for (ModelId id : contentPacksById.keySet()) {
final ImmutableCollection<ContentPack> contentPacks = contentPacksById.get(id);
final ContentPack latestContentPackRevision = Collections.max(contentPacks, Comparator.comparingInt(Revisioned::revision));
latestContentPacks.add(latestContentPackRevision);
}
return latestContentPacks.build();
}
use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.
the class ContentPackPersistenceService method filterMissingResourcesAndInsert.
public Optional<ContentPack> filterMissingResourcesAndInsert(final ContentPack pack) {
ContentPackV1 cpv1 = (ContentPackV1) pack;
final Set<String> allStreams = streamService.loadAll().stream().map(stream -> stream.getTitle()).collect(Collectors.toSet());
final Map<String, String> streamsInContentPack = new HashMap<>();
cpv1.entities().stream().filter(entity -> "stream".equals(entity.type().name()) && "1".equals(entity.type().version())).map(entity -> new Tuple2<String, JsonNode>(entity.id().id(), ((EntityV1) entity).data().findValue("title"))).forEach(tuple2 -> {
JsonNode title = tuple2.v2().findValue("@value");
streamsInContentPack.put(tuple2.v1(), title.textValue());
});
cpv1.entities().stream().filter(entity -> "dashboard".equals(entity.type().name()) && "2".equals(entity.type().version())).map(entity -> ((EntityV1) entity).data().findValue("search")).map(node -> node.findValue("queries")).map(node -> node.findValue("search_types")).forEach(node -> {
final ObjectNode parent = (ObjectNode) node.findParent("streams");
final ArrayNode streams = (ArrayNode) node.findValue("streams");
if (streams != null) {
final ArrayNode filtered = streams.deepCopy();
filtered.removeAll();
streams.forEach(stream -> {
final String sid = stream.textValue();
final String stitle = streamsInContentPack.get(sid);
if (allStreams.contains(stitle))
filtered.add(stream);
});
parent.replace("streams", filtered);
}
});
return this.insert(cpv1);
}
use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.
the class ContentPackLoaderPeriodical method doRun.
@Override
public void doRun() {
if (!Files.exists(contentPacksDir.toAbsolutePath())) {
LOG.warn("Could not find content packs directory {}. Please check your graylog configuration", contentPacksDir.toAbsolutePath());
return;
}
final List<Path> files = getFiles(contentPacksDir);
final Map<String, ContentPack> contentPacks = new HashMap<>(files.size());
for (Path file : files) {
final String fileName = file.getFileName().toString();
LOG.debug("Reading content pack from {}", file);
final byte[] bytes;
try {
bytes = Files.readAllBytes(file);
} catch (IOException e) {
LOG.warn("Couldn't read " + file + ". Skipping.", e);
continue;
}
LOG.debug("Parsing content pack from {}", file);
final ContentPack contentPack;
try {
contentPack = objectMapper.readValue(bytes, ContentPack.class);
} catch (IOException e) {
LOG.warn("Couldn't parse content pack in file " + file + ". Skipping", e);
continue;
}
if (contentPack instanceof ContentPackV1) {
ContentPackV1 contentPackV1 = (ContentPackV1) contentPack;
if (contentPackV1.parameters().asList().size() > 0) {
LOG.warn("Cannot accept content packs with parameters. Content Pack {}/{} rejected", contentPack.id(), contentPack.revision());
}
}
final Optional<ContentPack> existingContentPack = contentPackPersistenceService.findByIdAndRevision(contentPack.id(), contentPack.revision());
if (existingContentPack.isPresent()) {
LOG.debug("Content pack {}/{} already exists in database. Skipping.", contentPack.id(), contentPack.revision());
contentPacks.put(fileName, existingContentPack.get());
continue;
}
final Optional<ContentPack> insertedContentPack = contentPackPersistenceService.insert(contentPack);
if (!insertedContentPack.isPresent()) {
LOG.error("Error while inserting content pack " + file + " into database. Skipping.");
continue;
}
contentPacks.put(fileName, insertedContentPack.get());
}
LOG.debug("Applying selected content packs");
for (Map.Entry<String, ContentPack> entry : contentPacks.entrySet()) {
final String fileName = entry.getKey();
final ContentPack contentPack = entry.getValue();
if (contentPacksAutoInstall.contains(fileName)) {
if (!contentPackInstallationPersistenceService.findByContentPackIdAndRevision(contentPack.id(), contentPack.revision()).isEmpty()) {
LOG.debug("Content pack {}/{} ({}) already applied. Skipping.", contentPack.id(), contentPack.revision(), fileName);
continue;
}
contentPackService.installContentPack(contentPack, Collections.emptyMap(), "Installed by auto loader", configuration.getRootUsername());
}
}
}
use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.
the class V20180924111644_AddDefaultGrokPatterns method upgrade.
@Override
public void upgrade() {
if (configService.get(MigrationCompleted.class) != null) {
LOG.debug("Migration already completed.");
return;
}
try {
final URL contentPackURL = V20180924111644_AddDefaultGrokPatterns.class.getResource("V20180924111644_AddDefaultGrokPatterns_Default_Grok_Patterns.json");
final ContentPack contentPack = this.objectMapper.readValue(contentPackURL, ContentPack.class);
final ContentPack pack = this.contentPackPersistenceService.insert(contentPack).orElseThrow(() -> {
configService.write(MigrationCompleted.create(contentPack.id().toString()));
return new ContentPackException("Content pack " + contentPack.id() + " with this revision " + contentPack.revision() + " already found!");
});
try {
contentPackService.installContentPack(pack, Collections.emptyMap(), "Add default Grok patterns", "admin");
} catch (ContentPackException e) {
LOG.warn("Could not install default grok patterns: the installation found some modified default grok" + "patterns in your setup and did not update them. If you wish to use the default grok" + "patterns we provide, please delete the modified grok pattern and install the 'Default grok" + "patterns' content pack manually.");
}
configService.write(MigrationCompleted.create(pack.id().toString()));
} catch (IOException e) {
LOG.error("Unable to import content pack for default grok patterns: {}", e);
}
}
Aggregations