2014-10-02 19:07:05 +02:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# 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/>.
|
2014-10-16 00:53:43 +02:00
|
|
|
|
2014-10-16 01:18:12 +02:00
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2015-11-16 22:12:57 +01:00
|
|
|
from multiprocessing.managers import SyncManager, DictProxy
|
2014-11-14 23:14:08 +01:00
|
|
|
import multiprocessing
|
|
|
|
import os
|
2015-09-03 06:45:42 +02:00
|
|
|
import tempfile
|
2014-10-16 00:53:43 +02:00
|
|
|
|
2015-04-29 08:06:33 +02:00
|
|
|
from ansible import constants as C
|
2014-11-14 23:14:08 +01:00
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from ansible.executor.play_iterator import PlayIterator
|
|
|
|
from ansible.executor.process.worker import WorkerProcess
|
|
|
|
from ansible.executor.process.result import ResultProcess
|
2015-03-25 19:51:40 +01:00
|
|
|
from ansible.executor.stats import AggregateStats
|
2015-07-21 18:12:22 +02:00
|
|
|
from ansible.playbook.play_context import PlayContext
|
2015-08-26 00:10:03 +02:00
|
|
|
from ansible.plugins import callback_loader, strategy_loader, module_loader
|
2015-05-02 06:48:11 +02:00
|
|
|
from ansible.template import Templar
|
2015-11-16 22:12:57 +01:00
|
|
|
from ansible.vars.hostvars import HostVars
|
2014-10-16 00:53:43 +02:00
|
|
|
|
2015-11-10 20:40:55 +01:00
|
|
|
try:
|
|
|
|
from __main__ import display
|
|
|
|
except ImportError:
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
display = Display()
|
|
|
|
|
2014-11-14 23:14:08 +01:00
|
|
|
__all__ = ['TaskQueueManager']
|
2014-10-16 00:53:43 +02:00
|
|
|
|
2015-11-10 20:40:55 +01:00
|
|
|
|
2014-11-14 23:14:08 +01:00
|
|
|
class TaskQueueManager:
|
|
|
|
|
|
|
|
'''
|
|
|
|
This class handles the multiprocessing requirements of Ansible by
|
|
|
|
creating a pool of worker forks, a result handler fork, and a
|
|
|
|
manager object with shared datastructures/queues for coordinating
|
|
|
|
work between all processes.
|
|
|
|
|
|
|
|
The queue manager is responsible for loading the play strategy plugin,
|
|
|
|
which dispatches the Play's tasks to hosts.
|
|
|
|
'''
|
|
|
|
|
2015-11-10 20:40:55 +01:00
|
|
|
def __init__(self, inventory, variable_manager, loader, options, passwords, stdout_callback=None):
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
self._inventory = inventory
|
|
|
|
self._variable_manager = variable_manager
|
|
|
|
self._loader = loader
|
|
|
|
self._options = options
|
2015-03-25 19:51:40 +01:00
|
|
|
self._stats = AggregateStats()
|
2015-04-07 04:31:55 +02:00
|
|
|
self.passwords = passwords
|
2015-06-30 04:49:02 +02:00
|
|
|
self._stdout_callback = stdout_callback
|
2015-07-04 06:07:17 +02:00
|
|
|
|
|
|
|
self._callbacks_loaded = False
|
2015-07-02 08:50:57 +02:00
|
|
|
self._callback_plugins = []
|
2015-09-30 20:12:38 +02:00
|
|
|
self._start_at_done = False
|
2015-11-04 21:16:20 +01:00
|
|
|
self._result_prc = None
|
2015-03-25 19:51:40 +01:00
|
|
|
|
2015-08-26 00:10:03 +02:00
|
|
|
# make sure the module path (if specified) is parsed and
|
|
|
|
# added to the module_loader object
|
|
|
|
if options.module_path is not None:
|
|
|
|
for path in options.module_path.split(os.pathsep):
|
|
|
|
module_loader.add_directory(path)
|
|
|
|
|
2014-11-14 23:14:08 +01:00
|
|
|
# a special flag to help us exit cleanly
|
|
|
|
self._terminated = False
|
|
|
|
|
|
|
|
# this dictionary is used to keep track of notified handlers
|
|
|
|
self._notified_handlers = dict()
|
|
|
|
|
|
|
|
# dictionaries to keep track of failed/unreachable hosts
|
|
|
|
self._failed_hosts = dict()
|
|
|
|
self._unreachable_hosts = dict()
|
|
|
|
|
|
|
|
self._final_q = multiprocessing.Queue()
|
|
|
|
|
2015-09-03 07:07:29 +02:00
|
|
|
# A temporary file (opened pre-fork) used by connection
|
|
|
|
# plugins for inter-process locking.
|
2015-09-03 06:45:42 +02:00
|
|
|
self._connection_lockfile = tempfile.TemporaryFile()
|
|
|
|
|
2015-11-06 18:03:20 +01:00
|
|
|
def _initialize_processes(self, num):
|
2014-11-14 23:14:08 +01:00
|
|
|
self._workers = []
|
2015-11-04 21:16:20 +01:00
|
|
|
|
2015-11-05 22:42:12 +01:00
|
|
|
for i in xrange(num):
|
2014-11-14 23:14:08 +01:00
|
|
|
main_q = multiprocessing.Queue()
|
|
|
|
rslt_q = multiprocessing.Queue()
|
|
|
|
|
2015-11-16 22:12:57 +01:00
|
|
|
prc = WorkerProcess(self, main_q, rslt_q, self._hostvars_manager, self._loader)
|
2014-11-14 23:14:08 +01:00
|
|
|
prc.start()
|
|
|
|
|
|
|
|
self._workers.append((prc, main_q, rslt_q))
|
|
|
|
|
|
|
|
self._result_prc = ResultProcess(self._final_q, self._workers)
|
|
|
|
self._result_prc.start()
|
|
|
|
|
|
|
|
def _initialize_notified_handlers(self, handlers):
|
|
|
|
'''
|
|
|
|
Clears and initializes the shared notified handlers dict with entries
|
|
|
|
for each handler in the play, which is an empty array that will contain
|
|
|
|
inventory hostnames for those hosts triggering the handler.
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Zero the dictionary first by removing any entries there.
|
|
|
|
# Proxied dicts don't support iteritems, so we have to use keys()
|
|
|
|
for key in self._notified_handlers.keys():
|
|
|
|
del self._notified_handlers[key]
|
|
|
|
|
|
|
|
# FIXME: there is a block compile helper for this...
|
|
|
|
handler_list = []
|
|
|
|
for handler_block in handlers:
|
2015-04-05 08:05:17 +02:00
|
|
|
for handler in handler_block.block:
|
|
|
|
handler_list.append(handler)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-04-28 15:36:42 +02:00
|
|
|
# then initialize it with the handler names from the handler list
|
2014-11-14 23:14:08 +01:00
|
|
|
for handler in handler_list:
|
|
|
|
self._notified_handlers[handler.get_name()] = []
|
|
|
|
|
2015-07-02 08:50:57 +02:00
|
|
|
def load_callbacks(self):
|
2015-04-29 08:06:33 +02:00
|
|
|
'''
|
|
|
|
Loads all available callbacks, with the exception of those which
|
|
|
|
utilize the CALLBACK_TYPE option. When CALLBACK_TYPE is set to 'stdout',
|
|
|
|
only one such callback plugin will be loaded.
|
|
|
|
'''
|
|
|
|
|
2015-07-04 06:07:17 +02:00
|
|
|
if self._callbacks_loaded:
|
|
|
|
return
|
|
|
|
|
2015-04-29 08:06:33 +02:00
|
|
|
stdout_callback_loaded = False
|
2015-07-02 08:50:57 +02:00
|
|
|
if self._stdout_callback is None:
|
|
|
|
self._stdout_callback = C.DEFAULT_STDOUT_CALLBACK
|
2015-04-29 08:06:33 +02:00
|
|
|
|
2015-07-02 08:50:57 +02:00
|
|
|
if self._stdout_callback not in callback_loader:
|
|
|
|
raise AnsibleError("Invalid callback for stdout specified: %s" % self._stdout_callback)
|
2015-04-29 08:06:33 +02:00
|
|
|
|
|
|
|
for callback_plugin in callback_loader.all(class_only=True):
|
|
|
|
if hasattr(callback_plugin, 'CALLBACK_VERSION') and callback_plugin.CALLBACK_VERSION >= 2.0:
|
|
|
|
# we only allow one callback of type 'stdout' to be loaded, so check
|
|
|
|
# the name of the current plugin and type to see if we need to skip
|
|
|
|
# loading this callback plugin
|
|
|
|
callback_type = getattr(callback_plugin, 'CALLBACK_TYPE', None)
|
2015-10-22 14:27:32 +02:00
|
|
|
callback_needs_whitelist = getattr(callback_plugin, 'CALLBACK_NEEDS_WHITELIST', False)
|
2015-04-29 08:06:33 +02:00
|
|
|
(callback_name, _) = os.path.splitext(os.path.basename(callback_plugin._original_path))
|
|
|
|
if callback_type == 'stdout':
|
2015-07-02 08:50:57 +02:00
|
|
|
if callback_name != self._stdout_callback or stdout_callback_loaded:
|
2015-04-29 08:06:33 +02:00
|
|
|
continue
|
|
|
|
stdout_callback_loaded = True
|
2015-10-22 14:27:32 +02:00
|
|
|
elif callback_needs_whitelist and (C.DEFAULT_CALLBACK_WHITELIST is None or callback_name not in C.DEFAULT_CALLBACK_WHITELIST):
|
2015-06-29 16:55:48 +02:00
|
|
|
continue
|
2015-04-29 08:06:33 +02:00
|
|
|
|
2015-11-11 19:19:58 +01:00
|
|
|
self._callback_plugins.append(callback_plugin())
|
2015-04-29 08:06:33 +02:00
|
|
|
|
2015-07-04 06:07:17 +02:00
|
|
|
self._callbacks_loaded = True
|
|
|
|
|
2014-11-14 23:14:08 +01:00
|
|
|
def run(self, play):
|
|
|
|
'''
|
|
|
|
Iterates over the roles/tasks in a play, using the given (or default)
|
|
|
|
strategy for queueing tasks. The default is the linear strategy, which
|
|
|
|
operates like classic Ansible by keeping all hosts in lock-step with
|
|
|
|
a given task (meaning no hosts move on to the next task until all hosts
|
|
|
|
are done with the current task).
|
|
|
|
'''
|
|
|
|
|
2015-07-04 06:07:17 +02:00
|
|
|
if not self._callbacks_loaded:
|
|
|
|
self.load_callbacks()
|
|
|
|
|
2015-04-05 08:05:17 +02:00
|
|
|
all_vars = self._variable_manager.get_vars(loader=self._loader, play=play)
|
2015-06-11 05:26:01 +02:00
|
|
|
templar = Templar(loader=self._loader, variables=all_vars)
|
2015-04-05 08:05:17 +02:00
|
|
|
|
|
|
|
new_play = play.copy()
|
2015-05-02 06:48:11 +02:00
|
|
|
new_play.post_validate(templar)
|
2015-04-05 08:05:17 +02:00
|
|
|
|
2015-11-16 22:12:57 +01:00
|
|
|
class HostVarsManager(SyncManager):
|
|
|
|
pass
|
|
|
|
|
|
|
|
hostvars = HostVars(
|
|
|
|
inventory=self._inventory,
|
|
|
|
variable_manager=self._variable_manager,
|
|
|
|
loader=self._loader,
|
|
|
|
)
|
|
|
|
|
|
|
|
HostVarsManager.register(
|
|
|
|
'hostvars',
|
|
|
|
callable=lambda: hostvars,
|
|
|
|
# FIXME: this is the list of exposed methods to the DictProxy object, plus our
|
2015-11-17 15:15:10 +01:00
|
|
|
# special ones (set_variable_manager/set_inventory). There's probably a better way
|
2015-11-16 22:12:57 +01:00
|
|
|
# to do this with a proper BaseProxy/DictProxy derivative
|
2015-11-17 15:15:10 +01:00
|
|
|
exposed=(
|
|
|
|
'set_variable_manager', 'set_inventory', '__contains__', '__delitem__',
|
2015-11-28 14:58:33 +01:00
|
|
|
'set_nonpersistent_facts', 'set_host_facts', 'set_host_variable',
|
2015-11-17 15:15:10 +01:00
|
|
|
'__getitem__', '__len__', '__setitem__', 'clear', 'copy', 'get', 'has_key',
|
|
|
|
'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'
|
|
|
|
),
|
2015-11-16 22:12:57 +01:00
|
|
|
)
|
|
|
|
self._hostvars_manager = HostVarsManager()
|
|
|
|
self._hostvars_manager.start()
|
|
|
|
|
|
|
|
# Fork # of forks, # of hosts or serial, whichever is lowest
|
|
|
|
contenders = [self._options.forks, play.serial, len(self._inventory.get_hosts(new_play.hosts))]
|
|
|
|
contenders = [ v for v in contenders if v is not None and v > 0 ]
|
|
|
|
self._initialize_processes(min(contenders))
|
|
|
|
|
2015-09-03 06:45:42 +02:00
|
|
|
play_context = PlayContext(new_play, self._options, self.passwords, self._connection_lockfile.fileno())
|
2015-03-25 19:51:40 +01:00
|
|
|
for callback_plugin in self._callback_plugins:
|
2015-07-21 18:12:22 +02:00
|
|
|
if hasattr(callback_plugin, 'set_play_context'):
|
|
|
|
callback_plugin.set_play_context(play_context)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-04-05 08:05:17 +02:00
|
|
|
self.send_callback('v2_playbook_on_play_start', new_play)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
# initialize the shared dictionary containing the notified handlers
|
2015-04-05 08:05:17 +02:00
|
|
|
self._initialize_notified_handlers(new_play.handlers)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
# load the specified strategy (or the default linear one)
|
2015-04-05 08:05:17 +02:00
|
|
|
strategy = strategy_loader.get(new_play.strategy, self)
|
2014-11-14 23:14:08 +01:00
|
|
|
if strategy is None:
|
2015-04-05 08:05:17 +02:00
|
|
|
raise AnsibleError("Invalid play strategy specified: %s" % new_play.strategy, obj=play._ds)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
# build the iterator
|
2015-09-03 20:10:39 +02:00
|
|
|
iterator = PlayIterator(
|
|
|
|
inventory=self._inventory,
|
|
|
|
play=new_play,
|
|
|
|
play_context=play_context,
|
|
|
|
variable_manager=self._variable_manager,
|
|
|
|
all_vars=all_vars,
|
2015-09-30 20:12:38 +02:00
|
|
|
start_at_done = self._start_at_done,
|
2015-09-03 20:10:39 +02:00
|
|
|
)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-09-30 20:12:38 +02:00
|
|
|
# during initialization, the PlayContext will clear the start_at_task
|
|
|
|
# field to signal that a matching task was found, so check that here
|
|
|
|
# and remember it so we don't try to skip tasks on future plays
|
2015-10-01 16:04:22 +02:00
|
|
|
if getattr(self._options, 'start_at_task', None) is not None and play_context.start_at_task is None:
|
2015-09-30 20:12:38 +02:00
|
|
|
self._start_at_done = True
|
|
|
|
|
2015-11-04 23:28:08 +01:00
|
|
|
# and run the play using the strategy and cleanup on way out
|
|
|
|
play_return = strategy.run(iterator, play_context)
|
2015-11-06 18:03:20 +01:00
|
|
|
self._cleanup_processes()
|
2015-11-16 22:12:57 +01:00
|
|
|
self._hostvars_manager.shutdown()
|
2015-11-04 23:28:08 +01:00
|
|
|
return play_return
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
def cleanup(self):
|
2015-11-10 20:40:55 +01:00
|
|
|
display.debug("RUNNING CLEANUP")
|
2014-11-14 23:14:08 +01:00
|
|
|
self.terminate()
|
|
|
|
self._final_q.close()
|
2015-11-06 18:03:20 +01:00
|
|
|
self._cleanup_processes()
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-11-06 18:03:20 +01:00
|
|
|
def _cleanup_processes(self):
|
2015-11-04 21:16:20 +01:00
|
|
|
if self._result_prc:
|
|
|
|
self._result_prc.terminate()
|
|
|
|
|
|
|
|
for (worker_prc, main_q, rslt_q) in self._workers:
|
|
|
|
rslt_q.close()
|
|
|
|
main_q.close()
|
|
|
|
worker_prc.terminate()
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-08-26 18:03:13 +02:00
|
|
|
def clear_failed_hosts(self):
|
2015-09-09 21:26:40 +02:00
|
|
|
self._failed_hosts = dict()
|
2015-08-26 18:03:13 +02:00
|
|
|
|
2014-11-14 23:14:08 +01:00
|
|
|
def get_inventory(self):
|
|
|
|
return self._inventory
|
|
|
|
|
|
|
|
def get_variable_manager(self):
|
|
|
|
return self._variable_manager
|
|
|
|
|
|
|
|
def get_loader(self):
|
|
|
|
return self._loader
|
|
|
|
|
|
|
|
def get_notified_handlers(self):
|
|
|
|
return self._notified_handlers
|
|
|
|
|
|
|
|
def get_workers(self):
|
|
|
|
return self._workers[:]
|
|
|
|
|
|
|
|
def terminate(self):
|
|
|
|
self._terminated = True
|
2015-03-25 19:51:40 +01:00
|
|
|
|
|
|
|
def send_callback(self, method_name, *args, **kwargs):
|
|
|
|
for callback_plugin in self._callback_plugins:
|
|
|
|
# a plugin that set self.disabled to True will not be called
|
|
|
|
# see osx_say.py example for such a plugin
|
|
|
|
if getattr(callback_plugin, 'disabled', False):
|
|
|
|
continue
|
|
|
|
methods = [
|
|
|
|
getattr(callback_plugin, method_name, None),
|
2015-07-09 01:36:30 +02:00
|
|
|
getattr(callback_plugin, 'v2_on_any', None)
|
2015-03-25 19:51:40 +01:00
|
|
|
]
|
|
|
|
for method in methods:
|
|
|
|
if method is not None:
|
2015-07-14 01:22:31 +02:00
|
|
|
try:
|
|
|
|
method(*args, **kwargs)
|
|
|
|
except Exception as e:
|
2015-10-29 00:10:41 +01:00
|
|
|
try:
|
|
|
|
v1_method = method.replace('v2_','')
|
|
|
|
v1_method(*args, **kwargs)
|
|
|
|
except Exception:
|
2015-11-10 20:40:55 +01:00
|
|
|
display.warning('Error when using %s: %s' % (method, str(e)))
|