From 37db35b6bf2742b6504196d6a8feb938377adb91 Mon Sep 17 00:00:00 2001 From: lucio Date: Tue, 27 Aug 2024 09:08:20 +0200 Subject: [PATCH] added scripts --- .../java/MongoToMinIOPortingSharedFolder.java | 10 +-- .../java/MongoToMinIOPortingVREFolders.java | 10 +-- .../java/MongoToMinioPortingNotShared.java | 10 +-- .../java/UpdatePayloadBackednToDefaultS3.java | 86 +++++++++++++++++++ 4 files changed, 101 insertions(+), 15 deletions(-) create mode 100644 src/main/java/UpdatePayloadBackednToDefaultS3.java diff --git a/src/main/java/MongoToMinIOPortingSharedFolder.java b/src/main/java/MongoToMinIOPortingSharedFolder.java index 5ab2fed..c460e56 100644 --- a/src/main/java/MongoToMinIOPortingSharedFolder.java +++ b/src/main/java/MongoToMinIOPortingSharedFolder.java @@ -15,7 +15,6 @@ import javax.jcr.version.VersionIterator; import javax.jcr.version.VersionManager; import org.apache.jackrabbit.api.JackrabbitSession; -import org.gcube.common.storagehub.model.Constants; import org.gcube.common.storagehub.model.NodeConstants; import org.gcube.common.storagehub.model.exceptions.StorageIdNotFoundException; import org.gcube.common.storagehub.model.items.AbstractFileItem; @@ -27,6 +26,7 @@ import org.gcube.common.storagehub.model.items.nodes.PayloadBackend; import org.gcube.common.storagehub.model.storages.MetaInfo; import org.gcube.common.storagehub.model.storages.StorageBackend; import org.gcube.common.storagehub.model.storages.StorageBackendFactory; +import org.gcube.common.storagehub.model.storages.StorageNames; import org.gcube.data.access.storagehub.scripting.AbstractScript; import org.gcube.data.access.storagehub.scripting.ScriptParameter; import org.gcube.data.access.storagehub.scripting.ScriptUtil; @@ -90,14 +90,14 @@ public class MongoToMinIOPortingSharedFolder implements AbstractScript{ StorageBackend mongo = null; StorageBackend minio= null; for (StorageBackendFactory factory : factories) { - if (factory.getName() == Constants.MONGO_STORAGE) + if (factory.getName() == StorageNames.MONGO_STORAGE) mongo = factory.create(file.getContent().getPayloadBackend()); - if (factory.getName() == Constants.DEFAULT_S3_STORAGE) { - minio = factory.create(new PayloadBackend(Constants.DEFAULT_S3_STORAGE, null)); + if (factory.getName() == StorageNames.DEFAULT_S3_STORAGE) { + minio = factory.create(new PayloadBackend(StorageNames.DEFAULT_S3_STORAGE, null)); } } - if (file.getContent().getPayloadBackend().getStorageName()==Constants.MONGO_STORAGE) { + if (file.getContent().getPayloadBackend().getStorageName()==StorageNames.MONGO_STORAGE) { stringBuilder.append("\n").append("elaborating file ").append(file.getId()).append(" - ").append(file.getTitle()); try { String mongoId = file.getContent().getStorageId(); diff --git a/src/main/java/MongoToMinIOPortingVREFolders.java b/src/main/java/MongoToMinIOPortingVREFolders.java index ab78c54..c86d404 100644 --- a/src/main/java/MongoToMinIOPortingVREFolders.java +++ b/src/main/java/MongoToMinIOPortingVREFolders.java @@ -15,7 +15,6 @@ import javax.jcr.version.VersionIterator; import javax.jcr.version.VersionManager; import org.apache.jackrabbit.api.JackrabbitSession; -import org.gcube.common.storagehub.model.Constants; import org.gcube.common.storagehub.model.NodeConstants; import org.gcube.common.storagehub.model.exceptions.StorageIdNotFoundException; import org.gcube.common.storagehub.model.items.AbstractFileItem; @@ -27,6 +26,7 @@ import org.gcube.common.storagehub.model.items.nodes.PayloadBackend; import org.gcube.common.storagehub.model.storages.MetaInfo; import org.gcube.common.storagehub.model.storages.StorageBackend; import org.gcube.common.storagehub.model.storages.StorageBackendFactory; +import org.gcube.common.storagehub.model.storages.StorageNames; import org.gcube.data.access.storagehub.scripting.AbstractScript; import org.gcube.data.access.storagehub.scripting.ScriptParameter; import org.gcube.data.access.storagehub.scripting.ScriptUtil; @@ -96,14 +96,14 @@ public class MongoToMinIOPortingVREFolders implements AbstractScript{ StorageBackend mongo = null; StorageBackend minio= null; for (StorageBackendFactory factory : factories) { - if (factory.getName() == Constants.MONGO_STORAGE) + if (factory.getName() == StorageNames.MONGO_STORAGE) mongo = factory.create(file.getContent().getPayloadBackend()); - if (factory.getName() == Constants.DEFAULT_S3_STORAGE) { - minio = factory.create(new PayloadBackend(Constants.DEFAULT_S3_STORAGE, null)); + if (factory.getName() == StorageNames.DEFAULT_S3_STORAGE) { + minio = factory.create(new PayloadBackend(StorageNames.DEFAULT_S3_STORAGE, null)); } } - if (file.getContent().getPayloadBackend().getStorageName()==Constants.MONGO_STORAGE) { + if (file.getContent().getPayloadBackend().getStorageName()==StorageNames.MONGO_STORAGE) { stringBuilder.append("\n").append("elaborating file ").append(file.getId()).append(" - ").append(file.getTitle()); try { String mongoId = file.getContent().getStorageId(); diff --git a/src/main/java/MongoToMinioPortingNotShared.java b/src/main/java/MongoToMinioPortingNotShared.java index 2e3584b..e9084dc 100644 --- a/src/main/java/MongoToMinioPortingNotShared.java +++ b/src/main/java/MongoToMinioPortingNotShared.java @@ -15,7 +15,6 @@ import javax.jcr.version.VersionIterator; import javax.jcr.version.VersionManager; import org.apache.jackrabbit.api.JackrabbitSession; -import org.gcube.common.storagehub.model.Constants; import org.gcube.common.storagehub.model.NodeConstants; import org.gcube.common.storagehub.model.Path; import org.gcube.common.storagehub.model.Paths; @@ -28,6 +27,7 @@ import org.gcube.common.storagehub.model.items.nodes.PayloadBackend; import org.gcube.common.storagehub.model.storages.MetaInfo; import org.gcube.common.storagehub.model.storages.StorageBackend; import org.gcube.common.storagehub.model.storages.StorageBackendFactory; +import org.gcube.common.storagehub.model.storages.StorageNames; import org.gcube.data.access.storagehub.scripting.AbstractScript; import org.gcube.data.access.storagehub.scripting.ScriptParameter; import org.gcube.data.access.storagehub.scripting.ScriptUtil; @@ -91,14 +91,14 @@ public class MongoToMinioPortingNotShared implements AbstractScript{ StorageBackend mongo = null; StorageBackend minio= null; for (StorageBackendFactory factory : factories) { - if (factory.getName() == Constants.MONGO_STORAGE) + if (factory.getName() == StorageNames.MONGO_STORAGE) mongo = factory.create(file.getContent().getPayloadBackend()); - if (factory.getName() == Constants.DEFAULT_S3_STORAGE) { - minio = factory.create(new PayloadBackend(Constants.DEFAULT_S3_STORAGE, null)); + if (factory.getName() == StorageNames.DEFAULT_S3_STORAGE) { + minio = factory.create(new PayloadBackend(StorageNames.DEFAULT_S3_STORAGE, null)); } } - if (file.getContent().getPayloadBackend().getStorageName()==Constants.MONGO_STORAGE) { + if (file.getContent().getPayloadBackend().getStorageName()==StorageNames.MONGO_STORAGE) { stringBuilder.append("\n").append("elaborating file ").append(file.getId()).append(" - ").append(file.getTitle()); try { String mongoId = file.getContent().getStorageId(); diff --git a/src/main/java/UpdatePayloadBackednToDefaultS3.java b/src/main/java/UpdatePayloadBackednToDefaultS3.java new file mode 100644 index 0000000..7af0312 --- /dev/null +++ b/src/main/java/UpdatePayloadBackednToDefaultS3.java @@ -0,0 +1,86 @@ +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.List; + +import javax.jcr.Node; +import javax.jcr.NodeIterator; +import javax.jcr.PathNotFoundException; + +import org.apache.jackrabbit.api.JackrabbitSession; +import org.gcube.common.storagehub.model.Excludes; +import org.gcube.common.storagehub.model.Paths; +import org.gcube.common.storagehub.model.items.FolderItem; +import org.gcube.common.storagehub.model.items.Item; +import org.gcube.data.access.storagehub.scripting.AbstractScript; +import org.gcube.data.access.storagehub.scripting.ScriptParameter; +import org.gcube.data.access.storagehub.scripting.ScriptUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * this script changes the storage backend of the folders from default-gcube-minio (an old test) to default-storage-s3 + * + */ +public class UpdatePayloadBackednToDefaultS3 implements AbstractScript { + + private static Logger log = LoggerFactory.getLogger(UpdatePayloadBackednToDefaultS3.class); + + @Override + public String run(JackrabbitSession session, ScriptParameter prameters, ScriptUtil scriptUtil) { + + StringBuilder stringBuilder = new StringBuilder(); + try { + stringBuilder.append("script started\n"); + Node node = session.getNode("/Home"); + + NodeIterator it = node.getNodes(); + while (it.hasNext()) { + Node home = it.nextNode(); + stringBuilder.append("checking home " + home.getPath() + " \n"); + + String workspacePath = Paths.append(Paths.getPath(home.getPath()), "Workspace").toPath(); + + if (!session.nodeExists(workspacePath)) continue; + Node child = session.getNode(workspacePath); + stringBuilder.append("checking folder " + child.getPath() + " \n"); + List childItems = scriptUtil.getChildren(null, child, Excludes.ALL, true, FolderItem.class); + + visitChildrenAndUpdate(childItems, scriptUtil, stringBuilder); + } + session.save(); + } catch (Exception e) { + StringWriter writer = new StringWriter(); + PrintWriter sw = new PrintWriter(writer, true); + e.printStackTrace(sw); + return writer.toString(); + } + log.info(stringBuilder.toString()); + return stringBuilder.toString(); + + } + + private void visitChildrenAndUpdate(List items, ScriptUtil scriptUtil, StringBuilder stringBuilder) + throws Exception { + for (Item item : items) { + + if (item instanceof FolderItem fi) { + stringBuilder.append("checking folder " + fi.getPath() + " \n"); + Node folderNode = (Node) item.getRelatedNode(); + if (fi.getBackend().getStorageName().equals("default-gcube-minio")) { + + try { + Node payload = folderNode.getNode("hl:payloadBackend"); + payload.setProperty("hl:storageName", "default-gcube-s3"); + stringBuilder.append("folder node ").append(item.getTitle()).append(" updated \n"); + } catch (PathNotFoundException pnf) { + stringBuilder.append("storage path not found \n"); + } + } + List childItems = scriptUtil.getChildren(null, folderNode, Excludes.GET_ONLY_CONTENT, true, + FolderItem.class); + visitChildrenAndUpdate(childItems, scriptUtil, stringBuilder); + } + } + } + +}