1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| import sys import subprocess import time import setting from tools.import_file import import_file from tools.hipchat import HipChat
class ChatOps(object):
def __init__(self, token, room_id): self._hipchat = HipChat(token, token, room_id) self._prosess_queue = [] self._current_process = None self._current_cmdinfo = None
def prosess_job(self): """ 登録コマンドの実行処理(並列実行はしない) """ while True: if self._current_process and self._current_process.poll() == 0: self._current_process = None self._current_cmdinfo = None
if self._prosess_queue and self._current_process is None: cmd_info = self._prosess_queue.pop(0) self._current_cmdinfo = cmd_info self._current_process = cmd_info['func'](cmd_info['user'], cmd_info['date'][:19], cmd_info['message'].replace('mo_deploy', '')) yield None
def polling_job(self): """ ChatOps部屋のコマンド監視、コマンド登録 """ allow_time = 0
while True: if time.time() > allow_time: history_list = self._hipchat.get_history() if history_list: for history in history_list: if any(history['message'].startswith(x) for x in ['./deploy.sh']): command_info = {'func': self.deploy_process, 'date': history['date'], 'user': history['from']['mention_name'], 'message': history['message']} self._prosess_queue.append(command_info) if any(history['message'].startswith(x) for x in ['chat_status']): self._hipchat.send_notify(self.get_status(), 'yellow') allow_time = time.time() + 5 yield None
def deploy_process(self, user, date, message): cmd = message.split() return subprocess.Popen(cmd, cwd='./', stdout=1, stderr=1)
def get_status(self): massege = u'============ 実行中 ==============\n' if self._current_cmdinfo: massege += u'{} / {} {}\n'.format(self._current_cmdinfo['date'][11:19], self._current_cmdinfo['user'], self._current_cmdinfo['message']) else: massege += u'none.\n'
massege += u'\n============ 実行待ち ============\n' if self._prosess_queue: for num, info in enumerate(self._prosess_queue, 1): massege += u'{}. {} / {} {}\n'.format(num, info['date'][11:19], info['user'], info['message']) else: massege += u'none.\n' return massege
def run(self): prosess_job = self.prosess_job() polling_job = self.polling_job()
try: while True: try: prosess_job.next() polling_job.next() time.sleep(3) except KeyboardInterrupt: print '!!!! keyboard interrupted !!!!' raise finally: if self._current_process: self._current_process.terminate()
if __name__ == '__main__': access_token = sys.argv[1] room_id = sys.argv[2] ChatOps(access_token, room_id).run()
|