use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.
the class ContentPackService method uninstallContentPack.
private ContentPackUninstallation uninstallContentPack(ContentPackInstallation installation, ContentPackV1 contentPack) {
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> removedEntities = new HashSet<>();
final Set<NativeEntityDescriptor> failedEntities = new HashSet<>();
final Set<NativeEntityDescriptor> skippedEntities = new HashSet<>();
try {
for (Entity entity : entitiesInOrder) {
if (entity.equals(rootEntity)) {
continue;
}
final Optional<NativeEntityDescriptor> nativeEntityDescriptorOptional = installation.entities().stream().filter(descriptor -> entity.id().equals(descriptor.contentPackEntityId())).findFirst();
final EntityWithExcerptFacade facade = entityFacades.getOrDefault(entity.type(), UnsupportedEntityFacade.INSTANCE);
if (nativeEntityDescriptorOptional.isPresent()) {
final NativeEntityDescriptor nativeEntityDescriptor = nativeEntityDescriptorOptional.get();
final Optional nativeEntityOptional = facade.loadNativeEntity(nativeEntityDescriptor);
final ModelId entityId = nativeEntityDescriptor.id();
final long installCount = contentPackInstallationPersistenceService.countInstallationOfEntityById(entityId);
final long systemFoundCount = contentPackInstallationPersistenceService.countInstallationOfEntityByIdAndFoundOnSystem(entityId);
if (installCount > 1 || (installCount == 1 && systemFoundCount >= 1)) {
skippedEntities.add(nativeEntityDescriptor);
LOG.debug("Did not remove entity since other content pack installations still use them: {}", nativeEntityDescriptor);
} else if (nativeEntityOptional.isPresent()) {
final Object nativeEntity = nativeEntityOptional.get();
LOG.trace("Removing existing native entity for {} ({})", nativeEntityDescriptor);
try {
// The EntityFacade#delete() method expects the actual entity object
// noinspection unchecked
facade.delete(((NativeEntity) nativeEntity).entity());
removedEntities.add(nativeEntityDescriptor);
} catch (Exception e) {
LOG.warn("Couldn't remove native entity {}", nativeEntity);
failedEntities.add(nativeEntityDescriptor);
}
} else {
LOG.trace("Couldn't find existing native entity for {} ({})", nativeEntityDescriptor);
}
}
}
} catch (Exception e) {
throw new ContentPackException("Failed to remove content pack <" + contentPack.id() + "/" + contentPack.revision() + ">", e);
}
final int deletedInstallations = contentPackInstallationPersistenceService.deleteById(installation.id());
LOG.debug("Deleted {} installation(s) of content pack {}", deletedInstallations, contentPack.id());
return ContentPackUninstallation.builder().entities(ImmutableSet.copyOf(removedEntities)).skippedEntities(ImmutableSet.copyOf(skippedEntities)).failedEntities(ImmutableSet.copyOf(failedEntities)).build();
}
use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.
the class V20191219090834_AddSourcesPage method upgrade.
@Override
public void upgrade() {
if (configService.get(V20191219090834_AddSourcesPage.MigrationCompleted.class) != null) {
LOG.debug("Migration already completed.");
return;
}
try {
final URL contentPackURL = V20191219090834_AddSourcesPage.class.getResource("V20191219090834_AddSourcesPage_Content_Pack.json");
final ContentPack contentPack = this.objectMapper.readValue(contentPackURL, ContentPack.class);
final ContentPack pack = this.contentPackPersistenceService.insert(contentPack).orElseThrow(() -> {
configService.write(V20191219090834_AddSourcesPage.MigrationCompleted.create(contentPack.id().toString()));
return new ContentPackException("Content pack " + contentPack.id() + " with this revision " + contentPack.revision() + " already found!");
});
contentPackService.installContentPack(pack, Collections.emptyMap(), "Add Sources Page", "admin");
configService.write(V20191219090834_AddSourcesPage.MigrationCompleted.create(pack.id().toString()));
} catch (Exception e) {
throw new RuntimeException("Could not install Source Page Content Pack.", e);
}
}
use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.
the class ContentPackResource method downloadContentPackRevisions.
@GET
@Path("{contentPackId}/{revision}/download")
@Timed
@ApiOperation(value = "Download a revision of a content pack")
@ApiResponses(value = { @ApiResponse(code = 500, message = "Error loading content packs") })
@JsonView(ContentPackView.HttpView.class)
public ContentPack downloadContentPackRevisions(@ApiParam(name = "contentPackId", value = "Content pack ID", required = true) @PathParam("contentPackId") ModelId id, @ApiParam(name = "revision", value = "Content pack revision", required = true) @PathParam("revision") int revision) {
checkPermission(RestPermissions.CONTENT_PACK_READ, id.toString());
ContentPack contentPack = contentPackPersistenceService.findByIdAndRevision(id, revision).orElseThrow(() -> new NotFoundException("Content pack " + id + " with revision " + revision + " not found!"));
return contentPack;
}
use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.
the class ContentPackResource method installContentPack.
@POST
@Path("{contentPackId}/{revision}/installations")
@Timed
@ApiOperation(value = "Install a revision of a content pack")
@ApiResponses(value = { @ApiResponse(code = 500, message = "Error loading content packs") })
@AuditEvent(type = AuditEventTypes.CONTENT_PACK_INSTALL)
@JsonView(ContentPackView.HttpView.class)
public ContentPackInstallation installContentPack(@ApiParam(name = "contentPackId", value = "Content pack ID", required = true) @PathParam("contentPackId") ModelId id, @ApiParam(name = "revision", value = "Content pack revision", required = true) @PathParam("revision") int revision, @ApiParam(name = "installation request", value = "Content pack installation request", required = true) @Valid @NotNull ContentPackInstallationRequest contentPackInstallationRequest) {
checkPermission(RestPermissions.CONTENT_PACK_INSTALL, id.toString());
final ContentPack contentPack = contentPackPersistenceService.findByIdAndRevision(id, revision).orElseThrow(() -> new NotFoundException("Content pack " + id + " with revision " + revision + " not found!"));
final User currentUser = getCurrentUser();
final String userName = currentUser == null ? "unknown" : currentUser.getName();
final ContentPackInstallation installation = contentPackService.installContentPack(contentPack, contentPackInstallationRequest.parameters(), contentPackInstallationRequest.comment(), userName);
return installation;
}
use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.
the class ContentPackResource method uninstallDetails.
@GET
@Path("{contentPackId}/installations/{installationId}/uninstall_details")
@Timed
@ApiOperation(value = "Get details about which entities will actually be uninstalled")
@ApiResponses(value = { @ApiResponse(code = 500, message = "Error loading content packs") })
@JsonView(ContentPackView.HttpView.class)
public ContentPackUninstallDetails uninstallDetails(@ApiParam(name = "contentPackId", value = "Content pack ID", required = true) @PathParam("contentPackId") ModelId id, @ApiParam(name = "installationId", value = "Installation ID", required = true) @PathParam("installationId") String installationId) {
final ContentPackInstallation installation = contentPackInstallationPersistenceService.findById(new ObjectId(installationId)).orElseThrow(() -> new NotFoundException("Couldn't find installation " + installationId));
final ContentPack contentPack = contentPackPersistenceService.findByIdAndRevision(installation.contentPackId(), installation.contentPackRevision()).orElseThrow(() -> new NotFoundException("Couldn't find content pack " + installation.contentPackId() + " rev " + installation.contentPackRevision()));
return contentPackService.getUninstallDetails(contentPack, installation);
}
Aggregations