/
stack.py
268 lines (209 loc) · 9.29 KB
/
stack.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
import logging
from .framework import datastore
from . import dependencies
from . import resource
from . import sync_point
from . import template
logger = logging.getLogger('stack')
stacks = datastore.Datastore('Stack',
'key', 'name', 'tmpl_key', 'prev_tmpl_key',
'current_traversal', 'current_deps')
class Stack(object):
'''
Class to represent a Stack.
This is the equivalent of the heat.engine.stack.Stack class in this
simulation.
'''
def __init__(self, name, tmpl, prev_tmpl_key=None, current_traversal=0,
current_deps=tuple(), key=None):
self.key = key
self.tmpl = tmpl
self.data = {
'name': name,
'tmpl_key': tmpl.key,
'prev_tmpl_key': prev_tmpl_key,
'current_traversal': current_traversal,
'current_deps': current_deps,
}
def __str__(self):
return '<Stack %r>' % self.key
@classmethod
def load(cls, key):
s = stacks.read(key)
return cls(s.name, template.Template.load(s.tmpl_key), s.prev_tmpl_key,
s.current_traversal, s.current_deps, key=s.key)
@classmethod
def load_by_name(cls, stack_name):
candidates = list(stacks.find(name=stack_name))
if not candidates:
raise stacks.NotFound('Stack "%s" not found' % stack_name)
assert len(candidates) == 1, 'Multiple stacks "%s" found' % stack_name
return cls.load(candidates[0])
def store(self):
if self.key is None:
self.key = stacks.create(**self.data)
else:
stacks.update(self.key, **self.data)
@property
def current_traversal(self):
return self.data['current_traversal']
@property
def current_deps(self):
return dependencies.Dependencies(self.data['current_deps'])
def create(self):
self.store()
self._create_or_update()
def update(self, tmpl):
old_tmpl, self.tmpl = self.tmpl, tmpl
self.data['tmpl_key'] = tmpl.key
logger.info('[%s(%d)] Updating...' % (self.data['name'], self.key))
self._create_or_update(old_tmpl.key)
@staticmethod
def _dependencies(existing_resources,
current_template_deps, current_resources):
'''
Return the Dependencies graph for a traversal.
There is a node for each resource that appears in the current template
(to be traversed in the forward direction for updates) and for each
existing resource (to be traversed in the reverse direction for
cleanup.
'''
def make_graph_key(res_name):
return (resource.GraphKey(res_name,
current_resources[res_name].key),
True)
deps = current_template_deps.translate(make_graph_key)
for key, rsrc in existing_resources.items():
deps += (key, False), None
# Note: reversed edges as this is the cleanup part of the graph
for requirement in rsrc.requirements:
if requirement in existing_resources:
deps += (requirement, False), (key, False)
if rsrc.replaces in existing_resources:
deps += ((resource.GraphKey(rsrc.name,
rsrc.replaces), False),
(key, False))
if rsrc.name in current_template_deps:
deps += (key, False), make_graph_key(rsrc.name)
return deps
def delete(self):
old_tmpl, self.tmpl = self.tmpl, template.Template()
self.data['tmpl_key'] = None
logger.info('[%s(%d)] Deleting...' % (self.data['name'], self.key))
self._create_or_update(old_tmpl.key)
def rollback(self):
'''
Roll the Stack back to the previously successful template.
This essentially performs an update with the most recent template to
successfully complete (where 'complete' currently means that all
desired resources are in service, not necessarily that any extras have
been cleaned up.
'''
old_tmpl_key = self.data['prev_tmpl_key']
if old_tmpl_key == self.tmpl.key:
# Nothing to roll back
return
if old_tmpl_key is None:
self.tmpl = template.Template()
else:
self.tmpl = template.Template.load(old_tmpl_key)
current_tmpl_key = self.data['tmpl_key']
self.data['tmpl_key'] = old_tmpl_key
logger.info('[%s(%d)] Rolling back to template %s',
self.data['name'], self.key, old_tmpl_key)
self._create_or_update(current_tmpl_key)
def _create_or_update(self, current_tmpl_key=None):
'''
Update the stack.
Create and Delete are special cases of Update, where the previous or
new templates (respectively) are empty.
'''
previous_traversal = self.current_traversal
self.data['current_traversal'] += 1
self.store()
sync_point.delete_all(self.key, previous_traversal)
definitions = self.tmpl.resources
tmpl_deps = self.tmpl.dependencies()
logger.debug('[%s(%d)] Dependencies: %s' % (self.data['name'],
self.key,
tmpl_deps.graph()))
# Load all extant resources from the DB
ext_rsrcs = set(resource.Resource.load_all_from_stack(self))
def key(r):
'''Return the GraphKey for a resource name.'''
return resource.GraphKey(r, rsrcs[r].key)
def best_existing_resource(rsrc_name):
'''Return the best existing version of resource we want to keep.'''
candidate = None
for rsrc in ext_rsrcs:
if rsrc.name != rsrc_name:
continue
if rsrc.template_key == self.tmpl.key:
# BINGO! Rollback where the previous resource still exists
return rsrc
elif rsrc.template_key == current_tmpl_key:
# Current resource is otherwise a good candidate
candidate = rsrc
return candidate
def get_resource(rsrc_name):
'''
Return the resource with the given name, creating it if necessary.
This is the resource that will go into the update part of the
graph. We also ensure its dependencies are updated to reflect the
new template.
'''
rsrc = best_existing_resource(rsrc_name)
if rsrc is None:
rsrc = resource.Resource(rsrc_name, self,
definitions[rsrc_name], self.tmpl.key)
rqrs = set(key(r) for r in tmpl_deps.required_by(rsrc_name))
rsrc.requirers = rsrc.requirers | rqrs
return rsrc
# Resources that will form the update part of the graph
rsrcs = {}
for rsrc_name in reversed(tmpl_deps):
rsrc = get_resource(rsrc_name)
rsrc.store()
rsrcs[rsrc_name] = rsrc
# Generate the entire graph
dependencies = self._dependencies({resource.GraphKey(r.name, r.key): r
for r in ext_rsrcs},
tmpl_deps, rsrcs)
graph = dependencies.graph()
# Create SyncPoints to measure progress
for resource_key, forward in dependencies:
sync_point.create(sync_point.make_key(resource_key.key,
self.current_traversal,
'update' if forward
else 'cleanup'),
self.key)
roots = set(key for (key, fwd), node in graph.items()
if fwd and not any(f for k, f in node.required_by()))
sync_point.create(sync_point.make_key(self.key,
self.current_traversal),
self.key)
self.data['current_deps'] = tuple(dependencies.edges())
self.store()
# Start the traversal by sending notifications to the leaf nodes
from . import processes
for graph_key, forward in dependencies.leaves():
processes.converger.check_resource(graph_key,
self.current_traversal,
{}, forward)
def mark_complete(self, traversal_id):
'''
Mark the update as complete.
This currently occurs when all resources have been updated; there may
still be resources being cleaned up, but the Stack should now be in
service.
'''
if traversal_id != self.current_traversal:
return
logger.info('[%s(%d)] update traversal %d complete',
self.data['name'], self.key, traversal_id)
prev_prev_key = self.data['prev_tmpl_key']
self.data['prev_tmpl_key'] = self.data['tmpl_key']
self.store()
if (prev_prev_key is not None and
prev_prev_key != self.data['tmpl_key']):
template.templates.delete(prev_prev_key)