Skip to content

Instantly share code, notes, and snippets.

@CUXIDUMDUM
Created January 26, 2015 10:19
Show Gist options
  • Save CUXIDUMDUM/c88f5bf73f203cfac596 to your computer and use it in GitHub Desktop.
Save CUXIDUMDUM/c88f5bf73f203cfac596 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import logging
import multiprocessing as mp
import os
import okaara
import subprocess
import sys
import signal
import textwrap
import time
from gettext import gettext as _
from okaara.cli import Cli, Command
from okaara.prompt import COLOR_RED
PULPADMIN = "/usr/bin/pulp-admin"
logging.basicConfig(level=logging.DEBUG,
format=('%(asctime)s %(name)-12s %(process)s '
'%(levelname)-8s %(message)s'),
datefmt='%m-%d %H:%M',)
# def signalhandler(signum, frame):
# logging.debug("Got Interrupt")
#
# signal.signal(signal.SIGINT, signalhandler)
#signal.signal(signal.SIGINT, signal.SIG_IGN)
class Run(object):
def __init__(self, command, timeout=None):
self.command = command
self.stdout = None
self.stderr = None
self.exitcode = None
self.pid = None
self.done = False
self.timeout = timeout
def run(self):
starttime = time.time()
if self.timeout:
signal.alarm(self.timeout)
p = None
try:
p = subprocess.Popen(self.command, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
self.pid = p.pid
p.wait()
self.stdout, self.stderr = p.communicate()
self.exitcode = (p.returncode or -1)
except KeyboardInterrupt:
print "Child got int"
self.stdout, self.stderr = p.communicate()
self.exitcode = (p.returncode or p.poll() or -1)
logging.error(self.__dict__)
except Exception as e:
self.prompt.write("Cmd [%s] failed: %s" % (self.command, e),
color = COLOR_RED)
finally:
if p:
self.stdout, self.stderr = p.communicate()
self.exitcode = (p.returncode or p.poll() or -1)
logging.error(self.__dict__)
endtime = time.time()
self.timetaken = int(endtime) - int(starttime)
self.done = True
return self
def __repr__(self):
wrapper = textwrap.TextWrapper(width=78,
initial_indent="\n ",
subsequent_indent=" ")
stdout = self.stdout.rstrip()
if stdout:
stdout = "\nstdout:\n%s" % stdout
stderr = self.stderr.rstrip()
if stderr:
stderr = "\nstderr:\n%s" % stderr
return ("* Command: %s\n"
"Exit status: %s\n"
"%s"
"%s"
% (wrapper.fill(self.command), self.exitcode,
stdout, stderr))
class CommandFailed(Exception):
def __init__(self, result):
super(CommandFailed, self).__init__(result)
self.message = result
def run_command(command, is_dryrun=False):
logging.debug("Running command: [%s]" % command)
if is_dryrun:
return
run_obj = Run(command)
run_obj.run()
# if run_obj.exitcode != 0:
# raise CommandFailed(run_obj)
# else:
# logging.debug(run_obj)
return run_obj
if __name__ == '__main__':
self = okaara.prompt.Prompt()
results = None
pool = None
try:
pool = mp.Pool(2)
commands = [("sleep 100 && echo 1"), ("sleep 200 && echo 2")]
results = pool.map(run_command, commands)
#pool.join()
[logging.debug(r.__dict__) for r in results]
except KeyboardInterrupt:
print "Got keyboard interuppt"
results = pool.terminate()
except Exception, e:
print e
self.write('deploy repo failed on some consumers',
color=COLOR_RED)
if pool:
print results
pool.terminate()
print results
#pool.join()
#raise
finally:
print results
print "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment