我想使用Fabric并在本地运行命令,而不必建立任何其他连接。
我如何在面料2中做到这一点? ... documentation似乎错过任何一个例子。
在织物2中放弃local
命令的设计决定是PITA恕我直言,但我能够通过使用来自Invoke的Context
而不是Connection
来模拟它。
from fabric import Connection
from invoke.context import Context
@task
def hostname(c):
c.run('hostname')
@task
def test(c):
conn = Connection('user@host')
hostname(conn)
local_ctx = Context(c.config) # can be passed into @task;
# Connection is a subclass of Context
hostname(local_ctx)
run,sudo和local都是这样做的:
from fabric import Connection
cn = Connection('[email protected]') # presumes ssh keys were exchanged
cn.run('ls -al') # assuming ssh to linux server - as scott
cn.sudo('whoami') # as root
cn.local('echo ---------- now from local')
cn.local('dir /w') # assuming client is windows