-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconfig.py
More file actions
185 lines (165 loc) · 7.81 KB
/
config.py
File metadata and controls
185 lines (165 loc) · 7.81 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
import logging
from django.db import models
from django.contrib.auth import get_user_model
from api.utils import dict_diff
from api.exceptions import DryccException, UnprocessableEntity
from .release import Release
from .base import UuidAuditedModel
User = get_user_model()
logger = logging.getLogger(__name__)
class Config(UuidAuditedModel):
"""
Set of configuration values applied as environment variables
during runtime execution of the Application.
"""
ptype_fields = ("lifecycle_post_start", "lifecycle_pre_stop", "tags", "limits",
"typed_values", "healthcheck", "termination_grace_period")
allof_fields = ("values", "registry") + ptype_fields
owner = models.ForeignKey(User, on_delete=models.PROTECT)
app = models.ForeignKey('App', on_delete=models.CASCADE)
values = models.JSONField(default=dict, blank=True)
typed_values = models.JSONField(default=dict, blank=True)
lifecycle_post_start = models.JSONField(default=dict, blank=True)
lifecycle_pre_stop = models.JSONField(default=dict, blank=True)
tags = models.JSONField(default=dict, blank=True)
limits = models.JSONField(default=dict, blank=True)
registry = models.JSONField(default=dict, blank=True)
healthcheck = models.JSONField(default=dict, blank=True)
termination_grace_period = models.JSONField(default=dict, blank=True)
class Meta:
get_latest_by = 'created'
ordering = ['-created']
unique_together = (('app', 'uuid'),)
def __str__(self):
return "{}-{}".format(self.app.id, str(self.uuid)[:7])
def previous(self):
"""
Return the previous Config to this one.
:return: the previous :class:`Config`, or None
"""
configs = self.app.config_set
if self.pk:
configs = configs.exclude(pk=self.pk)
try:
# Get the Config previous to this one
prev_config = configs.latest()
except Config.DoesNotExist:
prev_config = None
return prev_config
def diff(self, config=None):
old_config = config if config else self.previous()
result = {}
for field in self.allof_fields:
result[field] = dict_diff(getattr(self, field), getattr(old_config, field))
return result
def save(self, **kwargs):
"""merge the old config with the new"""
try:
# Get config from the latest available release
try:
previous_config = self.app.release_set.filter(failed=False).latest().config
except Release.DoesNotExist:
# If that doesn't exist then fallback on app config
# usually means a totally new app
previous_config = self.app.config_set.latest()
for attr in ['registry', 'values', 'lifecycle_post_start',
'lifecycle_pre_stop', 'termination_grace_period']:
data = getattr(previous_config, attr, {}).copy()
new_data = getattr(self, attr, {}).copy()
self._merge_data(attr, data, new_data)
setattr(self, attr, data)
self._set_typed_values(previous_config)
self._set_limits(previous_config)
self._set_healthcheck(previous_config)
self._set_registry()
self._set_tags(previous_config)
except Config.DoesNotExist:
self._set_tags(previous_config={'tags': {}})
return super(Config, self).save(**kwargs)
def _merge_data(self, field, data, new_data):
# remove config keys if a null value is provided
for key, value in new_data.items():
if value is None:
# error if unsetting non-existing key
if key not in data:
raise UnprocessableEntity('{} does not exist under {}'.format(key, field)) # noqa
data.pop(key)
else:
data[key] = value
return data
def _set_registry(self):
# lower case all registry options for consistency
self.registry = {key.lower(): value for key, value in self.registry.copy().items()}
# PORT must be set if private registry is being used
if self.registry and self.values.get('PORT', None) is None:
# only thing that can get past post_save in the views
raise DryccException(
'PORT needs to be set in the config '
'when using a private registry')
def _set_tags(self, previous_config):
"""verify the tags exist on any nodes as labels"""
data = getattr(previous_config, 'tags', {}).copy()
new_data = getattr(self, 'tags', {}).copy()
# remove config keys if a null value is provided
for ptype, values in new_data.items():
if not values:
# error if unsetting non-existing key
if ptype not in data:
raise UnprocessableEntity(
'{} does not exist under {}'.format(ptype, 'tags'))
data.pop(ptype)
else:
if not self.scheduler().node.get(labels=values).json()['items']:
labels = ['{}={}'.format(key, value) for key, value in values.items()]
message = 'No nodes matched the provided labels: {}'.format(', '.join(labels))
# Find out if there are any other tags around
old_tags = previous_config.tags.get(ptype, {})
if old_tags:
old = ['{}={}'.format(key, value) for key, value in old_tags.items()]
new = set(labels) - set(old)
if new:
message += ' - Addition of {} is the cause'.format(', '.join(new))
raise DryccException(message)
data[ptype] = self._merge_data(
'tags', data.get(ptype, {}), values)
setattr(self, 'tags', data)
def _set_limits(self, previous_config):
data = getattr(previous_config, 'limits', {}).copy()
new_data = getattr(self, 'limits', {}).copy()
# check procfile
for key, value in new_data.items():
if value is None:
if key in self.app.ptypes:
raise UnprocessableEntity(
"the %s has already been used and cannot be deleted" % key)
self._merge_data('limits', data, new_data)
setattr(self, 'limits', data)
def _set_healthcheck(self, previous_config):
data = getattr(previous_config, 'healthcheck', {}).copy()
new_data = getattr(self, 'healthcheck', {}).copy()
# remove config keys if a null value is provided
for key, value in new_data.items():
if value is None:
# error if unsetting non-existing key
if key not in data:
raise UnprocessableEntity(
'{} does not exist under {}'.format(key, 'healthcheck'))
data.pop(key)
else:
data[key] = self._merge_data('healthcheck', data.get(key, {}), value)
setattr(self, 'healthcheck', data)
def _set_typed_values(self, previous_config):
data = getattr(previous_config, 'typed_values', {}).copy()
new_data = getattr(self, 'typed_values', {}).copy()
# remove config keys if a null value is provided
for ptype, values in new_data.items():
if not values:
# error if unsetting non-existing key
if ptype not in data:
raise UnprocessableEntity(
'{} does not exist under {}'.format(ptype, 'typed_values'))
data.pop(ptype)
else:
data[ptype] = self._merge_data(
'typed_values', data.get(ptype, {}), values)
setattr(self, 'typed_values', data)