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;
}
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);
}
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());
}
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);
}
}
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);
}
}
Aggregations