diff --git a/conf.py b/conf.py index cd4fa28..35dda32 100644 --- a/conf.py +++ b/conf.py @@ -3,11 +3,14 @@ import logging import sys from nodefilter import NodeFilter + class Conf(object): """Configuration parameters""" hard_filter = None soft_filter = NodeFilter() - ssh = {'opts': '-oConnectTimeout=2 -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oLogLevel=error -lroot -oBatchMode=yes', + ssh = {'opts': '''-oConnectTimeout=2 -oStrictHostKeyChecking=no + -oUserKnownHostsFile=/dev/null -oLogLevel=error + -lroot -oBatchMode=yes''', 'vars': 'OPENRC=/root/openrc IPTABLES_STR="iptables -nvL"'} cluster = None fuelip = 'localhost' @@ -27,7 +30,7 @@ class Conf(object): self.hard_filter = NodeFilter(**entries['hard_filter']) if 'soft_filter' in entries: self.soft_filter = NodeFilter(**entries['soft_filter']) - + @staticmethod def load_conf(filename): try: @@ -40,7 +43,7 @@ class Conf(object): logging.error("Could not convert data") sys.exit(1) except yaml.parser.ParserError as e: - logging.error("Could not parse %s:\n%s" %(filename, str(e))) + logging.error("Could not parse %s:\n%s" % (filename, str(e))) sys.exit(1) except: logging.error("Unexpected error: %s" % sys.exc_info()[0]) diff --git a/nodefilter.py b/nodefilter.py index 4efcf64..35fd316 100644 --- a/nodefilter.py +++ b/nodefilter.py @@ -1,9 +1,9 @@ class NodeFilter(object): - status = ['ready', 'discover'] - online = True - roles = [] - node_ids = [] - def __init__(self, **entries): - self.__dict__.update(entries) + status = ['ready', 'discover'] + online = True + roles = [] + node_ids = [] + def __init__(self, **entries): + self.__dict__.update(entries) diff --git a/timmy.py b/timmy.py index f428ac1..17cfb46 100755 --- a/timmy.py +++ b/timmy.py @@ -22,6 +22,7 @@ import sys import os from conf import Conf import flock +from tools import import_subprocess def main(argv=None): if argv is None: @@ -61,6 +62,7 @@ def main(argv=None): loglevel = logging.INFO logging.basicConfig(level=loglevel, format='%(asctime)s %(levelname)s %(message)s') + import_subprocess() config = Conf.load_conf(args.config) n = nodes.Nodes(conf=config, extended=args.extended, diff --git a/tools.py b/tools.py index 9d957dc..8668f55 100644 --- a/tools.py +++ b/tools.py @@ -21,9 +21,27 @@ tools module import os import logging -import subprocess import sys +def import_subprocess(): + if 'subprocess' not in globals(): + global subprocess + try: + import subprocess32 as subprocess + logging.info("using improved subprocess32 module\n") + except: + import subprocess + logging.warning(("Please upgrade the module 'subprocess' to the latest version: " + "https://pypi.python.org/pypi/subprocess32/")) + ### set not_ok python + global ok_python + ok_python = True + if sys.version > (2,7,0): + ok_python = False + logging.warning('this subprocess module does not support timeouts') + else: + logging.info('subprocess is already loaded') + def get_dir_structure(rootdir): """ @@ -60,13 +78,22 @@ def launch_cmd(command, timeout): shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - try: - outs, errs = p.communicate(timeout=timeout+1) - except subprocess.TimeoutExpired: - p.kill() - outs, errs = p.communicate() - logging.error("command: %s err: %s, returned: %s" % - (command, errs, p.returncode)) + if ok_python: + try: + outs, errs = p.communicate(timeout=timeout+1) + except subprocess.TimeoutExpired: + p.kill() + outs, errs = p.communicate() + logging.error("command: %s err: %s, returned: %s" % + (command, errs, p.returncode)) + else: + try: + outs, errs = p.communicate() + except: + p.kill() + outs, errs = p.communicate() + logging.error("command: %s err: %s, returned: %s" % + (command, errs, p.returncode)) logging.debug("ssh return: err:%s\nouts:%s\ncode:%s" % (errs, outs, p.returncode)) logging.info("ssh return: err:%s\ncode:%s" % @@ -117,13 +144,23 @@ def get_files_rsync(ip, data, sshopts, dpath, timeout=15): stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - try: - outs, errs = p.communicate(input=data, timeout=timeout+1) - except subprocess.TimeoutExpired: - p.kill() - outs, errs = p.communicate() - logging.error("ip: %s, command: %s err: %s, returned: %s" % - (ip, cmd, errs, p.returncode)) + if ok_python: + try: + outs, errs = p.communicate(input=data, timeout=timeout+1) + except subprocess.TimeoutExpired: + p.kill() + outs, errs = p.communicate() + logging.error("ip: %s, command: %s err: %s, returned: %s" % + (ip, cmd, errs, p.returncode)) + else: + try: + outs, errs = p.communicate(input=data) + except: + p.kill() + outs, errs = p.communicate() + logging.error("ip: %s, command: %s err: %s, returned: %s" % + (ip, cmd, errs, p.returncode)) + logging.debug("ip: %s, ssh return: err:%s\nouts:%s\ncode:%s" % (ip, errs, outs, p.returncode)) logging.info("ip: %s, ssh return: err:%s\ncode:%s" %