我已根据要求更新了下面的代码以反映完整文件。
我正在使用面料做一些工作:
#! /usr/bin/python25
import os
import subprocess
import ConfigParser
from fabric.api import env, execute, parallel, \
run, runs_once, task
# --- sudo consts --- #
# relative path, regardless of where we're executing from
RELATIVE_PATH = os.path.abspath(os.path.dirname(__file__))
BASE_SERVER_FILE = os.path.join(RELATIVE_PATH, 'cfg', 'veri.serv.cfg')
BASE_CONFIG_FILE = os.path.join(RELATIVE_PATH, 'cfg', 'veri.base.cfg')
# --- end consts --- #
# --- globals because why not --- #
# did we specify a server override file?
subServSpecified = False
# dict of host objects
hostObjs = {}
# --- end globals --- #
@task
def SetHosts(hostlist):
env.hosts = list(hostlist)
@task
@parallel
def DoWork(cmd):
hostObjs[env.host].cmdResults[cmd] = run(cmd)
print env.host
print cmd
print hostObjs[env.host].cmdResults.items()
print len(hostObjs[env.host].cmdResults)
print "*" * 50
class Host:
fqdn = ''
# dict {'command string': 'result string'}
cmdResults = {}
desiredResults = []
def __init__(self, fqdn):
self.fqdn = fqdn
hostObjs[self.fqdn] = self
def CreateHosts(servlist, commands, desiredResults):
for serv in servlist:
h = Host(serv)
h.desiredResults = list(desiredResults)
baseConfigParser = ConfigParser.SafeConfigParser()
baseConfigParser.read(BASE_CONFIG_FILE)
runlist = baseConfigParser.get('Config List', 'runlist').strip().split("\n")
print runlist
baseServConfigParser = ConfigParser.SafeConfigParser()
baseServConfigParser.read(BASE_SERVER_FILE)
servlist = baseServConfigParser.get('Server List', 'servers').strip().split("\n")
print servlist
for subDir in runlist:
print subDir
relativeRunPath = os.path.join(RELATIVE_PATH, 'cfg', subDir)
subConfigParser = ConfigParser.SafeConfigParser()
subConfigParser.read(os.path.join(relativeRunPath, 'veri.cfg'))
commands = subConfigParser.get('Command List', 'commands').strip().split("\n")
if os.path.isfile(os.path.join(relativeRunPath, 'veri.serv.cfg')):
subServSpecified = True
subServConfigParser = ConfigParser.SafeConfigParser()
subServConfigParser.read(os.path.join(relativeRunPath, 'veri.serv.cfg'))
subServList = subServConfigParser.get('Server List', 'servers').strip().split("\n")
else:
subServList = servlist
CreateHosts(subServList, commands, "succeeded!")
execute(SetHosts, subServList)
for cmd in commands:
execute(DoWork, cmd)
我可以看到我的cmdResults
字典实际上并没有增长:
my.serv.com
nc -zw1 another.serv.com 9988 | gawk '{print $7}'
[("nc -zw1 another.serv.com 9988 | gawk '{print $7}'", 'succeeded!')]
1
**************************************************
my.serv.com
nc -zw1 another.serv.com 8050 | gawk '{print $7}'
[("nc -zw1 another.serv.com 8050 | gawk '{print $7}'", 'succeeded!')]
1
**************************************************
my.serv.com
nc -zw1 another.serv.com 80 | gawk '{print $7}'
[("nc -zw1 another.serv.com 80 | gawk '{print $7}'", 'succeeded!')]
1
**************************************************
上面的 1 出现在循环的每次迭代中,items()
的打印输出始终只是单个条目。谁能看到我在这里失踪的东西?
//veri.cfg
[Command List]
commands:
; T
nc -zw1 another.serv.com 9988 | gawk '{print $7}'
; P
nc -zw1 another.serv.com 8050 | gawk '{print $7}'
; G
nc -zw1 another.serv.com 80 | gawk '{print $7}'
//veri.serv.cfg
[Server List]
servers:
my.serv.com
my.serv2.com
//veri.base.cfg
[Config List]
runlist:
veri.acl
root
-/cfg (veri.serv.cfg / veri.base.cfg)
-/cfg/veri.acl (veri.cfg)
-script.py