Search in sources :

Example 21 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference 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 22 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class AbstractFileBasedRepository method duplicate.

protected void duplicate(DefinitionsChildId oldId, DefinitionsChildId newId, boolean moveOnly) throws IOException {
    Objects.requireNonNull(oldId);
    Objects.requireNonNull(newId);
    if (oldId.equals(newId)) {
        // we do not do anything - even not throwing an error
        return;
    }
    TDefinitions definitions = this.getDefinitions(oldId);
    RepositoryFileReference oldRef = BackendUtils.getRefOfDefinitions(oldId);
    RepositoryFileReference newRef = BackendUtils.getRefOfDefinitions(newId);
    // oldRef points to the definitions file,
    // getParent() returns the directory
    // we need toFile(), because we rely on FileUtils.moveDirectoryToDirectory
    File oldDir = this.id2AbsolutePath(oldRef.getParent()).toFile();
    File newDir = this.id2AbsolutePath(newRef.getParent()).toFile();
    if (moveOnly) {
        org.apache.commons.io.FileUtils.moveDirectory(oldDir, newDir);
    } else {
        org.apache.commons.io.FileUtils.copyDirectory(oldDir, newDir);
    }
    // Update definitions and store it
    // This also updates the definitions of componentInstanceResource
    BackendUtils.updateWrapperDefinitions(newId, definitions, this);
    // This works, because the definitions object here is the same as the definitions object treated at copyIdToFields
    // newId has to be passed, because the id is final at AbstractComponentInstanceResource
    BackendUtils.copyIdToFields((HasIdInIdOrNameField) definitions.getElement(), newId);
    try {
        BackendUtils.persist(this, newId, definitions);
    } catch (InvalidPathException e) {
        LOGGER.debug("Invalid path during write", e);
    // QUICK FIX
    // Somewhere, the first letter is deleted --> /odetypes/http%3A%2F%2Fwww.example.org%2F05/
    // We just ignore it for now
    }
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) File(java.io.File) InvalidPathException(org.eclipse.jgit.dircache.InvalidPathException)

Example 23 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class AbstractFileBasedRepository method getNamespaceManager.

@Override
public NamespaceManager getNamespaceManager() {
    NamespaceManager manager;
    RepositoryFileReference ref = BackendUtils.getRefOfJsonConfiguration(new NamespacesId());
    manager = new JsonBasedNamespaceManager(ref2AbsolutePath(ref).toFile(), isLocal);
    Configuration configuration = this.getConfiguration(new NamespacesId());
    if (!configuration.isEmpty()) {
        ConfigurationBasedNamespaceManager old = new ConfigurationBasedNamespaceManager(configuration);
        manager.replaceAll(old.getAllNamespaces());
        try {
            forceDelete(BackendUtils.getRefOfConfiguration(new NamespacesId()));
        } catch (IOException e) {
            LOGGER.error("Could not remove old namespace configuration.", e);
        }
    }
    return manager;
}
Also used : NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) Configuration(org.apache.commons.configuration2.Configuration) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) NamespacesId(org.eclipse.winery.model.ids.admin.NamespacesId) IOException(java.io.IOException)

Example 24 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class WriterUtils method storeTypes.

public static void storeTypes(IRepository repository, Path path, String namespace, String id) {
    LOGGER.debug("Store type: {}", id);
    try {
        MediaType mediaType = MediaTypes.MEDIATYPE_XSD;
        TImport.Builder builder = new TImport.Builder(Namespaces.XML_NS);
        builder.setNamespace(namespace);
        builder.setLocation(id + ".xsd");
        GenericImportId rid = new XSDImportId(namespace, id, false);
        TDefinitions definitions = BackendUtils.createWrapperDefinitions(rid, repository);
        definitions.getImport().add(builder.build());
        CsarImporter.storeDefinitions(repository, rid, definitions);
        RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(rid);
        List<File> files = Files.list(path).filter(Files::isRegularFile).map(Path::toFile).collect(Collectors.toList());
        for (File file : files) {
            BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file));
            RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), file.getName());
            repository.putContentToFile(fileRef, stream, mediaType);
        }
    } catch (IllegalArgumentException | IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : XSDImportId(org.eclipse.winery.model.ids.definitions.imports.XSDImportId) TImport(org.eclipse.winery.model.tosca.TImport) IOException(java.io.IOException) GenericImportId(org.eclipse.winery.model.ids.definitions.imports.GenericImportId) FileInputStream(java.io.FileInputStream) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) MediaType(org.apache.tika.mime.MediaType) Files(java.nio.file.Files) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) File(java.io.File)

Example 25 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference 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

RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)79 IOException (java.io.IOException)33 Path (java.nio.file.Path)24 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)17 QName (javax.xml.namespace.QName)14 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)14 InputStream (java.io.InputStream)13 ArrayList (java.util.ArrayList)12 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)12 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)9 Test (org.junit.jupiter.api.Test)9 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)8 PlanId (org.eclipse.winery.model.ids.elements.PlanId)8 PlansId (org.eclipse.winery.model.ids.elements.PlansId)8 IRepository (org.eclipse.winery.repository.backend.IRepository)8 MediaType (org.apache.tika.mime.MediaType)7 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)7 BufferedInputStream (java.io.BufferedInputStream)6 HashMap (java.util.HashMap)6 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)6