Search in sources :

Example 71 with IndyDataException

use of org.commonjava.indy.data.IndyDataException in project indy by Commonjava.

the class FoloTrackingListener method createEntry.

private TrackedContentEntry createEntry(final TrackingKey trackingKey, final StoreKey affectedStore, final AccessChannel accessChannel, final String path, final StoreEffect effect, final EventMetadata eventMetadata) throws IndyWorkflowException {
    TrackedContentEntry entry = null;
    final Transfer txfr = downloadManager.getStorageReference(affectedStore, path);
    if (txfr != null) {
        try {
            String remoteUrl = null;
            if (StoreType.remote == affectedStore.getType()) {
                final RemoteRepository repo = (RemoteRepository) storeManager.getArtifactStore(affectedStore);
                if (repo != null) {
                    remoteUrl = UrlUtils.buildUrl(repo.getUrl(), path);
                }
            }
            TransferMetadata artifactData = contentDigester.digest(affectedStore, path, eventMetadata);
            Map<ContentDigest, String> digests = artifactData.getDigests();
            //TODO: As localUrl needs a apiBaseUrl which is from REST service context, to avoid deep propagate
            //      of it, this step will be done in REST layer. Will think better way in the future.
            entry = new TrackedContentEntry(trackingKey, affectedStore, accessChannel, remoteUrl, path, effect, artifactData.getSize(), digests.get(ContentDigest.MD5), digests.get(ContentDigest.SHA_1), digests.get(ContentDigest.SHA_256));
        } catch (final IndyDataException e) {
            throw new IndyWorkflowException("Cannot retrieve RemoteRepository: %s to calculate remote URL for: %s. Reason: %s", e, trackingKey, path, e.getMessage());
        } catch (final MalformedURLException e) {
            throw new IndyWorkflowException("Cannot format URL. Reason: %s", e, e.getMessage());
        }
    }
    return entry;
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) MalformedURLException(java.net.MalformedURLException) TransferMetadata(org.commonjava.maven.galley.io.checksum.TransferMetadata) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) TrackedContentEntry(org.commonjava.indy.folo.model.TrackedContentEntry) ContentDigest(org.commonjava.maven.galley.io.checksum.ContentDigest)

Example 72 with IndyDataException

use of org.commonjava.indy.data.IndyDataException in project indy by Commonjava.

the class AutoProxDataManagerDecorator method getHostedRepository.

private HostedRepository getHostedRepository(final StoreKey key, final StoreKey impliedBy) throws IndyDataException {
    HostedRepository repo = (HostedRepository) dataManager.getArtifactStore(key);
    if (!catalog.isEnabled()) {
        logger.debug("AutoProx decorator disabled; returning: {}", repo);
        return repo;
    }
    logger.debug("AutoProx decorator active");
    if (repo == null) {
        logger.info("AutoProx: creating repository for: {}", key);
        try {
            repo = catalog.createHostedRepository(key);
        } catch (final AutoProxRuleException e) {
            throw new IndyDataException("[AUTOPROX] Failed to create new hosted repository from factory matching: '%s'. Reason: %s", e, key, e.getMessage());
        }
        if (repo != null) {
            final ChangeSummary changeSummary = new ChangeSummary(ChangeSummary.SYSTEM_USER, "AUTOPROX: Creating hosted repository for: '" + key + "'");
            final EventMetadata eventMetadata = new EventMetadata().set(StoreDataManager.EVENT_ORIGIN, AutoProxConstants.STORE_ORIGIN).set(AutoProxConstants.ORIGINATING_STORE, impliedBy == null ? repo.getKey() : impliedBy);
            dataManager.storeArtifactStore(repo, changeSummary, false, true, eventMetadata);
        }
    }
    return repo;
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) HostedRepository(org.commonjava.indy.model.core.HostedRepository) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 73 with IndyDataException

use of org.commonjava.indy.data.IndyDataException in project indy by Commonjava.

the class AutoProxDataManagerDecorator method checkValidity.

/**
     * Validates the remote connection, produced from rule-set for given key,
     * for a remote repo or group containing a remote. If:
     *
     * <ul>
     *   <li>rule.isValidationEnabled() == false, return true</li>
     *   <li>rule.getValidationRemote() == null, return true</li>
     *   <li>
     *     rule.getRemoteValidationPath() != null, validate remote.getUrl() + validationPath
     *     <ul>
     *       <li>if response code is 200 OK, then return true</li>
     *       <li>otherwise, return false</li>
     *     </ul>
     *   </li>
     * </ul>
     * @throws IndyDataException if the selected rule encounters an error while creating the new group/repository instance(s).
     */
private boolean checkValidity(final StoreKey key) throws IndyDataException {
    if (catalog.isValidationEnabled(key)) {
        try {
            final RemoteRepository validationRepo = catalog.createValidationRemote(key);
            if (validationRepo == null) {
                logger.info("No validation repository was created: assuming {} is valid.", key);
                return true;
            }
            String path = catalog.getRemoteValidationPath(key);
            if (path == null) {
                path = PathUtils.ROOT;
            }
            logger.debug("\n\n\n\n\n[AutoProx] Checking path: {} under remote URL: {}", path, validationRepo.getUrl());
            boolean result = false;
            try {
                result = transferManager.exists(new ConcreteResource(LocationUtils.toLocation(validationRepo), path));
            } catch (final TransferException e) {
                logger.warn("[AutoProx] Cannot connect to target repository: '{}'. Reason: {}", validationRepo, e.getMessage());
                logger.debug("[AutoProx] exception from validation attempt for: " + validationRepo, e);
            }
            logger.debug("Validation result for: {} is: {}", validationRepo, result);
            return result;
        } catch (final AutoProxRuleException e) {
            throw new IndyDataException("[AUTOPROX] Failed to create new group from factory matching: '%s'. Reason: %s", e, key, e.getMessage());
        }
    }
    return true;
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) TransferException(org.commonjava.maven.galley.TransferException) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository)

Example 74 with IndyDataException

use of org.commonjava.indy.data.IndyDataException in project indy by Commonjava.

the class PackageTypedStorageMigrationAction method migrate.

@Override
public boolean migrate() throws IndyLifecycleException {
    Set<ArtifactStore> stores;
    try {
        stores = storeDataManager.getAllArtifactStores();
    } catch (IndyDataException e) {
        throw new IndyLifecycleException("Cannot retrieve list of repositories and groups in order to review storage locations. Reason: %s", e, e.getMessage());
    }
    File storageRoot = config.getStorageRootDirectory();
    File nfsStorageRoot = config.getNFSStorageRootDirectory();
    int migrations = 0;
    Map<File, File> unmigratedNfs = new HashMap<>();
    for (ArtifactStore store : stores) {
        File old = deprecatedStoragePath(storageRoot, store);
        File migrated = packageTypedStoragePath(storageRoot, store);
        if (old.exists()) {
            logger.info("Attempting to migrate existing storage from old directory structure: {} " + "to package-typed structure: {}", old, migrated);
            try {
                if (migrated.exists()) {
                    FileUtils.copyDirectory(old, migrated);
                    FileUtils.forceDelete(old);
                } else {
                    FileUtils.moveDirectory(old, migrated);
                }
                migrations++;
            } catch (IOException e) {
                throw new IndyLifecycleException("Failed to migrate: %s to: %s. Reason: %s", e, old, migrated);
            }
        }
        if (nfsStorageRoot != null) {
            File oldNfs = deprecatedStoragePath(nfsStorageRoot, store);
            File migratedNfs = packageTypedStoragePath(nfsStorageRoot, store);
            if (oldNfs.exists() && !migratedNfs.exists()) {
                unmigratedNfs.put(oldNfs, migratedNfs);
            }
        }
    }
    if (!unmigratedNfs.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        sb.append("ERROR: Un-migrated directories detected on NFS storage!!!!");
        sb.append("\n\nThese directories still use the old <type>/<name> directory format. Indy now supports");
        sb.append("\nmultiple package types, and the storage format has changed accordingly. The new format is:");
        sb.append("\n\n    <package-type>/<type>/<name>");
        sb.append("\n\nPlease migrate these NFS directories manually. For Maven repositories:");
        sb.append("\n\n    maven/<type>/<name>");
        sb.append("\n\nFor HTTProx repositories (httprox_*):");
        sb.append("\n\n    generic-http/<type>/<name>");
        sb.append("\n\nThe following directories were detected:\n");
        unmigratedNfs.forEach((o, n) -> sb.append("\n    ").append(o).append("  =>  ").append(n));
        sb.append("\n\n");
        logger.error(sb.toString());
        throw new IndyLifecycleException("Un-migrated NFS directories detected. Indy cannot start until this has been resolved.");
    }
    return migrations > 0;
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) HashMap(java.util.HashMap) IOException(java.io.IOException) IndyLifecycleException(org.commonjava.indy.action.IndyLifecycleException) File(java.io.File)

Example 75 with IndyDataException

use of org.commonjava.indy.data.IndyDataException in project indy by Commonjava.

the class MemoryStoreDataManager method store.

private boolean store(final ArtifactStore store, final ChangeSummary summary, final boolean skipIfExists, final boolean fireEvents, final EventMetadata eventMetadata) throws IndyDataException {
    ReentrantLock opLock = getOpLock(store.getKey());
    try {
        opLock.lock();
        ArtifactStore original = stores.get(store.getKey());
        if (original == store) {
            // if they're the same instance, warn that preUpdate events may not work correctly!
            logger.warn("Storing changes on existing instance of: {}! You forgot to call {}.copyOf(), so preUpdate events may not accurately reflect before/after differences for this change!", store, store.getClass().getSimpleName());
        }
        if (!skipIfExists || original == null) {
            preStore(store, original, summary, original != null, fireEvents, eventMetadata);
            final ArtifactStore old = stores.put(store.getKey(), store);
            try {
                postStore(store, original, summary, original != null, fireEvents, eventMetadata);
                return true;
            } catch (final IndyDataException e) {
                logger.error("postStore() failed for: {}. Rolling back to old value: {}", store, old);
                stores.put(old.getKey(), old);
            }
        }
        return false;
    } finally {
        opLock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) IndyDataException(org.commonjava.indy.data.IndyDataException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore)

Aggregations

IndyDataException (org.commonjava.indy.data.IndyDataException)75 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)50 StoreKey (org.commonjava.indy.model.core.StoreKey)30 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)26 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)24 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)24 Transfer (org.commonjava.maven.galley.model.Transfer)19 ArrayList (java.util.ArrayList)17 Group (org.commonjava.indy.model.core.Group)17 IOException (java.io.IOException)16 Logger (org.slf4j.Logger)16 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)14 HashSet (java.util.HashSet)10 List (java.util.List)7 StoreType (org.commonjava.indy.model.core.StoreType)7 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)7 HostedRepository (org.commonjava.indy.model.core.HostedRepository)6 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)6 ContentGenerator (org.commonjava.indy.content.ContentGenerator)5 InputStream (java.io.InputStream)4