This commit is contained in:
Nunzio Andrea Galante 2017-04-06 08:59:46 +00:00
parent f41d1cb5d5
commit 6ebbad413c
3 changed files with 250 additions and 244 deletions

View File

@ -55,7 +55,7 @@ public interface PoolManager {
void addAlgToIs(Algorithm algo);
Set<Algorithm> getAlgoFromIs();
List<String> getOriginalContents(String file, List<String> ldep) throws SVNException;
List<String> updateSVN(String file, List<String> ldep) throws SVNException, IOException;
}

View File

@ -171,9 +171,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmatesoft.svn.core.SVNException;
@Path("/")
public class RestPoolManager implements PoolManager {
@ -182,141 +179,133 @@ public class RestPoolManager implements PoolManager {
private PoolManager service = new DataminerPoolManager();
/*
/scopes/<scope> POST // add an algorithm to all dataminers in the scope
/hosts/<hostname> POST // add an algorithm to the given host
*/
* /scopes/<scope> POST // add an algorithm to all dataminers in the scope
* /hosts/<hostname> POST // add an algorithm to the given host
*/
@GET
@Path("/scopes/add")
@Produces("text/plain")
public String addAlgorithmToVRE(
@QueryParam("algorithm") String algorithm,
@QueryParam("vre") String vre,
@QueryParam("name") String name,
@QueryParam("description") String description,
@QueryParam("category") String category,
@DefaultValue("transducerers") @QueryParam("algorithmType") String algorithmType,
@DefaultValue("N") @QueryParam("skipJava") String skipJava,
@DefaultValue("false") @QueryParam("publish") boolean publish) throws IOException, InterruptedException, SVNException {
Algorithm algo= this.getAlgorithm(algorithm, vre, null, name, description, category, algorithmType, skipJava);
//publish algo
if (publish){
service.addAlgToIs(algo);
}
//update svn
for (Dependency d : algo.getDependencies()){
if (d.getType().equals("os")){
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.getOriginalContents("r_deb_pkgs.txt", ls);
}
if (d.getType().equals("cran")){
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.getOriginalContents("r_cran_pkgs.txt", ls);
}
if (d.getType().equals("custom")){
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.getOriginalContents("r_github_pkgs.txt", ls);
}
if (d.getType().equals("")){
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.getOriginalContents("r_deb_pkgs.txt", ls);
}
}
return service.addAlgorithmToVRE(algo, vre);
}
@GET
@Path("/scopes/add")
@Produces("text/plain")
public String addAlgorithmToVRE(@QueryParam("algorithm") String algorithm, @QueryParam("vre") String vre,
@QueryParam("name") String name, @QueryParam("description") String description,
@QueryParam("category") String category,
@DefaultValue("transducerers") @QueryParam("algorithmType") String algorithmType,
@DefaultValue("N") @QueryParam("skipJava") String skipJava,
@DefaultValue("false") @QueryParam("publish") boolean publish)
throws IOException, InterruptedException, SVNException {
Algorithm algo = this.getAlgorithm(algorithm, vre, null, name, description, category, algorithmType, skipJava);
@GET
@Path("/hosts/add")
@Produces("text/plain")
public String addAlgorithmToHost(
@QueryParam("algorithm") String algorithm,
@QueryParam("hostname") String hostname,
@QueryParam("name") String name,
@QueryParam("description") String description,
@QueryParam("category") String category,
@DefaultValue("transducerers") @QueryParam("algorithmType") String algorithmType,
@DefaultValue("N") @QueryParam("skipJava") String skipJava,
@DefaultValue("false") @QueryParam("publish") boolean publish) throws IOException, InterruptedException, SVNException {
Algorithm algo= this.getAlgorithm(algorithm, null, hostname, name, description, category, algorithmType, skipJava);
//publish algo
if (publish){
service.addAlgToIs(algo);
}
//update svn
for (Dependency d : algo.getDependencies()){
if (d.getType().equals("os")){
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.getOriginalContents("r_deb_pkgs.txt", ls);
}
if (d.getType().equals("cran")){
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.getOriginalContents("r_cran_pkgs.txt", ls);
}
if (d.getType().equals("custom")){
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.getOriginalContents("r_github_pkgs.txt", ls);
}
if (d.getType().equals("")){
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.getOriginalContents("r_deb_pkgs.txt", ls);
}
}
return service.addAlgorithmToHost(algo, hostname);
}
// publish algo
if (publish) {
service.addAlgToIs(algo);
}
// update svn
for (Dependency d : algo.getDependencies()) {
private Algorithm getAlgorithm(
String algorithm,
String vre,
String hostname,
String name,
String description,
String category,
String algorithmType,
String skipJava) throws IOException, InterruptedException {
LOGGER.debug("Adding algorithm =" + algorithm + " to VRE =" + vre);
Algorithm algo = service.extractAlgorithm(algorithm);
if (d.getType().equals("os")) {
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.updateSVN("r_deb_pkgs.txt", ls);
}
if (d.getType().equals("cran")) {
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.updateSVN("r_cran_pkgs.txt", ls);
}
if (d.getType().equals("custom")) {
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.updateSVN("r_github_pkgs.txt", ls);
}
if (d.getType().equals("")) {
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.updateSVN("r_deb_pkgs.txt", ls);
}
}
return service.addAlgorithmToVRE(algo, vre);
}
@GET
@Path("/hosts/add")
@Produces("text/plain")
public String addAlgorithmToHost(@QueryParam("algorithm") String algorithm, @QueryParam("hostname") String hostname,
@QueryParam("name") String name, @QueryParam("description") String description,
@QueryParam("category") String category,
@DefaultValue("transducerers") @QueryParam("algorithmType") String algorithmType,
@DefaultValue("N") @QueryParam("skipJava") String skipJava,
@DefaultValue("false") @QueryParam("publish") boolean publish)
throws IOException, InterruptedException, SVNException {
Algorithm algo = this.getAlgorithm(algorithm, null, hostname, name, description, category, algorithmType,
skipJava);
// publish algo
if (publish) {
service.addAlgToIs(algo);
}
// update svn
for (Dependency d : algo.getDependencies()) {
if (d.getType().equals("os")) {
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.updateSVN("r_deb_pkgs.txt", ls);
}
if (d.getType().equals("cran")) {
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.updateSVN("r_cran_pkgs.txt", ls);
}
if (d.getType().equals("custom")) {
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.updateSVN("r_github_pkgs.txt", ls);
}
if (d.getType().equals("")) {
List<String> ls = new LinkedList<String>();
ls.add(d.getName());
service.updateSVN("r_deb_pkgs.txt", ls);
}
}
return service.addAlgorithmToHost(algo, hostname);
}
private Algorithm getAlgorithm(String algorithm, String vre, String hostname, String name, String description,
String category, String algorithmType, String skipJava) throws IOException, InterruptedException {
LOGGER.debug("Adding algorithm =" + algorithm + " to VRE =" + vre);
Algorithm algo = service.extractAlgorithm(algorithm);
if (algo.getCategory() == null) {
algo.setCategory(category);
} else
algo.setCategory(algo.getCategory());
if (algo.getAlgorithmType() == null) {
algo.setAlgorithmType(algorithmType);
} else
algo.setAlgorithmType(algo.getCategory());
if (algo.getSkipJava() == null) {
algo.setSkipJava(skipJava);
} else
algo.setSkipJava(algo.getSkipJava());
if (algo.getName() == null) {
algo.setName(name);
} else
algo.setName(algo.getName());
if (algo.getDescription() == null) {
algo.setDescription(description);
;
} else
algo.setDescription(algo.getDescription());
return algo;
}
if (algo.getCategory() == null){
algo.setCategory(category);
} else algo.setCategory(algo.getCategory());
if (algo.getAlgorithmType() == null){
algo.setAlgorithmType(algorithmType);
} else algo.setAlgorithmType(algo.getCategory());
if (algo.getSkipJava() == null){
algo.setSkipJava(skipJava);
} else algo.setSkipJava(algo.getSkipJava());
if (algo.getName() == null){
algo.setName(name);
} else algo.setName(algo.getName());
if (algo.getDescription() == null){
algo.setDescription(description);;
} else algo.setDescription(algo.getDescription());
return algo;
}
@GET
@Path("/log")
@Produces("text/plain")
@ -325,10 +314,6 @@ public class RestPoolManager implements PoolManager {
LOGGER.debug("Returning Log =" + logUrl);
return service.getScriptFromURL(service.getURLfromWorkerLog(logUrl));
}
@Override
public Algorithm extractAlgorithm(String url) throws IOException {
@ -336,49 +321,44 @@ public class RestPoolManager implements PoolManager {
return null;
}
public static void main(String[] args) throws IOException, InterruptedException, SVNException {
//System.out.println(System.getProperty("user.home")+File.separator+"/gcube/dataminer-pool-manager");
//ProxySelector.setDefault(new PropertiesBasedProxySelector("/home/ngalante/.proxy-settings"));
ScopeProvider.instance.set("/gcube/devNext/NextNext");
//PoolManager aa = new DataminerPoolManager();
//System.out.println(aa.getAlgoById("ICHTHYOP_MODEL_ONE_BY_ONE@3141d3aa-5f93-409f-b6f8-9fae0a6c0ee3"));
//System.out.println(aa.getAlgoFromIs());
// System.out.println(System.getProperty("user.home")+File.separator+"/gcube/dataminer-pool-manager");
// ProxySelector.setDefault(new
// PropertiesBasedProxySelector("/home/ngalante/.proxy-settings"));
ScopeProvider.instance.set("/gcube/devNext/NextNext");
// PoolManager aa = new DataminerPoolManager();
// System.out.println(aa.getAlgoById("ICHTHYOP_MODEL_ONE_BY_ONE@3141d3aa-5f93-409f-b6f8-9fae0a6c0ee3"));
// System.out.println(aa.getAlgoFromIs());
// RestPoolManager a = new RestPoolManager();
// a.addAlgorithmToVRE(
// "http://data.d4science.org/S2h1RHZGd0JpWnBjZk9qTytQTndqcDRLVHNrQUt6QjhHbWJQNStIS0N6Yz0",
// "/gcube/devNext/NextNext",
// null, null, "test", "transducerers", "N");
// a.addAlgorithmToHost(
// "http://data.d4science.org/dENQTTMxdjNZcGRpK0NHd2pvU0owMFFzN0VWemw3Zy9HbWJQNStIS0N6Yz0",
// "dataminer1-devnext.d4science.org",
// "ICHTHYOP_MODEL_ONE_BY_ONE", null, "ICHTHYOP_MODEL", "transducerers",
// "N",false);
//
//
//RestPoolManager a = new RestPoolManager();
//a.addAlgorithmToVRE(
//"http://data.d4science.org/S2h1RHZGd0JpWnBjZk9qTytQTndqcDRLVHNrQUt6QjhHbWJQNStIS0N6Yz0",
//"/gcube/devNext/NextNext",
//null, null, "test", "transducerers", "N");
// a.addAlgorithmToHost(
// "http://data.d4science.org/dENQTTMxdjNZcGRpK0NHd2pvU0owMFFzN0VWemw3Zy9HbWJQNStIS0N6Yz0",
// "dataminer1-devnext.d4science.org",
// "ICHTHYOP_MODEL_ONE_BY_ONE", null, "ICHTHYOP_MODEL", "transducerers", "N",false);
//
//
PoolManager aa = new DataminerPoolManager();
List<String> ls = new LinkedList<String>();
String afa = "nunzio";
String b = "galante";
ls.add(b);
String afa = "test";
ls.add(afa);
System.out.println(aa.getOriginalContents("r_deb_pkgs.txt",ls));
System.out.println(aa.updateSVN("r_deb_pkgs.txt", ls));
}
@Override
public void getLogId(Algorithm algo, String vre) {
// TODO Auto-generated method stub
}
}
@Override
public String getScriptFromURL(URL logId) throws IOException {
@ -386,19 +366,17 @@ public class RestPoolManager implements PoolManager {
return null;
}
@Override
public String addAlgorithmToVRE(Algorithm algo, String vre) throws IOException, InterruptedException {
// TODO Auto-generated method stub
return null;
}
@Override
public String addAlgorithmToHost(Algorithm algo, String hostname)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
return null;
}
@Override
public String addAlgorithmToHost(Algorithm algo, String hostname) throws IOException, InterruptedException {
// TODO Auto-generated method stub
return null;
}
@Override
public URL getURLfromWorkerLog(String logUrl) throws MalformedURLException, UnknownHostException {
@ -409,7 +387,7 @@ public class RestPoolManager implements PoolManager {
@Override
public void addAlgToIs(Algorithm algo) {
// TODO Auto-generated method stub
}
@Override
@ -418,15 +396,12 @@ public class RestPoolManager implements PoolManager {
return null;
}
@Override
public List<String> getOriginalContents(String file, List<String> ldep) throws SVNException {
public List<String> updateSVN(String file, List<String> ldep) throws SVNException {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -266,6 +266,7 @@ import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@ -275,11 +276,14 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.UUID;
import org.gcube.common.resources.gcore.GenericResource;
@ -304,11 +308,16 @@ import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.wc.SVNFileUtil;
import org.tmatesoft.svn.core.internal.wc.admin.SVNChecksumInputStream;
import org.tmatesoft.svn.core.io.ISVNEditor;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.io.diff.SVNDeltaGenerator;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
public class DataminerPoolManager implements PoolManager {
@ -531,7 +540,7 @@ public class DataminerPoolManager implements PoolManager {
// create the cluster (dataminers in the vre)
Cluster cluster = new Cluster();
//for (Host h : new ISClient().listDataMinersByCluster()) {
// for (Host h : new ISClient().listDataMinersByCluster()) {
for (Host h : new ISClient().listDataminersInVRE()) {
cluster.addHost(h);
}
@ -557,64 +566,86 @@ public class DataminerPoolManager implements PoolManager {
}
private SVNRepository getSvnRepository(String url) throws SVNException {
String usr = "nunzioandrea.galante";
String pwd = "Newuser1";
SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(
usr, pwd);
repository.setAuthenticationManager(authManager);
System.out.println(repository.getLocation());
return repository;
SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(usr, pwd);
repository.setAuthenticationManager(authManager);
System.out.println(repository.getLocation());
return repository;
}
@Override
public List<String> getOriginalContents(String file, List<String> ldep) throws SVNException {
final SVNRepository svnRepository = this.getSvnRepository("https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/RConfiguration/RPackagesManagement/");
try {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
svnRepository.getFile(file, SVNRepository.INVALID_REVISION, null, byteArrayOutputStream);
String lines[] = byteArrayOutputStream.toString().split("\\r?\\n");
List<String> aa = this.checkMatch(lines, ldep);
return aa;
} finally {
svnRepository.closeSession();
}
public List<String> updateSVN(String file, List<String> ldep) throws SVNException, IOException {
final SVNRepository svnRepository = this.getSvnRepository(
"https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/RConfiguration/RPackagesManagement/");
try {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
svnRepository.getFile(file, SVNRepository.INVALID_REVISION, null, byteArrayOutputStream);
String lines[] = byteArrayOutputStream.toString().split("\\r?\\n");
List<String> aa = this.checkMatch(lines, ldep);
Collections.sort(aa);
final SVNDeltaGenerator deltaGenerator = new SVNDeltaGenerator();
System.out.println("aaaa"+byteArrayOutputStream);
byte[] originalContents = byteArrayOutputStream.toByteArray();
System.out.println(byteArrayOutputStream.toByteArray());
final ISVNEditor commitEditor = svnRepository.getCommitEditor("update dependencies", null);
commitEditor.openRoot(-1);
commitEditor.openFile(file, -1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (String line : aa) {
baos.write(line.getBytes());
baos.write("\n".getBytes());
}
System.out.println("bbbb"+baos);
byte[] bytes = baos.toByteArray();
commitEditor.applyTextDelta(file,md5(originalContents));
final String checksum = deltaGenerator.sendDelta(file, new ByteArrayInputStream(originalContents), 0,
new ByteArrayInputStream(bytes), commitEditor, true);
commitEditor.closeFile(file, checksum);
commitEditor.closeEdit();
return aa;
} finally {
svnRepository.closeSession();
}
}
public static String md5(byte[] contents) {
final byte[] tmp = new byte[1024];
final SVNChecksumInputStream checksumStream = new SVNChecksumInputStream(new ByteArrayInputStream(contents), "md5");
try {
while (checksumStream.read(tmp) > 0) {
//
}
return checksumStream.getDigest();
} catch (IOException e) {
//never happens
e.printStackTrace();
return null;
} finally {
SVNFileUtil.closeFile(checksumStream);
}
}
public List<String> checkMatch(String[] lines, List<String> ls) {
Set<String> ss = new HashSet<String>(ls);
ss.addAll(Arrays.asList(lines));
// for (int z = 0; z < aa.size(); z++) {
// for (int i = 0; i < ls.size(); i++) {
// if (!ls.get(i).equals(aa.get(z))) {
// aa.add(ls.get(i));
// }
// }
//}
return new ArrayList<>(ss);
}
public void checkSVNdeps(/*List<Dependency> listdep*/) throws IOException{
//take svn page (one for each kind of dep)
//take deps from metadata
//if dep.typology available in svn.typology nothing to do
//else add dep to svn page
}
public Algorithm extractAlgorithm(String url) throws IOException {
return new AlgorithmPackageParser().parsePackage(url);
}
@ -673,11 +704,10 @@ public class DataminerPoolManager implements PoolManager {
}
private String getAlgoBody(Algorithm algo) {
return "<category>" + algo.getCategory() + "</category>" + "\n"
+ "<clazz>" + algo.getClazz() + "</clazz>" + "\n" + "<algorithmType>" + algo.getAlgorithmType()
+ "</algorithmType>" + "\n" + "<skipJava>" + algo.getSkipJava() + "</skipJava>" + "\n" + "<packageURL>"
+ algo.getPackageURL() + "</packageURL>" + "\n" + "<dependencies>"
+ algo.getDependencies() + "</dependencies>";
return "<category>" + algo.getCategory() + "</category>" + "\n" + "<clazz>" + algo.getClazz() + "</clazz>"
+ "\n" + "<algorithmType>" + algo.getAlgorithmType() + "</algorithmType>" + "\n" + "<skipJava>"
+ algo.getSkipJava() + "</skipJava>" + "\n" + "<packageURL>" + algo.getPackageURL() + "</packageURL>"
+ "\n" + "<dependencies>" + algo.getDependencies() + "</dependencies>";
}
public void updateAlg(Algorithm algo) {
@ -703,7 +733,7 @@ public class DataminerPoolManager implements PoolManager {
private Algorithm convertAlgo(GenericResource a) {
Algorithm out = new Algorithm();
//out.setId(a.profile().body().getElementsByTagName("id").item(0).getTextContent());
// out.setId(a.profile().body().getElementsByTagName("id").item(0).getTextContent());
out.setAlgorithmType(a.profile().body().getElementsByTagName("algorithmType").item(0).getTextContent());
out.setCategory(a.profile().body().getElementsByTagName("category").item(0).getTextContent());
out.setClazz(a.profile().body().getElementsByTagName("clazz").item(0).getTextContent());
@ -722,14 +752,14 @@ public class DataminerPoolManager implements PoolManager {
return out;
}
// public Algorithm getAlgoById(String id) {
// for (Algorithm aa : this.getAlgoFromIs()) {
// if (aa.getId().equals(id)) {
// return aa;
// }
// }
// return null;
// }
// public Algorithm getAlgoById(String id) {
// for (Algorithm aa : this.getAlgoFromIs()) {
// if (aa.getId().equals(id)) {
// return aa;
// }
// }
// return null;
// }
@Override
public Set<Algorithm> getAlgoFromIs() {
@ -737,7 +767,8 @@ public class DataminerPoolManager implements PoolManager {
Set<Algorithm> out = new HashSet<Algorithm>();
SimpleQuery query = queryFor(GenericResource.class);
query.addCondition("$resource/Profile/SecondaryType/text() eq 'StatisticalManagerAlgorithm'").setResult("$resource");
query.addCondition("$resource/Profile/SecondaryType/text() eq 'StatisticalManagerAlgorithm'")
.setResult("$resource");
DiscoveryClient<GenericResource> client = clientFor(GenericResource.class);
List<GenericResource> ds = client.submit(query);
for (GenericResource a : ds) {