Search in sources :

Example 1 with ArtifactTemplateFilesDirectoryId

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

the class BackendUtils method synchronizeReferences.

public static void synchronizeReferences(ArtifactTemplateId id) throws IOException {
    TArtifactTemplate template = RepositoryFactory.getRepository().getElement(id);
    DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(id);
    SortedSet<RepositoryFileReference> files = RepositoryFactory.getRepository().getContainedFiles(fileDir);
    if (files.isEmpty()) {
        // clear artifact references
        template.setArtifactReferences(null);
    } else {
        TArtifactTemplate.ArtifactReferences artifactReferences = new TArtifactTemplate.ArtifactReferences();
        template.setArtifactReferences(artifactReferences);
        List<TArtifactReference> artRefList = artifactReferences.getArtifactReference();
        for (RepositoryFileReference ref : files) {
            // determine path
            // path relative from the root of the CSAR is ok (COS01, line 2663)
            // double encoded - see ADR-0003
            String path = Util.getUrlPath(ref);
            // put path into data structure
            // we do not use Include/Exclude as we directly reference a concrete file
            TArtifactReference artRef = new TArtifactReference();
            artRef.setReference(path);
            artRefList.add(artRef);
        }
    }
    BackendUtils.persist(id, template);
}
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.common.RepositoryFileReference) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference)

Example 2 with ArtifactTemplateFilesDirectoryId

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

the class ScriptPlugin method downloadDependenciesBasedOnArtifact.

@Override
public GeneratedArtifacts downloadDependenciesBasedOnArtifact(QName artifactTemplate, IRepository repository) {
    ArtifactTemplateId originalId = new ArtifactTemplateId(artifactTemplate);
    QName selfContainedVersion = VersionSupport.getSelfContainedVersion(originalId);
    ArtifactTemplateId selfContainedId = new ArtifactTemplateId(selfContainedVersion);
    if (!repository.exists(selfContainedId)) {
        try {
            repository.duplicate(originalId, selfContainedId);
        } catch (IOException e) {
            logger.error("Could not create self-containd artifact template {}", selfContainedId, e);
        }
    }
    ArtifactTemplateFilesDirectoryId originalFilesId = new ArtifactTemplateFilesDirectoryId(selfContainedId);
    GeneratedArtifacts generatedArtifacts = new GeneratedArtifacts(artifactTemplate);
    generatedArtifacts.selfContainedArtifactQName = selfContainedVersion;
    boolean createdSelfContainedVersion = false;
    for (RepositoryFileReference containedFile : repository.getContainedFiles(originalFilesId)) {
        if (containedFile.getFileName().endsWith(".sh")) {
            StringBuilder newScriptContents = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new FileReader(repository.ref2AbsolutePath(containedFile).toFile()))) {
                String line;
                ArrayList<String> packageNames = new ArrayList<>();
                int packageNameCount = 0;
                while ((line = reader.readLine()) != null) {
                    List<String> strings = Arrays.asList(line.replaceAll("[;&]", "").split("\\s+"));
                    Iterator<String> words = strings.iterator();
                    if (words.hasNext() && StringUtils.isNotBlank(line) && line.contains("apt")) {
                        String word = words.next();
                        while ("sudo".equals(word) || word.startsWith("-")) {
                            word = words.next();
                        }
                        if (words.hasNext() && ("apt-get".equals(word) || "apt".equals(word))) {
                            word = words.next();
                            while (word.startsWith("-")) {
                                word = words.next();
                            }
                            if (word.equals("install") && words.hasNext()) {
                                words.forEachRemaining(packageToInstall -> {
                                    if (!packageToInstall.startsWith("-")) {
                                        packageNames.add(createDeploymentArtifact(originalId, repository, generatedArtifacts, packageToInstall));
                                    }
                                });
                            }
                        }
                    }
                    if (!packageNames.isEmpty() && packageNameCount++ < packageNames.size()) {
                        createdSelfContainedVersion = true;
                        packageNames.forEach(packet -> newScriptContents.append(this.updateScriptFile(packet)));
                    } else {
                        newScriptContents.append(line).append("\n");
                    }
                }
            } catch (IOException e) {
                logger.error("Error while reading script file {}", repository.ref2AbsolutePath(containedFile), e);
            }
            if (newScriptContents.length() > 0) {
                try (BufferedWriter writer = new BufferedWriter(new FileWriter(repository.ref2AbsolutePath(containedFile).toFile()))) {
                    writer.write(newScriptContents.toString());
                    writer.flush();
                } catch (IOException e) {
                    logger.error("Error while writing to script file {}", repository.ref2AbsolutePath(containedFile), e);
                }
            }
        }
    }
    if (createdSelfContainedVersion) {
        return generatedArtifacts;
    }
    try {
        repository.forceDelete(selfContainedId);
    } catch (IOException e) {
        logger.error("Could not delete not required self-contained {}!", selfContainedId, e);
    }
    return null;
}
Also used : QName(javax.xml.namespace.QName) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) BufferedWriter(java.io.BufferedWriter) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 3 with ArtifactTemplateFilesDirectoryId

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

the class BackendUtilsTest method synchronizeReferencesDoesNotRemoveUrls.

@Test
public void synchronizeReferencesDoesNotRemoveUrls() 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 = createArtifactTemplateWithSingleReferenceToAnUrl();
    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 4 with ArtifactTemplateFilesDirectoryId

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

the class BackendUtilsTest method synchronizeReferencesDoesNotRemoveExistentFileAndDoesNotRemoveUrls.

@Test
public void synchronizeReferencesDoesNotRemoveExistentFileAndDoesNotRemoveUrls() 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);
    SortedSet<RepositoryFileReference> containedReferences = new TreeSet<>();
    RepositoryFileReference repositoryFileReference = new RepositoryFileReference(artifactTemplateId, "exists.txt");
    containedReferences.add(repositoryFileReference);
    when(repository.getContainedFiles(artifactTemplateFilesDirectoryId)).thenReturn(containedReferences);
    TArtifactTemplate artifactTemplate = createArtifactTemplateWithReferenceToAnUrlAndExistentFile();
    when(repository.getElement(artifactTemplateId)).thenReturn(artifactTemplate);
    TArtifactTemplate synchronizedArtifactTemplate = BackendUtils.synchronizeReferences(repository, artifactTemplateId);
    assertEquals(createArtifactTemplateWithReferenceToAnUrlAndExistentFile(), synchronizedArtifactTemplate);
}
Also used : ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) TreeSet(java.util.TreeSet) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) Test(org.junit.jupiter.api.Test)

Example 5 with ArtifactTemplateFilesDirectoryId

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

the class WriterUtils method storeDefinitions.

// FIXME this used to be targeting Definitions, check whether that was necessary
public static void storeDefinitions(IRepository repository, TDefinitions definitions, boolean overwrite, Path dir) {
    Path path = null;
    try {
        path = Files.createTempDirectory("winery");
    } catch (IOException e) {
        e.printStackTrace();
    }
    LOGGER.debug("Store definition: {}", definitions.getId());
    saveDefinitions(definitions, path, definitions.getTargetNamespace(), definitions.getId());
    TDefinitions cleanDefinitions = loadDefinitions(path, definitions.getTargetNamespace(), definitions.getId());
    CsarImporter csarImporter = new CsarImporter(repository);
    List<Exception> exceptions = new ArrayList<>();
    cleanDefinitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().forEach(entry -> {
        String namespace = csarImporter.getNamespace(entry, definitions.getTargetNamespace());
        csarImporter.setNamespace(entry, namespace);
        String id = ModelUtilities.getId(entry);
        Class<? extends DefinitionsChildId> widClazz = Util.getComponentIdClassForTExtensibleElements(entry.getClass());
        final DefinitionsChildId wid = BackendUtils.getDefinitionsChildId(widClazz, namespace, id, false);
        if (repository.exists(wid)) {
            if (overwrite) {
                try {
                    repository.forceDelete(wid);
                } catch (IOException e) {
                    exceptions.add(e);
                }
            } else {
                return;
            }
        }
        if (entry instanceof TArtifactTemplate) {
            List<TArtifactReference> artifactReferences = ((TArtifactTemplate) entry).getArtifactReferences();
            artifactReferences.stream().filter(Objects::nonNull).forEach(ref -> {
                String reference = ref.getReference();
                URI refURI;
                try {
                    refURI = new URI(reference);
                } catch (URISyntaxException e) {
                    LOGGER.error("Invalid URI {}", reference);
                    return;
                }
                if (refURI.isAbsolute()) {
                    return;
                }
                Path artifactPath = dir.resolve(reference);
                if (!Files.exists(artifactPath)) {
                    LOGGER.error("File not found {}", artifactPath);
                    return;
                }
                ArtifactTemplateFilesDirectoryId aDir = new ArtifactTemplateFilesDirectoryId((ArtifactTemplateId) wid);
                RepositoryFileReference aFile = new RepositoryFileReference(aDir, artifactPath.getFileName().toString());
                try (InputStream is = Files.newInputStream(artifactPath);
                    BufferedInputStream bis = new BufferedInputStream(is)) {
                    MediaType mediaType = BackendUtils.getMimeType(bis, artifactPath.getFileName().toString());
                    repository.putContentToFile(aFile, bis, mediaType);
                } catch (IOException e) {
                    LOGGER.error("Could not read artifact template file: {}", artifactPath);
                }
            });
        }
        final TDefinitions part = BackendUtils.createWrapperDefinitions(wid, repository);
        part.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(entry);
        RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(wid);
        String content = BackendUtils.getXMLAsString(part, true, repository);
        try {
            repository.putContentToFile(ref, content, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
        } catch (Exception e) {
            exceptions.add(e);
        }
    });
}
Also used : Path(java.nio.file.Path) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) CsarImporter(org.eclipse.winery.repository.importing.CsarImporter) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) TransformerException(javax.xml.transform.TransformerException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) MediaType(org.apache.tika.mime.MediaType) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions)

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