-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
tasks.py
414 lines (356 loc) · 15.5 KB
/
tasks.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import os
from typing import Any, Mapping, NamedTuple, Optional, Sequence
import dagster._check as check
import requests
from dagster._utils.backoff import backoff
from dagster._utils.merger import merge_dicts
class DagsterEcsTaskDefinitionConfig(
NamedTuple(
"_DagsterEcsTaskDefinitionConfig",
[
("family", str),
("image", str),
("container_name", str),
("command", Optional[Sequence[str]]),
("log_configuration", Optional[Mapping[str, Any]]),
("secrets", Sequence[Mapping[str, str]]),
("environment", Sequence[Mapping[str, str]]),
("execution_role_arn", Optional[str]),
("task_role_arn", Optional[str]),
("sidecars", Sequence[Mapping[str, Any]]),
("requires_compatibilities", Sequence[str]),
("cpu", str),
("memory", str),
("ephemeral_storage", Optional[int]),
("runtime_platform", Mapping[str, Any]),
("mount_points", Sequence[Mapping[str, Any]]),
("volumes", Sequence[Mapping[str, Any]]),
("repository_credentials", Optional[str]),
("linux_parameters", Optional[Mapping[str, Any]]),
("health_check", Optional[Mapping[str, Any]]),
],
)
):
"""All the information that Dagster needs to create a task definition and compare two task
definitions to see if they should be reused.
"""
def __new__(
cls,
family: str,
image: str,
container_name: str,
command: Optional[Sequence[str]],
log_configuration: Optional[Mapping[str, Any]],
secrets: Optional[Sequence[Mapping[str, str]]],
environment: Optional[Sequence[Mapping[str, str]]],
execution_role_arn: Optional[str],
task_role_arn: Optional[str],
sidecars: Optional[Sequence[Mapping[str, Any]]],
requires_compatibilities: Optional[Sequence[str]],
cpu: Optional[str] = None,
memory: Optional[str] = None,
ephemeral_storage: Optional[int] = None,
runtime_platform: Optional[Mapping[str, Any]] = None,
mount_points: Optional[Sequence[Mapping[str, Any]]] = None,
volumes: Optional[Sequence[Mapping[str, Any]]] = None,
repository_credentials: Optional[str] = None,
linux_parameters: Optional[Mapping[str, Any]] = None,
health_check: Optional[Mapping[str, Any]] = None,
):
return super(DagsterEcsTaskDefinitionConfig, cls).__new__(
cls,
check.str_param(family, "family"),
check.str_param(image, "image"),
check.str_param(container_name, "container_name"),
check.opt_sequence_param(command, "command"),
check.opt_mapping_param(log_configuration, "log_configuration"),
sorted(check.opt_sequence_param(secrets, "secrets"), key=lambda s: s["name"]),
sorted(check.opt_sequence_param(environment, "environment"), key=lambda e: e["name"]),
check.opt_str_param(execution_role_arn, "execution_role_arn"),
check.opt_str_param(task_role_arn, "task_role_arn"),
check.opt_sequence_param(sidecars, "sidecars"),
check.opt_sequence_param(requires_compatibilities, "requires_compatibilities"),
check.opt_str_param(cpu, "cpu", default="256"),
check.opt_str_param(memory, "memory", default="512"),
check.opt_int_param(ephemeral_storage, "ephemeral_storage"),
check.opt_mapping_param(runtime_platform, "runtime_platform"),
check.opt_sequence_param(mount_points, "mount_points"),
check.opt_sequence_param(volumes, "volumes"),
check.opt_str_param(repository_credentials, "repository_credentials"),
check.opt_mapping_param(linux_parameters, "linux_parameters"),
check.opt_mapping_param(health_check, "health_check"),
)
def task_definition_dict(self):
kwargs = dict(
family=self.family,
requiresCompatibilities=self.requires_compatibilities,
networkMode="awsvpc",
containerDefinitions=[
merge_dicts(
{
"name": self.container_name,
"image": self.image,
},
(
{"logConfiguration": self.log_configuration}
if self.log_configuration
else {}
),
({"command": self.command} if self.command else {}),
({"secrets": self.secrets} if self.secrets else {}),
({"environment": self.environment} if self.environment else {}),
({"mountPoints": self.mount_points} if self.mount_points else {}),
(
{
"repositoryCredentials": {
"credentialsParameter": self.repository_credentials
}
}
if self.repository_credentials
else {}
),
({"linuxParameters": self.linux_parameters} if self.linux_parameters else {}),
({"healthCheck": self.health_check} if self.health_check else {}),
),
*self.sidecars,
],
cpu=self.cpu,
memory=self.memory,
)
if self.execution_role_arn:
kwargs.update(dict(executionRoleArn=self.execution_role_arn))
if self.task_role_arn:
kwargs.update(dict(taskRoleArn=self.task_role_arn))
if self.runtime_platform:
kwargs.update(dict(runtimePlatform=self.runtime_platform))
if self.ephemeral_storage:
kwargs.update(dict(ephemeralStorage={"sizeInGiB": self.ephemeral_storage}))
if self.volumes:
kwargs.update(dict(volumes=self.volumes))
return kwargs
@staticmethod
def from_task_definition_dict(task_definition_dict, container_name):
matching_container_defs = [
container
for container in task_definition_dict["containerDefinitions"]
if container["name"] == container_name
]
if not matching_container_defs:
raise Exception(f"No container in task definition with expected name {container_name}")
container_definition = matching_container_defs[0]
sidecars = [
container
for container in task_definition_dict["containerDefinitions"]
if container["name"] != container_name
]
return DagsterEcsTaskDefinitionConfig(
family=task_definition_dict["family"],
image=container_definition["image"],
container_name=container_name,
command=container_definition.get("command"),
log_configuration=container_definition.get("logConfiguration"),
secrets=container_definition.get("secrets"),
environment=container_definition.get("environment"),
execution_role_arn=task_definition_dict.get("executionRoleArn"),
task_role_arn=task_definition_dict.get("taskRoleArn"),
sidecars=sidecars,
requires_compatibilities=task_definition_dict.get("requiresCompatibilities"),
cpu=task_definition_dict.get("cpu"),
memory=task_definition_dict.get("memory"),
ephemeral_storage=task_definition_dict.get("ephemeralStorage", {}).get("sizeInGiB"),
runtime_platform=task_definition_dict.get("runtimePlatform"),
mount_points=container_definition.get("mountPoints"),
volumes=task_definition_dict.get("volumes"),
repository_credentials=container_definition.get("repositoryCredentials", {}).get(
"credentialsParameter"
),
linux_parameters=container_definition.get("linuxParameters"),
health_check=container_definition.get("healthCheck"),
)
# 9 retries polls for up to 51.1 seconds with exponential backoff.
BACKOFF_RETRIES = 9
# The ECS API is eventually consistent:
# https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RunTask.html
# describe_tasks might initially return nothing even if a task exists.
class EcsEventualConsistencyTimeout(Exception):
pass
class EcsNoTasksFound(Exception):
pass
def get_task_definition_dict_from_current_task(
ecs,
family,
current_task,
image,
container_name,
environment,
command=None,
secrets=None,
include_sidecars=False,
task_role_arn=None,
execution_role_arn=None,
runtime_platform=None,
cpu=None,
memory=None,
ephemeral_storage=None,
mount_points=None,
volumes=None,
additional_sidecars=None,
repository_credentials=None,
):
current_container_name = current_ecs_container_name()
current_task_definition_arn = current_task["taskDefinitionArn"]
current_task_definition_dict = ecs.describe_task_definition(
taskDefinition=current_task_definition_arn
)["taskDefinition"]
current_container_definition = next(
iter(
[
container
for container in current_task_definition_dict["containerDefinitions"]
if container["name"] == current_container_name
]
)
)
# Don't automatically include health check - may be specific to the current task
current_container_definition = {
key: val for key, val in current_container_definition.items() if key != "healthCheck"
}
# Start with the current process's task's definition but remove
# extra keys that aren't useful for creating a new task definition
# (status, revision, etc.)
expected_keys = [
key for key in ecs.meta.service_model.shape_for("RegisterTaskDefinitionRequest").members
]
task_definition = dict(
(key, current_task_definition_dict[key])
for key in expected_keys
if key in current_task_definition_dict.keys()
)
# The current process might not be running in a container that has the
# job's code installed. Inherit most of the process's container
# definition (things like environment, dependencies, etc.) but replace
# the image with the job origin's image and give it a new name.
# Also remove entryPoint. We plan to set containerOverrides. If both
# entryPoint and containerOverrides are specified, they're concatenated
# and the command will fail
# https://aws.amazon.com/blogs/opensource/demystifying-entrypoint-cmd-docker/
new_container_definition = {
**current_container_definition,
"name": container_name,
"image": image,
"entryPoint": [],
"command": command if command else [],
**(
{"repositoryCredentials": {"credentialsParameter": repository_credentials}}
if repository_credentials
else {}
),
**({"secrets": secrets} if secrets else {}),
**({} if include_sidecars else {"dependsOn": []}),
}
if environment:
new_container_definition["environment"] = [
*new_container_definition["environment"],
*environment,
]
if mount_points:
new_container_definition["mountPoints"] = (
new_container_definition.get("mountPoints", []) + mount_points
)
if include_sidecars:
# Start with all the sidecars
container_definitions = [
container_definition
for container_definition in current_task_definition_dict.get("containerDefinitions", [])
if container_definition["name"] != current_container_name
]
# add the adjusted container based on the current container
container_definitions.append(new_container_definition)
else:
container_definitions = [new_container_definition]
if additional_sidecars:
container_definitions = [*container_definitions, *additional_sidecars]
task_definition = {
**task_definition,
"family": family,
"containerDefinitions": container_definitions,
**({"taskRoleArn": task_role_arn} if task_role_arn else {}),
**({"executionRoleArn": execution_role_arn} if execution_role_arn else {}),
**({"runtimePlatform": runtime_platform} if runtime_platform else {}),
**({"cpu": cpu} if cpu else {}),
**({"memory": memory} if memory else {}),
**({"ephemeralStorage": {"sizeInGiB": ephemeral_storage}} if ephemeral_storage else {}),
}
if volumes:
task_definition["volumes"] = task_definition.get("volumes", []) + volumes
return task_definition
class CurrentEcsTaskMetadata(
NamedTuple("_CurrentEcsTaskMetadata", [("cluster", str), ("task_arn", str)])
):
pass
def get_current_ecs_task_metadata() -> CurrentEcsTaskMetadata:
task_metadata_uri = _container_metadata_uri() + "/task"
response = requests.get(task_metadata_uri).json()
cluster = response.get("Cluster")
task_arn = response.get("TaskARN")
return CurrentEcsTaskMetadata(cluster=cluster, task_arn=task_arn)
def _container_metadata_uri():
"""Get the metadata uri for the current ECS task.
ECS injects an environment variable into each Fargate task. The value
of this environment variable is a url that can be queried to introspect
information about the current processes's running task:
https://docs.aws.amazon.com/AmazonECS/latest/userguide/task-metadata-endpoint-v4-fargate.html
"""
return os.environ.get("ECS_CONTAINER_METADATA_URI_V4")
def current_ecs_container_name():
return requests.get(_container_metadata_uri()).json()["Name"]
def get_current_ecs_task(ecs, task_arn, cluster):
def describe_task_or_raise(task_arn, cluster):
try:
return ecs.describe_tasks(tasks=[task_arn], cluster=cluster)["tasks"][0]
except IndexError:
raise EcsNoTasksFound
try:
task = backoff(
describe_task_or_raise,
retry_on=(EcsNoTasksFound,),
kwargs={"task_arn": task_arn, "cluster": cluster},
max_retries=BACKOFF_RETRIES,
)
except EcsNoTasksFound:
raise EcsEventualConsistencyTimeout
return task
def get_task_kwargs_from_current_task(
ec2,
cluster,
task,
):
run_task_kwargs = {"cluster": cluster}
if not task.get("capacityProviderStrategy"):
run_task_kwargs["launchType"] = task.get("launchType") or "FARGATE"
else:
run_task_kwargs["capacityProviderStrategy"] = task.get("capacityProviderStrategy")
if run_task_kwargs["launchType"] != "EXTERNAL":
enis = []
subnets = []
for attachment in task["attachments"]:
if attachment["type"] == "ElasticNetworkInterface":
for detail in attachment["details"]:
if detail["name"] == "subnetId":
subnets.append(detail["value"])
if detail["name"] == "networkInterfaceId":
enis.append(ec2.NetworkInterface(detail["value"]))
public_ip = False
security_groups = []
for eni in enis:
if (eni.association_attribute or {}).get("PublicIp"):
public_ip = True
for group in eni.groups:
security_groups.append(group["GroupId"])
aws_vpc_config = {
"subnets": subnets,
"assignPublicIp": "ENABLED" if public_ip else "DISABLED",
"securityGroups": security_groups,
}
run_task_kwargs["networkConfiguration"] = {"awsvpcConfiguration": aws_vpc_config}
return run_task_kwargs