4
from contextlib import contextmanager
5
from shlex import split
7
from workspace import Workspace
9
# Wrapper and convenience methods for charming w/ docker in python
12
Wrapper class to communicate with the Docker daemon on behalf of
13
a charmer. Provides stateless operations of a running docker daemon
16
def __init__(self, socket="unix:///var/run/docker.sock", workspace=None):
18
@param socket - URI to the Docker daemon socket
19
default: unix://var/run/docker.sock
21
@param workspace - Path to directory containing a Dockerfile
26
self.workspace = Workspace(workspace)
30
Predicate method to determine if the daemon we are talking to is
31
actually online and recieving events.
33
ex: bootstrap = Docker(socket="unix://var/run/docker-boostrap.sock")
37
# TODO: Add TCP:// support for running check
38
return os.path.isfile(self.socket)
40
def run(self, image, options="", volumes=[], ports=[]):
41
volumes = " --volume=".join(volumes)
42
ports = " -p".join(ports)
43
cmd = "docker run {opts} {vols} {ports} {image}".format(
44
opts=options, vols=volumes, ports=ports, image=image)
47
subprocess.check_output(split(cmd))
48
except subprocess.CalledProcessError as expec:
49
print "Error: ", expect.returncode, expect.output