织物的独立 fabfile?
是否可以使 fabfile 独立运行?
我不太喜欢运行外部工具“fab”。如果我设法获得独立的 fabfile,我可以从(Eclipse / Pydev)IDE 中运行该文件,轻松调试它,使用项目配置和路径等。
为什么这不起作用:
from fabric.api import run
def host_type():
run('uname -s')
if __name__ == '__main__':
host_type()
Is it possible to make the fabfile stand-alone?
I'm not very fond of running the external tool 'fab'. If I manage to get the fabfile standalone I can run the file from within the (Eclipse / Pydev) IDE, easily debug it, use project configurations and paths etc.
Why doesn't this work:
from fabric.api import run
def host_type():
run('uname -s')
if __name__ == '__main__':
host_type()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我最终找到了解决方案(而且非常简单!)。
在我的 fabfile 中,我添加了:
我希望这可以帮助人们......
I eventually found the solution (and it is really simple!).
In my fabfile, I added:
I hope this helps people...
如果我没记错的话,我也无法让 Fabric API 执行我想要的操作。我决定完全放弃额外的层,直接使用 Paramiko (Fabric 使用的底层 SSH 库) :
虽然还涉及更多步骤,但这样做可以让您直接利用 SSH 层,而不是使用
subprocess
来传播另一个 Python 实例,或找出 Fabric API。我有几个项目,包括网络项目和控制台项目,都以这种方式使用 Paramiko,并且没有遇到太多麻烦。Paramiko 有大量文档。
If I recall correctly, I couldn't get the Fabric API to do what I wanted either. I decided to abandon the extra layer entirely and use Paramiko (the underlying SSH library used by Fabric) directly:
While there are a few more steps involved, doing it this way allows you to leverage the SSH layer directly, as opposed to using
subprocess
to spwan another Python instance, or figuring out the Fabric API. I have several projects, both web- and console- using Paramiko in this manner and I haven't had too much trouble.Paramiko is extensively documented.
这不是一个很好的解决方案,但会起作用:
This isn't a really nice solution, but will work:
我将上面的示例微调为过去的 argv 参数,您可能希望传递给本地命令并指定可选的 default_commands 列表而不是硬编码的命令名称。注意,文件名必须有 .py 扩展名,否则 fab 不会将其检测为 fab 文件!
I fine tuned the above example to past through argv arguments you might want to pass to local commands and specify an optional default_commands list instead of a hard coded command name. Note, the filename must have a .py extension or fab will not detect it as a fab file!
docs.fabfile.org/en/1.4.0/usage/library.html
“正如该部分提到的,关键就是 run、sudo 和其他
连接时操作仅查看一处: env.host_string 。所有的
设置主机的其他机制由 fab 工具解释
当它运行时,作为库运行时并不重要。”
当我发现这个问题时,我正在考虑同样的问题。另外,在查看时我记得提到在 fabfile 中使用时,环境更改不应该在相同的位置def as run, sudo。谁知道在“库”模式下使用时这是否仍然适用?
编辑:这是所述实现的示例。
docs.fabfile.org/en/1.4.0/usage/library.html
"As that section mentions, the key is simply that run, sudo and the other
operations only look in one place when connecting: env.host_string . All of
the other mechanisms for setting hosts are interpreted by the fab tool
when it runs, and don’t matter when running as a library."
I was looking at this same problem when I found this. Also, while looking I recall mention that when used in a fabfile, env changes should not be in the same def as run, sudo. Who knows if this still applies when used in "library" mode.
EDIT: Here is an example of said implementation
从 1.5.0 开始,有一种比乱搞更好的方法来做到这一点argv。
这也可以用作 setup.py 中的控制台脚本
Since 1.5.0 there is a way better way to do this than messing around with argv.
This can also be utilized as a console script in setup.py
将其添加到 fab 文件的底部。
Add this to the bottom of your fab file.
这是我对 Greg 的答案 的修改版本,它更改了默认行为以显示可用命令,而不是列出大量结构选项。
This is my modified version of Greg's answer that changes default behavior to show available commands instead of listing tons of fabric options.
我在 run Fabric 上间接找到了解决方案文件重命名为 fabfile.py 和无密码 ssh 以外的文件
如果您的文件没有
.py
扩展名,这种方法也有效。I found the solution indirectly on run fabric file renamed other than fabfile.py and password-less ssh
This way also work if your file doesn't have
.py
extension.