1

我正在编写一个程序来使用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()

问题是当我运行工作程序和客户端代码时,工作程序仍然要求我提供主机名,即使我已经提供了主机名。有没有其他方法可以在 Fabric 中设置主机名?我不打算产生一个子进程并运行结构 cmd。

4

1 回答 1

0

修复 ,

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

:)

于 2011-12-19T10:41:35.610 回答