From 4e33c4f8774451de386d9e6a12165254601c9b2a Mon Sep 17 00:00:00 2001 From: Vasiliy Kaygorodov Date: Sat, 4 Feb 2017 01:00:06 +0100 Subject: [PATCH] Various improvements for Python 3.5.2 --- ansible.py | 27 +++++++++++++-------------- lib/utils.py | 9 +++++++-- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/ansible.py b/ansible.py index ddf1810..0889620 100644 --- a/ansible.py +++ b/ansible.py @@ -60,23 +60,23 @@ def check_configuration(self, configuration): "".join([configuration['PLAYBOOK_DIR'], '/']) super(Ansible, self).check_configuration(configuration) - @arg_botcmd('variables', type=str, nargs=argparse.REMAINDER, default=None, + @arg_botcmd('--variables', dest='variables', type=str, nargs=argparse.REMAINDER, default=None, help="optional playbook variables") - @arg_botcmd('timeout', type=int, + @arg_botcmd('--timeout', dest='timeout', type=int, default=180, help="Timeout for playbook execution") @arg_botcmd('inventory', type=str, help="filename of the inventory file") @arg_botcmd('playbook', type=str, help="filename of the playbook file") - def ansible(self, mess, inventory=None, playbook=None, timeout=180, + def ansible(self, mess, inventory=None, playbook=None, timeout=None, variables=None): """ Runs specified Ansible playbook on the specific inventory """ _from = mess.frm - inventory_file = "".join([self.config['INVENTORY_DIR'], inventory]) - playbook_file = "".join([self.config['PLAYBOOK_DIR'], playbook]) + inventory_file = "/".join([self.config['INVENTORY_DIR'], inventory]) + playbook_file = "/".join([self.config['PLAYBOOK_DIR'], playbook]) ssh_key = self.config['ANSIBLE_SSH_KEY'] remote_user = self.config['ANSIBLE_REMOTE_USER'] ansible_bin = path.join(self.config['ANSIBLE_BIN_DIR'], @@ -148,7 +148,7 @@ def ansible_cmd(self, mess, inventory=None, host=None, command=None): return raw_result @arg_botcmd('uuid', type=str, nargs='?', - help="Task UUID") + help="Task UUID", template='task_info') def task_info(self, mess, uuid=None): """ Obtains various types of information about queued tasks @@ -157,9 +157,7 @@ def task_info(self, mess, uuid=None): if not uuid: return "Listing all jobs not implemented yet, please specify UUID of a job" (result, status) = tasks.get_task_info(uuid) - if result: - return "Task {} status: {}\n\n{}".format(uuid, status, result) - else: return "Task {} status: {}".format(uuid, status) + return {'uuid': uuid, 'status': status, 'task_info': result} def task_poller(self): """ @@ -174,16 +172,17 @@ def task_poller(self): for uuid in list(tasklist): author = tasklist[uuid] (result, status) = tasks.get_task_info(uuid) - self.log.debug("Processing task: {}; status: {}," + self.log.debug("Processing task: {}; status: {}, " "result:\n{}".format(uuid, status, result)) if status in ['finished', 'failed'] and result: - self.send(self.build_identifier(author), - "Task {} status: {}\n\n{}".format( - uuid, status, result)) + self.send_templated(self.build_identifier(author), + 'task_info', {'uuid': uuid, 'status': status, 'task_info': result}) del tasklist[uuid] self['tasks'] = tasklist + elif status == 'started': + self.log.debug("Task {} is still in progress, status: {}".format(uuid, status)) else: - self.log.debug("Task {} looks weird, ignoring. Status: {}" + self.log.debug("Task {} looks weird, will not track it anymore. Status: {} " "Result: {}".format(uuid, status, result)) del tasklist[uuid] self['tasks'] = tasklist diff --git a/lib/utils.py b/lib/utils.py index 8023ae1..c22b6ab 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -1,4 +1,5 @@ from os import walk, path +import codecs from errbot.templating import tenv def myreaddir(directory): @@ -10,18 +11,22 @@ def myreaddir(directory): array = [] # walk() and path() come from "os" module for (dirpath, dirnames, filenames) in walk(directory): + try: + dirnames.remove('roles') + except ValueError: + pass for fil in filenames: obj = {'fname': path.join(dirpath, fil), 'comment': ""} array.append(obj) for idx, obj in enumerate(array): fname = obj['fname'] - with open(fname, 'r') as fhandle: + with codecs.open(fname, 'r', encoding='utf-8', errors='ignore') as fhandle: line = fhandle.readline() if line.startswith('#'): obj['comment'] = line.rstrip() array[idx] = obj absname = array[idx]['fname'] - relname = absname[len(directory)+1:] + relname = absname[len(directory):] array[idx]['fname'] = relname return array