summaryrefslogtreecommitdiff
path: root/docker/objects/objects.py
blob: b70bcc909e6181db32e5b5035be5c39155f45c62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from .. import Client

CLIENT = None


def init(client=None, base_url="unix://var/run/docker.sock", version="1.4"):
    global CLIENT
    if client:
        CLIENT = client
    else:
        CLIENT = Client(base_url, version)


class Identifiable(object):
    def __init__(self, id):
        if isinstance(id, dict):
            id = id.get('Id')
        self.id = id


class Image(Identifiable):
    pass


class Container(Identifiable):
    @classmethod
    def new(cls, image, **kwargs):
        res = CLIENT.create_container(image, **kwargs)
        return cls(res)

    def start(self, binds=None, lxc_conf=None):
        CLIENT.start(self.id, binds, lxc_conf)

    def stop(self, timeout=10):
        CLIENT.stop(self.id)

    def restart(self, timeout=10):
        CLIENT.restart(self.id)

    def commit(self, repository=None, tag=None, message=None, author=None,
               conf=None):
        return Image(CLIENT.commit(self.id, repository, tag, message, author,
                                   conf))

    def diff(self):
        return CLIENT.diff(self.id)

    def export(self):
        return CLIENT.export(self.id)

    def kill(self):
        return CLIENT.kill(self.id)

    def logs(self):
        return CLIENT.logs(self.id)

    def port(self, private_port):
        return CLIENT.port(self.id, private_port)

    def top(self):
        return CLIENT.top(self.id)

    def remove(self, v=False):
        return CLIENT.remove_container(self.id, v)

    def wait(self):
        return CLIENT.wait(self.id)