/
stack.py
281 lines (225 loc) 路 9.63 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
269
270
271
272
273
274
275
276
277
278
279
280
281
# Copyright 2016-2018, Pulumi Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Support for automatic stack components.
"""
import asyncio
from inspect import isawaitable
from typing import Callable, Any, Dict, List, TYPE_CHECKING
from ..resource import ComponentResource, Resource, ResourceTransformation
from .settings import (
get_project,
get_stack,
get_root_resource,
is_dry_run,
set_root_resource,
)
from .rpc_manager import RPC_MANAGER
from .sync_await import _all_tasks, _get_current_task
from .. import log
if TYPE_CHECKING:
from .. import Output
def _get_running_tasks() -> List[asyncio.Task]:
pending = []
for task in _all_tasks():
# Don't kill ourselves, that would be silly.
if not task == _get_current_task():
pending.append(task)
return pending
async def run_pulumi_func(func: Callable):
try:
func()
finally:
await wait_for_rpcs()
# By now, all tasks have exited and we're good to go.
log.debug("run_pulumi_func completed")
async def wait_for_rpcs(await_all_outstanding_tasks=True) -> None:
log.debug("Waiting for outstanding RPCs to complete")
while True:
# Pump the event loop, giving all of the RPCs that we just queued up time to fully execute.
# The asyncio scheduler does not expose a "yield" primitive, so this will have to do.
#
# Note that "asyncio.sleep(0)" is the blessed way to do this:
# https://github.com/python/asyncio/issues/284#issuecomment-154180935
#
# We await each RPC in turn so that this loop will actually block rather than busy-wait.
while len(RPC_MANAGER.rpcs) > 0:
await asyncio.sleep(0)
log.debug(
f"waiting for quiescence; {len(RPC_MANAGER.rpcs)} RPCs outstanding"
)
await RPC_MANAGER.rpcs.pop()
if RPC_MANAGER.unhandled_exception is not None:
raise RPC_MANAGER.unhandled_exception.with_traceback(
RPC_MANAGER.exception_traceback
)
log.debug("RPCs successfully completed")
# If the RPCs have successfully completed, now await all remaining outstanding tasks.
if await_all_outstanding_tasks:
outstanding_tasks = _get_running_tasks()
if len(outstanding_tasks) == 0:
log.debug("No outstanding tasks to complete")
else:
log.debug(
f"Waiting for {len(outstanding_tasks)} outstanding tasks to complete"
)
done, pending = await asyncio.wait(
outstanding_tasks, return_when="FIRST_EXCEPTION"
)
if len(pending) > 0:
# If there are any pending tasks, it's because an exception was thrown.
# Cancel any pending tasks.
log.debug(f"Cancelling {len(pending)} remaining tasks.")
for task in pending:
task.cancel()
for task in done:
exception = task.exception()
if exception is not None:
log.debug(
"A future resolved in an exception, raising exception."
)
raise exception
log.debug("All outstanding tasks completed.")
# Check to see if any more RPCs have been scheduled, and repeat the cycle if so.
# Break if no RPCs remain.
if len(RPC_MANAGER.rpcs) == 0:
break
async def run_in_stack(func: Callable):
"""
Run the given function inside of a new stack resource. This ensures that any stack export calls
will end up as output properties on the resulting stack component in the checkpoint file. This
is meant for internal runtime use only and is used by the Python SDK entrypoint program.
"""
await run_pulumi_func(lambda: Stack(func))
class Stack(ComponentResource):
"""
A synthetic stack component that automatically parents resources as the program runs.
"""
outputs: Dict[str, Any]
def __init__(self, func: Callable) -> None:
# Ensure we don't already have a stack registered.
if get_root_resource() is not None:
raise Exception("Only one root Pulumi Stack may be active at once")
# Now invoke the registration to begin creating this resource.
name = f"{get_project()}-{get_stack()}"
super().__init__("pulumi:pulumi:Stack", name, None, None)
# Invoke the function while this stack is active and then register its outputs.
self.outputs = {}
set_root_resource(self)
try:
func()
finally:
self.register_outputs(massage(self.outputs, []))
# Intentionally leave this resource installed in case subsequent async work uses it.
def output(self, name: str, value: Any):
"""
Export a stack output with a given name and value.
"""
self.outputs[name] = value
# Note: we use a List here instead of a set as many objects are unhashable. This is inefficient,
# but python seems to offer no alternative.
def massage(attr: Any, seen: List[Any]):
"""
massage takes an arbitrary python value and attempts to *deeply* convert it into
plain-old-python-value that can registered as an output. In general, this means leaving alone
things like strings, ints, bools. However, it does mean trying to make other values into either
lists or dictionaries as appropriate. In general, iterable things are turned into lists, and
dictionary-like things are turned into dictionaries.
"""
from .. import Output # pylint: disable=import-outside-toplevel
# Basic primitive types (numbers, booleans, strings, etc.) don't need any special handling.
if is_primitive(attr):
return attr
if isinstance(attr, Output):
return attr.apply(lambda v: massage(v, seen))
if isawaitable(attr):
return Output.from_input(attr).apply(lambda v: massage(v, seen))
# from this point on, we have complex objects. If we see them again, we don't want to emit them
# again fully or else we'd loop infinitely.
if reference_contains(attr, seen):
if isinstance(attr, Resource):
return massage(attr.urn, seen)
# otherwise just emit as nothing to stop the looping.
return None
try:
seen.append(attr)
return massage_complex(attr, seen)
finally:
popped = seen.pop()
if popped is not attr:
raise Exception("Invariant broken when processing stack outputs")
def massage_complex(attr: Any, seen: List[Any]) -> Any:
def is_public_key(key: str) -> bool:
return not key.startswith("_")
def serialize_all_keys(include: Callable[[str], bool]):
plain_object: Dict[str, Any] = {}
for key in attr.__dict__.keys():
if include(key):
plain_object[key] = massage(attr.__dict__[key], seen)
return plain_object
if isinstance(attr, Resource):
serialized_attr = serialize_all_keys(is_public_key)
# In preview only, we mark the result with "@isPulumiResource" to indicate that it is derived
# from a resource. This allows the engine to perform resource-specific filtering of unknowns
# from output diffs during a preview. This filtering is not necessary during an update because
# all property values are known.
return (
serialized_attr
if not is_dry_run()
else {**serialized_attr, "@isPulumiResource": True}
)
# first check if the value is an actual dictionary. If so, massage the values of it to deeply
# make sure this is a popo.
if isinstance(attr, dict):
# Don't use attr.items() here, as it will error in the case of outputs with an `items` property.
return {
key: massage(attr[key], seen) for key in attr if not key.startswith("_")
}
if hasattr(attr, "__iter__"):
return [massage(item, seen) for item in attr]
return serialize_all_keys(is_public_key)
def reference_contains(val1: Any, seen: List[Any]) -> bool:
for val2 in seen:
if val1 is val2:
return True
return False
def is_primitive(attr: Any) -> bool:
if attr is None:
return True
if isinstance(attr, str):
return True
# dictionaries, lists and dictionary-like things are not primitive.
if isinstance(attr, dict):
return False
if hasattr(attr, "__dict__"):
return False
try:
iter(attr)
return False
except TypeError:
pass
return True
def register_stack_transformation(t: ResourceTransformation):
"""
Add a transformation to all future resources constructed in this Pulumi stack.
"""
root_resource = get_root_resource()
if root_resource is None:
raise Exception(
"The root stack resource was referenced before it was initialized."
)
if root_resource._transformations is None:
root_resource._transformations = [t]
else:
root_resource._transformations = root_resource._transformations + [t]