diff --git a/pom.xml b/pom.xml index 6bf97f1..285e3d0 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,16 @@ common-smartgears-utils 1.0.1-SNAPSHOT + + org.gcube.common + authorization-client + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + + + org.gcube.common + common-authorization + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + org.gcube.resources registry-publisher diff --git a/src/main/java/org/gcube/smartgears/utils/sweeper/AppSweeperClient.java b/src/main/java/org/gcube/smartgears/utils/sweeper/AppSweeperClient.java deleted file mode 100644 index 9b82458..0000000 --- a/src/main/java/org/gcube/smartgears/utils/sweeper/AppSweeperClient.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.gcube.smartgears.utils.sweeper; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - * @author andrea - * - */ -public class AppSweeperClient { - - public static void main (String args[]) { - - - Logger logger = LoggerFactory.getLogger(AppSweeperClient.class); - - if (args.length <1) { - logger.error("Missing app name parameter"); - System.exit(1); - } - - String name = args[0]; - - Sweeper sw = null; - try { - sw = new Sweeper(); - } catch (Exception e) { - logger.error("Error initializing the Sweeper ", e); - System.exit(1); - } - - - try { - sw.cleanRIProfile(name); - } catch (Exception e) { - logger.error("Error cleaning the RunningInstance profile ", e); - System.exit(1); - } - } - -} diff --git a/src/main/java/org/gcube/smartgears/utils/sweeper/ContainerSweeperClient.java b/src/main/java/org/gcube/smartgears/utils/sweeper/ContainerSweeperClient.java index cbf22d2..b3bb880 100644 --- a/src/main/java/org/gcube/smartgears/utils/sweeper/ContainerSweeperClient.java +++ b/src/main/java/org/gcube/smartgears/utils/sweeper/ContainerSweeperClient.java @@ -19,6 +19,8 @@ public class ContainerSweeperClient { try { sw = new Sweeper(); } catch (Exception e) { + System.out.println("Error initializing the Sweeper"); + e.printStackTrace(); logger.error("Error initializing the Sweeper ", e); System.exit(1); } @@ -26,17 +28,12 @@ public class ContainerSweeperClient { try { - sw.cleanGHNProfile(); + sw.forceDeleteHostingNode(); } catch (Exception e) { logger.error("Error cleaning the GHN profile ", e); System.exit(1); } - try { - sw.cleanRIProfiles(); - } catch (Exception e) { - logger.error("Error cleaning the RunningInstance profiles ", e); - System.exit(1); - } + } } diff --git a/src/main/java/org/gcube/smartgears/utils/sweeper/Sweeper.java b/src/main/java/org/gcube/smartgears/utils/sweeper/Sweeper.java index b114860..bc4678d 100644 --- a/src/main/java/org/gcube/smartgears/utils/sweeper/Sweeper.java +++ b/src/main/java/org/gcube/smartgears/utils/sweeper/Sweeper.java @@ -1,22 +1,21 @@ package org.gcube.smartgears.utils.sweeper; +import static org.gcube.common.authorization.client.Constants.authorizationService; + import java.io.File; -import java.io.FileNotFoundException; -import java.util.ArrayList; +import java.io.FileInputStream; +import java.io.ObjectInputStream; +import java.util.List; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; - -import org.gcube.common.resources.gcore.GCoreEndpoint; +import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.resources.gcore.HostingNode; -import org.gcube.common.resources.gcore.Resource; import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.common.scope.impl.ScopeBean; -import org.gcube.common.scope.impl.ScopeBean.Type; -import org.gcube.informationsystem.publisher.AdvancedPublisher; import org.gcube.informationsystem.publisher.RegistryPublisher; import org.gcube.informationsystem.publisher.RegistryPublisherFactory; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.resources.discovery.icclient.ICFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,124 +27,62 @@ import org.slf4j.LoggerFactory; * */ public class Sweeper { - + String ghn_state_path = ""; - + Logger logger; - + public Sweeper () throws Exception { - + logger = LoggerFactory.getLogger(Sweeper.class); - + String ghn_path = System.getenv("GHN_HOME"); - + if (ghn_path == null ) { logger.error("GHN_HOME not defined"); throw new Exception ("GHN_HOME not defined"); } - + ghn_state_path=ghn_path+File.separator+"state"; - - - } - - public HostingNode getGHNProfile() throws JAXBException, FileNotFoundException { - - JAXBContext jc = JAXBContext.newInstance(HostingNode.class); - - Unmarshaller um = jc.createUnmarshaller(); - - HostingNode hostingNode = (HostingNode) - um.unmarshal(new java.io.FileInputStream(ghn_state_path+File.separator+"ghn.xml" )); - - return hostingNode; } - public GCoreEndpoint getRunningInstanceProfile(String name) throws Exception { - - JAXBContext jc = JAXBContext.newInstance(GCoreEndpoint.class); - - Unmarshaller um = jc.createUnmarshaller(); - - GCoreEndpoint ri = null; - - File subfolder = new File(ghn_state_path+ File.separator+ name); - - - try { - ri = (GCoreEndpoint) - um.unmarshal(new java.io.FileInputStream(subfolder.getAbsolutePath()+File.separator+"endpoint.xml" )); - } catch (FileNotFoundException | JAXBException e) { - e.printStackTrace(); - throw new Exception(e); - } - - return ri; - - } - - - public ArrayList getRunningInstanceProfiles() throws Exception { - - ArrayList endpoints = new ArrayList(); - - JAXBContext jc = JAXBContext.newInstance(GCoreEndpoint.class); - - Unmarshaller um = jc.createUnmarshaller(); - - File file = new File(ghn_state_path); - - String[] files = file.list(); - - for (String name : files){ - File subfolder = new File(ghn_state_path+ File.separator+ name); - if (subfolder.isDirectory()) { - GCoreEndpoint ri; - try { - ri = (GCoreEndpoint) - um.unmarshal(new java.io.FileInputStream(subfolder.getAbsolutePath()+File.separator+"endpoint.xml" )); - } catch (FileNotFoundException | JAXBException e) { - e.printStackTrace(); - continue; - } - endpoints.add(ri); - } - + @SuppressWarnings("unchecked") + public void forceDeleteHostingNode(){ + String id ; + List tokens ; + try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(ghn_state_path+File.separator+"ghn.xml"))){ + id = (String)ois.readObject(); + tokens = (List) ois.readObject(); + }catch(Exception e){ + throw new RuntimeException("error loading persisted state"); } - - return endpoints; - } - - - public void cleanGHNProfile() throws Exception{ - forceDeleteResource(this.getGHNProfile()); - } - - public void cleanRIProfile(String name) throws Exception{ - forceDeleteResource(this.getRunningInstanceProfile(name)); - - } - - public void cleanRIProfiles() throws Exception{ - for (GCoreEndpoint endp : this.getRunningInstanceProfiles()){ - forceDeleteResource(endp); - } - } + RegistryPublisher rp=RegistryPublisherFactory.create(); + System.out.println(" id to remove is "+id); + try{ + DiscoveryClient client = ICFactory.clientFor(HostingNode.class); + SimpleQuery query = ICFactory.queryFor(HostingNode.class); + query.addCondition("$resource/ID/text() = '"+id+"'"); + for (String token : tokens){ + AuthorizationEntry entry = authorizationService().get(token); + SecurityTokenProvider.instance.set(token); + ScopeProvider.instance.set(entry.getContext()); + System.out.println("trying to remove resource in scope "+entry.getContext()); + List nodes = client.submit(query); + System.out.println(" node is empty? "+nodes.isEmpty()); + if (nodes.isEmpty()) continue; + rp.remove(nodes.get(0)); + } - private void forceDeleteResource(Resource resource){ - RegistryPublisher rp; - AdvancedPublisher advancedPublisher; - ScopeBean scope = new ScopeBean(resource.scopes().iterator().next()); - while (!(scope.is(Type.INFRASTRUCTURE))) - scope =scope.enclosingScope(); - ScopeProvider.instance.set(scope.toString()); - rp=RegistryPublisherFactory.create(); - advancedPublisher=new AdvancedPublisher(rp); - advancedPublisher.forceRemove(resource); - logger.debug("Correctly Removed resource " + resource.id()); - + + }catch(Exception e){ + throw new RuntimeException("error removing hosting node resource",e); } + + + + + } } diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties deleted file mode 100644 index f700a10..0000000 --- a/src/main/resources/log4j.properties +++ /dev/null @@ -1,12 +0,0 @@ -# Set root category priority to WARN and its only appender to A1. -log4j.rootCategory=DEBUG, A0 - -log4j.appender.A0=org.apache.log4j.ConsoleAppender -log4j.appender.A0.layout=org.apache.log4j.PatternLayout -log4j.appender.A0.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p %c{2} [%t,%M:%L] %m%n - -log4j.appender.A1=org.apache.log4j.ConsoleAppender -log4j.appender.A1.layout=org.apache.log4j.PatternLayout -log4j.appender.A1.layout.ConversionPattern=[DT] %d{HH:mm:ss,SSS} %-5p %c{2} [%t,%M:%L] %m%n -log4j.category.org.gcube=DEBUG,A1 -log4j.additivity.org.gcube=false diff --git a/src/test/java/org/gcube/smartgears/utils/sweeper/test/SweeperTest.java b/src/test/java/org/gcube/smartgears/utils/sweeper/test/SweeperTest.java index 18d5802..bade898 100644 --- a/src/test/java/org/gcube/smartgears/utils/sweeper/test/SweeperTest.java +++ b/src/test/java/org/gcube/smartgears/utils/sweeper/test/SweeperTest.java @@ -1,16 +1,7 @@ package org.gcube.smartgears.utils.sweeper.test; -import java.io.FileNotFoundException; -import java.util.ArrayList; - -import javax.xml.bind.JAXBException; - -import org.gcube.common.resources.gcore.GCoreEndpoint; -import org.gcube.common.resources.gcore.HostingNode; import org.gcube.smartgears.utils.sweeper.Sweeper; -import org.junit.Assert; import org.junit.Before; -import org.junit.Test; public class SweeperTest { @@ -27,47 +18,4 @@ public class SweeperTest { } - @Test - public void getGHNProfile() { - - HostingNode node = null; - - try { - node = sw.getGHNProfile(); - } catch (FileNotFoundException | JAXBException e) { - e.printStackTrace(); - } - - Assert.assertNotNull(node); - System.out.println(node.id()); - - } - - @Test - public void getRunningInstanceProfiles() { - ArrayList list = new ArrayList (); - try { - list =sw.getRunningInstanceProfiles(); - } catch (Exception e) { - e.printStackTrace(); - } - - System.out.println("Endooint size :"+ list.size()); - for (GCoreEndpoint end : list){ - Assert.assertNotNull(end); - System.out.println(end.id()); - } - - } - - @Test - public void removeGHNProfile() { - - try { - sw.cleanGHNProfile(); - } catch (Exception e) { - e.printStackTrace(); - } - - } }