Search in sources :

Example 41 with ChangeSummary

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;
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) IOException(java.io.IOException) Logger(org.slf4j.Logger) ChangeSummary(org.commonjava.indy.audit.ChangeSummary)

Example 42 with ChangeSummary

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);
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) GroupPromoteRequest(org.commonjava.indy.promote.model.GroupPromoteRequest) Group(org.commonjava.indy.model.core.Group) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 43 with ChangeSummary

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);
    }
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) Group(org.commonjava.indy.model.core.Group) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) ValidationResult(org.commonjava.indy.promote.model.ValidationResult) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) HostedRepository(org.commonjava.indy.model.core.HostedRepository)

Example 44 with ChangeSummary

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;
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) Logger(org.slf4j.Logger) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 45 with ChangeSummary

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) Transfer(org.commonjava.maven.galley.model.Transfer) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) HostedRepository(org.commonjava.indy.model.core.HostedRepository) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Aggregations

ChangeSummary (org.commonjava.indy.audit.ChangeSummary)68 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)37 Test (org.junit.Test)29 IndyDataException (org.commonjava.indy.data.IndyDataException)24 DataFile (org.commonjava.indy.subsys.datafile.DataFile)21 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)17 StoreKey (org.commonjava.indy.model.core.StoreKey)14 IOException (java.io.IOException)11 HostedRepository (org.commonjava.indy.model.core.HostedRepository)11 Group (org.commonjava.indy.model.core.Group)10 File (java.io.File)8 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)8 PathsPromoteRequest (org.commonjava.indy.promote.model.PathsPromoteRequest)8 Transfer (org.commonjava.maven.galley.model.Transfer)8 Logger (org.slf4j.Logger)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 PathsPromoteResult (org.commonjava.indy.promote.model.PathsPromoteResult)7 ArrayList (java.util.ArrayList)6 IndyLifecycleException (org.commonjava.indy.action.IndyLifecycleException)5 BMRules (org.jboss.byteman.contrib.bmunit.BMRules)5