3
from contextlib import contextmanager
4
from shlex import split
5
from subprocess import check_output
6
from workspace import Workspace
8
# Wrapper and convenience methods for charming w/ docker compose in python
10
def __init__(self, workspace, strict=True):
12
Object to manage working with Docker-Compose on the CLI. exposes
13
a natural language for performing common tasks with docker in
16
@param workspace - Define the CWD for docker-compose execution
18
@param strict - Enable/disable workspace validation
20
self.workspace = Workspace(workspace)
22
self.workspace.validate()
24
def up(self, service=None):
26
Convenience method that wraps `docker-compose up`
28
usage: c.up('nginx') to start the 'nginx' service from the
29
defined `docker-compose.yml` as a daemon
32
cmd = "docker-compose up -d {}".format(service)
34
cmd = "docker-compose up -d"
37
def kill(self, service=None):
39
Convenience method that wraps `docker-compose kill`
41
usage: c.kill('nginx') to kill the 'nginx' service from the
42
defined `docker-compose.yml`
45
cmd = "docker-compose kill {}".format(service)
47
cmd = "docker-compose kill"
52
chdir sets working context on the workspace
54
@param: cmd - String of the command to run. eg: echo "hello world"
55
the string is passed through shlex.parse() for convenience.
57
returns STDOUT of command execution
59
usage: c.run('docker-compose ps')
61
with chdir("{}".format(self.workspace)):
62
out = check_output(split(cmd))
66
# This is helpful for setting working directory context
69
'''Change the current working directory to a different directory to run
70
commands and return to the previous directory after the command is done.'''