2014-03-11 12:37:01 +01:00
|
|
|
package org.gcube.smartgears.utils.sweeper;
|
|
|
|
|
2017-03-23 17:16:26 +01:00
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
2014-03-11 12:37:01 +01:00
|
|
|
|
2017-03-23 17:16:26 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
2017-03-24 10:00:00 +01:00
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
2017-03-23 17:16:26 +01:00
|
|
|
import java.io.ObjectInputStream;
|
2020-04-07 15:25:38 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
2017-03-23 17:16:26 +01:00
|
|
|
import java.util.List;
|
2020-04-07 15:25:38 +02:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.stream.Collectors;
|
2014-03-11 12:37:01 +01:00
|
|
|
|
2017-03-23 17:16:26 +01:00
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2014-03-11 12:37:01 +01:00
|
|
|
import org.gcube.common.resources.gcore.HostingNode;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
import org.gcube.informationsystem.publisher.RegistryPublisher;
|
|
|
|
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
|
2020-04-07 15:25:38 +02:00
|
|
|
import org.gcube.informationsystem.publisher.ScopedPublisher;
|
2017-03-23 17:16:26 +01:00
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
|
|
import org.gcube.resources.discovery.icclient.ICFactory;
|
2014-03-11 12:37:01 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author Andrea Manzi(CERN)
|
|
|
|
*
|
|
|
|
* Implements the sweeping on the IS in case a container state is cleaned
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class Sweeper {
|
2017-03-23 17:16:26 +01:00
|
|
|
|
2014-03-11 12:37:01 +01:00
|
|
|
String ghn_state_path = "";
|
2017-03-23 17:16:26 +01:00
|
|
|
|
2014-03-11 12:37:01 +01:00
|
|
|
Logger logger;
|
2017-03-24 10:00:00 +01:00
|
|
|
|
|
|
|
String ghn_path;
|
|
|
|
|
|
|
|
String id ;
|
2020-04-07 15:25:38 +02:00
|
|
|
Set<ContextBean> contextBeans = new HashSet<ContextBean>();
|
2017-03-23 17:16:26 +01:00
|
|
|
|
2020-04-07 15:25:38 +02:00
|
|
|
public Sweeper (String ghnPath) throws Exception {
|
2017-03-23 17:16:26 +01:00
|
|
|
|
2014-03-11 12:37:01 +01:00
|
|
|
logger = LoggerFactory.getLogger(Sweeper.class);
|
2017-03-23 17:16:26 +01:00
|
|
|
|
2020-04-07 15:25:38 +02:00
|
|
|
ghn_path = ghnPath;
|
2017-03-23 17:16:26 +01:00
|
|
|
|
2014-03-11 12:37:01 +01:00
|
|
|
if (ghn_path == null ) {
|
|
|
|
logger.error("GHN_HOME not defined");
|
|
|
|
throw new Exception ("GHN_HOME not defined");
|
|
|
|
}
|
|
|
|
|
2017-03-23 17:16:26 +01:00
|
|
|
ghn_state_path=ghn_path+File.separator+"state";
|
2014-03-27 06:57:43 +01:00
|
|
|
|
2017-03-24 10:00:00 +01:00
|
|
|
deserializeState();
|
2014-03-27 06:57:43 +01:00
|
|
|
}
|
|
|
|
|
2017-03-23 17:16:26 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2017-03-24 10:00:00 +01:00
|
|
|
private void deserializeState(){
|
2017-03-23 17:16:26 +01:00
|
|
|
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(ghn_state_path+File.separator+"ghn.xml"))){
|
|
|
|
id = (String)ois.readObject();
|
2017-03-24 10:00:00 +01:00
|
|
|
List<String> tokens = (List<String>) ois.readObject();
|
|
|
|
for (String token : tokens){
|
|
|
|
AuthorizationEntry entry = authorizationService().get(token);
|
|
|
|
contextBeans.add(new ContextBean(token, entry.getContext()));
|
|
|
|
}
|
2017-03-23 17:16:26 +01:00
|
|
|
}catch(Exception e){
|
2017-03-28 15:53:57 +02:00
|
|
|
throw new RuntimeException("error loading persisted state",e);
|
2014-03-11 12:37:01 +01:00
|
|
|
}
|
2017-03-24 10:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void forceDeleteHostingNode(){
|
2020-04-07 15:25:38 +02:00
|
|
|
ScopedPublisher rp=RegistryPublisherFactory.scopedPublisher();
|
2017-03-23 17:16:26 +01:00
|
|
|
try{
|
|
|
|
DiscoveryClient<HostingNode> client = ICFactory.clientFor(HostingNode.class);
|
|
|
|
SimpleQuery query = ICFactory.queryFor(HostingNode.class);
|
|
|
|
query.addCondition("$resource/ID/text() = '"+id+"'");
|
2020-04-07 15:25:38 +02:00
|
|
|
List<String> scopes = contextBeans.stream().map(b -> b.getContext()).collect(Collectors.toList());
|
|
|
|
ScopeProvider.instance.set(scopes.get(0));
|
|
|
|
HostingNode node =null;
|
2017-03-24 10:00:00 +01:00
|
|
|
for (ContextBean contextBean : contextBeans){
|
|
|
|
SecurityTokenProvider.instance.set(contextBean.getToken());
|
|
|
|
ScopeProvider.instance.set(contextBean.getContext());
|
2017-03-23 17:16:26 +01:00
|
|
|
List<HostingNode> nodes = client.submit(query);
|
|
|
|
if (nodes.isEmpty()) continue;
|
2020-04-07 15:25:38 +02:00
|
|
|
else {
|
|
|
|
node = nodes.get(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-23 17:16:26 +01:00
|
|
|
}
|
2020-04-07 15:25:38 +02:00
|
|
|
rp.remove(node, scopes);
|
|
|
|
ScopeProvider.instance.reset();
|
2017-03-23 17:16:26 +01:00
|
|
|
}catch(Exception e){
|
|
|
|
throw new RuntimeException("error removing hosting node resource",e);
|
2014-03-11 12:37:01 +01:00
|
|
|
}
|
2017-03-23 17:16:26 +01:00
|
|
|
|
2017-03-24 10:00:00 +01:00
|
|
|
}
|
2017-03-23 17:16:26 +01:00
|
|
|
|
2017-03-24 10:00:00 +01:00
|
|
|
public void saveTokens(String fileName){
|
|
|
|
File file = new File(ghn_path+File.separator+fileName);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (file.exists())
|
|
|
|
file.delete();
|
|
|
|
file.createNewFile();
|
|
|
|
} catch (IOException e1) {
|
|
|
|
throw new RuntimeException(e1);
|
|
|
|
}
|
|
|
|
|
|
|
|
try(FileWriter fw = new FileWriter(file)){
|
|
|
|
for (ContextBean bean: contextBeans){
|
|
|
|
fw.write("<token>"+bean.getToken()+"</token> <!--- "+bean.getContext()+" -->\n");
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
|
2017-03-23 17:16:26 +01:00
|
|
|
}
|
2017-03-24 10:00:00 +01:00
|
|
|
|
2014-03-11 12:37:01 +01:00
|
|
|
}
|