Created
May 12, 2017 17:55
-
-
Save alex-dr/ac57ca17691216ceb197e63e37437ce7 to your computer and use it in GitHub Desktop.
Docker compose wrapper with environment variable setup and complex command macros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import click | |
import docker | |
from .compose_wrapper import comp, dc_passthrough | |
from .env import get_env, setup_env | |
def dc(): | |
"""Use docker-compose CLI directly. | |
docker login and environment variable setup are handled for you. | |
""" | |
setup_env() # Docker login and set os.environ | |
dc_passthrough() # send sys.argv to Docker Compose CLI handler | |
@click.group() | |
def dev(): | |
"""Utilities for working with your compose-based Development Environment.""" | |
setup_env() | |
@dev.command() | |
@click.argument('options', nargs=-1) | |
def grunt(options): | |
"""Dockerized Grunt wrapper.""" | |
if options: | |
command = ['grunt'] + list(options) | |
else: | |
command = None | |
comp('run', | |
files=['docker-compose.build-tasks.yml'], | |
flags=['--rm'], | |
services=['grunt'], | |
command=command) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Wrapper for calling the Docker Compose CLI.""" | |
import sys | |
from compose.cli.main import main as dc_passthrough | |
def clean(lst): | |
"""Clean unicode etc from an iterable of strings. | |
TODO: cover more cases than u'foo' -> 'foo'. | |
""" | |
return map(bytes, lst) | |
def comp(subcommand, options=None, services=None, files=None, flags=None, | |
command=None): | |
"""Wrapper for calling docker-compose CLI command. | |
Parameters | |
---------- | |
subcommand : str | |
docker-compose subcommand to run (run, build, up, down) | |
options : [str] | |
Options to Docker compse (eg --project-name) | |
services : [str] | |
List of services to operate on | |
files : [str] | |
docker-compose files to use (-f) | |
All file paths relative to the path of the first file. | |
flags : [str] | |
Any flags to the subcommand | |
command : [str] | |
COMMAND override, for `run` commands. | |
""" | |
argv = [] | |
if options is not None: | |
argv += clean(options) | |
if files is not None: | |
files = clean(files) | |
files = '-f ' + ' -f '.join(files) | |
argv += files.split() | |
argv += [subcommand] | |
if flags is not None: | |
argv += clean(flags) | |
if services is not None: | |
argv += clean(services) | |
if command is not None: | |
argv += clean(command) | |
print('Running `dc {}`'.format(' '.join(argv))) | |
old_argv = sys.argv | |
sys.argv[1:] = argv | |
exc = None | |
try: | |
dc_passthrough() | |
except BaseException as exc: | |
pass # cleanup and raise later | |
finally: | |
sys.argv = old_argv | |
if exc is not None: | |
if isinstance(exc, SystemExit): | |
if exc.code != 0: | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment