Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import logging
  4. import multiprocessing as mp
  5. import os
  6. import okaara
  7. import subprocess
  8. import sys
  9. import signal
  10. import textwrap
  11. import time
  12.  
  13. from gettext import gettext as _
  14. from okaara.cli import Cli, Command
  15. from okaara.prompt import COLOR_RED
  16.  
  17. PULPADMIN = "/usr/bin/pulp-admin"
  18.  
  19. logging.basicConfig(level=logging.DEBUG,
  20. format=('%(asctime)s %(name)-12s %(process)s '
  21. '%(levelname)-8s %(message)s'),
  22. datefmt='%m-%d %H:%M',)
  23.  
  24.  
  25. # def signalhandler(signum, frame):
  26. # logging.debug("Got Interrupt")
  27. #
  28. # signal.signal(signal.SIGINT, signalhandler)
  29. #signal.signal(signal.SIGINT, signal.SIG_IGN)
  30.  
  31.  
  32. class Run(object):
  33. def __init__(self, command, timeout=None):
  34. self.command = command
  35. self.stdout = None
  36. self.stderr = None
  37. self.exitcode = None
  38. self.pid = None
  39. self.done = False
  40. self.timeout = timeout
  41.  
  42. def run(self):
  43. starttime = time.time()
  44. if self.timeout:
  45. signal.alarm(self.timeout)
  46. p = None
  47. try:
  48. p = subprocess.Popen(self.command, shell=True,
  49. stdout=subprocess.PIPE,
  50. stderr=subprocess.PIPE)
  51. self.pid = p.pid
  52. p.wait()
  53. self.stdout, self.stderr = p.communicate()
  54. self.exitcode = (p.returncode or -1)
  55. except KeyboardInterrupt:
  56. print "Child got int"
  57. self.stdout, self.stderr = p.communicate()
  58. self.exitcode = (p.returncode or p.poll() or -1)
  59. logging.error(self.__dict__)
  60. except Exception as e:
  61. self.prompt.write("Cmd [%s] failed: %s" % (self.command, e),
  62. color = COLOR_RED)
  63. finally:
  64. if p:
  65. self.stdout, self.stderr = p.communicate()
  66. self.exitcode = (p.returncode or p.poll() or -1)
  67. logging.error(self.__dict__)
  68. endtime = time.time()
  69. self.timetaken = int(endtime) - int(starttime)
  70. self.done = True
  71. return self
  72.  
  73. def __repr__(self):
  74. wrapper = textwrap.TextWrapper(width=78,
  75. initial_indent="\n ",
  76. subsequent_indent=" ")
  77.  
  78. stdout = self.stdout.rstrip()
  79. if stdout:
  80. stdout = "\nstdout:\n%s" % stdout
  81.  
  82. stderr = self.stderr.rstrip()
  83. if stderr:
  84. stderr = "\nstderr:\n%s" % stderr
  85.  
  86. return ("* Command: %s\n"
  87. "Exit status: %s\n"
  88. "%s"
  89. "%s"
  90. % (wrapper.fill(self.command), self.exitcode,
  91. stdout, stderr))
  92.  
  93.  
  94. class CommandFailed(Exception):
  95. def __init__(self, result):
  96. super(CommandFailed, self).__init__(result)
  97. self.message = result
  98.  
  99.  
  100. def run_command(command, is_dryrun=False):
  101. logging.debug("Running command: [%s]" % command)
  102. if is_dryrun:
  103. return
  104.  
  105. run_obj = Run(command)
  106. run_obj.run()
  107.  
  108. # if run_obj.exitcode != 0:
  109. # raise CommandFailed(run_obj)
  110. # else:
  111. # logging.debug(run_obj)
  112. return run_obj
  113.  
  114.  
  115. if __name__ == '__main__':
  116. self = okaara.prompt.Prompt()
  117. results = None
  118. pool = None
  119. try:
  120. pool = mp.Pool(2)
  121. commands = [("sleep 100 && echo 1"), ("sleep 200 && echo 2")]
  122. results = pool.map(run_command, commands)
  123. #pool.join()
  124. [logging.debug(r.__dict__) for r in results]
  125. except KeyboardInterrupt:
  126. print "Got keyboard interuppt"
  127. results = pool.terminate()
  128. except Exception, e:
  129. print e
  130. self.write('deploy repo failed on some consumers',
  131. color=COLOR_RED)
  132. if pool:
  133. print results
  134. pool.terminate()
  135. print results
  136. #pool.join()
  137. #raise
  138. finally:
  139. print results
  140. print "done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement