Port async_status to use the new common code.

reviewable/pr18780/r1
Michael DeHaan 13 years ago
parent aa20ae5c02
commit aae41c0d46

@ -18,66 +18,29 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# #
try:
import json
except ImportError:
import simplejson as json
import shlex
import os
import subprocess
import sys
import datetime import datetime
import traceback import traceback
import syslog
# ===========================================
# FIXME: better error handling
argsfile = sys.argv[1]
args = open(argsfile, 'r').read()
items = shlex.split(args)
syslog.openlog('ansible-%s' % os.path.basename(__file__))
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args)
params = {} def main():
for x in items:
(k, v) = x.split("=")
params[k] = v
mode = params.get('mode', 'status') module = AnsibleModule(argument_spec=dict(
jid = params.get('jid', None) jid=dict(required=True),
mode=dict(default='status', choices=['status','cleanup']),
# =========================================== ))
if jid is None:
print json.dumps({
"failed" : True,
"msg" : "jid=INTEGER is required"
})
sys.exit(1)
mode = module.params['mode']
jid = module.params['jid']
# setup logging directory # setup logging directory
logdir = os.path.expanduser("~/.ansible_async") logdir = os.path.expanduser("~/.ansible_async")
log_path = os.path.join(logdir, jid) log_path = os.path.join(logdir, jid)
if not os.path.exists(log_path): if not os.path.exists(log_path):
print json.dumps({ module.fail_json(msg="could not find job", ansible_job_id=jid)
"failed" : 1,
"msg" : "could not find job",
"ansible_job_id" : jid
})
sys.exit(1)
if mode == 'cleanup': if mode == 'cleanup':
os.unlink(log_path) os.unlink(log_path)
print json.dumps({ module.exit_json(ansible_job_id=jid, erased=log_path)
"ansible_job_id" : jid,
"erased" : log_path
})
sys.exit(0)
# NOT in cleanup mode, assume regular status mode # NOT in cleanup mode, assume regular status mode
# no remote kill mode currently exists, but probably should # no remote kill mode currently exists, but probably should
@ -89,25 +52,20 @@ try:
except Exception, e: except Exception, e:
if data == '': if data == '':
# file not written yet? That means it is running # file not written yet? That means it is running
print json.dumps({ module.exit_json(results_file=log_path, ansible_job_id=jid, started=1)
"results_file" : log_path,
"ansible_job_id" : jid,
"started" : 1,
})
else: else:
print json.dumps({ module_fail_json(ansible_job_id=jid, results_file=log_path,
"failed" : True, msg="Could not parse job output: %s" % data)
"ansible_job_id" : jid,
"results_file" : log_path,
"msg" : "Could not parse job output: %s" % data,
})
sys.exit(0)
if not data.has_key("started"): if not data.has_key("started"):
data['finished'] = 1 data['finished'] = 1
data['ansible_job_id'] = jid data['ansible_job_id'] = jid
module.exit_json(**data)
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()
print json.dumps(data)
sys.exit(0)

Loading…
Cancel
Save