2017-03-03 19:10:02 +01:00
|
|
|
package org.gcube.dataanalysis.dataminer.poolmanager.ansible;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
***REMOVED***
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.PrintStream;
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
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***
|
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***
|
|
|
|
|
2017-11-29 16:29:01 +01:00
|
|
|
***REMOVED*** public File getWorkdir() ***REMOVED***
|
|
|
|
***REMOVED*** return this.workerRoot;
|
|
|
|
***REMOVED*** ***REMOVED***
|
2017-03-03 19:10:02 +01:00
|
|
|
|
|
|
|
public File getRolesDir() ***REMOVED***
|
2017-11-29 16:29:01 +01:00
|
|
|
return new File(this.workerRoot, ROLES_DIR);
|
2017-03-03 19:10:02 +01:00
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public String getWorkerId() ***REMOVED***
|
|
|
|
return this.workerRoot.getName();
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public void ensureWorkStructure() ***REMOVED***
|
|
|
|
***REMOVED*** generate root
|
2017-11-29 16:29:01 +01:00
|
|
|
this.workerRoot.mkdirs();
|
2017-03-03 19:10:02 +01:00
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public void removeWorkStructure() ***REMOVED***
|
|
|
|
***REMOVED*** remove the working dir
|
2017-11-29 16:29:01 +01:00
|
|
|
this.workerRoot.delete();
|
2017-03-03 19:10:02 +01:00
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public File getPlaybookFile() ***REMOVED***
|
2017-11-29 16:29:01 +01:00
|
|
|
return new File(this.workerRoot, PLAYBOOK_NAME);
|
2017-03-03 19:10:02 +01:00
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
public File getInventoryFile() ***REMOVED***
|
2017-11-29 16:29:01 +01:00
|
|
|
return new File(this.workerRoot, INVENTORY_NAME);
|
2017-03-03 19:10:02 +01:00
|
|
|
***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 18:38:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
public int execute(PrintStream ps)
|
|
|
|
throws IOException, InterruptedException, SVNException ***REMOVED***
|
|
|
|
|
2017-11-29 16:29:01 +01:00
|
|
|
System.out.println(this.workerRoot);
|
2017-05-09 18:38:15 +02:00
|
|
|
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-07-13 18:13:18 +02:00
|
|
|
***REMOVED*** writer.println(this.getStatus(p.waitFor()));
|
|
|
|
***REMOVED*** writer.close();
|
2017-05-09 18:38:15 +02:00
|
|
|
|
|
|
|
return p.waitFor();
|
|
|
|
|
|
|
|
***REMOVED*** catch (IOException e) ***REMOVED***
|
|
|
|
e.printStackTrace();
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
***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***
|
2017-11-29 16:29:01 +01:00
|
|
|
sc.close();
|
2017-03-03 19:10:02 +01:00
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***).start();
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the worker:
|
|
|
|
* - remove the working dir
|
|
|
|
*/
|
|
|
|
public void destroy() ***REMOVED***
|
|
|
|
this.removeWorkStructure();
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
***REMOVED***
|