added scripts

This commit is contained in:
lucio 2024-08-27 09:08:20 +02:00
parent ba412f8154
commit 37db35b6bf
4 changed files with 101 additions and 15 deletions

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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<Item> 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<Item> 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<Item> childItems = scriptUtil.getChildren(null, folderNode, Excludes.GET_ONLY_CONTENT, true,
FolderItem.class);
visitChildrenAndUpdate(childItems, scriptUtil, stringBuilder);
}
}
}
}