Discussion:
[Fab-user] How do I execute fabric script from a Java Program
Tameem Ahmed
2015-11-25 02:35:06 UTC
Permalink
Hi,

I have this simple fab script and I want to execute from a java program.

from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm
env.hosts = ['localhost']

def updatefile():
with shell_env(TERM='vt100'):
with cd('/Users/'):
run("pwd")
run("ls -l")

def execute():
updatefile()


When I execute this script from command line it works : fab -f test.py
executes but I want to execute via java. Tried with ..

public class ExecuteScript {
@Testpublic void testExecuteScript() throws NumberFormatException, IOException{

StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec("fab -f
src/test/resources/scripts/test.py execute");
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}

} catch (Exception e) {
e.printStackTrace();
}
System.out.println(output.toString());}}

It doesn't work.. looked at the documentation for java examples
inhttp://fabric8.io/gitbook/quickstarts.html the links are broken.


Thanks,

Tameem
Brandon Whaley
2015-11-25 15:33:23 UTC
Permalink
The docs for fabric are here: http://docs.fabfile.org/en/1.10/

I don't think that fabric8.io is in any way affiliated.

I don't know what you mean by "Doesn't work", can you elaborate?
Post by Tameem Ahmed
Hi,
I have this simple fab script and I want to execute from a java program.
from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm
env.hosts = ['localhost']
run("pwd")
run("ls -l")
updatefile()
When I execute this script from command line it works : fab -f test.py executes but I want to execute via java. Tried with ..
public class ExecuteScript {
@Testpublic void testExecuteScript() throws NumberFormatException, IOException{
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec("fab -f src/test/resources/scripts/test.py execute");
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(output.toString());}}
It doesn't work.. looked at the documentation for java examples inhttp://fabric8.io/gitbook/quickstarts.html the links are broken.
Thanks,
Tameem
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
Nathan M
2015-11-25 16:04:00 UTC
Permalink
You need to use the absolute path of the fab binary and give the absolute
path to your fabfile, like this :
/home/user/venv/bin/fab --fabfile=/home/user/myfabfile mytask

Loading...