This repository has been archived by the owner on Jun 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
registry.py
415 lines (335 loc) · 13.5 KB
/
registry.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import logging
import kpm.semver as semver
from kpm.models.etcd.package import Package
from kpm.models.etcd.channel import Channel
logger = logging.getLogger(__name__)
def _get_package(package, version_query='latest', package_class=Package):
"""
Fetch the package data from the datastore
and instantiate a :obj:`kpm.models.package_base.PackageModelBase`
Args:
package (:obj:`str`): package name in the format "namespace/name" or "domain.com/name"
version_query (:obj:`str`): a version query, eg: ">=1.5,<2.0"
package_class (:obj:`kpm.models.package_base:PackageBase`): the implemented Package class to use
Returns:
:obj:`kpm.kub_jsonnet.KubJsonnet`: :obj:`kpm.models.package_base.PackageModelBase`
See Also:
* :obj:`kpm.api.models.package_base.PackageModelBase`
* :obj:`kpm.api.models.etcd.package.Package`
Raises:
:obj:`kpm.exception.PackageNotFound`: package not found
:obj:`kpm.exception.InvalidVersion`: version-query malformated
:obj:`kpm.exception.PackageVersionNotFound`: version-query didn't match any release
"""
# if version is None; Find latest version
p = package_class.get(package, version_query)
return p
def pull(package, version='latest', package_class=Package):
"""
Retrives the package blob from the datastore
Args:
package (:obj:`str`): package name in the format "namespace/name" or "domain.com/name"
version_query (:obj:`str`): a version query, eg: ">=1.5,<2.0"
package_class (:obj:`kpm.models.package_base:PackageBase`): the implemented Package class to use
Returns:
:obj:`dict`: package data
* package: package name
* version: version that matched the version query
* filename: suggested filename to create the tarball
* blob: a `tar.gz` encoded in base64.
Example:
>>> kpm.api.impl.registry.pull("coreos/etcd", version=">=3")
{
'blob': 'H4sICHDFvlcC/3RpdF9yb2NrZXRjaGF0XzEuMTAuMGt1Yi50YXIA7ZdRb5swEM',
'filename': u'coreos_etcd_3.0.1.tar.gz',
'package': 'coreos_etcd',
'version': u'3.0.1'
}
Raises:
:obj:`kpm.exception.PackageNotFound`: package not found
:obj:`kpm.exception.InvalidVersion`: version-query malformated
:obj:`kpm.exception.PackageVersionNotFound`: version-query didn't match any release
See Also:
* :obj:`kpm.api.registry.pull`
"""
packagemodel = _get_package(package, version, package_class=package_class)
resp = {"package": package,
"blob": packagemodel.blob,
"version": packagemodel.version,
"filename": "%s_%s.tar.gz" % (packagemodel.package.replace("/", "_"), packagemodel.version)}
return resp
def push(package, version, blob, force=False, package_class=Package):
"""
Push a new package release in the the datastore
Args:
package (:obj:`str`): package name in the format "namespace/name" or "domain.com/name"
version (:obj:`str`): the 'exact' package version (this is not a version_query)
blob (:obj:`str`): the package directory in `tar.gz` and encoded in base64
force (:obj:`boolean`): if the package exists already, overwrite it
package_class (:obj:`kpm.models.package_base:PackageBase`): the implemented Package class to use
Returns:
:obj:`dict`: push status
Example:
>>> kpm.api.impl.registry.push("coreos/etcd", "3.0.1",
"H4sICHDFvlcC/3RpdF9yb2NrZXRjaGF0XzEuMTAuMGt1Yi50YXIA7ZdRb5swEM")
{
'status': u'ok'
}
Raises:
PackageAlreadyExists: if package already exists and `force` is False
See Also:
* :obj:`kpm.api.registry.push`
"""
p = package_class(package, version, blob)
p.save(force=force)
return {"status": "ok"}
def search_reindex(package_class=Package):
Package.reindex()
return {"status": "ok"}
def search(query, package_class=Package):
"""
Search packages
Args:
package (:obj:`str`): package name in the format "namespace/name" or "domain.com/name"
package_class (:obj:`kpm.models.package_base:PackageBase`): the implemented Package class to use
Returns:
:obj:`list`: list of package names
Example:
>>> kpm.api.impl.registry.search("etcd")
[
'coreos/etcd',
'ant31/etcd',
'my-etcd/stable'
]
See Also:
* :obj:`kpm.api.registry.search`
"""
p = package_class.search(query)
return p
def list_packages(organization=None, package_class=Package):
"""
List all packages, filters can be applied
Must have at least a release to be visible
Todos:
- sort_by: name, created_at, downloads, number of stars
- filter_by: users
Args:
organization (:obj:`str`): returns packages from the `organization` only
package_class (:obj:`kpm.models.package_base:PackageBase`): the implemented Package class to use
Returns:
:obj:`list of dict`: list packages
* name: package name
* available_versions (list of str): All releases
* created_at (datetime, optional): package creation date
* downloads (int, optional): number of downloads
* version: release name
Example:
>>> kpm.api.impl.registry.list_packages()
[
{
'available_versions': ['0.1.0'],
'name': u'quentinm/rados-gateway',
'version': '0.1.0',
'created_at": "2016-04-22T11:58:34.103Z",
'downloads': 41
},
{
'available_versions': ['0.1.0'],
'name': u'quentinm/nova',
'version': '0.1.0'
},
]
See Also:
* :obj:`kpm.api.registry.list_packages`
"""
resp = package_class.all(organization)
return resp
def show_package(package,
version="latest",
pullmode=False,
channel_class=Channel,
package_class=Package):
"""
Returns package details
Args:
package (:obj:`str`): package name in the format "namespace/name" or "domain.com/name"
version (:obj:`str`): the 'exact' package version (this is not a version_query)
pullmode (:obj:`boolean`): include the package blob in the response
channel_class (:obj:`kpm.models.channel_base:ChannelBase`): the implemented Channel class to use
package_class (:obj:`kpm.models.package_base:PackageBase`): the implemented Package class to use
Returns:
:obj:`dict`: package data
* version (str)
* name (str)
* created_at (str)
* digest (str)
* channels (list)
* available_versions (list)
* dependencies (list)
* variables (dict)
* manifest (str)
Example:
>>> kpm.api.impl.registry.show_package("ns/mypackage")
{
"version": "3.2.0-rc",
"name": "ns/mypackage",
"created_at": "2016-08-25T10:16:16.366758",
"digest": "93de60f59238f9aebce5b9f8bc708e02a0d740364fcd4b185c6da7fc1cdfe1ba",
"channels": [
{
"current": "3.1.0",
"channel": "stable",
"releases": [
"3.1.0"
"3.0.1"
]
},
],
"available_versions": [
"3.2.0-rc"
"3.1.0",
"3.0.1"
],
"dependencies": [
"ns/dep1",
"ns/dep2",
"ns/dep3"
],
"variables": {
"replicas": 1,
"image": "ns/mypackage:latest",
"namespace": "default",
"cluster": "cluster.local",
"mail_url": "smtp://"
},
"manifest": "---...."
}
Raises:
:obj:`kpm.exception.PackageNotFound`: package not found
:obj:`kpm.exception.InvalidVersion`: version-query malformated
:obj:`kpm.exception.PackageVersionNotFound`: version-query didn't match any release
See Also:
* :obj:`kpm.api.registry.show_package`
"""
stable = False
packagemodel = _get_package(package, version, package_class)
manifest = packagemodel.manifest()
response = {"manifest": packagemodel.packager.manifest,
"version": packagemodel.version,
"name": package,
"created_at": packagemodel.created_at,
"digest": packagemodel.digest,
"variables": manifest.variables,
"dependencies": manifest.dependencies,
"channels": packagemodel.channels(channel_class),
"all_channels": channel_class.all_channels(package).values(),
"available_versions": [str(x) for x in sorted(semver.versions(packagemodel.versions(), stable),
reverse=True)]}
if pullmode:
response['kub'] = packagemodel.blob
return response
# CHANNELS
def list_channels(package, channel_class=Channel):
"""
List all channels for a given package
Args:
package (:obj:`str`): package name in the format "namespace/name" or "domain.com/name"
channel_class (:obj:`kpm.models.channel_base:ChannelBase`): the implemented Channel class to use
Returns:
:obj:`list of dict`: list channels:
* channel (str): channel name
* current (str): latest/default release associated to the channel
* releases (list): list channel's releases
Example:
>>> kpm.api.impl.registry.list_channels("myns/package")
[{'channel': u'stable', 'current': '1.10.2', 'releases': [u'1.10.2']},
{'channel': u'dev', 'current': 2.0.0-beta, 'releases': [1.10.2, 2.0.0-beta]}]
See Also:
* :obj:`kpm.api.registry.list_channels`
"""
channels = channel_class.all_channels(package).values()
return channels
def show_channel(package, name, channel_class=Channel):
"""
Show channel info
Args:
package (:obj:`str`): package name in the format "namespace/name" or "domain.com/name"
name (:obj:`str`): channel name to inspect
channel_class (:obj:`kpm.models.channel_base:ChannelBase`): the implemented Channel class to use
Returns:
:obj:`dict`: channel info
* channel (str): channel name
* current (str): latest/default release associated to the channel
* releases (list): list channel's releases
Example:
>>> kpm.api.impl.registry.list_channels("tit/rocketchat", 'dev')
{'channel': u'dev', 'current': '2.0.0-beta', 'releases': [u'1.10.2']}
Raises:
:obj:`kpm.api.exception.ChannelNotFound`: channel not found
See Also:
* :obj:`kpm.api.registry.show_channel`
"""
c = channel_class(name, package)
return c.to_dict()
def add_channel_release(package, name, release, channel_class=Channel, package_class=Package):
"""
Add a package-release to a channel
Args:
package (:obj:`str`): package name in the format "namespace/name" or "domain.com/name"
name (:obj:`str`): channel name to inspect
release (:obj:`str`): package version to add
channel_class (:obj:`kpm.models.channel_base:ChannelBase`): the implemented Channel class to use
package_class (:obj:`kpm.models.package_base:PackageBase`): the implemented Package class to use
Returns:
:obj:`dict`: channel info
* channel (str): channel name
* current (str): latest/default release associated to the channel
* releases (list): list channel's releases
Example:
>>> kpm.api.impl.registry.list_channels("tit/rocketchat", 'dev')
{'channel': u'dev', 'current': '2.0.0-beta', 'releases': [u'1.10.2']}
Raises:
:obj:`kpm.api.exception.ChannelNotFound`: channel not found
See Also:
* :obj:`kpm.api.registry.add_channel_release`
"""
channel = channel_class(name, package)
channel.add_release(release, package_class)
return channel.to_dict()
def delete_channel_release(package, name, release, channel_class=Channel, package_class=Package):
"""
Remove a release from a channel
Args:
package (:obj:`str`): package name in the format "namespace/name" or "domain.com/name"
name (:obj:`str`): channel name to inspect
release (:obj:`str`): package version to add
channel_class (:obj:`kpm.models.channel_base:ChannelBase`): the implemented Channel class to use
package_class (:obj:`kpm.models.package_base:PackageBase`): the implemented Package class to use
Returns:
:obj:`dict`: channel info
* channel (str): channel name
* current (str): latest/default release associated to the channel
* releases (list): list channel's releases
Example:
>>> kpm.api.impl.registry.list_channels("tit/rocketchat", 'dev')
{'channel': u'dev', 'current': '2.0.0-beta', 'releases': [u'1.10.2']}
Raises:
:obj:`kpm.api.exception.ChannelNotFound`: channel not found
See Also:
* :obj:`kpm.api.registry.delete_channel_release`
"""
channel = channel_class(name, package)
channel.remove_release(release)
return channel.to_dict()
def create_channel(package, name, channel_class=Channel):
channel = channel_class(name, package)
channel.save()
return channel.to_dict()
def delete_channel(package, name, channel_class=Channel):
channel = channel_class(name, package)
channel.delete()
return {"channel": channel.name, "package": package, "action": 'delete'}
def delete_package(package, version="latest", package_class=Package, channel_class=Channel):
packagemodel = _get_package(package, version, package_class)
package_class.delete(packagemodel.package, packagemodel.version, channel_class)
return {"status": "delete", "package": packagemodel.package, "version": packagemodel.version}