2017-03-03 19:10:02 +01:00
|
|
|
package org.gcube.dataanalysis.dataminer.poolmanager.ansible;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
2017-04-22 19:24:33 +02:00
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
2017-03-03 19:10:02 +01:00
|
|
|
import java.io.File;
|
|
|
|
***REMOVED***
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.PrintStream;
|
2017-04-22 19:24:33 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
***REMOVED***
|
2017-03-03 19:10:02 +01:00
|
|
|
import java.util.Scanner;
|
2017-04-22 19:24:33 +02:00
|
|
|
***REMOVED***
|
2017-03-03 19:10:02 +01:00
|
|
|
|
|
|
|
import org.gcube.dataanalysis.dataminer.poolmanager.ansible.model.Inventory;
|
|
|
|
import org.gcube.dataanalysis.dataminer.poolmanager.ansible.model.Playbook;
|
|
|
|
import org.gcube.dataanalysis.dataminer.poolmanager.ansible.model.Role;
|
|
|
|
import org.gcube.dataanalysis.dataminer.poolmanager.ansiblebridge.AnsibleSerializeHelper;
|
2017-04-22 19:24:33 +02:00
|
|
|
***REMOVED***
|
|
|
|
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.AlgorithmSet;
|
|
|
|
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Dependency;
|
|
|
|
***REMOVED***
|
2017-05-09 17:10:03 +02:00
|
|
|
***REMOVED***
|
2017-04-22 19:24:33 +02:00
|
|
|
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;
|
2017-03-03 19:10:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is responsible for the interface with ansible, retrieving log,
|
|
|
|
* etc. etc. It's not supposed to access templates and static stuff files. It
|
|
|
|
* does not know the service datamodel.
|
|
|
|
*
|
|
|
|
* @author paolo
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class AnsibleWorker ***REMOVED***
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the inventory
|
|
|
|
*/
|
|
|
|
private static String INVENTORY_NAME = "inventory.yaml";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The directory containing roles
|
|
|
|
*/
|
|
|
|
private static String ROLES_DIR = "roles";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the playbook
|
|
|
|
*/
|
|
|
|
private static String PLAYBOOK_NAME = "playbook.yaml";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The root of the worker. This corresponds to a standard ansible working dir.
|
|
|
|
*/
|
|
|
|
private File workerRoot;
|
|
|
|
|
|
|
|
public AnsibleWorker(File root) ***REMOVED***
|
|
|
|
this.workerRoot = root;
|
|
|
|
this.ensureWorkStructure();
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public File getWorkdir() ***REMOVED***
|
|
|
|
return this.workerRoot;
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public File getRolesDir() ***REMOVED***
|
|
|
|
return new File(this.getWorkdir(), ROLES_DIR);
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public String getWorkerId() ***REMOVED***
|
|
|
|
return this.workerRoot.getName();
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public void ensureWorkStructure() ***REMOVED***
|
|
|
|
***REMOVED*** generate root
|
|
|
|
this.getWorkdir().mkdirs();
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public void removeWorkStructure() ***REMOVED***
|
|
|
|
***REMOVED*** remove the working dir
|
|
|
|
this.getWorkdir().delete();
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public File getPlaybookFile() ***REMOVED***
|
|
|
|
return new File(this.getWorkdir(), PLAYBOOK_NAME);
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public File getInventoryFile() ***REMOVED***
|
|
|
|
return new File(this.getWorkdir(), INVENTORY_NAME);
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
|
|
|
|
public void setInventory(Inventory inventory) throws IOException ***REMOVED***
|
|
|
|
***REMOVED*** serialize the string to the 'inventory' file
|
|
|
|
AnsibleSerializeHelper.serialize(inventory, this.getInventoryFile());
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public void setPlaybook(Playbook playbook) throws IOException ***REMOVED***
|
|
|
|
***REMOVED*** serialize the string to the 'playbook' file
|
|
|
|
AnsibleSerializeHelper.serialize(playbook, this.getPlaybookFile());
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public void addRole(Role r) throws IOException ***REMOVED***
|
|
|
|
***REMOVED*** Serialize role in the workdir
|
|
|
|
AnsibleSerializeHelper.serializeRole(r, this.getRolesDir());
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-05-09 17:10:03 +02:00
|
|
|
public void apply(AlgorithmSet as, PrintStream ps, boolean test)
|
2017-04-28 16:26:40 +02:00
|
|
|
throws IOException, InterruptedException, SVNException ***REMOVED***
|
|
|
|
***REMOVED*** TODO execute the playbook and return output
|
|
|
|
System.out.println(this.getWorkdir());
|
|
|
|
try ***REMOVED***
|
|
|
|
Process p = Runtime.getRuntime().exec("ansible-playbook -v -i " + this.getInventoryFile().getAbsolutePath()
|
|
|
|
+ " " + this.getPlaybookFile().getAbsolutePath());
|
|
|
|
|
|
|
|
inheritIO(p.getInputStream(), ps);
|
|
|
|
inheritIO(p.getErrorStream(), ps);
|
2017-05-09 17:10:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
int exitValue = p.waitFor();
|
|
|
|
if (exitValue == 0) ***REMOVED***
|
|
|
|
if (test) ***REMOVED***
|
2017-04-28 16:26:40 +02:00
|
|
|
for (Algorithm algo : as.getAlgorithms()) ***REMOVED***
|
2017-05-09 17:10:03 +02:00
|
|
|
|
2017-04-28 16:26:40 +02:00
|
|
|
for (Dependency d : algo.getDependencies()) ***REMOVED***
|
|
|
|
|
|
|
|
if (d.getType().equals("os")) ***REMOVED***
|
|
|
|
List<String> ls = new LinkedList<String>();
|
|
|
|
ls.add(d.getName());
|
2017-05-09 17:10:03 +02:00
|
|
|
this.updateSVN("test_r_deb_pkgs.txt", ls);
|
2017-04-28 16:26:40 +02:00
|
|
|
***REMOVED***
|
|
|
|
if (d.getType().equals("cran")) ***REMOVED***
|
|
|
|
List<String> ls = new LinkedList<String>();
|
|
|
|
ls.add(d.getName());
|
2017-05-09 17:10:03 +02:00
|
|
|
this.updateSVN("test_r_cran_pkgs.txt", ls);
|
2017-04-28 16:26:40 +02:00
|
|
|
***REMOVED***
|
|
|
|
if (d.getType().equals("github")) ***REMOVED***
|
|
|
|
List<String> ls = new LinkedList<String>();
|
|
|
|
ls.add(d.getName());
|
2017-05-09 17:10:03 +02:00
|
|
|
this.updateSVN("test_r_github_pkgs.txt", ls);
|
2017-04-28 16:26:40 +02:00
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***
|
2017-05-09 17:10:03 +02:00
|
|
|
|
2017-04-28 16:26:40 +02:00
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
***REMOVED***
|
2017-05-09 17:10:03 +02:00
|
|
|
else if(!test)***REMOVED***
|
|
|
|
for (Algorithm algo : as.getAlgorithms()) ***REMOVED***
|
|
|
|
|
|
|
|
for (Dependency d : algo.getDependencies()) ***REMOVED***
|
|
|
|
|
|
|
|
if (d.getType().equals("os")) ***REMOVED***
|
|
|
|
List<String> ls = new LinkedList<String>();
|
|
|
|
ls.add(d.getName());
|
|
|
|
this.updateSVN("r_deb_pkgs.txt", ls);
|
|
|
|
***REMOVED***
|
|
|
|
if (d.getType().equals("cran")) ***REMOVED***
|
|
|
|
List<String> ls = new LinkedList<String>();
|
|
|
|
ls.add(d.getName());
|
|
|
|
this.updateSVN("r_cran_pkgs.txt", ls);
|
|
|
|
***REMOVED***
|
|
|
|
if (d.getType().equals("github")) ***REMOVED***
|
|
|
|
List<String> ls = new LinkedList<String>();
|
|
|
|
ls.add(d.getName());
|
|
|
|
this.updateSVN("r_github_pkgs.txt", ls);
|
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***
|
2017-04-28 16:26:40 +02:00
|
|
|
|
2017-05-09 17:10:03 +02:00
|
|
|
for (Algorithm algo : as.getAlgorithms()) ***REMOVED***
|
|
|
|
DataminerPoolManager a = new DataminerPoolManager();
|
|
|
|
a.addAlgToIs(algo);
|
|
|
|
***REMOVED***
|
2017-04-28 16:26:40 +02:00
|
|
|
***REMOVED***
|
2017-05-09 17:10:03 +02:00
|
|
|
|
2017-04-28 16:26:40 +02:00
|
|
|
|
|
|
|
***REMOVED*** catch (IOException e) ***REMOVED***
|
|
|
|
e.printStackTrace();
|
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***
|
2017-03-03 19:10:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-22 19:24:33 +02:00
|
|
|
private SVNRepository getSvnRepository(String url) throws SVNException ***REMOVED***
|
|
|
|
SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
|
|
|
|
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager();
|
|
|
|
repository.setAuthenticationManager(authManager);
|
2017-04-26 17:39:24 +02:00
|
|
|
***REMOVED***System.out.println(repository.getLocation());
|
2017-04-22 19:24:33 +02:00
|
|
|
|
|
|
|
return repository;
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<String> updateSVN(String file, List<String> ldep) throws SVNException, IOException ***REMOVED***
|
|
|
|
final SVNRepository svnRepository = this.getSvnRepository(
|
|
|
|
"https:***REMOVED***svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/RConfiguration/RPackagesManagement/");
|
|
|
|
try ***REMOVED***
|
|
|
|
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();
|
|
|
|
|
|
|
|
byte[] originalContents = 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) ***REMOVED***
|
|
|
|
baos.write(line.getBytes());
|
|
|
|
baos.write("\n".getBytes());
|
|
|
|
***REMOVED***
|
|
|
|
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;
|
|
|
|
|
|
|
|
***REMOVED*** finally ***REMOVED***
|
|
|
|
svnRepository.closeSession();
|
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String md5(byte[] contents) ***REMOVED***
|
|
|
|
final byte[] tmp = new byte[1024];
|
|
|
|
final SVNChecksumInputStream checksumStream = new SVNChecksumInputStream(new ByteArrayInputStream(contents), "md5");
|
|
|
|
try ***REMOVED***
|
|
|
|
while (checksumStream.read(tmp) > 0) ***REMOVED***
|
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***
|
|
|
|
return checksumStream.getDigest();
|
|
|
|
***REMOVED*** catch (IOException e) ***REMOVED***
|
|
|
|
***REMOVED***never happens
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
***REMOVED*** finally ***REMOVED***
|
|
|
|
SVNFileUtil.closeFile(checksumStream);
|
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public List<String> checkMatch(String[] lines, List<String> ls) ***REMOVED***
|
|
|
|
Set<String> ss = new HashSet<String>(ls);
|
|
|
|
ss.addAll(Arrays.asList(lines));
|
|
|
|
return new ArrayList<>(ss);
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-03-03 19:10:02 +01:00
|
|
|
private static void inheritIO(final InputStream src, final PrintStream dest) ***REMOVED***
|
|
|
|
new Thread(new Runnable() ***REMOVED***
|
|
|
|
public void run() ***REMOVED***
|
|
|
|
Scanner sc = new Scanner(src);
|
|
|
|
while (sc.hasNextLine()) ***REMOVED***
|
|
|
|
dest.println(sc.nextLine());
|
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***).start();
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the worker:
|
|
|
|
* - remove the working dir
|
|
|
|
*/
|
|
|
|
public void destroy() ***REMOVED***
|
|
|
|
this.removeWorkStructure();
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
***REMOVED***
|