2012-03-03 17:25:56 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>, and others
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# 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 traceback
|
2012-03-03 18:19:33 +00:00
|
|
|
import signal
|
|
|
|
import time
|
2012-03-03 17:25:56 +00:00
|
|
|
|
2012-03-11 23:27:43 +00:00
|
|
|
def daemonize_self():
|
|
|
|
# daemonizing code: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
|
|
|
|
# logger.info("cobblerd started")
|
|
|
|
try:
|
|
|
|
pid = os.fork()
|
|
|
|
if pid > 0:
|
|
|
|
# exit first parent
|
|
|
|
sys.exit(0)
|
|
|
|
except OSError, e:
|
|
|
|
print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# decouple from parent environment
|
|
|
|
os.chdir("/")
|
|
|
|
os.setsid()
|
|
|
|
os.umask(022)
|
|
|
|
|
|
|
|
# do second fork
|
|
|
|
try:
|
|
|
|
pid = os.fork()
|
|
|
|
if pid > 0:
|
|
|
|
# print "Daemon PID %d" % pid
|
|
|
|
sys.exit(0)
|
|
|
|
except OSError, e:
|
|
|
|
print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
dev_null = file('/dev/null','rw')
|
|
|
|
os.dup2(dev_null.fileno(), sys.stdin.fileno())
|
|
|
|
os.dup2(dev_null.fileno(), sys.stdout.fileno())
|
|
|
|
os.dup2(dev_null.fileno(), sys.stderr.fileno())
|
|
|
|
|
2012-03-03 17:25:56 +00:00
|
|
|
if len(sys.argv) < 3:
|
|
|
|
print json.dumps({
|
|
|
|
"failed" : True,
|
2012-03-14 23:57:56 +00:00
|
|
|
"msg" : "usage: async_wrapper <jid> <time_limit> <modulescript> <argsfile>. Humans, do not call directly!"
|
2012-03-03 17:25:56 +00:00
|
|
|
})
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
jid = sys.argv[1]
|
2012-03-03 18:06:43 +00:00
|
|
|
time_limit = sys.argv[2]
|
|
|
|
wrapped_module = sys.argv[3]
|
2012-03-14 23:57:56 +00:00
|
|
|
argsfile = sys.argv[4]
|
|
|
|
cmd = "%s %s" % (wrapped_module, argsfile)
|
2012-03-03 17:25:56 +00:00
|
|
|
|
|
|
|
# setup logging directory
|
|
|
|
logdir = os.path.expanduser("~/.ansible_async")
|
|
|
|
log_path = os.path.join(logdir, jid)
|
|
|
|
|
|
|
|
if not os.path.exists(logdir):
|
|
|
|
try:
|
|
|
|
os.makedirs(logdir)
|
|
|
|
except:
|
|
|
|
print json.dumps({
|
|
|
|
"failed" : 1,
|
|
|
|
"msg" : "could not create: %s" % logdir
|
|
|
|
})
|
|
|
|
|
|
|
|
def _run_command(wrapped_cmd, jid, log_path):
|
|
|
|
|
2012-03-11 23:27:43 +00:00
|
|
|
logfile = open(log_path, "w")
|
2012-03-03 17:25:56 +00:00
|
|
|
logfile.write(json.dumps({ "started" : 1, "ansible_job_id" : jid }))
|
2012-03-11 23:27:43 +00:00
|
|
|
logfile.close()
|
|
|
|
logfile = open(log_path, "w")
|
2012-03-03 17:25:56 +00:00
|
|
|
result = {}
|
2012-03-14 23:57:56 +00:00
|
|
|
|
|
|
|
outdata = ''
|
2012-03-03 17:25:56 +00:00
|
|
|
try:
|
|
|
|
cmd = shlex.split(wrapped_cmd)
|
|
|
|
script = subprocess.Popen(cmd, shell=False,
|
2012-03-11 23:27:43 +00:00
|
|
|
stdin=None, stdout=logfile, stderr=logfile)
|
|
|
|
script.communicate()
|
2012-03-14 23:57:56 +00:00
|
|
|
outdata = file(log_path).read()
|
|
|
|
result = json.loads(outdata)
|
2012-03-11 23:27:43 +00:00
|
|
|
|
2012-03-03 17:25:56 +00:00
|
|
|
except (OSError, IOError), e:
|
|
|
|
result = {
|
|
|
|
"failed": 1,
|
2012-03-03 18:06:43 +00:00
|
|
|
"cmd" : wrapped_cmd,
|
2012-03-03 17:25:56 +00:00
|
|
|
"msg": str(e),
|
|
|
|
}
|
2012-03-11 23:27:43 +00:00
|
|
|
result['ansible_job_id'] = jid
|
|
|
|
logfile.write(json.dumps(result))
|
2012-03-03 17:25:56 +00:00
|
|
|
except:
|
|
|
|
result = {
|
|
|
|
"failed" : 1,
|
2012-03-03 18:06:43 +00:00
|
|
|
"cmd" : wrapped_cmd,
|
2012-03-14 23:57:56 +00:00
|
|
|
"data" : outdata, # temporary debug only
|
2012-03-03 17:25:56 +00:00
|
|
|
"msg" : traceback.format_exc()
|
|
|
|
}
|
2012-03-11 23:27:43 +00:00
|
|
|
result['ansible_job_id'] = jid
|
|
|
|
logfile.write(json.dumps(result))
|
2012-03-03 17:25:56 +00:00
|
|
|
logfile.close()
|
|
|
|
|
2012-03-03 18:19:33 +00:00
|
|
|
# immediately exit this process, leaving an orphaned process
|
|
|
|
# running which immediately forks a supervisory timing process
|
2012-03-03 18:06:43 +00:00
|
|
|
|
|
|
|
pid = os.fork()
|
2012-03-11 23:27:43 +00:00
|
|
|
if pid != 0:
|
|
|
|
# the parent indicates the job has started
|
|
|
|
# print "RETURNING SUCCESS IN PARENT"
|
|
|
|
print json.dumps({ "started" : 1, "ansible_job_id" : jid, "results_file" : log_path })
|
|
|
|
sys.stdout.flush()
|
2012-04-01 06:35:19 +00:00
|
|
|
# we need to not return immmediately such that the launched command has an attempt
|
|
|
|
# to initialize PRIOR to ansible trying to clean up the launch directory (and argsfile)
|
|
|
|
# this probably could be done with some IPC later. Modules should always read
|
|
|
|
# the argsfile at the very first start of their execution anyway
|
|
|
|
time.sleep(1)
|
2012-03-03 18:06:43 +00:00
|
|
|
sys.exit(0)
|
|
|
|
else:
|
2012-03-11 23:27:43 +00:00
|
|
|
# the kid manages the job
|
|
|
|
# WARNING: the following call may be total overkill
|
|
|
|
daemonize_self()
|
|
|
|
|
|
|
|
# we are now daemonized in this other fork but still
|
|
|
|
# want to create a supervisory process
|
|
|
|
|
|
|
|
#print "DAEMONIZED KID MAKING MORE KIDS"
|
2012-03-03 18:19:33 +00:00
|
|
|
sub_pid = os.fork()
|
|
|
|
if sub_pid == 0:
|
2012-03-11 23:27:43 +00:00
|
|
|
#print "RUNNING IN KID A"
|
2012-03-03 18:19:33 +00:00
|
|
|
_run_command(cmd, jid, log_path)
|
2012-03-11 23:27:43 +00:00
|
|
|
#print "KID A COMPLETE"
|
|
|
|
sys.stdout.flush()
|
2012-03-03 18:19:33 +00:00
|
|
|
sys.exit(0)
|
|
|
|
else:
|
2012-03-11 23:27:43 +00:00
|
|
|
#print "WATCHING IN KID B"
|
2012-03-03 18:19:33 +00:00
|
|
|
remaining = int(time_limit)
|
|
|
|
if os.path.exists("/proc/%s" % sub_pid):
|
2012-03-11 23:27:43 +00:00
|
|
|
#print "STILL RUNNING"
|
2012-03-03 18:19:33 +00:00
|
|
|
time.sleep(1)
|
|
|
|
remaining = remaining - 1
|
|
|
|
else:
|
2012-03-11 23:27:43 +00:00
|
|
|
#print "DONE IN KID B"
|
|
|
|
sys.stdout.flush()
|
2012-03-03 18:19:33 +00:00
|
|
|
sys.exit(0)
|
|
|
|
if remaining == 0:
|
2012-03-11 23:27:43 +00:00
|
|
|
#print "SLAYING IN KID B"
|
2012-03-03 18:19:33 +00:00
|
|
|
os.kill(sub_pid, signals.SIGKILL)
|
2012-03-11 23:27:43 +00:00
|
|
|
sys.stdout.flush()
|
2012-03-03 18:19:33 +00:00
|
|
|
sys.exit(1)
|
2012-03-03 18:06:43 +00:00
|
|
|
|
2012-03-11 23:27:43 +00:00
|
|
|
sys.stdout.flush()
|
2012-03-03 18:06:43 +00:00
|
|
|
sys.exit(0)
|
2012-03-03 17:25:56 +00:00
|
|
|
|
|
|
|
|