import subprocess
import sys
import uuid


usage = """\
Run a command using a temporary docker-compose cluster, removing all containers \
and images after command completion (regardless of success or failure.)

Generally, this would be used with the ``run`` command to provide a clean room \
testing environment.
"""


if not sys.argv[1:]:
    sys.stderr.write(usage)
    sys.exit(1)


project = uuid.uuid1().hex
sys.stderr.write('Starting ephemeral cluster: {0}\n'.format(project))

try:
    sys.exit(subprocess.check_call(['docker-compose', '-p', project] + sys.argv[1:]))
finally:
    subprocess.check_call(['docker-compose', '-p', project, 'stop'])
    subprocess.check_call(['docker-compose', '-p', project, 'rm', '-f', '-v'])

import subprocess
import sys
import uuid


usage = """\
Run a command using a temporary docker-compose cluster, removing all containers \
and associated volumes after command completion (regardless of success or \
failure.)

Generally, this would be used with the ``run`` command to provide a clean room \
testing environment.
"""


if not sys.argv[1:]:
    sys.stderr.write(usage)
    sys.exit(1)


project = uuid.uuid1().hex
sys.stderr.write('Setting up ephemeral cluster ({0})...\n'.format(project))

try:
    subprocess.check_call(['docker-compose', '-p', project] + sys.argv[1:])
except subprocess.CalledProcessError as error:
    raise SystemExit(error.returncode)
finally:
    sys.stderr.write('\nCleaning up ephemeral cluster ({0})...\n'.format(project))
    subprocess.check_call(['docker-compose', '-p', project, 'stop'])
    subprocess.check_call(['docker-compose', '-p', project, 'rm', '-f', '-v'])

Fix forwarding ephemeral cluster exit code.
Fix forwarding ephemeral cluster exit code.

Summary: Also improves logging a little bit.

Test Plan:
    $ python ephemeral-cluster.py run --rm --entrypoint=bash pgshovel -c "exit 10"
    $ test $? -eq 10

Reviewers: jeff, tail

Reviewed By: tail

Differential Revision: http://phabricator.local.disqus.net/D19564

  import subprocess
  import sys
  import uuid
  
  
  usage = """\
  Run a command using a temporary docker-compose cluster, removing all containers \
- and images after command completion (regardless of success or failure.)
+ and associated volumes after command completion (regardless of success or \
+ failure.)
  
  Generally, this would be used with the ``run`` command to provide a clean room \
  testing environment.
  """
  
  
  if not sys.argv[1:]:
      sys.stderr.write(usage)
      sys.exit(1)
  
  
  project = uuid.uuid1().hex
- sys.stderr.write('Starting ephemeral cluster: {0}\n'.format(project))
+ sys.stderr.write('Setting up ephemeral cluster ({0})...\n'.format(project))
  
  try:
-     sys.exit(subprocess.check_call(['docker-compose', '-p', project] + sys.argv[1:]))
+     subprocess.check_call(['docker-compose', '-p', project] + sys.argv[1:])
+ except subprocess.CalledProcessError as error:
+     raise SystemExit(error.returncode)
  finally:
+     sys.stderr.write('\nCleaning up ephemeral cluster ({0})...\n'.format(project))
      subprocess.check_call(['docker-compose', '-p', project, 'stop'])
      subprocess.check_call(['docker-compose', '-p', project, 'rm', '-f', '-v'])
  
Fix forwarding ephemeral cluster exit code.
## Code Before:
import subprocess
import sys
import uuid


usage = """\
Run a command using a temporary docker-compose cluster, removing all containers \
and images after command completion (regardless of success or failure.)

Generally, this would be used with the ``run`` command to provide a clean room \
testing environment.
"""


if not sys.argv[1:]:
    sys.stderr.write(usage)
    sys.exit(1)


project = uuid.uuid1().hex
sys.stderr.write('Starting ephemeral cluster: {0}\n'.format(project))

try:
    sys.exit(subprocess.check_call(['docker-compose', '-p', project] + sys.argv[1:]))
finally:
    subprocess.check_call(['docker-compose', '-p', project, 'stop'])
    subprocess.check_call(['docker-compose', '-p', project, 'rm', '-f', '-v'])

## Instruction:
Fix forwarding ephemeral cluster exit code.
## Code After:
import subprocess
import sys
import uuid


usage = """\
Run a command using a temporary docker-compose cluster, removing all containers \
and associated volumes after command completion (regardless of success or \
failure.)

Generally, this would be used with the ``run`` command to provide a clean room \
testing environment.
"""


if not sys.argv[1:]:
    sys.stderr.write(usage)
    sys.exit(1)


project = uuid.uuid1().hex
sys.stderr.write('Setting up ephemeral cluster ({0})...\n'.format(project))

try:
    subprocess.check_call(['docker-compose', '-p', project] + sys.argv[1:])
except subprocess.CalledProcessError as error:
    raise SystemExit(error.returncode)
finally:
    sys.stderr.write('\nCleaning up ephemeral cluster ({0})...\n'.format(project))
    subprocess.check_call(['docker-compose', '-p', project, 'stop'])
    subprocess.check_call(['docker-compose', '-p', project, 'rm', '-f', '-v'])
