~brianlbaird/charms/trusty/pcrf/trunk

« back to all changes in this revision

Viewing changes to trusty/pcrf/lib/charms/docker/__init__.py

  • Committer: brian baird
  • Date: 2016-08-24 18:05:17 UTC
  • Revision ID: brianlbaird@gmail.com-20160824180517-uyp6100mfwuj6les
dt demo

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import subprocess
 
3
 
 
4
from contextlib import contextmanager
 
5
from shlex import split
 
6
 
 
7
from workspace import Workspace
 
8
 
 
9
# Wrapper and convenience methods for charming w/ docker in python
 
10
class Docker:
 
11
    '''
 
12
    Wrapper class to communicate with the Docker daemon on behalf of
 
13
    a charmer. Provides stateless operations of a running docker daemon
 
14
    '''
 
15
 
 
16
    def __init__(self, socket="unix:///var/run/docker.sock", workspace=None):
 
17
        '''
 
18
        @param socket - URI to the Docker daemon socket
 
19
            default: unix://var/run/docker.sock
 
20
 
 
21
        @param workspace - Path to directory containing a Dockerfile
 
22
            default: None
 
23
        '''
 
24
        self.socket = socket
 
25
        if workspace:
 
26
            self.workspace = Workspace(workspace)
 
27
 
 
28
    def running(self):
 
29
        '''
 
30
        Predicate method to determine if the daemon we are talking to is
 
31
        actually online and recieving events.
 
32
 
 
33
        ex: bootstrap = Docker(socket="unix://var/run/docker-boostrap.sock")
 
34
        bootstrap.running()
 
35
        > True
 
36
        '''
 
37
        # TODO: Add TCP:// support for running check
 
38
        return os.path.isfile(self.socket)
 
39
 
 
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)
 
45
 
 
46
        try:
 
47
            subprocess.check_output(split(cmd))
 
48
        except subprocess.CalledProcessError as expec:
 
49
            print "Error: ", expect.returncode, expect.output