Search in sources :

Example 1 with RepositoryHpkrIngressException

use of org.haiku.haikudepotserver.repository.model.RepositoryHpkrIngressException in project haikudepotserver by haiku.

the class RepositoryHpkrIngressJobRunner method runImportHpkrForRepositorySource.

private void runImportHpkrForRepositorySource(ObjectContext mainContext, RepositorySource repositorySource) {
    URL url = repositorySource.tryGetInternalFacingDownloadHpkrURL().orElseThrow(() -> new RuntimeException("unable to import for [" + repositorySource + "] as there is no download url able to be derived."));
    // now shift the URL's data into a temporary file and then process it.
    File temporaryFile = null;
    try {
        temporaryFile = File.createTempFile(repositorySource.getCode() + "__import", ".hpkr");
        LOGGER.info("will copy repository hpkr [{}] ({}) to temporary file", repositorySource, url.toString());
        FileHelper.streamUrlDataToFile(url, temporaryFile, TIMEOUT_REPOSITORY_SOURCE_FETCH);
        LOGGER.info("did copy {} bytes for repository hpkr [{}] ({}) to temporary file", temporaryFile.length(), repositorySource, url);
        HpkrFileExtractor fileExtractor = new HpkrFileExtractor(temporaryFile);
        long startTimeMs = System.currentTimeMillis();
        LOGGER.info("will process data for repository hpkr {}", repositorySource.getCode());
        // import any packages that are in the repository.
        Set<String> repositoryImportPkgNames = Sets.newHashSet();
        PkgIterator pkgIterator = new PkgIterator(fileExtractor.getPackageAttributesIterator());
        while (pkgIterator.hasNext()) {
            ObjectContext pkgImportContext = serverRuntime.newContext();
            Pkg pkg = pkgIterator.next();
            repositoryImportPkgNames.add(pkg.getName());
            try {
                pkgImportService.importFrom(pkgImportContext, repositorySource.getObjectId(), pkg, shouldPopulateFromPayload);
                pkgImportContext.commitChanges();
            } catch (Throwable th) {
                throw new RepositoryHpkrIngressException("unable to store package [" + pkg + "]", th);
            }
        }
        // [apl 6.aug.2014] #5
        // Packages may be removed from a repository.  In this case there is no trigger to indicate that the
        // package version should be removed.  Check all of the packages that have an active version in this
        // repository and then if the package simply doesn't exist in that repository any more, mark all of
        // those versions are inactive.
        pkgService.fetchPkgNamesWithAnyPkgVersionAssociatedWithRepositorySource(mainContext, repositorySource).forEach((persistedPkgName) -> {
            if (!repositoryImportPkgNames.contains(persistedPkgName)) {
                ObjectContext removalContext = serverRuntime.newContext();
                RepositorySource removalRepositorySource = RepositorySource.get(removalContext, repositorySource.getObjectId());
                int changes = pkgService.deactivatePkgVersionsForPkgAssociatedWithRepositorySource(removalContext, org.haiku.haikudepotserver.dataobjects.Pkg.getByName(removalContext, persistedPkgName), removalRepositorySource);
                if (changes > 0) {
                    removalContext.commitChanges();
                    LOGGER.info("did remove all versions of package {} from repository source {} because this package is no longer in the repository", persistedPkgName, repositorySource);
                }
            }
        });
        LOGGER.info("did process data for repository hpkr {} in {}ms", repositorySource, System.currentTimeMillis() - startTimeMs);
    } catch (Throwable th) {
        throw new RuntimeException("a problem has arisen processing a repository file for repository hpkr " + repositorySource + " from url '" + url.toString() + "'", th);
    } finally {
        if (null != temporaryFile && temporaryFile.exists()) {
            if (!temporaryFile.delete()) {
                LOGGER.error("unable to delete the file; {}" + temporaryFile.getAbsolutePath());
            }
        }
    }
}
Also used : Pkg(org.haiku.pkg.model.Pkg) URL(java.net.URL) HpkrFileExtractor(org.haiku.pkg.HpkrFileExtractor) RepositoryHpkrIngressException(org.haiku.haikudepotserver.repository.model.RepositoryHpkrIngressException) PkgIterator(org.haiku.pkg.PkgIterator) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) ObjectContext(org.apache.cayenne.ObjectContext) File(java.io.File)

Example 2 with RepositoryHpkrIngressException

use of org.haiku.haikudepotserver.repository.model.RepositoryHpkrIngressException in project haikudepotserver by haiku.

the class RepositoryHpkrIngressJobRunner method runImportInfoForRepositorySource.

/**
 * <p>Each repository has a little &quot;repo.info&quot; file that resides next to the HPKR data.
 * This method will pull this in and process the data into the repository source.</p>
 */
private void runImportInfoForRepositorySource(ObjectContext mainContext, RepositorySource repositorySource) throws RepositoryHpkrIngressException {
    URL url = repositorySource.tryGetInternalFacingDownloadRepoInfoURL().orElseThrow(() -> new RepositoryHpkrIngressException("unable to download for [" + repositorySource.getCode() + "] as no download repo info url was available"));
    // now shift the URL's data into a temporary file and then process it.
    File temporaryFile = null;
    try {
        temporaryFile = File.createTempFile(repositorySource.getCode() + "__import", ".repo-info");
        LOGGER.info("will copy data for repository info [{}] ({}) to temporary file", repositorySource, url.toString());
        FileHelper.streamUrlDataToFile(url, temporaryFile, TIMEOUT_REPOSITORY_SOURCE_FETCH);
        try (InputStream inputStream = new FileInputStream(temporaryFile);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charsets.UTF_8);
            BufferedReader reader = new BufferedReader(inputStreamReader)) {
            List<Parameter> parameters = DriverSettings.parse(reader);
            String identifierParameterValue = ObjectUtils.firstNonNull(tryGetParameterValue(parameters, PARAMETER_NAME_IDENTIFIER).orElse(null), tryGetParameterValue(parameters, PARAMETER_NAME_URL).orElse(null));
            if (StringUtils.isEmpty(identifierParameterValue)) {
                throw new DriverSettingsException("expected to find the parameter [" + PARAMETER_NAME_IDENTIFIER + "] or [" + PARAMETER_NAME_URL + "]");
            }
            if (!Objects.equals(identifierParameterValue, repositorySource.getIdentifier())) {
                LOGGER.info("updated the repo info identifier to [{}] for repository source [{}]", identifierParameterValue, repositorySource.getCode());
                repositorySource.setIdentifier(identifierParameterValue);
                repositorySource.getRepository().setModifyTimestamp();
                mainContext.commitChanges();
            }
            Optional<String> architectureCodeOptional = tryGetParameterValue(parameters, PARAMETER_ARCHITECTURE);
            if (architectureCodeOptional.isEmpty()) {
                throw new RepositoryHpkrIngressException("repository source [" + repositorySource.getCode() + "] has no architecture code");
            }
            Optional<Architecture> architectureOptional = Architecture.tryGetByCode(mainContext, architectureCodeOptional.get());
            if (architectureOptional.isEmpty()) {
                throw new RepositoryHpkrIngressException("repository source [" + repositorySource.getCode() + "] has unknown architecture code [" + architectureCodeOptional.get() + "]");
            }
            repositorySource.setArchitecture(architectureOptional.get());
        }
    } catch (IOException | DriverSettingsException e) {
        throw new RepositoryHpkrIngressException("a problem has arisen parsing or dealing with the 'repo.info' file", e);
    } finally {
        if (null != temporaryFile && temporaryFile.exists()) {
            if (!temporaryFile.delete()) {
                LOGGER.error("unable to delete the file; {}" + temporaryFile.getAbsolutePath());
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DriverSettingsException(org.haiku.driversettings.DriverSettingsException) Architecture(org.haiku.haikudepotserver.dataobjects.Architecture) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) FileInputStream(java.io.FileInputStream) RepositoryHpkrIngressException(org.haiku.haikudepotserver.repository.model.RepositoryHpkrIngressException) BufferedReader(java.io.BufferedReader) Parameter(org.haiku.driversettings.Parameter) File(java.io.File)

Aggregations

File (java.io.File)2 URL (java.net.URL)2 RepositoryHpkrIngressException (org.haiku.haikudepotserver.repository.model.RepositoryHpkrIngressException)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ObjectContext (org.apache.cayenne.ObjectContext)1 DriverSettingsException (org.haiku.driversettings.DriverSettingsException)1 Parameter (org.haiku.driversettings.Parameter)1 Architecture (org.haiku.haikudepotserver.dataobjects.Architecture)1 RepositorySource (org.haiku.haikudepotserver.dataobjects.RepositorySource)1 HpkrFileExtractor (org.haiku.pkg.HpkrFileExtractor)1 PkgIterator (org.haiku.pkg.PkgIterator)1 Pkg (org.haiku.pkg.model.Pkg)1