Port async_status to use the new common code.

pull/675/merge
Michael DeHaan 13 years ago
parent 45354c6be5
commit f4a4649086

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

Loading…
Cancel
Save