在fabric中无fabfile远程ssh

1 投票
1 回答
920 浏览
提问于 2025-04-17 08:37

我正在写一个程序,目的是通过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中设置主机名?我不打算创建一个子进程来运行fabric命令。

1 个回答

0

解决了这个问题,

  1. 因为json返回的数据是unicode格式,所以我用了simplejson这个库。
  2. env.host_string是用来存放主机名的变量。

:)

撰写回答