-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocks.py
133 lines (110 loc) · 4.15 KB
/
docks.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import ConfigParser
import docker
from synapse.resources.resources import ResourceException
from synapse.config import config
from synapse.logger import logger
from synapse.resources.resources import ResourcesController
from synapse.task import OutgoingMessage, AmqpTask
@logger
class DocksController(ResourcesController):
__resource__ = 'docks'
# The configuration file of the docks plugin
DOCKS_CONFIG_FILE = os.path.join(config.paths['config_path'],
'plugins', 'docks.conf')
def __init__(self, mod):
super(DocksController, self).__init__(mod)
# Loads the configuration file.
self.config = self._load_config_file()
# For each dock, create the Docker client.
self.docks = {}
for key, value in self.config.iteritems():
self.docks[key] = docker.Client(base_url=value['url'],
version='1.12')
def read(self, res_id=None, attributes=None):
"""
"""
status = {}
if res_id is None or res_id == '':
status['docks'] = self.config.keys()
elif res_id in self.docks:
try:
client = self.docks[res_id]
if attributes.get('container'):
status = client.inspect_container(attributes['container'])
else:
status = client.containers()
except Exception as e:
raise ResourceException(e)
else:
raise ResourceException("%s not found." % res_id)
return status
def create(self, res_id, attributes):
"""
collection: docks
id: Antwerp
attributes:
image: ....
name: hostname
memory (MB): ...
cpu_share: ...
command: ...
"""
status = {}
try:
client = self.docks[res_id]
ports = []
volumes = {}
binds = {}
if attributes.get('ports'):
for port in attributes['ports']:
ports.append("%s:%s" % (port['local'], port['remote']))
if attributes.get('volumes'):
for vol in attributes['volumes']:
volumes[vol['remote']] = {}
binds[vol['local']] = vol['remote']
container_id = client.create_container(attributes['image'],
attributes['command'],
ports=ports,
volumes=volumes)['Id']
client.start(container_id, binds=binds)
try:
status = self.read(res_id, {'container': container_id})
except ResourceException:
raise ResourceException("The container does not exist or is "
"terminated.")
except KeyError as e:
raise ResourceException("%s attribute missing." % e)
return status
def update(self, res_id, attributes):
"""
"""
return self.create(res_id, attributes)
def delete(self, res_id, attributes):
"""
"""
status = {}
try:
client = self.docks[res_id]
client.kill(attributes['container'])
status['Id'] = attributes['container']
except KeyError as e:
raise ResourceException("%s attribute missing." % e)
return status
def ping(self):
result = self.read(res_id='')
msg = OutgoingMessage(collection=self.__resource__,
status=result,
msg_type='status',
status_message=True)
task = AmqpTask(msg)
self.publish(task)
def _load_config_file(self):
""" Loads the configuration file.
"""
conf = {}
parser = ConfigParser.SafeConfigParser()
parser.read(self.DOCKS_CONFIG_FILE)
for section in parser.sections():
conf[section] = dict(parser.items(section))
return conf