git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/Common/common-smartgears-utils@146302 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
5b3e37cafb
commit
e17e68c829
10
pom.xml
10
pom.xml
|
@ -10,6 +10,16 @@
|
||||||
<artifactId>common-smartgears-utils</artifactId>
|
<artifactId>common-smartgears-utils</artifactId>
|
||||||
<version>1.0.1-SNAPSHOT</version>
|
<version>1.0.1-SNAPSHOT</version>
|
||||||
<dependencies>
|
<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>
|
<dependency>
|
||||||
<groupId>org.gcube.resources</groupId>
|
<groupId>org.gcube.resources</groupId>
|
||||||
<artifactId>registry-publisher</artifactId>
|
<artifactId>registry-publisher</artifactId>
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -19,6 +19,8 @@ public class ContainerSweeperClient {
|
||||||
try {
|
try {
|
||||||
sw = new Sweeper();
|
sw = new Sweeper();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error initializing the Sweeper");
|
||||||
|
e.printStackTrace();
|
||||||
logger.error("Error initializing the Sweeper ", e);
|
logger.error("Error initializing the Sweeper ", e);
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
@ -26,17 +28,12 @@ public class ContainerSweeperClient {
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
sw.cleanGHNProfile();
|
sw.forceDeleteHostingNode();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Error cleaning the GHN profile ", e);
|
logger.error("Error cleaning the GHN profile ", e);
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
sw.cleanRIProfiles();
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("Error cleaning the RunningInstance profiles ", e);
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,21 @@
|
||||||
package org.gcube.smartgears.utils.sweeper;
|
package org.gcube.smartgears.utils.sweeper;
|
||||||
|
|
||||||
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileInputStream;
|
||||||
import java.util.ArrayList;
|
import java.io.ObjectInputStream;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBContext;
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||||
import javax.xml.bind.JAXBException;
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
import javax.xml.bind.Unmarshaller;
|
|
||||||
|
|
||||||
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
|
||||||
import org.gcube.common.resources.gcore.HostingNode;
|
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.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.RegistryPublisher;
|
||||||
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -28,124 +27,62 @@ import org.slf4j.LoggerFactory;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Sweeper {
|
public class Sweeper {
|
||||||
|
|
||||||
String ghn_state_path = "";
|
String ghn_state_path = "";
|
||||||
|
|
||||||
Logger logger;
|
Logger logger;
|
||||||
|
|
||||||
|
|
||||||
public Sweeper () throws Exception {
|
public Sweeper () throws Exception {
|
||||||
|
|
||||||
logger = LoggerFactory.getLogger(Sweeper.class);
|
logger = LoggerFactory.getLogger(Sweeper.class);
|
||||||
|
|
||||||
String ghn_path = System.getenv("GHN_HOME");
|
String ghn_path = System.getenv("GHN_HOME");
|
||||||
|
|
||||||
if (ghn_path == null ) {
|
if (ghn_path == null ) {
|
||||||
logger.error("GHN_HOME not defined");
|
logger.error("GHN_HOME not defined");
|
||||||
throw new Exception ("GHN_HOME not defined");
|
throw new Exception ("GHN_HOME not defined");
|
||||||
}
|
}
|
||||||
|
|
||||||
ghn_state_path=ghn_path+File.separator+"state";
|
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 {
|
@SuppressWarnings("unchecked")
|
||||||
|
public void forceDeleteHostingNode(){
|
||||||
JAXBContext jc = JAXBContext.newInstance(GCoreEndpoint.class);
|
String id ;
|
||||||
|
List<String> tokens ;
|
||||||
Unmarshaller um = jc.createUnmarshaller();
|
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(ghn_state_path+File.separator+"ghn.xml"))){
|
||||||
|
id = (String)ois.readObject();
|
||||||
GCoreEndpoint ri = null;
|
tokens = (List<String>) ois.readObject();
|
||||||
|
}catch(Exception e){
|
||||||
File subfolder = new File(ghn_state_path+ File.separator+ name);
|
throw new RuntimeException("error loading persisted state");
|
||||||
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return endpoints;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void cleanGHNProfile() throws Exception{
|
|
||||||
forceDeleteResource(this.getGHNProfile());
|
|
||||||
|
|
||||||
}
|
RegistryPublisher rp=RegistryPublisherFactory.create();
|
||||||
|
System.out.println(" id to remove is "+id);
|
||||||
public void cleanRIProfile(String name) throws Exception{
|
try{
|
||||||
forceDeleteResource(this.getRunningInstanceProfile(name));
|
DiscoveryClient<HostingNode> client = ICFactory.clientFor(HostingNode.class);
|
||||||
|
SimpleQuery query = ICFactory.queryFor(HostingNode.class);
|
||||||
}
|
query.addCondition("$resource/ID/text() = '"+id+"'");
|
||||||
|
for (String token : tokens){
|
||||||
public void cleanRIProfiles() throws Exception{
|
AuthorizationEntry entry = authorizationService().get(token);
|
||||||
for (GCoreEndpoint endp : this.getRunningInstanceProfiles()){
|
SecurityTokenProvider.instance.set(token);
|
||||||
forceDeleteResource(endp);
|
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;
|
}catch(Exception e){
|
||||||
AdvancedPublisher advancedPublisher;
|
throw new RuntimeException("error removing hosting node resource",e);
|
||||||
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());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
|
|
@ -1,16 +1,7 @@
|
||||||
package org.gcube.smartgears.utils.sweeper.test;
|
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.gcube.smartgears.utils.sweeper.Sweeper;
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class SweeperTest {
|
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue