use of org.commonjava.indy.data.IndyDataException in project indy by Commonjava.
the class PromotionManager method promoteToGroup.
public GroupPromoteResult promoteToGroup(GroupPromoteRequest request, String user, String baseUrl) throws PromotionException {
if (!storeManager.hasArtifactStore(request.getSource())) {
String error = String.format("Cannot promote from missing source: %s", request.getSource());
logger.warn(error);
return new GroupPromoteResult(request, error);
}
synchronized (getTargetKey(request.getTargetGroup())) {
Group target;
try {
target = (Group) storeManager.getArtifactStore(request.getTargetKey());
} catch (IndyDataException e) {
throw new PromotionException("Cannot retrieve target group: %s. Reason: %s", e, request.getTargetGroup(), e.getMessage());
}
if (target == null) {
String error = String.format("No such target group: %s.", request.getTargetGroup());
logger.warn(error);
return new GroupPromoteResult(request, error);
}
ValidationResult validation = new ValidationResult();
logger.info("Running validations for promotion of: {} to group: {}", request.getSource(), request.getTargetGroup());
validator.validate(request, validation, baseUrl);
if (validation.isValid()) {
if (!request.isDryRun() && !target.getConstituents().contains(request.getSource())) {
// give the preUpdate event a different object to compare vs. the original group.
target = target.copyOf();
target.addConstituent(request.getSource());
try {
final ChangeSummary changeSummary = new ChangeSummary(user, "Promoting " + request.getSource() + " into membership of group: " + target.getKey());
storeManager.storeArtifactStore(target, changeSummary, false, true, new EventMetadata());
if (hosted == request.getSource().getType() && config.isAutoLockHostedRepos()) {
HostedRepository source = (HostedRepository) storeManager.getArtifactStore(request.getSource());
source.setReadonly(true);
final ChangeSummary readOnlySummary = new ChangeSummary(user, "Promoting " + request.getSource() + " into membership of group: " + target.getKey());
storeManager.storeArtifactStore(source, readOnlySummary, false, true, new EventMetadata());
}
} catch (IndyDataException e) {
throw new PromotionException("Failed to store group: %s with additional member: %s. Reason: %s", e, target.getKey(), request.getSource(), e.getMessage());
}
}
}
return new GroupPromoteResult(request, validation);
}
}
use of org.commonjava.indy.data.IndyDataException in project indy by Commonjava.
the class PromotionValidator method getRequestStore.
private ArtifactStore getRequestStore(PromoteRequest promoteRequest, String baseUrl) throws PromotionValidationException {
final ArtifactStore store;
final Logger logger = LoggerFactory.getLogger(getClass());
if (needTempRepo(promoteRequest)) {
logger.info("Promotion temporary repo is needed for {}, points to {} ", promoteRequest.getSource(), baseUrl);
final PathsPromoteRequest pathsReq = (PathsPromoteRequest) promoteRequest;
String tempName = PROMOTE_REPO_PREFIX + "tmp_" + pathsReq.getSource().getName() + new SimpleDateFormat("yyyyMMdd.hhmmss.SSSZ").format(new Date());
final RemoteRepository tempRemote = new RemoteRepository(tempName, baseUrl);
tempRemote.setPathMaskPatterns(pathsReq.getPaths());
store = tempRemote;
try {
final ChangeSummary changeSummary = new ChangeSummary(ChangeSummary.SYSTEM_USER, "create temp remote repository");
storeDataMgr.storeArtifactStore(tempRemote, changeSummary, false, true, new EventMetadata());
} catch (IndyDataException e) {
throw new PromotionValidationException("Can not store the temp remote repository correctly", e);
}
} else {
logger.info("Promotion temporary repo is not needed for {} ", promoteRequest.getSource());
try {
store = storeDataMgr.getArtifactStore(promoteRequest.getSource());
} catch (IndyDataException e) {
throw new PromotionValidationException("Failed to retrieve source ArtifactStore: {}. Reason: {}", e, promoteRequest.getSource(), e.getMessage());
}
}
return store;
}
use of org.commonjava.indy.data.IndyDataException in project indy by Commonjava.
the class PromotionManagerTest method promoteAllByPath_RaceToPromote_FirstLocksTargetStore.
@Test
public void promoteAllByPath_RaceToPromote_FirstLocksTargetStore() throws Exception {
Random rand = new Random();
final HostedRepository[] sources = { new HostedRepository(MAVEN_PKG_KEY, "source1"), new HostedRepository(MAVEN_PKG_KEY, "source2") };
final String[] paths = { "/path/path1", "/path/path2", "/path3", "/path/path/4" };
Stream.of(sources).forEach((source) -> {
try {
storeManager.storeArtifactStore(source, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
Stream.of(paths).forEach((path) -> {
byte[] buf = new byte[1024 * 1024 * 2];
rand.nextBytes(buf);
try {
contentManager.store(source, path, new ByteArrayInputStream(buf), TransferOperation.UPLOAD, new EventMetadata());
} catch (IndyWorkflowException e) {
e.printStackTrace();
Assert.fail("failed to store generated file to: " + source + path);
}
});
} catch (IndyDataException e) {
e.printStackTrace();
Assert.fail("failed to store hosted repository: " + source);
}
});
final HostedRepository target = new HostedRepository(MAVEN_PKG_KEY, "target");
storeManager.storeArtifactStore(target, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
PathsPromoteResult[] results = new PathsPromoteResult[2];
CountDownLatch cdl = new CountDownLatch(2);
AtomicInteger counter = new AtomicInteger(0);
Stream.of(sources).forEach((source) -> {
int idx = counter.getAndIncrement();
executor.execute(() -> {
try {
results[idx] = manager.promotePaths(new PathsPromoteRequest(source.getKey(), target.getKey(), paths), FAKE_BASE_URL);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Promotion from source: " + source + " failed.");
} finally {
cdl.countDown();
}
});
try {
Thread.sleep(25);
} catch (InterruptedException e) {
Assert.fail("Test interrupted");
}
});
assertThat("Promotions failed to finish.", cdl.await(30, TimeUnit.SECONDS), equalTo(true));
// first one should succeed.
PathsPromoteResult result = results[0];
assertThat(result.getRequest().getSource(), equalTo(sources[0].getKey()));
assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
Set<String> pending = result.getPendingPaths();
assertThat(pending == null || pending.isEmpty(), equalTo(true));
Set<String> skipped = result.getSkippedPaths();
assertThat(skipped == null || skipped.isEmpty(), equalTo(true));
Set<String> completed = result.getCompletedPaths();
assertThat(completed, notNullValue());
assertThat(completed.size(), equalTo(paths.length));
assertThat(result.getError(), nullValue());
Stream.of(paths).forEach((path) -> {
HostedRepository src = sources[0];
Transfer sourceRef = downloadManager.getStorageReference(src, path);
Transfer targetRef = downloadManager.getStorageReference(target, path);
assertThat(targetRef.exists(), equalTo(true));
try (InputStream sourceIn = sourceRef.openInputStream();
InputStream targetIn = targetRef.openInputStream()) {
int s = -1, t = -1;
while ((s = sourceIn.read()) == (t = targetIn.read())) {
if (s == -1) {
break;
}
}
if (s != -1 && s != t) {
Assert.fail(path + " doesn't match between source: " + src + " and target: " + target);
}
} catch (IOException e) {
e.printStackTrace();
Assert.fail("Failed to compare contents of: " + path + " between source: " + src + " and target: " + target);
}
});
// second one should be completely skipped.
result = results[1];
assertThat(result.getRequest().getSource(), equalTo(sources[1].getKey()));
assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
pending = result.getPendingPaths();
assertThat(pending == null || pending.isEmpty(), equalTo(true));
skipped = result.getSkippedPaths();
assertThat(skipped, notNullValue());
assertThat(skipped.size(), equalTo(paths.length));
completed = result.getCompletedPaths();
assertThat(completed == null || completed.isEmpty(), equalTo(true));
assertThat(result.getError(), nullValue());
}
use of org.commonjava.indy.data.IndyDataException in project indy by Commonjava.
the class GroupConsistencyListener method processChanged.
private void processChanged(final ArtifactStore store) {
final StoreKey key = store.getKey();
try {
final Set<Group> groups = storeDataManager.query().getGroupsContaining(key);
for (final Group group : groups) {
logger.debug("Removing {} from membership of group: {}", key, group.getKey());
Group g = group.copyOf();
g.removeConstituent(key);
storeDataManager.storeArtifactStore(g, new ChangeSummary(ChangeSummary.SYSTEM_USER, "Auto-update groups containing: " + key + " (to maintain consistency)"), false, false, new EventMetadata().set(StoreDataManager.EVENT_ORIGIN, GROUP_CONSISTENCY_ORIGIN));
}
} catch (final IndyDataException e) {
logger.error(String.format("Failed to remove group constituent listings for: %s. Error: %s", key, e.getMessage()), e);
}
}
use of org.commonjava.indy.data.IndyDataException in project indy by Commonjava.
the class StoreContentListener method processAllPaths.
private void processAllPaths(final Iterable<ArtifactStore> stores, Predicate<? super String> pathFilter, boolean deleteOriginPath) {
StreamSupport.stream(stores.spliterator(), true).forEach((store) -> {
final StoreKey key = store.getKey();
Set<String> paths = listPaths(key, pathFilter);
if (!paths.isEmpty()) {
Set<Group> groups = null;
try {
groups = storeDataManager.query().packageType(key.getPackageType()).getGroupsAffectedBy(key);
} catch (IndyDataException e) {
e.printStackTrace();
}
clearPaths(paths, key, groups, deleteOriginPath);
}
});
}
Aggregations