Skip to content

Commit

Permalink
#87: Add generic codeflow for copying/refering/duplicating
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-sita committed Aug 5, 2024
1 parent 763e6f5 commit 2b649a2
Show file tree
Hide file tree
Showing 9 changed files with 506 additions and 302 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import place.sita.labelle.core.automation.tasks.AutomationTasksCommon;
import place.sita.labelle.core.repository.inrepository.InRepositoryService;
import place.sita.labelle.core.repository.inrepository.image.ImageResponse;
import place.sita.labelle.core.repository.inrepository.tags.PersistableImagesTags;
import place.sita.labelle.core.repository.inrepository.image.replication.ReplicationParam;
import place.sita.labelle.core.repository.taskapi.RepositoryApi;
import place.sita.labelle.datasource.util.CloseableIterator;
import place.sita.magicscheduler.TaskContext;
import place.sita.magicscheduler.TaskResult;
import place.sita.magicscheduler.tasktype.TaskType;
import place.sita.magicscheduler.scheduler.resources.resource.Resource;
import place.sita.magicscheduler.tasktype.TaskType;

import java.util.HashMap;
import java.util.List;
Expand All @@ -37,65 +34,14 @@ public TaskResult<UUID> runTask(CloneRepositoryTaskInput parameter, TaskContext<

AutomationTasksCommon.createRepo(parameter.newRepositoryName(), taskContext, newRepoId);

addParents(parameter, taskContext, newRepoId);

copyImages(parameter, taskContext, newRepoId);
taskContext.getApi().getImageReplication().execute(new ReplicationParam.HardCopyToNewRepo(
parameter.repositoryToClone(),
newRepoId
));

return TaskResult.success(newRepoId);
}

private static void copyImages(CloneRepositoryTaskInput parameter, TaskContext<RepositoryApi> taskContext, UUID newRepoId) {
InRepositoryService inRepositoryService = taskContext.getApi().getInRepositoryService();

try (CloseableIterator<ImageResponse> imageIterator = (inRepositoryService.images().images()).process().filterByRepository(parameter.repositoryToClone()).getIterator()) {
imageIterator.forEachRemaining(image -> {
UUID imageId = copyImage(taskContext, newRepoId, image);

addTags(taskContext, image, imageId);
});
}
}

private static void addTags(TaskContext<RepositoryApi> taskContext, ImageResponse image, UUID imageId) {
PersistableImagesTags persistableImagesTags = new PersistableImagesTags();

taskContext.getApi()
.getInRepositoryService()
.getTags(image.id())
.forEach(tagResponse -> {
persistableImagesTags.addTag(imageId, tagResponse.category(), tagResponse.tag());
});

taskContext.getApi()
.getInRepositoryService()
.addTags(persistableImagesTags);
}

private static UUID copyImage(TaskContext<RepositoryApi> taskContext, UUID newRepoId, ImageResponse image) {
InRepositoryService inRepositoryService = taskContext.getApi()
.getInRepositoryService();
UUID imageId = inRepositoryService.images().copyImage(newRepoId, image.id());
return imageId;
}

private static void addParents(CloneRepositoryTaskInput parameter, TaskContext<RepositoryApi> taskContext, UUID newRepoId) {
taskContext
.getApi()
.getRepositoryService()
.getParents(parameter.repositoryToClone())
.forEach(parent -> {
UUID remappedParent = parameter.parentsRemapping().getOrDefault(parent.id(), parent.id());
taskContext.getApi().getRepositoryService().addParentChild(newRepoId, remappedParent);
});
}

private static void createRepo(CloneRepositoryTaskInput parameter, TaskContext<RepositoryApi> taskContext, UUID newRepoId) {
taskContext
.getApi()
.getRepositoryService()
.addRepository(newRepoId, parameter.newRepositoryName());
}

@Override
public String sampleValue() {
return serializeParam(new CloneRepositoryTaskInput(UUID.randomUUID(), UUID.randomUUID(), "new-repo", new HashMap<>()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import place.sita.labelle.core.automation.tasks.AutomationTasksCommon;
import place.sita.labelle.core.repository.acrossrepository.PullableImageResponse;
import place.sita.labelle.core.repository.inrepository.InRepositoryService;
import place.sita.labelle.core.repository.inrepository.tags.PersistableImagesTags;
import place.sita.labelle.core.repository.inrepository.tags.Tag;
import place.sita.labelle.core.repository.inrepository.image.replication.ReplicationParam;
import place.sita.labelle.core.repository.taskapi.RepositoryApi;
import place.sita.magicscheduler.TaskContext;
import place.sita.magicscheduler.TaskResult;
import place.sita.magicscheduler.tasktype.TaskType;
import place.sita.magicscheduler.scheduler.resources.resource.Resource;
import place.sita.magicscheduler.tasktype.TaskType;

import java.util.*;
import java.util.List;
import java.util.UUID;

@Component
public class CreateChildRepositoryTask implements TaskType<CreateChildRepositoryTaskInput, RepositoryApi, UUID> {
Expand All @@ -36,75 +34,14 @@ public TaskResult<UUID> runTask(CreateChildRepositoryTaskInput parameter, TaskCo

addParents(parameter, taskContext, newRepoId);

Map<String, List<UUID>> parentImagesReferences = getParentImagesReferences(parameter, taskContext);

Map<String, UUID> referenceToNewImageId = createImagesInNewRepo(taskContext, parentImagesReferences, newRepoId);

addTagsToNewImages(taskContext, parentImagesReferences, referenceToNewImageId);
taskContext.getApi().getImageReplication().execute(new ReplicationParam.FillChildRepo(
parameter.parents(),
newRepoId
));

return TaskResult.success(newRepoId);
}

private static Map<UUID, List<Tag>> getTagsOfParents(TaskContext<RepositoryApi> taskContext, Map<String, List<UUID>> parentImagesReferences) {
List<UUID> allReferencedImages = parentImagesReferences.values()
.stream()
.flatMap(Collection::stream)
.toList();

return taskContext.getApi().getAcrossRepositoryService().getTags(allReferencedImages);
}

private static void addTagsToNewImages(TaskContext<RepositoryApi> taskContext, Map<String, List<UUID>> parentImagesReferences, Map<String, UUID> referenceToNewImageId) {
Map<UUID, List<Tag>> tags = getTagsOfParents(taskContext, parentImagesReferences);

PersistableImagesTags persistableImagesTags = new PersistableImagesTags();

for (var newImage : referenceToNewImageId.entrySet()) {
String newImageReference = newImage.getKey();
UUID newImageId = newImage.getValue();
List<UUID> parentImageIds = parentImagesReferences.get(newImageReference);
for (UUID parentImageId : parentImageIds) {
List<Tag> tagValues = tags.get(parentImageId);
for (Tag tagValue : tagValues) {
persistableImagesTags.addTag(newImageId, tagValue);
}
}
}

taskContext.getApi().getInRepositoryService().addTags(persistableImagesTags);
}

private static Map<String, UUID> createImagesInNewRepo(TaskContext<RepositoryApi> taskContext, Map<String, List<UUID>> parentImagesReferences, UUID newRepoId) {
Map<String, UUID> referenceToNewImageId = new HashMap<>();

parentImagesReferences.forEach((reference, parents) -> {
InRepositoryService inRepositoryService = taskContext.getApi().getInRepositoryService();
UUID imageId = inRepositoryService.images().referImage(newRepoId, parents.get(0), reference);
referenceToNewImageId.put(reference, imageId);
});
return referenceToNewImageId;
}

private static Map<String, List<UUID>> getParentImagesReferences(CreateChildRepositoryTaskInput parameter, TaskContext<RepositoryApi> taskContext) {
Map<String, List<UUID>> referencesToImageIds = new HashMap<>(); // I think it's safe to say that pulling from 2+ images
// with vastly different configuration should be undefined. // todo report that

for (UUID parent : parameter.parents()) {
List<PullableImageResponse> imagesInRepo = taskContext.getApi().getAcrossRepositoryService().getImages(parent, 0, Integer.MAX_VALUE, "");

imagesInRepo.forEach(image -> {
if (referencesToImageIds.containsKey(image.reference())) {
referencesToImageIds.get(image.reference()).add(image.imageId());
} else {
List<UUID> imageIds = new ArrayList<>();
imageIds.add(image.imageId());
referencesToImageIds.put(image.reference(), imageIds);
}
});
}
return referencesToImageIds;
}

private static void addParents(CreateChildRepositoryTaskInput parameter, TaskContext<RepositoryApi> taskContext, UUID newRepoId) {
parameter.parents().forEach(parent -> {
taskContext.getApi().getRepositoryService().addParentChild(newRepoId, parent);
Expand Down

This file was deleted.

Loading

0 comments on commit 2b649a2

Please sign in to comment.