2012-02-24 00:42:05 +00:00
|
|
|
# Copyright (c) 2012 Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person
|
|
|
|
# obtaining a copy of this software and associated documentation
|
|
|
|
# files (the "Software"), to deal in the Software without restriction,
|
|
|
|
# including without limitation the rights to use, copy, modify, merge,
|
|
|
|
# publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
# and to permit persons to whom the Software is furnished to do so,
|
|
|
|
# subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
# included in all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
|
|
|
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
|
|
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2012-02-23 19:17:24 +00:00
|
|
|
import fnmatch
|
|
|
|
from multiprocessing import Process, Pipe
|
|
|
|
from itertools import izip
|
|
|
|
import os
|
|
|
|
import json
|
2012-02-24 02:37:39 +00:00
|
|
|
import traceback
|
2012-02-24 03:09:23 +00:00
|
|
|
import select
|
2012-02-23 19:17:24 +00:00
|
|
|
|
|
|
|
# non-core
|
|
|
|
import paramiko
|
|
|
|
|
2012-02-24 02:37:39 +00:00
|
|
|
DEFAULT_HOST_LIST = '/etc/ansible/hosts'
|
|
|
|
DEFAULT_MODULE_PATH = '/usr/share/ansible'
|
2012-02-23 19:17:24 +00:00
|
|
|
DEFAULT_MODULE_NAME = 'ping'
|
|
|
|
DEFAULT_PATTERN = '*'
|
|
|
|
DEFAULT_FORKS = 3
|
|
|
|
DEFAULT_MODULE_ARGS = ''
|
2012-02-24 02:37:39 +00:00
|
|
|
DEFAULT_TIMEOUT = 60
|
2012-02-24 03:04:09 +00:00
|
|
|
DEFAULT_REMOTE_USER = 'root'
|
2012-02-23 19:17:24 +00:00
|
|
|
|
|
|
|
class Pooler(object):
|
|
|
|
|
|
|
|
# credit: http://stackoverflow.com/questions/3288595/multiprocessing-using-pool-map-on-a-function-defined-in-a-class
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def spawn(cls, f):
|
|
|
|
def fun(pipe,x):
|
|
|
|
pipe.send(f(x))
|
|
|
|
pipe.close()
|
|
|
|
return fun
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def parmap(cls, f, X):
|
|
|
|
pipe=[Pipe() for x in X]
|
|
|
|
proc=[Process(target=cls.spawn(f),args=(c,x)) for x,(p,c) in izip(X,pipe)]
|
|
|
|
[p.start() for p in proc]
|
|
|
|
[p.join() for p in proc]
|
|
|
|
return [p.recv() for (p,c) in pipe]
|
|
|
|
|
|
|
|
class Runner(object):
|
|
|
|
|
2012-02-24 02:37:39 +00:00
|
|
|
def __init__(self,
|
|
|
|
host_list=DEFAULT_HOST_LIST,
|
|
|
|
module_path=DEFAULT_MODULE_PATH,
|
|
|
|
module_name=DEFAULT_MODULE_NAME,
|
|
|
|
module_args=DEFAULT_MODULE_ARGS,
|
|
|
|
forks=DEFAULT_FORKS,
|
|
|
|
timeout=DEFAULT_TIMEOUT,
|
|
|
|
pattern=DEFAULT_PATTERN,
|
2012-02-24 03:04:09 +00:00
|
|
|
remote_user=DEFAULT_REMOTE_USER,
|
2012-02-24 02:37:39 +00:00
|
|
|
verbose=False):
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
Constructor.
|
|
|
|
'''
|
|
|
|
|
|
|
|
self.host_list = self._parse_hosts(host_list)
|
2012-02-23 19:17:24 +00:00
|
|
|
self.module_path = module_path
|
|
|
|
self.module_name = module_name
|
|
|
|
self.forks = forks
|
|
|
|
self.pattern = pattern
|
|
|
|
self.module_args = module_args
|
|
|
|
self.timeout = timeout
|
2012-02-24 02:37:39 +00:00
|
|
|
self.verbose = verbose
|
2012-02-24 03:04:09 +00:00
|
|
|
self.remote_user = remote_user
|
2012-02-24 02:37:39 +00:00
|
|
|
|
|
|
|
def _parse_hosts(self, host_list):
|
|
|
|
''' parse the host inventory file if not sent as an array '''
|
|
|
|
if type(host_list) != list:
|
|
|
|
host_list = os.path.expanduser(host_list)
|
|
|
|
return file(host_list).read().split("\n")
|
|
|
|
return host_list
|
|
|
|
|
2012-02-23 19:17:24 +00:00
|
|
|
|
|
|
|
def _matches(self, host_name):
|
2012-02-24 02:37:39 +00:00
|
|
|
''' returns if a hostname is matched by the pattern '''
|
2012-02-23 19:17:24 +00:00
|
|
|
if host_name == '':
|
|
|
|
return False
|
|
|
|
if fnmatch.fnmatch(host_name, self.pattern):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _connect(self, host):
|
2012-02-24 02:37:39 +00:00
|
|
|
''' obtains a paramiko connection to the host '''
|
2012-02-23 19:17:24 +00:00
|
|
|
ssh = paramiko.SSHClient()
|
|
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
|
try:
|
2012-02-24 03:04:09 +00:00
|
|
|
ssh.connect(host, username=self.remote_user,
|
2012-02-23 19:17:24 +00:00
|
|
|
allow_agent=True, look_for_keys=True)
|
2012-02-24 03:04:09 +00:00
|
|
|
return [ True, ssh ]
|
2012-02-23 19:17:24 +00:00
|
|
|
except:
|
2012-02-24 03:04:09 +00:00
|
|
|
return [ False, traceback.format_exc() ]
|
2012-02-23 19:17:24 +00:00
|
|
|
|
|
|
|
def _executor(self, host):
|
2012-02-24 02:37:39 +00:00
|
|
|
''' callback executed in parallel for each host '''
|
2012-02-23 19:17:24 +00:00
|
|
|
# TODO: try/catch returning none
|
2012-02-24 02:37:39 +00:00
|
|
|
|
2012-02-24 03:04:09 +00:00
|
|
|
ok, conn = self._connect(host)
|
|
|
|
if not ok:
|
|
|
|
return [ host, False, conn ]
|
2012-02-24 02:37:39 +00:00
|
|
|
|
2012-02-23 21:07:10 +00:00
|
|
|
if self.module_name != "copy":
|
2012-02-24 02:37:39 +00:00
|
|
|
# transfer a module, set it executable, and run it
|
2012-02-23 21:07:10 +00:00
|
|
|
outpath = self._copy_module(conn)
|
|
|
|
self._exec_command(conn, "chmod +x %s" % outpath)
|
|
|
|
cmd = self._command(outpath)
|
|
|
|
result = self._exec_command(conn, cmd)
|
2012-02-24 03:04:09 +00:00
|
|
|
return [ host, True, json.loads(result) ]
|
2012-02-23 21:07:10 +00:00
|
|
|
else:
|
2012-02-24 02:37:39 +00:00
|
|
|
# SFTP file copy module is not really a module
|
2012-02-23 21:07:10 +00:00
|
|
|
ftp = conn.open_sftp()
|
|
|
|
ftp.put(self.module_args[0], self.module_args[1])
|
|
|
|
ftp.close()
|
2012-02-24 03:04:09 +00:00
|
|
|
return [ host, True, 1 ]
|
2012-02-23 21:07:10 +00:00
|
|
|
|
2012-02-23 19:17:24 +00:00
|
|
|
|
|
|
|
def _command(self, outpath):
|
2012-02-24 02:37:39 +00:00
|
|
|
''' form up a command string '''
|
2012-02-23 21:07:10 +00:00
|
|
|
cmd = "%s %s" % (outpath, " ".join(self.module_args))
|
2012-02-23 19:17:24 +00:00
|
|
|
return cmd
|
|
|
|
|
|
|
|
def _exec_command(self, conn, cmd):
|
2012-02-24 02:37:39 +00:00
|
|
|
''' execute a command over SSH '''
|
2012-02-23 19:17:24 +00:00
|
|
|
stdin, stdout, stderr = conn.exec_command(cmd)
|
2012-02-24 03:09:23 +00:00
|
|
|
results = "\n".join(stdout.readlines())
|
2012-02-23 19:17:24 +00:00
|
|
|
return results
|
|
|
|
|
|
|
|
def _copy_module(self, conn):
|
2012-02-24 02:37:39 +00:00
|
|
|
''' transfer a module over SFTP '''
|
|
|
|
in_path = os.path.expanduser(
|
|
|
|
os.path.join(self.module_path, self.module_name)
|
|
|
|
)
|
|
|
|
out_path = os.path.join(
|
|
|
|
"/var/spool/",
|
|
|
|
"ansible_%s" % self.module_name
|
|
|
|
)
|
|
|
|
sftp = conn.open_sftp()
|
|
|
|
sftp.put(in_path, out_path)
|
|
|
|
sftp.close()
|
|
|
|
return out_path
|
2012-02-23 19:17:24 +00:00
|
|
|
|
|
|
|
def run(self):
|
2012-02-24 02:37:39 +00:00
|
|
|
''' xfer & run module on all matched hosts '''
|
2012-02-23 19:17:24 +00:00
|
|
|
hosts = [ h for h in self.host_list if self._matches(h) ]
|
|
|
|
def executor(x):
|
|
|
|
return self._executor(x)
|
|
|
|
results = Pooler.parmap(executor, hosts)
|
2012-02-24 03:04:09 +00:00
|
|
|
results2 = {
|
|
|
|
"successful" : {},
|
|
|
|
"failed" : {}
|
|
|
|
}
|
|
|
|
for x in results:
|
|
|
|
(host, is_ok, result) = x
|
|
|
|
if not is_ok:
|
|
|
|
results2["failed"][host] = result
|
|
|
|
else:
|
|
|
|
results2["successful"][host] = result
|
|
|
|
return results2
|
2012-02-23 19:17:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2012-02-24 02:37:39 +00:00
|
|
|
# test code...
|
2012-02-23 19:56:14 +00:00
|
|
|
|
|
|
|
r = Runner(
|
2012-02-24 02:37:39 +00:00
|
|
|
host_list = DEFAULT_HOST_LIST,
|
2012-02-23 19:56:14 +00:00
|
|
|
module_name='ping',
|
|
|
|
module_args='',
|
|
|
|
pattern='*',
|
|
|
|
forks=3
|
|
|
|
)
|
|
|
|
print r.run()
|
2012-02-23 19:17:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|