This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
/
repository.py
354 lines (278 loc) · 12.5 KB
/
repository.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
"""
Repository related Django models.
"""
from contextlib import suppress
from django.db import models
from django.db.utils import IntegrityError
from pulpcore.app.models import Model, Notes, MasterModel, GenericKeyValueRelation
from pulpcore.app.models.storage import get_tls_path
class Repository(Model):
"""
Collection of content.
Fields:
name (models.TextField): The repository name.
description (models.TextField): An optional description.
last_version (models.PositiveIntegerField): A record of the last created version number.
Used when a repository version is deleted so as not to create a new vesrion with the
same version number.
Relations:
notes (GenericKeyValueRelation): Arbitrary repository properties.
content (models.ManyToManyField): Associated content.
"""
name = models.TextField(db_index=True, unique=True)
description = models.TextField(blank=True)
last_version = models.PositiveIntegerField(default=0)
notes = GenericKeyValueRelation(Notes)
content = models.ManyToManyField('Content', through='RepositoryContent',
related_name='repositories')
class Meta:
verbose_name_plural = 'repositories'
def natural_key(self):
"""
Get the model's natural key.
:return: The model's natural key.
:rtype: tuple
"""
return (self.name,)
class ContentAdaptor(MasterModel):
"""
An Abstract model for objects that import or publish content.
Fields:
name (models.TextField): The ContentAdaptor name.
last_updated (models.DatetimeField): When the adaptor was last updated.
Relations:
repository (models.ForeignKey): The associated repository.
"""
name = models.TextField(db_index=True)
last_updated = models.DateTimeField(auto_now=True, blank=True, null=True)
class Meta:
abstract = True
unique_together = ('repository', 'name')
def natural_key(self):
"""
Get the model's natural key.
Returns:
tuple: The model's natural key.
"""
return (self.repository, self.name)
class Importer(ContentAdaptor):
"""
A content importer.
Fields:
feed_url (models.TextField): The URL of an external content source.
validate (models.BooleanField): If True, the plugin will validate imported files.
ssl_ca_certificate (models.TextField): A PEM encoded CA certificate used to validate the
server certificate presented by the external source.
ssl_client_certificate (models.TextField): A PEM encoded client certificate used
for authentication.
ssl_client_key (models.TextField): A PEM encoded private key used for authentication.
ssl_validation (models.BooleanField): If True, SSL peer validation must be performed.
proxy_url (models.TextField): The optional proxy URL.
Format: scheme://user:password@host:port
username (models.TextField): The username to be used for authentication when syncing.
password (models.TextField): The password to be used for authentication when syncing.
download_policy (models.TextField): The policy for downloading content.
last_synced (models.DatetimeField): Timestamp of the most recent successful sync.
sync_mode (models.TextField) How the importer should sync from the upstream repository.
Relations:
repository (models.ForeignKey): The repository that owns this Importer
"""
TYPE = 'importer'
# Download Policies
IMMEDIATE = 'immediate'
ON_DEMAND = 'on_demand'
BACKGROUND = 'background'
DOWNLOAD_POLICIES = (
(IMMEDIATE, 'Update the repository content and download all artifacts immediately.'),
(ON_DEMAND, 'Update the repository content but no artifacts are downloaded.'),
(BACKGROUND, 'Update the repository content and download artifacts in the background.'))
# Sync Modes
ADDITIVE = 'additive'
MIRROR = 'mirror'
SYNC_MODES = (
(ADDITIVE, 'Add new content from the remote repository.'),
(MIRROR, 'Add new content and remove content is no longer in the remote repository.'))
def tls_storage_path(self, name):
"""
Returns storage path for TLS file
Args:
name (str): Original name of the uploaded file.
"""
return get_tls_path(self, name)
# Setting this with "unique=True" will trigger a model validation warning, telling us that we
# should use a OneToOneField here instead. While it is correct, doing it this way makes it
# easy to allow multiple importers later: Move the 'repository' field from Importer and
# Publisher to ContentAdaptor (without unique=True). This should make any migration that
# allows multiple importers to be simple, since all that's needed is removing a constraint.
# Using a OneToOneField here would break forward-compatibility with the idea of having
# multiple importers associated with a Repository, since this exposes a ManyRelatedManager
# on Repository with name "importers", and a OneToOneField would instead expose the single
# related Importer instance.
repository = models.ForeignKey(Repository, on_delete=models.CASCADE, unique=True)
feed_url = models.TextField()
validate = models.BooleanField(default=True)
ssl_ca_certificate = models.FileField(blank=True, upload_to=tls_storage_path, max_length=255)
ssl_client_certificate = models.FileField(blank=True, upload_to=tls_storage_path,
max_length=255)
ssl_client_key = models.FileField(blank=True, upload_to=tls_storage_path, max_length=255)
ssl_validation = models.BooleanField(default=True)
proxy_url = models.TextField(blank=True)
username = models.TextField(blank=True)
password = models.TextField(blank=True)
download_policy = models.TextField(choices=DOWNLOAD_POLICIES)
sync_mode = models.TextField(choices=SYNC_MODES)
last_synced = models.DateTimeField(blank=True, null=True)
class Meta(ContentAdaptor.Meta):
default_related_name = 'importers'
@property
def is_deferred(self):
"""
Get whether downloading is deferred.
Returns:
bool: True when deferred.
"""
return self.download_policy != self.IMMEDIATE
class Publisher(ContentAdaptor):
"""
A content publisher.
Fields:
auto_publish (models.BooleanField): Indicates that the adaptor may publish automatically
when the associated repository's content has changed.
last_published (models.DatetimeField): When the last successful publish occurred.
Relations:
"""
TYPE = 'publisher'
repository = models.ForeignKey(Repository, on_delete=models.CASCADE)
auto_publish = models.BooleanField(default=True)
last_published = models.DateTimeField(blank=True, null=True)
class Meta(ContentAdaptor.Meta):
default_related_name = 'publishers'
class RepositoryContent(Model):
"""
Association between a repository and its contained content.
Fields:
created (models.DatetimeField): When the association was created.
Relations:
content (models.ForeignKey): The associated content.
repository (models.ForeignKey): The associated repository.
version_added (models.ForeignKey): The RepositoryVersion which added the referenced
Content.
version_removed (models.ForeignKey): The RepositoryVersion which removed the referenced
Content.
"""
content = models.ForeignKey('Content', on_delete=models.CASCADE,
related_name='version_memberships')
repository = models.ForeignKey(Repository, on_delete=models.CASCADE)
version_added = models.ForeignKey('RepositoryVersion', related_name='added_memberships')
version_removed = models.ForeignKey('RepositoryVersion', null=True,
related_name='removed_memberships')
class Meta:
unique_together = (('repository', 'content', 'version_added'),
('repository', 'content', 'version_removed'))
class RepositoryVersion(Model):
"""
A version of a repository's content set.
Fields:
number (models.PositiveIntegerField): A positive integer that uniquely identifies a version
of a specific repository. Each new version for a repo should have this field set to
1 + the most recent version.
created (models.DateTimeField): When the version was created.
action (models.TextField): The action that produced the version.
complete (models.BooleanField): False indicates that the Repository Version is still being
created, or the task failed before completion.
Relations:
repository (models.ForeignKey): The associated repository.
"""
repository = models.ForeignKey(Repository)
number = models.PositiveIntegerField(db_index=True)
created = models.DateTimeField(auto_now_add=True)
complete = models.BooleanField(default=False)
class Meta:
default_related_name = 'versions'
unique_together = ('repository', 'number')
get_latest_by = 'number'
ordering = ('number',)
def content(self):
"""
Returns:
QuerySet: The Content objects that are related to this version.
"""
relationships = RepositoryContent.objects.filter(
repository=self.repository, version_added__number__lte=self.number).exclude(
version_removed__number__lte=self.number
)
# Surely there is a better way to access the model. Maybe it should be in this module.
content_model = self.repository.content.model
# This causes a SQL subquery to happen.
return content_model.objects.filter(version_memberships__in=relationships)
@property
def content_summary(self):
"""
The contained content summary.
Returns:
dict: of {<type>: <count>}
"""
mapping = self.content().values('type').annotate(count=models.Count('type'))
return {m['type']: m['count'] for m in mapping}
def added(self):
"""
Returns:
QuerySet: The Content objects that were added by this version.
"""
# Surely there is a better way to access the model. Maybe it should be in this module.
content_model = self.repository.content.model
return content_model.objects.filter(version_memberships__version_added=self)
def removed(self):
"""
Returns:
QuerySet: The Content objects that were removed by this version.
"""
# Surely there is a better way to access the model. Maybe it should be in this module.
content_model = self.repository.content.model
return content_model.objects.filter(version_memberships__version_removed=self)
def add_content(self, content):
"""
Add a content unit to this version.
Args:
content (pulpcore.plugin.models.Content): a content model to add
"""
# duplicates are ok
with suppress(IntegrityError):
association = RepositoryContent(
repository=self.repository,
content=content,
version_added=self
)
association.save()
def remove_content(self, content):
"""
Remove content from the repository.
Args:
content (pulpcore.plugin.models.Content): A content model to remove
"""
q_set = RepositoryContent.objects.filter(
repository=self.repository,
content=content,
version_removed=None,
)
q_set.update(version_removed=self)
def save(self, *args, **kwargs):
"""
Save the version while setting the number automatically.
"""
if self.number is None:
self.number = self.repository.last_version + 1
super().save(*args, **kwargs)
def next(self):
"""
Returns:
pulpcore.app.models.RepositoryVersion: The next RepositoryVersion with the same
repository.
Raises:
RepositoryVersion.DoesNotExist: if there is not a RepositoryVersion for the same
repository and with a higher "number".
"""
try:
return self.repository.versions.filter(number__gt=self.number).order_by('number')[0]
except IndexError:
raise self.DoesNotExist