Skip to content

Commit

Permalink
Various improvements for Python 3.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
br0ziliy committed Feb 4, 2017
1 parent 9a4cd2d commit 4e33c4f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
27 changes: 13 additions & 14 deletions ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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
Expand All @@ -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):
"""
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions lib/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os import walk, path
import codecs
from errbot.templating import tenv

def myreaddir(directory):
Expand All @@ -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

Expand Down

0 comments on commit 4e33c4f

Please sign in to comment.