use of org.commonjava.maven.galley.model.Transfer 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.maven.galley.model.Transfer in project indy by Commonjava.
the class PromotionManagerTest method promoteAllByPath_PushTwoArtifactsToHostedRepo_VerifyCopiedToOtherHostedRepo.
@Test
public void promoteAllByPath_PushTwoArtifactsToHostedRepo_VerifyCopiedToOtherHostedRepo() throws Exception {
final HostedRepository source = new HostedRepository(MAVEN_PKG_KEY, "source");
storeManager.storeArtifactStore(source, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
final String first = "/first/path";
final String second = "/second/path";
contentManager.store(source, first, new ByteArrayInputStream("This is a test".getBytes()), TransferOperation.UPLOAD, new EventMetadata());
contentManager.store(source, second, new ByteArrayInputStream("This is a test".getBytes()), TransferOperation.UPLOAD, new EventMetadata());
final HostedRepository target = new HostedRepository(MAVEN_PKG_KEY, "target");
storeManager.storeArtifactStore(target, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
final PathsPromoteResult result = manager.promotePaths(new PathsPromoteRequest(source.getKey(), target.getKey()), FAKE_BASE_URL);
assertThat(result.getRequest().getSource(), equalTo(source.getKey()));
assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
final Set<String> pending = result.getPendingPaths();
assertThat(pending == null || pending.isEmpty(), equalTo(true));
final Set<String> completed = result.getCompletedPaths();
assertThat(completed, notNullValue());
assertThat(completed.size(), equalTo(2));
assertThat(result.getError(), nullValue());
Transfer ref = downloadManager.getStorageReference(target, first);
assertThat(ref.exists(), equalTo(true));
ref = downloadManager.getStorageReference(target, second);
assertThat(ref.exists(), equalTo(true));
}
use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.
the class KojiContentManagerDecorator method getTransfer.
@Override
public Transfer getTransfer(final ArtifactStore store, final String path, final TransferOperation operation) throws IndyWorkflowException {
Logger logger = LoggerFactory.getLogger(getClass());
logger.info("KOJI: Delegating initial getTransfer() attempt for: {}/{}", store.getKey(), path);
Transfer result = delegate.getTransfer(store, path, operation);
if (result == null && TransferOperation.DOWNLOAD == operation && StoreType.group == store.getKey().getType()) {
logger.info("KOJI: Checking for Koji build matching: {}", path);
Group group = (Group) store;
RemoteRepository kojiProxy = findKojiBuildAnd(store, path, new EventMetadata(), null, this::createRemoteRepository);
if (kojiProxy != null) {
adjustTargetGroup(kojiProxy, group);
EventMetadata eventMetadata = new EventMetadata().set(ContentManager.ENTRY_POINT_STORE, store.getKey());
result = delegate.retrieve(kojiProxy, path, eventMetadata);
}
if (result != null && result.exists()) {
nfc.clearMissing(new ConcreteResource(LocationUtils.toLocation(store), path));
}
}
// Finally, pass the Transfer back.
return result;
}
use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.
the class RelatePomStorageListener method onPomStorage.
public void onPomStorage(@Observes final FileStorageEvent event) {
final Logger logger = LoggerFactory.getLogger(getClass());
if (!config.isEnabled()) {
logger.debug("Relate Add-on is not enabled.");
return;
}
logger.debug("FILE STORAGE: {}", event);
final TransferOperation op = event.getType();
if (op != TransferOperation.UPLOAD && op != TransferOperation.DOWNLOAD) {
logger.debug("Not a download/upload transfer operation. No .rel generation.");
return;
}
ThreadContext threadContext = ThreadContext.getContext(false);
if (threadContext != null) {
Object obj = threadContext.get(REL_DIRECT_GENERATING);
if (obj != null) {
logger.debug("Direct .rel generation in progress. Ignore POM storage event. ");
return;
}
}
final Transfer transfer = event.getTransfer();
executor.execute(() -> {
relateGenerationManager.generateRelationshipFile(transfer, op);
});
}
use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.
the class ArtifactStoreSubStore method createFolder.
@Override
public void createFolder(final ITransaction transaction, final String folderUri) throws WebdavException {
final StoreURIMatcher matcher = new StoreURIMatcher(folderUri);
if (!matcher.hasStorePath()) {
throw new WebdavException("No store-level path specified: '" + folderUri + "'. This URI references either a list of stores, a root store directory, or something else equally read-only.");
}
final StorageAdvice advice = getStorageAdviceFor(matcher);
final String path = matcher.getStorePath();
final Transfer item = fileManager.getStorageReference(advice.getHostedStore(), path);
try {
item.mkdirs();
} catch (final IOException e) {
logger.error("Failed to create folder: {} in store: {}. Reason: {}", e, path, advice.getStore().getKey(), e.getMessage());
throw new WebdavException("Failed to create folder: " + folderUri);
}
}
Aggregations