2012-03-03 18:25:56 +01:00
|
|
|
#!/usr/bin/python
|
2012-08-03 03:29:10 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-03-03 18:25:56 +01:00
|
|
|
|
|
|
|
# (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 traceback
|
2012-03-03 19:19:33 +01:00
|
|
|
import signal
|
|
|
|
import time
|
2012-05-10 00:08:45 +02:00
|
|
|
import syslog
|
2012-03-03 18:25:56 +01:00
|
|
|
|
2015-11-01 02:35:48 +01:00
|
|
|
syslog.openlog('ansible-%s' % os.path.basename(__file__))
|
|
|
|
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % " ".join(sys.argv[1:]))
|
|
|
|
|
|
|
|
def notice(msg):
|
|
|
|
syslog.syslog(syslog.LOG_NOTICE, msg)
|
|
|
|
|
2012-03-12 00:27:43 +01:00
|
|
|
def daemonize_self():
|
|
|
|
# daemonizing code: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
|
|
|
|
try:
|
|
|
|
pid = os.fork()
|
|
|
|
if pid > 0:
|
|
|
|
# exit first parent
|
|
|
|
sys.exit(0)
|
2016-06-13 20:40:13 +02:00
|
|
|
except OSError:
|
2016-06-17 00:12:38 +02:00
|
|
|
e = sys.exc_info()[1]
|
2016-06-16 21:27:37 +02:00
|
|
|
sys.exit("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
|
2012-03-12 00:27:43 +01:00
|
|
|
|
|
|
|
# decouple from parent environment
|
|
|
|
os.chdir("/")
|
|
|
|
os.setsid()
|
2016-06-13 20:40:13 +02:00
|
|
|
os.umask(int('022', 8))
|
2012-03-12 00:27:43 +01:00
|
|
|
|
|
|
|
# do second fork
|
|
|
|
try:
|
|
|
|
pid = os.fork()
|
|
|
|
if pid > 0:
|
|
|
|
# print "Daemon PID %d" % pid
|
|
|
|
sys.exit(0)
|
2016-06-13 20:40:13 +02:00
|
|
|
except OSError:
|
2016-06-17 00:12:38 +02:00
|
|
|
e = sys.exc_info()[1]
|
2016-06-16 21:27:37 +02:00
|
|
|
sys.exit("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
|
2012-03-12 00:27:43 +01:00
|
|
|
|
|
|
|
dev_null = file('/dev/null','rw')
|
|
|
|
os.dup2(dev_null.fileno(), sys.stdin.fileno())
|
|
|
|
os.dup2(dev_null.fileno(), sys.stdout.fileno())
|
2012-08-07 02:07:02 +02:00
|
|
|
os.dup2(dev_null.fileno(), sys.stderr.fileno())
|
2012-03-12 00:27:43 +01:00
|
|
|
|
2012-03-03 18:25:56 +01:00
|
|
|
|
2015-11-01 02:35:48 +01:00
|
|
|
def _run_module(wrapped_cmd, jid, job_path):
|
2012-03-03 18:25:56 +01:00
|
|
|
|
2015-11-01 02:35:48 +01:00
|
|
|
jobfile = open(job_path, "w")
|
|
|
|
jobfile.write(json.dumps({ "started" : 1, "ansible_job_id" : jid }))
|
|
|
|
jobfile.close()
|
|
|
|
jobfile = open(job_path, "w")
|
2012-03-03 18:25:56 +01:00
|
|
|
result = {}
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-03-15 00:57:56 +01:00
|
|
|
outdata = ''
|
2012-03-03 18:25:56 +01:00
|
|
|
try:
|
|
|
|
cmd = shlex.split(wrapped_cmd)
|
2015-11-01 02:35:48 +01:00
|
|
|
script = subprocess.Popen(cmd, shell=False, stdin=None, stdout=jobfile, stderr=jobfile)
|
2012-03-12 00:27:43 +01:00
|
|
|
script.communicate()
|
2015-11-01 02:35:48 +01:00
|
|
|
outdata = file(job_path).read()
|
2012-03-15 00:57:56 +01:00
|
|
|
result = json.loads(outdata)
|
2012-03-12 00:27:43 +01:00
|
|
|
|
2016-06-13 20:40:13 +02:00
|
|
|
except (OSError, IOError):
|
2016-06-17 00:12:38 +02:00
|
|
|
e = sys.exc_info()[1]
|
2012-03-03 18:25:56 +01:00
|
|
|
result = {
|
|
|
|
"failed": 1,
|
2012-03-03 19:06:43 +01:00
|
|
|
"cmd" : wrapped_cmd,
|
2012-03-03 18:25:56 +01:00
|
|
|
"msg": str(e),
|
|
|
|
}
|
2012-03-12 00:27:43 +01:00
|
|
|
result['ansible_job_id'] = jid
|
2015-11-01 02:35:48 +01:00
|
|
|
jobfile.write(json.dumps(result))
|
2012-03-03 18:25:56 +01:00
|
|
|
except:
|
|
|
|
result = {
|
|
|
|
"failed" : 1,
|
2012-03-03 19:06:43 +01:00
|
|
|
"cmd" : wrapped_cmd,
|
2015-11-01 02:35:48 +01:00
|
|
|
"data" : outdata, # temporary notice only
|
2012-03-03 18:25:56 +01:00
|
|
|
"msg" : traceback.format_exc()
|
2012-08-07 02:07:02 +02:00
|
|
|
}
|
2012-03-12 00:27:43 +01:00
|
|
|
result['ansible_job_id'] = jid
|
2015-11-01 02:35:48 +01:00
|
|
|
jobfile.write(json.dumps(result))
|
|
|
|
jobfile.close()
|
2012-03-03 18:25:56 +01:00
|
|
|
|
2012-03-03 19:06:43 +01:00
|
|
|
|
2015-11-01 02:35:48 +01:00
|
|
|
####################
|
|
|
|
## main ##
|
|
|
|
####################
|
|
|
|
if __name__ == '__main__':
|
2012-03-03 18:25:56 +01:00
|
|
|
|
2015-11-01 02:35:48 +01:00
|
|
|
if len(sys.argv) < 3:
|
2016-06-13 20:40:13 +02:00
|
|
|
print(json.dumps({
|
2015-11-01 02:35:48 +01:00
|
|
|
"failed" : True,
|
|
|
|
"msg" : "usage: async_wrapper <jid> <time_limit> <modulescript> <argsfile>. Humans, do not call directly!"
|
2016-06-13 20:40:13 +02:00
|
|
|
}))
|
2015-11-01 02:35:48 +01:00
|
|
|
sys.exit(1)
|
2012-03-03 18:25:56 +01:00
|
|
|
|
2015-11-01 02:35:48 +01:00
|
|
|
jid = "%s.%d" % (sys.argv[1], os.getpid())
|
|
|
|
time_limit = sys.argv[2]
|
|
|
|
wrapped_module = sys.argv[3]
|
2016-04-02 04:50:58 +02:00
|
|
|
if len(sys.argv) >= 5:
|
|
|
|
argsfile = sys.argv[4]
|
|
|
|
cmd = "%s %s" % (wrapped_module, argsfile)
|
|
|
|
else:
|
|
|
|
cmd = wrapped_module
|
2015-11-01 02:35:48 +01:00
|
|
|
step = 5
|
|
|
|
|
|
|
|
# setup job output directory
|
|
|
|
jobdir = os.path.expanduser("~/.ansible_async")
|
|
|
|
job_path = os.path.join(jobdir, jid)
|
|
|
|
|
|
|
|
if not os.path.exists(jobdir):
|
|
|
|
try:
|
|
|
|
os.makedirs(jobdir)
|
|
|
|
except:
|
2016-06-13 20:40:13 +02:00
|
|
|
print(json.dumps({
|
2015-11-01 02:35:48 +01:00
|
|
|
"failed" : 1,
|
|
|
|
"msg" : "could not create: %s" % jobdir
|
2016-06-13 20:40:13 +02:00
|
|
|
}))
|
2015-11-01 02:35:48 +01:00
|
|
|
# immediately exit this process, leaving an orphaned process
|
|
|
|
# running which immediately forks a supervisory timing process
|
2012-04-28 17:56:55 +02:00
|
|
|
|
2015-11-01 02:35:48 +01:00
|
|
|
try:
|
|
|
|
pid = os.fork()
|
|
|
|
if pid:
|
|
|
|
# Notify the overlord that the async process started
|
|
|
|
|
|
|
|
# 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
|
|
|
|
notice("Return async_wrapper task started.")
|
2016-06-13 20:40:13 +02:00
|
|
|
print(json.dumps({ "started" : 1, "ansible_job_id" : jid, "results_file" : job_path }))
|
2015-11-01 02:35:48 +01:00
|
|
|
sys.stdout.flush()
|
|
|
|
time.sleep(1)
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
# The actual wrapper process
|
|
|
|
|
|
|
|
# Daemonize, so we keep on running
|
|
|
|
daemonize_self()
|
|
|
|
|
|
|
|
# we are now daemonized, create a supervisory process
|
|
|
|
notice("Starting module and watcher")
|
|
|
|
|
|
|
|
sub_pid = os.fork()
|
|
|
|
if sub_pid:
|
|
|
|
# the parent stops the process after the time limit
|
|
|
|
remaining = int(time_limit)
|
|
|
|
|
|
|
|
# set the child process group id to kill all children
|
|
|
|
os.setpgid(sub_pid, sub_pid)
|
|
|
|
|
|
|
|
notice("Start watching %s (%s)"%(sub_pid, remaining))
|
|
|
|
time.sleep(step)
|
|
|
|
while os.waitpid(sub_pid, os.WNOHANG) == (0, 0):
|
|
|
|
notice("%s still running (%s)"%(sub_pid, remaining))
|
|
|
|
time.sleep(step)
|
|
|
|
remaining = remaining - step
|
|
|
|
if remaining <= 0:
|
|
|
|
notice("Now killing %s"%(sub_pid))
|
|
|
|
os.killpg(sub_pid, signal.SIGKILL)
|
|
|
|
notice("Sent kill to group %s"%sub_pid)
|
|
|
|
time.sleep(1)
|
|
|
|
sys.exit(0)
|
|
|
|
notice("Done in kid B.")
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
# the child process runs the actual module
|
|
|
|
notice("Start module (%s)"%os.getpid())
|
|
|
|
_run_module(cmd, jid, job_path)
|
|
|
|
notice("Module complete (%s)"%os.getpid())
|
|
|
|
sys.exit(0)
|
|
|
|
|
2015-11-20 11:06:08 +01:00
|
|
|
except SystemExit:
|
|
|
|
# On python2.4, SystemExit is a subclass of Exception.
|
|
|
|
# This block makes python2.4 behave the same as python2.5+
|
2016-06-16 21:55:45 +02:00
|
|
|
raise
|
2015-11-20 11:06:08 +01:00
|
|
|
|
2016-06-13 20:40:13 +02:00
|
|
|
except Exception:
|
2016-06-16 21:21:57 +02:00
|
|
|
e = sys.exc_info()[1]
|
2016-06-13 20:40:13 +02:00
|
|
|
notice("error: %s"%(e))
|
|
|
|
print(json.dumps({
|
2015-11-01 02:35:48 +01:00
|
|
|
"failed" : True,
|
2016-06-13 20:40:13 +02:00
|
|
|
"msg" : "FATAL ERROR: %s" % str(e)
|
|
|
|
}))
|
2015-11-01 02:35:48 +01:00
|
|
|
sys.exit(1)
|