waller.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @Author: wushuiyong
  4. # @Created Time : 日 1/ 1 23:43:12 2017
  5. # @Description:
  6. from fabric2 import Connection
  7. from flask import current_app
  8. from walle.model.deploy import TaskRecordModel
  9. class Waller(Connection):
  10. connections, success, errors = {}, {}, {}
  11. release_version_tar, release_version = None, None
  12. def run(self, command, wenv=None, sudo=False, **kwargs):
  13. '''
  14. # TODO
  15. pty=True/False是直接影响到输出.False较适合在获取文本,True更适合websocket
  16. :param command:
  17. :param wenv:
  18. :param sudo:
  19. :param kwargs:
  20. :return:
  21. '''
  22. try:
  23. message = 'task_id=%s, host:%s command:%s' % (
  24. wenv['task_id'], self.host, command
  25. )
  26. current_app.logger.info(message)
  27. if sudo:
  28. result = super(Waller, self).sudo(command, pty=False, **kwargs)
  29. else:
  30. result = super(Waller, self).run(command, pty=False, **kwargs)
  31. message = 'task_id=%s, host:%s command:%s status:%s, success:%s, error:%s' % (
  32. wenv['task_id'], self.host, command, result.exited, result.stdout.strip(), result.stderr.strip()
  33. )
  34. # TODO
  35. if wenv.has_key('websocket') and wenv['websocket']:
  36. ws_dict = {
  37. 'host': self.host,
  38. 'cmd': command,
  39. 'status': result.exited,
  40. 'stage': wenv['stage'],
  41. 'sequence': wenv['sequence'],
  42. 'success': result.stdout.strip(),
  43. 'error': result.stderr.strip(),
  44. }
  45. wenv['websocket'].send_updates(ws_dict)
  46. TaskRecordModel().save_record(stage=wenv['stage'], sequence=wenv['sequence'], user_id=wenv['user_id'],
  47. task_id=wenv['task_id'], status=result.exited, host=self.host, user=self.user,
  48. command=result.command,success=result.stdout.strip(), error=result.stderr.strip())
  49. current_app.logger.info(message)
  50. return result
  51. except Exception, e:
  52. #current_app.logger.exception(e)
  53. #return None
  54. # TODO 貌似可能的异常有很多种,需要分层才能完美解决 something wrong without e.result
  55. TaskRecordModel().save_record(stage=wenv['stage'], sequence=wenv['sequence'], user_id=wenv['user_id'],
  56. task_id=wenv['task_id'], status=1, host=self.host, user=self.user,
  57. command=command, success='', error='e.result')
  58. if hasattr(e, 'resean') and hasattr(e, 'result'):
  59. message = 'task_id=%s, host:%s command:%s, status=1, reason:%s, result:%s' % (
  60. wenv['task_id'], self.host, command, e.reason, e.result
  61. )
  62. else:
  63. message = 'task_id=%s, host:%s command:%s, status=1, message:%s' % (
  64. wenv['task_id'], self.host, command, e.message
  65. )
  66. # TODO
  67. if wenv.has_key('websocket') and wenv['websocket']:
  68. ws_dict = {
  69. 'host': self.host,
  70. 'cmd': command,
  71. 'status': 1,
  72. 'stage': wenv['stage'],
  73. 'sequence': wenv['sequence'],
  74. 'success': '',
  75. 'error': e.message,
  76. }
  77. wenv['websocket'].send_updates(ws_dict)
  78. # current_app.logger.error(message)
  79. return False
  80. def sudo(self, command, wenv=None, **kwargs):
  81. return self.run(command, wenv=wenv, sudo=True, **kwargs)
  82. def get(self, remote, local=None, wenv=None):
  83. return self.sync(wtype='get', remote=remote, local=local, wenv=wenv)
  84. def put(self, local, remote=None, wenv=None, *args, **kwargs):
  85. return self.sync(wtype='put', local=local, remote=remote, wenv=wenv, *args, **kwargs)
  86. def sync(self, wtype, remote=None, local=None, wenv=None):
  87. try:
  88. if wtype == 'put':
  89. result = super(Waller, self).put(local=local, remote=remote)
  90. command = 'put: scp %s %s@%s:%s' % (result.local, self.user, self.host, result.remote)
  91. current_app.logger.info('put: local %s, remote %s', local, remote)
  92. else:
  93. result = super(Waller, self).get(remote=remote, local=local)
  94. command = 'get: scp %s@%s:%s %s' % (self.user, self.host, result.remote, result.local)
  95. current_app.logger.info('get: local %s, remote %s', local, remote)
  96. current_app.logger.info('get: orig_local %s, local %s', result.orig_local, result.local)
  97. current_app.logger.info('put: %s, %s', result, dir(result))
  98. # TODO 可能会有非22端口的问题
  99. TaskRecordModel().save_record(stage=wenv['stage'], sequence=wenv['sequence'], user_id=wenv['user_id'],
  100. task_id=wenv['task_id'], status=0, host=self.host, user=self.user,
  101. command=command, )
  102. message = 'task_id=%d, host:%s command:%s status:0, success:, error:' % (
  103. wenv['task_id'], self.host, command)
  104. current_app.logger.info(message)
  105. # TODO
  106. if wenv.has_key('websocket') and wenv['websocket']:
  107. ws_dict = {
  108. 'host': self.host,
  109. 'cmd': command,
  110. 'status': 1,
  111. 'stage': wenv['stage'],
  112. 'sequence': wenv['sequence'],
  113. 'success': '',
  114. 'error': result.stderr.strip(),
  115. }
  116. wenv['websocket'].send_updates(ws_dict)
  117. return result
  118. except Exception, e:
  119. # TODO 收尾下
  120. current_app.logger.info('put: %s, %s', e, dir(e))
  121. # TODO
  122. if wenv.has_key('websocket') and wenv['websocket']:
  123. # TODO command
  124. ws_dict = {
  125. 'host': self.host,
  126. 'cmd': 'command',
  127. 'status': 1,
  128. 'stage': wenv['stage'],
  129. 'sequence': wenv['sequence'],
  130. 'success': '',
  131. 'error': e.message,
  132. }
  133. wenv['websocket'].send_updates(ws_dict)