简体   繁体   English

无需使用fabfile即可在结构中进行远程ssh

[英]Remote ssh in fabric without using fabfile

I am writing a program to remotely execute a bunch of commands using fabric and gearman. 我正在编写一个程序,以使用Fabric和Gearman远程执行一堆命令。

The following is the Client code for 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)

The following is my Worker code 以下是我的工作者代码

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()

The problem is when i am running the worker and the client code , the worker still asks me for Hostname eventhough i have provided the hostname. 问题是,当我运行工作程序和客户端代码时,即使我提供了主机名,工作程序仍会要求我提供主机名。 Is there any other way to set the host name in Fabric ? 还有其他方法可以在Fabric中设置主机名吗? I dont intend to spawn a sub process and run the fabric cmd. 我不打算产生一个子进程并运行fabric cmd。

Fixed it , 修复 ,

  1. json was returning data in unicode format so used simplejson. json以Unicode格式返回数据,因此使用了simplejson。
  2. Its env.host_string was the variable to hold hostname. 它的env.host_string是保存主机名的变量。

:) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM