在不使用fabfile的情况下远程ssh在结构中

时间:2011-12-19 09:32:50

标签: python fabric gearman

我正在编写一个程序,使用fabric和gearman远程执行一系列命令。

以下是Gearman的客户端代码

import sys , os , json
from fabric.api import *
import gearman
from gearman import GearmanClient


def check_request_status(job_request):
    if job_request.complete:
        print "Job %s finished!  Result: %s - %s" % (job_request.job.unique, job_request.state, job_request.result)
    elif job_request.timed_out:
        print "Job %s timed out!" % job_request.unique
    elif job_request.state == JOB_UNKNOWN:
        print "Job %s connection failed!" % job_request.unique


#gearman client (test file)
gm_client = gearman.GearmanClient(['localhost:4730'])

# See gearman/job.py to see attributes on the GearmanJobRequest
d = {}
d['host'] = 'xyz.abc.com'
d['cmd'] = 'ls -l'
a = json.dumps(d)

completed_job_request = gm_client.submit_job("exe_job", a)
check_request_status(completed_job_request)

以下是我的工作人员代码

import sys , os , json
from fabric import *
from fabric.api import *
import gearman
from gearman import GearmanWorker




#executing the fab command . All the configurations are  mentioned in fabfile.py
def exe_job(gmWorker , gmJob ):
 #host = 'synergy.corp.yahoo.com'
 #d = json.loads(gmJob)
 #run (str(d[cmd]), str(d[host]))
 d = json.loads(gmJob.data)
 env.hoststring = [ str(d['host']) ]
 run ( str(d['cmd']) )
 return gmJob.data


#woker node id to be specified in here
gm_worker = gearman.GearmanWorker(['localhost:4730'])
#gm_worker.set_client_id('client1')
gm_worker.register_task('exe_job',exe_job)
gm_worker.work()

问题是当我运行worker和客户端代码时,虽然我提供了主机名,但是工作者仍然要求我输入Hostname。还有其他方法可以在Fabric中设置主机名吗?我不打算生成子进程并运行结构cmd。

1 个答案:

答案 0 :(得分:0)

修正了它,

  1. json以unicode格式返回数据,因此使用了simplejson。
  2. 它的env.host_string是保存主机名的变量。
  3. :)