简体   繁体   English

通过JAVA执行Fabric脚本

[英]Executing Fabric Script through JAVA

I have this simple fab script and I want to execute this through JAVA: 我有这个简单的fab脚本,我想通过JAVA执行此操作:

    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. 当我从命令行执行此脚本时,它可以工作:fab -f test.py执行,但我想通过Java执行。 Tried with .. 尝试过..

public class ExecuteScript {

@Test
public 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 in http://fabric8.io/gitbook/quickstarts.html the links are broken. 它不起作用..在http://fabric8.io/gitbook/quickstarts.html中查看了Java示例的文档,链接已断开。

Environment, make sure you're using your environment. 环境,请确保您正在使用环境。 I would suggest you use virtualenv , looks something like this: 我建议您使用virtualenv ,看起来像这样:

  1. virtualenv .env (dont do this in your src folder, if anything it should be one directory down, and also please don't check this into your source control) virtualenv .env (不要在src文件夹中执行此操作,如果有的话应该将其放在一个目录下,也请不要将此操作检入源代码管理中)
  2. source .env/bin/activate

then install your dependencies: 然后安装您的依赖项:

  1. pip install fabric
  2. ?? ?? what ever else you need. 您还需要什么。

then this is the important part: 那么这是重要的部分:

./.env/bin/fab -f src/test/resources/scripts/test.py execute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM