Fix async to use the new argfiles method (wrapping brain around rock, really must write module development guide)

This commit is contained in:
Michael DeHaan 2012-03-14 19:57:56 -04:00
parent 81a27b9c88
commit 842d7cca6f
3 changed files with 13 additions and 35 deletions

View file

@ -30,12 +30,12 @@ import datetime
import traceback import traceback
# =========================================== # ===========================================
# convert arguments of form a=b c=d
# to a dictionary
# FIXME: make more idiomatic
args = " ".join(sys.argv[1:]) # FIXME: better error handling
items = shlex.split(args)
argsfile = sys.argv[1]
items = shlex.split(file(argsfile).read())
params = {} params = {}
for x in items: for x in items:
(k, v) = x.split("=") (k, v) = x.split("=")

View file

@ -66,16 +66,15 @@ def daemonize_self():
if len(sys.argv) < 3: if len(sys.argv) < 3:
print json.dumps({ print json.dumps({
"failed" : True, "failed" : True,
"msg" : "usage: async_wrapper <jid> <module_script> <time_limit> <args>. Humans, do not call directly!" "msg" : "usage: async_wrapper <jid> <time_limit> <modulescript> <argsfile>. Humans, do not call directly!"
}) })
sys.exit(1) sys.exit(1)
jid = sys.argv[1] jid = sys.argv[1]
time_limit = sys.argv[2] time_limit = sys.argv[2]
wrapped_module = sys.argv[3] wrapped_module = sys.argv[3]
args = sys.argv[4:] argsfile = sys.argv[4]
cmd = "%s %s" % (wrapped_module, argsfile)
cmd = "%s %s" % (wrapped_module, " ".join(args))
# setup logging directory # setup logging directory
logdir = os.path.expanduser("~/.ansible_async") logdir = os.path.expanduser("~/.ansible_async")
@ -92,20 +91,20 @@ if not os.path.exists(logdir):
def _run_command(wrapped_cmd, jid, log_path): def _run_command(wrapped_cmd, jid, log_path):
print "RUNNING: %s" % wrapped_cmd
logfile = open(log_path, "w") logfile = open(log_path, "w")
logfile.write(json.dumps({ "started" : 1, "ansible_job_id" : jid })) logfile.write(json.dumps({ "started" : 1, "ansible_job_id" : jid }))
logfile.close() logfile.close()
logfile = open(log_path, "w") logfile = open(log_path, "w")
result = {} result = {}
outdata = ''
try: try:
cmd = shlex.split(wrapped_cmd) cmd = shlex.split(wrapped_cmd)
script = subprocess.Popen(cmd, shell=False, script = subprocess.Popen(cmd, shell=False,
stdin=None, stdout=logfile, stderr=logfile) stdin=None, stdout=logfile, stderr=logfile)
script.communicate() script.communicate()
#result = json.loads(out) outdata = file(log_path).read()
result = json.loads(file(log_path).read()) result = json.loads(outdata)
except (OSError, IOError), e: except (OSError, IOError), e:
result = { result = {
@ -119,6 +118,7 @@ def _run_command(wrapped_cmd, jid, log_path):
result = { result = {
"failed" : 1, "failed" : 1,
"cmd" : wrapped_cmd, "cmd" : wrapped_cmd,
"data" : outdata, # temporary debug only
"msg" : traceback.format_exc() "msg" : traceback.format_exc()
} }
result['ansible_job_id'] = jid result['ansible_job_id'] = jid

22
command
View file

@ -30,32 +30,10 @@ import traceback
import shlex import shlex
import os import os
if len(sys.argv) == 1:
print json.dumps({
"failed" : True,
"msg" : "the command module requires arguments (-a)"
})
sys.exit(1)
argfile = sys.argv[1] argfile = sys.argv[1]
if not os.path.exists(argfile):
print json.dumps({
"failed" : True,
"msg" : "Argument file not found"
})
sys.exit(1)
args = open(argfile, 'r').read() args = open(argfile, 'r').read()
args = shlex.split(args) args = shlex.split(args)
if not len(args):
print json.dumps({
"failed" : True,
"msg" : "the command module requires arguments (-a)"
})
sys.exit(1)
startd = datetime.datetime.now() startd = datetime.datetime.now()
try: try: