-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvolume.py
More file actions
81 lines (70 loc) · 3.08 KB
/
volume.py
File metadata and controls
81 lines (70 loc) · 3.08 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
import logging
from django.db import models, transaction
from django.conf import settings
from jsonfield import JSONField
from api.exceptions import DryccException, ServiceUnavailable, AlreadyExists
from api.models import UuidAuditedModel, validate_label
from scheduler import KubeException
logger = logging.getLogger(__name__)
class Volume(UuidAuditedModel):
owner = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.PROTECT)
app = models.ForeignKey('App', on_delete=models.CASCADE)
name = models.CharField(max_length=63, validators=[validate_label])
size = models.CharField(max_length=128)
path = JSONField(default={}, blank=True)
class Meta:
get_latest_by = 'created'
unique_together = (('app', 'name'),)
ordering = ['-created']
def __str__(self):
return self.name
@transaction.atomic
def save(self, *args, **kwargs):
# Attach volume, updates k8s
if self.created == self.updated:
self.attach(*args, **kwargs)
# Save to DB
return super(Volume, self).save(*args, **kwargs)
@transaction.atomic
def delete(self, *args, **kwargs):
if self.path:
raise DryccException("the volume is not unmounted")
# Deatch volume, updates k8s
self.detach(*args, **kwargs)
# Delete from DB
return super(Volume, self).delete(*args, **kwargs)
def attach(self, *args, **kwargs):
try:
self._scheduler.pvc.get(self.app.id, self.name)
err = "Volume {} already exists in this namespace".format(self.name) # noqa
self.log(err, logging.INFO)
raise AlreadyExists(err)
except KubeException as e:
logger.info(e)
try:
kwargs = {
"size": self.size
}
self._scheduler.pvc.create(self.app.id, self.name, **kwargs)
except KubeException as e:
msg = 'There was a problem creating the volume ' \
'{} for {}'.format(self.name, self.app_id)
raise ServiceUnavailable(msg) from e
def detach(self, *args, **kwargs):
try:
# We raise an exception when a volume doesn't exist
self._scheduler.pvc.get(self.app.id, self.name)
self._scheduler.pvc.delete(self.app.id, self.name)
except KubeException as e:
raise ServiceUnavailable("Could not delete volume {} for application \
{}".format(name, self.app_id)) from e # noqa
def log(self, message, level=logging.INFO):
"""Logs a message in the context of this service.
This prefixes log messages with an application "tag" that the customized
drycc-logspout will be on the lookout for. When it's seen, the message-- usually
an application event of some sort like releasing or scaling, will be considered
as "belonging" to the application instead of the controller and will be handled
accordingly.
"""
logger.log(level, "[{}]: {}".format(self.id, message))