This commit is contained in:
Lucio Lelii 2017-03-23 16:16:26 +00:00
parent 5b3e37cafb
commit e17e68c829
6 changed files with 64 additions and 226 deletions

10
pom.xml
View File

@ -10,6 +10,16 @@
<artifactId>common-smartgears-utils</artifactId>
<version>1.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-client</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>common-authorization</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>registry-publisher</artifactId>

View File

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

View File

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

View File

@ -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<GCoreEndpoint> getRunningInstanceProfiles() throws Exception {
ArrayList<GCoreEndpoint> endpoints = new ArrayList<GCoreEndpoint>();
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<String> tokens ;
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(ghn_state_path+File.separator+"ghn.xml"))){
id = (String)ois.readObject();
tokens = (List<String>) 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<HostingNode> 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<HostingNode> 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);
}
}
}

View File

@ -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

View File

@ -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<GCoreEndpoint> list = new ArrayList<GCoreEndpoint> ();
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();
}
}
}