/
hyper.py
220 lines (184 loc) · 7.91 KB
/
hyper.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import hashlib
import socket
import time
from twisted.internet import reactor as global_reactor
from twisted.internet import defer
from twisted.internet import threads
from twisted.python import threadpool
from buildbot import config
from buildbot.interfaces import LatentWorkerFailedToSubstantiate
from buildbot.util import service
from buildbot.util.logger import Logger
from buildbot.worker import AbstractLatentWorker
try:
import docker
from hyper_sh import Client as Hyper
[docker, Hyper]
except ImportError:
Hyper = None
log = Logger()
class HyperLatentManager(service.SingletonService):
"""A singleton class that manages all the connections to the hyper cloud
There is one instance of this manager per host, accesskey, secretkey tuple.
This manager manages its own thread pull, as Hyper_sh is blocking
"""
name = "HyperLatentManager"
MAX_THREADS = 5
def __init__(self, hyper_host, hyper_accesskey, hyper_secretkey):
service.SingletonService.__init__(self)
self.name = self.getName(hyper_host, hyper_accesskey, hyper_secretkey)
# Prepare the parameters for the Docker Client object.
self.client_args = {'clouds': {
hyper_host: {
"accesskey": hyper_accesskey,
"secretkey": hyper_secretkey
}
}}
def startService(self):
self.threadPool = threadpool.ThreadPool(
minthreads=1, maxthreads=self.MAX_THREADS, name='hyper')
self.threadPool.start()
self.client = Hyper(self.client_args)
def stopService(self):
self.client.close()
return self.threadPool.stop()
def runInThread(self, reactor, meth, *args, **kwargs):
return threads.deferToThreadPool(reactor, self.threadPool, meth, *args, **kwargs)
class HyperLatentWorker(AbstractLatentWorker):
"""hyper.sh is a docker CaaS company"""
instance = None
ALLOWED_SIZES = ['s1', 's2', 's3', 's4',
'm1', 'm2', 'm3', 'l1', 'l2', 'l3']
threadPool = None
client = None
image = None
reactor = global_reactor
def checkConfig(self, name, password, hyper_host,
hyper_accesskey, hyper_secretkey, image, hyper_size="s3", masterFQDN=None, **kwargs):
# Set build_wait_timeout to 0s if not explicitely set: Starting a
# container is almost immediate, we can affort doing so for each build.
if 'build_wait_timeout' not in kwargs:
kwargs['build_wait_timeout'] = 0
AbstractLatentWorker.checkConfig(self, name, password, **kwargs)
if not Hyper:
config.error("The python modules 'docker-py>=1.4' and 'hyper_sh' are needed to use a"
" HyperLatentWorker")
if hyper_size not in self.ALLOWED_SIZES:
config.error("Size is not valid %s vs %r".format(
hyper_size, self.ALLOWED_SIZES))
@property
def client(self):
if self.manager is None:
return None
return self.manager.client
@defer.inlineCallbacks
def reconfigService(self, name, password, hyper_host,
hyper_accesskey, hyper_secretkey, image, hyper_size="s3", masterFQDN=None, **kwargs):
yield AbstractLatentWorker.reconfigService(self, name, password, **kwargs)
self.manager = yield HyperLatentManager.getService(self.master, hyper_host, hyper_accesskey,
hyper_secretkey)
self.masterhash = hashlib.sha1(self.master.name).hexdigest()[:6]
self.size = hyper_size
self.image = image
if not masterFQDN: # also match empty string (for UI)
masterFQDN = socket.getfqdn()
self.masterFQDN = masterFQDN
def createEnvironment(self):
result = {
"BUILDMASTER": self.masterFQDN,
"WORKERNAME": self.name,
"WORKERPASS": self.password
}
if self.registration is not None:
result["BUILDMASTER_PORT"] = str(self.registration.getPBPort())
if ":" in self.masterFQDN:
result["BUILDMASTER"], result[
"BUILDMASTER_PORT"] = self.masterFQDN.split(":")
return result
def runInThread(self, meth, *args, **kwargs):
return self.manager.runInThread(self.reactor, meth, *args, **kwargs)
@defer.inlineCallbacks
def start_instance(self, build):
if self.instance is not None:
raise ValueError('instance active')
image = yield build.render(self.image)
yield self.runInThread(self._thd_start_instance, image)
defer.returnValue(True)
def getContainerName(self):
return ('%s-%s' % ('buildbot' + self.masterhash, self.workername)).replace("_", "-")
def _thd_cleanup_instance(self):
instances = self.client.containers(
filters=dict(name=self.getContainerName()))
for instance in instances:
self.client.remove_container(instance['Id'], v=True, force=True)
def _thd_start_instance(self, image):
t1 = time.time()
self._thd_cleanup_instance()
t2 = time.time()
instance = self.client.create_container(
image,
environment=self.createEnvironment(),
labels={
'sh_hyper_instancetype': self.size
},
name=self.getContainerName()
)
t3 = time.time()
if instance.get('Id') is None:
raise LatentWorkerFailedToSubstantiate(
'Failed to start container'
)
instance['image'] = image
self.instance = instance
self.client.start(instance)
t4 = time.time()
log.debug('{name}:{containerid}: Container started in {total_time:.2f}', name=self.name,
containerid=self.shortid,
clean_time=t2 - t1, create_time=t3 - t2, start_time=t4 - t3, total_time=t4 - t1)
return [instance['Id'], image]
def stop_instance(self, fast=False):
if self.instance is None:
# be gentle. Something may just be trying to alert us that an
# instance never attached, and it's because, somehow, we never
# started.
return defer.succeed(None)
return self.runInThread(self._thd_stop_instance, fast)
@property
def shortid(self):
if self.instance is None:
return None
return self.instance['Id'][:6]
def _thd_stop_instance(self, fast):
if self.instance is None:
return
log.debug('{name}:{containerid}: Stopping container', name=self.name,
containerid=self.shortid)
t1 = time.time()
self.client.stop(self.instance['Id'])
t2 = time.time()
if not fast:
self.client.wait(self.instance['Id'])
t3 = time.time()
self.client.remove_container(self.instance['Id'], v=True, force=True)
t4 = time.time()
log.debug('{name}:{containerid}: Stopped container in {total_time:.2f}', name=self.name,
containerid=self.shortid,
stop_time=t2 - t1, wait_time=t3 - t2, remove_time=t4 - t3, total_time=t4 - t1)
self.instance = None