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