use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.
the class ImpliedReposQueryDelegate method getGroupsContaining.
@Override
public Set<Group> getGroupsContaining(StoreKey key) throws IndyDataException {
ArtifactStore store = dataManager.getArtifactStore(key);
if (store == null) {
return Collections.emptySet();
}
boolean storeIsImplied = IMPLIED_REPO_ORIGIN.equals(store.getMetadata(METADATA_ORIGIN));
Set<Group> delegateGroups = delegate().getGroupsContaining(key);
if (!storeIsImplied || delegateGroups == null || delegateGroups.isEmpty()) {
return delegateGroups;
}
Set<Group> result = delegateGroups.stream().filter((group) -> config.isEnabledForGroup(group.getName())).collect(Collectors.toSet());
return result;
}
use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.
the class FoloPomDownloadListener method onFileUpload.
public void onFileUpload(@Observes final FileStorageEvent event) {
// check for a TransferOperation of DOWNLOAD
final TransferOperation op = event.getType();
if (op != TransferOperation.DOWNLOAD) {
logger.trace("Not a download transfer operation. No pom existence check performed.");
return;
}
// check if it is a path that doesn't end in with ".pom"
final Transfer transfer = event.getTransfer();
if (transfer == null) {
logger.trace("No transfer. No pom existence check performed.");
return;
}
String txfrPath = transfer.getPath();
if (txfrPath.endsWith(".pom")) {
logger.trace("This is a pom download.");
return;
}
// use ArtifactPathInfo to parse into a GAV, just to verify that it's looking at an artifact download
ArtifactPathInfo artPathInfo = ArtifactPathInfo.parse(txfrPath);
if (artPathInfo == null) {
logger.trace("Not an artifact download ({}). No pom existence check performed.", txfrPath);
return;
}
// verify that the associated .pom file exists
String pomFilename = String.format("%s-%s.pom", artPathInfo.getArtifactId(), artPathInfo.getVersion());
ConcreteResource pomResource = transfer.getResource().getParent().getChild(pomFilename);
if (cacheProvider.exists(pomResource)) {
logger.trace("Pom {} already exists.", cacheProvider.getFilePath(pomResource));
return;
}
// trigger pom download by requesting it from the same repository as the original artifact
StoreKey storeKey = StoreKey.fromString(transfer.getLocation().getName());
ArtifactStore store;
try {
store = storeManager.getArtifactStore(storeKey);
} catch (final IndyDataException ex) {
logger.error("Error retrieving artifactStore with key " + storeKey, ex);
return;
}
try {
logger.debug("Downloading POM as automatic response to associated artifact download: {}/{}", storeKey, pomResource.getPath());
contentManager.retrieve(store, pomResource.getPath(), event.getEventMetadata());
} catch (final IndyWorkflowException ex) {
logger.error("Error while retrieving pom artifact " + pomResource.getPath() + " from store " + store, ex);
return;
}
}
use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.
the class PromotionManager method promotePaths.
/**
* Promote artifacts from the source to the target given in the {@link PathsPromoteRequest}. If a set of paths are given, only try to promotePaths those.
* Otherwise, build a recursive list of available artifacts in the source store, and try to promotePaths them all.
*
* @param request The request containing source and target store keys, and an optional list of paths to promotePaths
*
* @return The result, including the source and target store keys used, the paths completed (promoted successfully), the pending paths (those that
* weren't processed due to some error...or null), and a nullable error explaining what (if anything) went wrong with the promotion.
*
* @throws PromotionException
* @throws IndyWorkflowException
*/
public PathsPromoteResult promotePaths(final PathsPromoteRequest request, final String baseUrl) throws PromotionException, IndyWorkflowException {
final Set<String> paths = request.getPaths();
final StoreKey source = request.getSource();
List<Transfer> contents;
if (paths == null || paths.isEmpty()) {
contents = downloadManager.listRecursively(source, DownloadManager.ROOT_PATH);
} else {
contents = getTransfersForPaths(source, paths);
}
final Set<String> pending = new HashSet<>();
for (final Transfer transfer : contents) {
pending.add(transfer.getPath());
}
ValidationResult validation = new ValidationResult();
validator.validate(request, validation, baseUrl);
if (request.isDryRun()) {
return new PathsPromoteResult(request, pending, Collections.emptySet(), Collections.emptySet(), validation);
} else if (validation.isValid()) {
return runPathPromotions(request, pending, Collections.emptySet(), Collections.emptySet(), contents, validation);
} else {
return new PathsPromoteResult(request, pending, Collections.emptySet(), Collections.emptySet(), validation);
}
}
use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.
the class PromotionManager method runPathPromotions.
private PathsPromoteResult runPathPromotions(final PathsPromoteRequest request, final Set<String> pending, final Set<String> prevComplete, final Set<String> prevSkipped, final List<Transfer> contents, ValidationResult validation) {
if (pending == null || pending.isEmpty()) {
return new PathsPromoteResult(request, pending, prevComplete, prevSkipped, validation);
}
StoreKey targetKey = request.getTarget();
ReentrantLock lock;
synchronized (byPathTargetLocks) {
lock = byPathTargetLocks.get(targetKey);
if (lock == null) {
lock = new ReentrantLock();
byPathTargetLocks.put(targetKey, lock);
}
}
final Set<String> complete = prevComplete == null ? new HashSet<>() : new HashSet<>(prevComplete);
final Set<String> skipped = prevSkipped == null ? new HashSet<>() : new HashSet<>(prevSkipped);
List<String> errors = new ArrayList<>();
boolean locked = false;
try {
ArtifactStore sourceStore = storeManager.getArtifactStore(request.getSource());
ArtifactStore targetStore = storeManager.getArtifactStore(request.getTarget());
if (errors.isEmpty()) {
locked = lock.tryLock(config.getLockTimeoutSeconds(), TimeUnit.SECONDS);
if (!locked) {
String error = String.format("Failed to acquire promotion lock on target: %s in %d seconds.", targetKey, config.getLockTimeoutSeconds());
errors.add(error);
logger.warn(error);
}
}
if (errors.isEmpty()) {
logger.info("Running promotions from: {} (key: {})\n to: {} (key: {})", sourceStore, request.getSource(), targetStore, request.getTarget());
final boolean purgeSource = request.isPurgeSource();
contents.forEach((transfer) -> {
try {
final String path = transfer.getPath();
Transfer target = contentManager.getTransfer(targetStore, path, TransferOperation.UPLOAD);
// TODO: Should the request object have an overwrite attribute? Is that something the user is qualified to decide?
if (target != null && target.exists()) {
logger.warn("NOT promoting: {} from: {} to: {}. Target file already exists.", path, request.getSource(), request.getTarget());
// TODO: There's no guarantee that the pre-existing content is the same!
pending.remove(path);
skipped.add(path);
} else {
try (InputStream stream = transfer.openInputStream(true)) {
contentManager.store(targetStore, path, stream, TransferOperation.UPLOAD, new EventMetadata());
pending.remove(path);
complete.add(path);
stream.close();
if (purgeSource) {
contentManager.delete(sourceStore, path, new EventMetadata());
}
} catch (final IOException e) {
String msg = String.format("Failed to open input stream for: %s. Reason: %s", transfer, e.getMessage());
errors.add(msg);
logger.error(msg, e);
}
}
} catch (final IndyWorkflowException e) {
String msg = String.format("Failed to promote path: %s to: %s. Reason: %s", transfer, targetStore, e.getMessage());
errors.add(msg);
logger.error(msg, e);
}
});
}
} catch (InterruptedException e) {
String error = String.format("Interrupted waiting for promotion lock on target: %s", targetKey);
errors.add(error);
logger.warn(error);
} catch (final IndyDataException e) {
String msg = String.format("Failed to retrieve artifact store: %s. Reason: %s", request.getSource(), e.getMessage());
errors.add(msg);
logger.error(msg, e);
} finally {
if (locked) {
lock.unlock();
}
}
String error = null;
if (!errors.isEmpty()) {
error = StringUtils.join(errors, "\n");
}
return new PathsPromoteResult(request, pending, complete, skipped, error, validation);
}
use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.
the class GroupPromoteRequestTest method roundTripJSON_Basic.
@Test
public void roundTripJSON_Basic() throws Exception {
final IndyObjectMapper mapper = new IndyObjectMapper(true);
final GroupPromoteRequest req = new GroupPromoteRequest(new StoreKey(StoreType.hosted, "source"), "target");
final String json = mapper.writeValueAsString(req);
System.out.println(json);
final GroupPromoteRequest result = mapper.readValue(json, GroupPromoteRequest.class);
assertThat(result.getSource(), equalTo(req.getSource()));
assertThat(result.getTargetGroup(), equalTo(req.getTargetGroup()));
}
Aggregations