-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.py
More file actions
293 lines (242 loc) · 8.52 KB
/
utils.py
File metadata and controls
293 lines (242 loc) · 8.52 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"""
Helper functions used by the Drycc server.
"""
import os
import re
import uuid
import time
import base64
import string
import concurrent
import hashlib
import logging
import random
import math
import pkgutil
import inspect
import requests
import jsonschema
from copy import deepcopy
from django.db import models
from django.conf import settings
from django.core.cache import cache
from asgiref.sync import sync_to_async
from requests_toolbelt import user_agent
from api import __version__ as drycc_version
from rest_framework.exceptions import ValidationError
logger = logging.getLogger(__name__)
session = None
def get_session():
global session
if session is None:
session = requests.Session()
session.headers = {
# https://toolbelt.readthedocs.org/en/latest/user-agent.html#user-agent-constructor
'User-Agent': user_agent('Drycc Controller', drycc_version),
}
# `mount` a custom adapter that retries failed connections for HTTP and HTTPS requests.
# http://docs.python-requests.org/en/latest/api/#requests.adapters.HTTPAdapter
session.mount('http://', requests.adapters.HTTPAdapter(max_retries=10))
session.mount('https://', requests.adapters.HTTPAdapter(max_retries=10))
return session
def import_all_models():
for _, modname, ispkg in pkgutil.iter_modules([
os.path.join(os.path.dirname(__file__), "models")
]):
if not ispkg:
mod = __import__(f"api.models.{modname}")
for subname in dir(mod):
attr = getattr(mod, subname)
if inspect.isclass(attr) and issubclass(attr, models.Model):
globals()[subname] = attr
def validate_label(value):
"""
Check that the value follows the kubernetes name constraints
http://kubernetes.io/v1.1/docs/design/identifiers.html
"""
match = re.match(r'^[a-z0-9-]+$', value)
if not match:
raise ValidationError("Can only contain a-z (lowercase), 0-9 and hyphens")
def random_string(num):
return ''.join(
[random.choice(string.ascii_lowercase) for i in range(num)])
def generate_app_name():
"""Return a randomly-generated memorable name."""
return "{}-{}".format(random_string(6), random_string(8))
def dict_diff(dict1, dict2):
"""
Returns the added, changed, and deleted items in dict1 compared with dict2.
:param dict1: a python dict
:param dict2: an earlier version of the same python dict
:return: a new dict, with 'added', 'changed', and 'removed' items if
any were found.
>>> d1 = {1: 'a'}
>>> dict_diff(d1, d1)
{}
>>> d2 = {1: 'a', 2: 'b'}
>>> dict_diff(d2, d1)
{'added': {2: 'b'}}
>>> d3 = {2: 'B', 3: 'c'}
>>> expected = {'added': {3: 'c'}, 'changed': {2: 'B'}, 'deleted': {1: 'a'}}
>>> dict_diff(d3, d2) == expected
True
"""
diff = {}
set1, set2 = set(dict1), set(dict2)
# Find items that were added to dict2
diff['added'] = {k: dict1[k] for k in (set1 - set2)}
# Find common items whose values differ between dict1 and dict2
diff['changed'] = {
k: dict1[k] for k in (set1 & set2) if dict1[k] != dict2[k]
}
# Find items that were deleted from dict2
diff['deleted'] = {k: dict2[k] for k in (set2 - set1)}
return {k: diff[k] for k in diff if diff[k]}
def fingerprint(key):
"""
Return the fingerprint for an SSH Public Key
"""
key = base64.b64decode(key.strip().split()[1].encode('ascii'))
fp_plain = hashlib.md5(key).hexdigest()
return ':'.join(a + b for a, b in zip(fp_plain[::2], fp_plain[1::2]))
def dict_merge(origin, merge):
"""
Recursively merges dict's. not just simple a["key"] = b["key"], if
both a and b have a key who's value is a dict then dict_merge is called
on both values and the result stored in the returned dictionary.
Also handles merging lists if they occur within the dict
"""
if not isinstance(merge, dict):
return merge
result = deepcopy(origin)
for key, value in merge.items():
if key in result and isinstance(result[key], dict):
result[key] = dict_merge(result[key], value)
else:
if isinstance(value, list):
if key not in result:
result[key] = value
else:
# merge lists without leaving potential duplicates
# result[key] = list(set(result[key] + value)) # changes the order as well
for item in value:
if item in result[key]:
continue
result[key].append(item)
else:
result[key] = deepcopy(value)
return result
def apply_tasks(tasks):
"""
run a group of tasks async
Requires the tasks arg to be a list of functools.partial()
"""
if not tasks:
return
executor = concurrent.futures.ThreadPoolExecutor(settings.DRYCC_APPLY_TASKS)
for future, callback in [(executor.submit(task[0]), task[1]) for task in tasks]:
future.add_done_callback(callback)
error = future.exception()
if error is not None:
raise error
executor.shutdown(wait=True)
def unit_to_bytes(size):
"""
size: str
where unit in K, M, G, T convert to B
"""
if size[-2:-1].isalpha() and size[-1].isalpha():
size = size[:-1]
if size[-1].isalpha():
size = size.upper()
_ = float(size[:-1])
if size[-1] == 'K':
_ *= math.pow(1024, 1)
elif size[-1] == 'M':
_ *= math.pow(1024, 2)
elif size[-1] == 'G':
_ *= math.pow(1024, 3)
elif size[-1] == 'G':
_ *= math.pow(1024, 3)
elif size[-1] == 'T':
_ *= math.pow(1024, 4)
elif size[-1] == 'P':
_ *= math.pow(1024, 5)
return round(_)
def validate_json(value, schema, raise_exception=jsonschema.ValidationError):
if value is not None:
try:
jsonschema.validate(value, schema)
except jsonschema.ValidationError as e:
raise raise_exception("could not validate {}: {}".format(value, e.message))
return value
class CacheLock(object):
def __init__(self, key):
self.key = key
self.value = uuid.uuid4().hex
def acquire(self, blocking=True, timeout=120):
value = None
for _ in range(timeout):
value = cache.get_or_set(self.key, self.value, timeout)
if not blocking or value == self.value:
break
time.sleep(1)
return value == self.value
def release(self):
value = cache.get(self.key, None)
if value != self.value:
return
cache.delete(self.key)
class DeployLock(CacheLock):
def __init__(self, app_id):
self.ptypes_key = f"app:deploy:procfile:types:{app_id}"
super(DeployLock, self).__init__(f"app:deploy:lock:{app_id}")
def locked(self, ptypes):
value = cache.get(self.ptypes_key, [])
locked_ptypes = []
for ptype in ptypes:
if ptype in value:
locked_ptypes.append(ptype)
return locked_ptypes
def acquire(self, ptypes, force=False):
try:
if super(DeployLock, self).acquire():
value = cache.get(self.ptypes_key, [])
if not force and len(self.locked(ptypes)) > 0:
return False
value.extend(ptypes)
value = list(set(value))
cache.set(self.ptypes_key, value, timeout=3600)
return True
finally:
super(DeployLock, self).release()
return False
def release(self, ptypes):
try:
if super(DeployLock, self).acquire():
value = cache.get(self.ptypes_key, [])
if value is None:
return
for ptype in ptypes:
if ptype in value:
value.remove(ptype)
cache.set(self.ptypes_key, value)
finally:
super(DeployLock, self).release()
class SyncIterToAsyncIter(object):
def __init__(self, iter_content):
self.iter_content = iter_content
def __aiter__(self):
return self
async def __anext__(self):
@sync_to_async
def async_next():
try:
return next(self.iter_content)
except StopIteration:
raise StopAsyncIteration
return await async_next()
iter_to_aiter = SyncIterToAsyncIter
if __name__ == "__main__":
import doctest
doctest.testmod()