use of org.commonjava.indy.audit.ChangeSummary in project indy by Commonjava.
the class PromotionRuleAproxMigrationAction method migrate.
@Override
public boolean migrate() throws IndyLifecycleException {
DataFile dataDir = ffManager.getDataFile(config.getBasedir(), PromoteValidationsManager.RULES_DIR);
Logger logger = LoggerFactory.getLogger(getClass());
logger.debug("Scanning {} for promotion validation rules...", dataDir);
int changed = 0;
if (dataDir.exists()) {
final DataFile[] scripts = dataDir.listFiles((pathname) -> {
logger.trace("Checking for promote validation rule script in: {}", pathname);
return pathname.getName().endsWith(".groovy");
});
logger.info("Migrating promotion validation rules.");
for (final DataFile script : scripts) {
try {
String scriptContent = script.readString();
String migrated = scriptContent.replaceAll("A[Pp]rox", "Indy").replaceAll("aprox", "indy");
if (!migrated.equals(scriptContent)) {
logger.info("Migrating promotion validation rule in: {}", script.getPath());
script.writeString(migrated, new ChangeSummary(ChangeSummary.SYSTEM_USER, "Migrating to Indy packages / naming"));
changed++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return changed != 0;
}
use of org.commonjava.indy.audit.ChangeSummary in project indy by Commonjava.
the class PromotionManager method rollbackGroupPromote.
public GroupPromoteResult rollbackGroupPromote(GroupPromoteResult result, String user) throws PromotionException {
GroupPromoteRequest request = result.getRequest();
if (!storeManager.hasArtifactStore(request.getSource())) {
String error = String.format("No such source/member store: %s", request.getSource());
logger.warn(error);
return new GroupPromoteResult(request, error);
}
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);
}
if (target.getConstituents().contains(request.getSource())) {
// give the preUpdate event a different object to compare vs. the original group.
target = target.copyOf();
target.removeConstituent(request.getSource());
try {
final ChangeSummary changeSummary = new ChangeSummary(user, "Removing " + request.getSource() + " from membership of group: " + target.getKey());
storeManager.storeArtifactStore(target, changeSummary, 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());
}
} else {
return new GroupPromoteResult(request, "Group: " + target.getKey() + " does not contain member: " + request.getSource());
}
return new GroupPromoteResult(request);
}
use of org.commonjava.indy.audit.ChangeSummary 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.audit.ChangeSummary 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.audit.ChangeSummary in project indy by Commonjava.
the class PromotionManagerTest method rollback_PushTwoArtifactsToHostedRepo_PromoteSuccessThenRollback.
@Test
public void rollback_PushTwoArtifactsToHostedRepo_PromoteSuccessThenRollback() 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());
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()));
Set<String> pending = result.getPendingPaths();
assertThat(pending == null || pending.isEmpty(), equalTo(true));
Set<String> completed = result.getCompletedPaths();
assertThat(completed, notNullValue());
assertThat(completed.size(), equalTo(2));
assertThat(result.getError(), nullValue());
result = manager.rollbackPathsPromote(result);
assertThat(result.getRequest().getSource(), equalTo(source.getKey()));
assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
completed = result.getCompletedPaths();
assertThat(completed == null || completed.isEmpty(), equalTo(true));
pending = result.getPendingPaths();
assertThat(pending, notNullValue());
assertThat(pending.size(), equalTo(2));
assertThat(result.getError(), nullValue());
Transfer ref = downloadManager.getStorageReference(target, first);
assertThat(ref.exists(), equalTo(false));
ref = downloadManager.getStorageReference(target, second);
assertThat(ref.exists(), equalTo(false));
}
Aggregations