Search in sources :

Example 16 with ArtifactTemplateFilesDirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId in project winery by eclipse.

the class ScriptPlugin method createDeploymentArtifact.

private String createDeploymentArtifact(ArtifactTemplateId artifactTemplate, IRepository repository, GeneratedArtifacts generatedArtifacts, String packageToInstall) {
    QName packageDaId = new QName(artifactTemplate.getQName().getNamespaceURI(), VersionSupport.getNewComponentVersionId(artifactTemplate, packageToInstall + "-DA"));
    ArtifactTemplateId deployArtId = new ArtifactTemplateId(packageDaId);
    String generatedPackage = packageToInstall + ".tar.gz";
    if (!repository.exists(deployArtId)) {
        String cmd = "/usr/bin/apt download $(/usr/bin/apt-cache depends --recurse --no-recommends --no-suggests --no-conflicts " + "--no-breaks --no-replaces --no-enhances --no-pre-depends " + packageToInstall + " | /bin/grep \"^\\w\")";
        logger.info("Executing command: \"{}\"", cmd);
        Path tempDirectory = null;
        try {
            repository.setElement(deployArtId, new TArtifactTemplate.Builder(deployArtId.getXmlId().getDecoded(), ToscaBaseTypes.archiveArtifactType).build());
            tempDirectory = Files.createTempDirectory(packageDaId.getLocalPart());
            ArtifactTemplateFilesDirectoryId filesId = new ArtifactTemplateFilesDirectoryId(deployArtId);
            try {
                Utils.execute(tempDirectory.toString(), "bash", "-c", cmd);
            } catch (IOException | InterruptedException e) {
                logger.info("Cannot perform download, skipping it! You must add the DA contents yourself!");
                logger.debug("Root cause:", e);
            }
            // Ensure the folder structure exists.
            RepositoryFileReference tarFileRef = new RepositoryFileReference(filesId, generatedPackage);
            if (repository.id2AbsolutePath(filesId).toFile().mkdirs()) {
                compressFolderContents(tempDirectory.toString(), repository.ref2AbsolutePath(tarFileRef).toString());
                BackendUtils.synchronizeReferences(repository, deployArtId);
            } else {
                logger.error("Could not create folders: {}", repository.id2AbsolutePath(filesId).toFile().getAbsolutePath());
            }
        } catch (Exception e) {
            logger.error("Error while downloading artifacts...", e);
        } finally {
            if (tempDirectory != null) {
                FileUtils.forceDelete(tempDirectory);
            }
        }
    }
    generatedArtifacts.deploymentArtifactsToAdd.add(packageDaId);
    return generatedPackage;
}
Also used : Path(java.nio.file.Path) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) QName(javax.xml.namespace.QName) IOException(java.io.IOException) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) IOException(java.io.IOException)

Example 17 with ArtifactTemplateFilesDirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId in project winery by eclipse.

the class BackendUtilsTest method synchronizeReferencesRemovesNonExistentFileAndDoesNotRemoveUrls.

@Test
public void synchronizeReferencesRemovesNonExistentFileAndDoesNotRemoveUrls() throws Exception {
    ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId("http://example.org", "test-artifact-template", false);
    // alternative test implementation: Use git-based repository
    // this test at hand is closer to the implementation, but easier to write
    IRepository repository = mock(IRepository.class);
    ArtifactTemplateFilesDirectoryId artifactTemplateFilesDirectoryId = new ArtifactTemplateFilesDirectoryId(artifactTemplateId);
    when(repository.getContainedFiles(artifactTemplateFilesDirectoryId)).thenReturn(Collections.emptySortedSet());
    TArtifactTemplate artifactTemplate = createArtifactTemplateWithReferenceToAnUrlAndANonExistentFile();
    when(repository.getElement(artifactTemplateId)).thenReturn(artifactTemplate);
    TArtifactTemplate synchronizhedArtifactTemplate = BackendUtils.synchronizeReferences(repository, artifactTemplateId);
    assertEquals(createArtifactTemplateWithSingleReferenceToAnUrl(), synchronizhedArtifactTemplate);
}
Also used : ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) Test(org.junit.jupiter.api.Test)

Example 18 with ArtifactTemplateFilesDirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId in project winery by eclipse.

the class XmlRepositoryTest method containedFilesRecursesIntoSubDirectories.

@Test
public void containedFilesRecursesIntoSubDirectories() throws Exception {
    this.setRevisionTo("5cda0035a773a9c405a70759731be3977f37e3f3");
    ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId("http://winery.opentosca.org/test/artifacttemplates/fruits", "baobab-ArtifactTemplate-Peel", false);
    ArtifactTemplateFilesDirectoryId directoryId = new ArtifactTemplateFilesDirectoryId(artifactTemplateId);
    final SortedSet<RepositoryFileReference> containedFiles = repository.getContainedFiles(directoryId);
    // TODO: real content (relative paths, ...) not checked
    assertEquals(3, containedFiles.size());
}
Also used : ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) Test(org.junit.jupiter.api.Test)

Example 19 with ArtifactTemplateFilesDirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId in project winery by eclipse.

the class CsarImporter method adjustArtifactTemplate.

/**
 * Adjusts the given artifact template to conform with the repository format
 * <p>
 * We import the files given at the artifact references
 */
private void adjustArtifactTemplate(Path rootPath, TOSCAMetaFile tmf, ArtifactTemplateId atid, TArtifactTemplate ci, final List<String> errors) {
    List<TArtifactReference> refs = ci.getArtifactReferences();
    if (refs == null) {
        // no references stored - break
        return;
    }
    for (TArtifactReference ref : refs) {
        String reference = ref.getReference();
        // URLs are stored encoded -> undo the encoding
        reference = EncodingUtil.URLdecode(reference);
        URI refURI;
        try {
            refURI = new URI(reference);
        } catch (URISyntaxException e) {
            errors.add(String.format("Invalid URI %1$s", ref));
            continue;
        }
        if (refURI.isAbsolute()) {
            // We have to do nothing
            continue;
        }
        Path path = rootPath.resolve(reference);
        if (!Files.exists(path)) {
            errors.add(String.format("Reference %1$s not found", reference));
            return;
        }
        Set<Path> allFiles;
        if (Files.isRegularFile(path)) {
            allFiles = new HashSet<>();
            allFiles.add(path);
        } else {
            if (!Files.isDirectory(path)) {
                LOGGER.error("path {} is not a directory", path);
            }
            Path localRoot = rootPath.resolve(path);
            List<TArtifactReference.IncludeOrExclude> includeOrExclude = ref.getIncludeOrExclude();
            if (includeOrExclude.get(0) instanceof Exclude) {
                // Implicit semantics of an exclude listed first:
                // include all files and then exclude the files matched by the pattern
                allFiles = this.getAllFiles(localRoot);
            } else {
                // semantics if include listed as first:
                // same as listed at other places
                allFiles = new HashSet<>();
            }
            for (Object object : includeOrExclude) {
                if (object instanceof Include) {
                    this.handleInclude((Include) object, localRoot, allFiles);
                } else {
                    assert (object instanceof Exclude);
                    this.handleExclude((Exclude) object, localRoot, allFiles);
                }
            }
        }
        DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(atid);
        this.importAllFiles(rootPath, allFiles, fileDir, tmf, errors);
    }
    if (refs.isEmpty()) {
        // everything is imported and is a file stored locally
        // we don't need the references stored locally: they are generated on the fly when exporting
        ci.setArtifactReferences(null);
    }
}
Also used : Path(java.nio.file.Path) Exclude(org.eclipse.winery.model.tosca.TArtifactReference.Exclude) TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference) Include(org.eclipse.winery.model.tosca.TArtifactReference.Include) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId)

Example 20 with ArtifactTemplateFilesDirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId in project winery by eclipse.

the class ToscaExportUtil method prepareForExport.

/**
 * Determines the referenced definition children Ids and also updates the references in the Artifact Template
 *
 * @return a collection of referenced definition child Ids
 */
protected void prepareForExport(IRepository repository, ArtifactTemplateId id) throws IOException {
    // Export files
    // This method is called BEFORE the concrete definitions element is written.
    // Therefore, we adapt the content of the attached files to the really existing files
    BackendUtils.synchronizeReferences(repository, id);
    DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(id);
    SortedSet<RepositoryFileReference> files = repository.getContainedFiles(fileDir);
    for (RepositoryFileReference ref : files) {
        // Even if writing a TOSCA only (!this.writingCSAR),
        // we put the virtual path in the TOSCA
        // Reason: Winery is mostly used as a service and local storage
        // reference to not make sense
        // The old implementation had absolutePath.toUri().toString();
        // there, but this does not work when using a cloud blob store.
        putRefAsReferencedItemInCsar(repository, ref);
    }
}
Also used : ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference)

Aggregations

ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)20 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)12 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)8 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)8 Path (java.nio.file.Path)7 IOException (java.io.IOException)6 DirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId)6 Test (org.junit.jupiter.api.Test)6 ArrayList (java.util.ArrayList)5 InputStream (java.io.InputStream)4 QName (javax.xml.namespace.QName)4 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)4 TArtifactReference (org.eclipse.winery.model.tosca.TArtifactReference)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 FileInputStream (java.io.FileInputStream)2 JAXBException (javax.xml.bind.JAXBException)2 TransformerException (javax.xml.transform.TransformerException)2 MediaType (org.apache.tika.mime.MediaType)2 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)2