-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathutils.py
More file actions
595 lines (491 loc) · 19.4 KB
/
utils.py
File metadata and controls
595 lines (491 loc) · 19.4 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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
"""Utility functions for AWS operations and CLI helpers."""
from __future__ import annotations
import json
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Any, Literal
import boto3
import typer
from botocore.exceptions import ClientError, NoCredentialsError
from rich.console import Console
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.table import Table
from typing_extensions import overload
from prefect.client.orchestration import get_client
from prefect.client.schemas.actions import WorkPoolUpdate
try:
from importlib.resources import files
except ImportError:
# Python < 3.9 fallback
from importlib_resources import files
if TYPE_CHECKING:
from mypy_boto3_cloudformation.client import CloudFormationClient
from mypy_boto3_ec2.client import EC2Client
from mypy_boto3_ecs.client import ECSClient
from mypy_boto3_sts.client import STSClient
console = Console()
# Tags that identify stacks deployed by this CLI
CLI_TAGS = {
"ManagedBy": "prefect-aws-cli",
"DeploymentType": "ecs-worker",
}
def get_template_path(template_name: str) -> str:
"""Get the path to a CloudFormation template.
Args:
template_name: Name of the template file (e.g., 'service.json')
Returns:
Path to the template file
"""
try:
template_files = files("prefect_aws.templates.ecs")
template_path = template_files / template_name
return str(template_path)
except (ImportError, FileNotFoundError) as e:
typer.echo(f"Error: Could not find template {template_name}: {e}", err=True)
raise typer.Exit(1)
def load_template(template_name: str) -> dict[str, Any]:
"""Load a CloudFormation template from the templates directory.
Args:
template_name: Name of the template file
Returns:
Template content as a dictionary
"""
try:
template_files = files("prefect_aws.templates.ecs")
template_content = (template_files / template_name).read_text()
return json.loads(template_content)
except (ImportError, FileNotFoundError, json.JSONDecodeError) as e:
typer.echo(f"Error loading template {template_name}: {e}", err=True)
raise typer.Exit(1)
@overload
def get_aws_client(
service: Literal["sts"],
region: str | None = None,
profile: str | None = None,
) -> STSClient:
pass
@overload
def get_aws_client(
service: Literal["ecs"],
region: str | None = None,
profile: str | None = None,
) -> ECSClient:
pass
@overload
def get_aws_client(
service: Literal["cloudformation"],
region: str | None = None,
profile: str | None = None,
) -> CloudFormationClient:
pass
@overload
def get_aws_client(
service: Literal["ec2"],
region: str | None = None,
profile: str | None = None,
) -> EC2Client:
pass
def get_aws_client(
service: Literal["sts", "ecs", "cloudformation", "ec2"],
region: str | None = None,
profile: str | None = None,
):
"""Get an AWS client with error handling.
Args:
service: AWS service name (e.g., 'cloudformation', 'ecs')
region: AWS region
profile: AWS profile name
Returns:
Boto3 client
"""
try:
session = boto3.Session(profile_name=profile, region_name=region)
return session.client(service)
except NoCredentialsError:
typer.echo(
"Error: AWS credentials not found. Please configure your credentials.",
err=True,
)
raise typer.Exit(1)
except Exception as e:
typer.echo(f"Error creating AWS client: {e}", err=True)
raise typer.Exit(1)
def validate_aws_credentials(
region: str | None = None, profile: str | None = None
) -> bool:
"""Validate that AWS credentials are available and working.
Args:
region: AWS region
profile: AWS profile name
Returns:
True if credentials are valid
"""
try:
sts = get_aws_client("sts", region, profile)
sts.get_caller_identity()
return True
except Exception:
return False
def add_cli_tags(work_pool_name: str, stack_type: str) -> list[dict[str, str]]:
"""Add CLI-specific tags to a CloudFormation stack.
Args:
work_pool_name: Name of the work pool
stack_type: Type of stack ('service' or 'events')
Returns:
List of tags for CloudFormation
"""
tags = [{"Key": k, "Value": v} for k, v in CLI_TAGS.items()]
tags.extend(
[
{"Key": "StackType", "Value": stack_type},
{"Key": "WorkPoolName", "Value": work_pool_name},
{"Key": "CreatedAt", "Value": datetime.now(timezone.utc).isoformat()},
]
)
return tags
def list_cli_deployed_stacks(cf_client) -> list[dict[str, Any]]:
"""List all stacks deployed by this CLI.
Args:
cf_client: CloudFormation client
Returns:
List of stack information dictionaries
"""
try:
paginator = cf_client.get_paginator("describe_stacks")
cli_stacks = []
for page in paginator.paginate():
for stack in page["Stacks"]:
if stack["StackStatus"] in ["DELETE_COMPLETE"]:
continue
tags = {tag["Key"]: tag["Value"] for tag in stack.get("Tags", [])}
# Check if stack was deployed by CLI
if (
tags.get("ManagedBy") == CLI_TAGS["ManagedBy"]
and tags.get("DeploymentType") == CLI_TAGS["DeploymentType"]
):
stack_info = {
"StackName": stack["StackName"],
"StackStatus": stack["StackStatus"],
"CreationTime": stack["CreationTime"],
"WorkPoolName": tags.get("WorkPoolName", "Unknown"),
"StackType": tags.get("StackType", "Unknown"),
"Description": stack.get("Description", ""),
}
if "LastUpdatedTime" in stack:
stack_info["LastUpdatedTime"] = stack["LastUpdatedTime"]
cli_stacks.append(stack_info)
return cli_stacks
except ClientError as e:
typer.echo(f"Error listing stacks: {e}", err=True)
raise typer.Exit(1)
def validate_stack_is_cli_managed(cf_client, stack_name: str) -> bool:
"""Validate that a stack was deployed by this CLI.
Args:
cf_client: CloudFormation client
stack_name: Name of the stack
Returns:
True if stack was deployed by CLI
"""
try:
response = cf_client.describe_stacks(StackName=stack_name)
stack = response["Stacks"][0]
tags = {tag["Key"]: tag["Value"] for tag in stack.get("Tags", [])}
return (
tags.get("ManagedBy") == CLI_TAGS["ManagedBy"]
and tags.get("DeploymentType") == CLI_TAGS["DeploymentType"]
)
except ClientError:
return False
def deploy_stack(
cf_client,
stack_name: str,
template_body: str,
parameters: list[dict[str, str]],
tags: list[dict[str, str]],
capabilities: list[str] | None = None,
wait: bool = True,
) -> None:
"""Deploy or update a CloudFormation stack.
Args:
cf_client: CloudFormation client
stack_name: Name of the stack
template_body: CloudFormation template as JSON string
parameters: List of parameter dictionaries
tags: List of tag dictionaries
capabilities: IAM capabilities if required
wait: Whether to wait for the stack operation to complete
"""
if capabilities is None:
capabilities = ["CAPABILITY_NAMED_IAM"]
operation = "create"
try:
# Check if stack exists
try:
cf_client.describe_stacks(StackName=stack_name)
stack_exists = True
except ClientError as e:
if e.response.get("Error", {}).get("Code") == "ValidationError":
stack_exists = False
else:
raise
operation = "update" if stack_exists else operation
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
) as progress:
task = progress.add_task(
f"[cyan]{operation.capitalize()}ing stack {stack_name}..."
)
if stack_exists:
try:
cf_client.update_stack(
StackName=stack_name,
TemplateBody=template_body,
Parameters=parameters,
Tags=tags,
Capabilities=capabilities,
)
except ClientError as e:
if "No updates are to be performed" in str(e):
progress.update(
task,
description=f"[green]Stack {stack_name} is already up to date",
)
return
raise
else:
cf_client.create_stack(
StackName=stack_name,
TemplateBody=template_body,
Parameters=parameters,
Tags=tags,
Capabilities=capabilities,
)
if wait:
# Wait for operation to complete
waiter_name = f"stack_{operation}_complete"
waiter = cf_client.get_waiter(waiter_name)
progress.update(
task, description=f"[yellow]Waiting for {operation} to complete..."
)
waiter.wait(
StackName=stack_name, WaiterConfig={"Delay": 10, "MaxAttempts": 120}
)
progress.update(
task,
description=f"[green]Stack {stack_name} {operation}d successfully!",
)
else:
progress.update(
task,
description=f"[green]Stack {stack_name} {operation} initiated. Check status with: prefect-aws ecs-worker status {stack_name}",
)
except ClientError as e:
error_msg = e.response.get("Error", {}).get("Message")
typer.echo(
f"Error performing {operation} action for stack: {error_msg}", err=True
)
raise typer.Exit(1)
def delete_stack(cf_client, stack_name: str, wait: bool = True) -> None:
"""Delete a CloudFormation stack.
Args:
cf_client: CloudFormation client
stack_name: Name of the stack
wait: Whether to wait for the stack deletion to complete
"""
try:
# Validate stack is CLI-managed
if not validate_stack_is_cli_managed(cf_client, stack_name):
typer.echo(
f"Error: Stack '{stack_name}' was not deployed by prefect-aws CLI",
err=True,
)
raise typer.Exit(1)
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
) as progress:
task = progress.add_task(f"[red]Deleting stack {stack_name}...")
cf_client.delete_stack(StackName=stack_name)
if wait:
# Wait for deletion to complete
waiter = cf_client.get_waiter("stack_delete_complete")
progress.update(
task, description="[yellow]Waiting for deletion to complete..."
)
waiter.wait(
StackName=stack_name, WaiterConfig={"Delay": 10, "MaxAttempts": 120}
)
progress.update(
task, description=f"[green]Stack {stack_name} deleted successfully!"
)
else:
progress.update(
task,
description=f"[green]Stack {stack_name} deletion initiated. Check status with: prefect-aws ecs-worker status {stack_name}",
)
except ClientError as e:
if (code := e.response.get("Error", {}).get("Code")) == "ValidationError":
typer.echo(f"Error: Stack '{stack_name}' not found", err=True)
else:
typer.echo(f"Error deleting stack: {code}", err=True)
raise typer.Exit(1)
def get_stack_status(cf_client, stack_name: str) -> dict[str, Any] | None:
"""Get the status of a CloudFormation stack.
Args:
cf_client: CloudFormation client
stack_name: Name of the stack
Returns:
Stack information or None if not found
"""
try:
response = cf_client.describe_stacks(StackName=stack_name)
return response["Stacks"][0]
except ClientError as e:
if e.response.get("Error", {}).get("Code") == "ValidationError":
return None
raise
def display_stacks_table(stacks: list[dict[str, Any]]) -> None:
"""Display stacks in a formatted table.
Args:
stacks: List of stack information dictionaries
"""
if not stacks:
typer.echo("No stacks found deployed by prefect-aws CLI")
return
table = Table(title="ECS Worker Stacks")
table.add_column("Stack Name", style="cyan")
table.add_column("Work Pool", style="green")
table.add_column("Type", style="yellow")
table.add_column("Status", style="magenta")
table.add_column("Created", style="blue")
for stack in stacks:
created_time = stack["CreationTime"].strftime("%B %d, %Y at %I:%M %p UTC")
table.add_row(
stack["StackName"],
stack["WorkPoolName"],
stack["StackType"],
stack["StackStatus"],
created_time,
)
console.print(table)
def validate_ecs_cluster(ecs_client, cluster_identifier: str) -> bool:
"""Validate that an ECS cluster exists.
Args:
ecs_client: ECS client
cluster_identifier: Cluster name or ARN
Returns:
True if cluster exists
"""
try:
response = ecs_client.describe_clusters(clusters=[cluster_identifier])
clusters = response["clusters"]
return len(clusters) > 0 and clusters[0]["status"] == "ACTIVE"
except ClientError:
return False
def validate_vpc_and_subnets(
ec2_client, vpc_id: str, subnet_ids: list[str]
) -> tuple[bool, str]:
"""Validate VPC and subnets exist and are compatible.
Args:
ec2_client: EC2 client
vpc_id: VPC ID
subnet_ids: List of subnet IDs
Returns:
Tuple of (is_valid, error_message)
"""
try:
# Validate VPC exists
vpc_response = ec2_client.describe_vpcs(VpcIds=[vpc_id])
if not vpc_response["Vpcs"]:
return False, f"VPC {vpc_id} not found"
# Validate subnets exist and belong to VPC
subnet_response = ec2_client.describe_subnets(SubnetIds=subnet_ids)
for subnet in subnet_response["Subnets"]:
if subnet["VpcId"] != vpc_id:
return False, f"Subnet {subnet['SubnetId']} is not in VPC {vpc_id}"
return True, ""
except ClientError as e:
return False, str(e)
def update_work_pool_defaults(
work_pool_name: str,
stack_outputs: dict[str, str],
) -> None:
"""Update work pool base job template defaults with stack deployment values.
Only updates fields that are currently empty/null to preserve user customizations.
Args:
work_pool_name: Name of the work pool to update
stack_outputs: CloudFormation stack outputs containing infrastructure values
"""
try:
with get_client(sync_client=True) as client:
# Get current work pool
try:
work_pool = client.read_work_pool(work_pool_name)
except Exception:
return
# Get current base job template
base_template = work_pool.base_job_template
if not base_template or "variables" not in base_template:
return
variables = base_template["variables"]
properties = variables.get("properties", {})
# Update VPC ID default if not set (treat None as empty)
vpc_id_prop = properties.get("vpc_id", {})
current_default = vpc_id_prop.get("default")
if (
current_default is None or not current_default
) and "VpcId" in stack_outputs:
vpc_id_prop["default"] = stack_outputs["VpcId"]
properties["vpc_id"] = vpc_id_prop
# Update cluster default if not set (treat None as empty)
cluster_prop = properties.get("cluster", {})
current_default = cluster_prop.get("default")
if (
current_default is None or not current_default
) and "ClusterArn" in stack_outputs:
cluster_prop["default"] = stack_outputs["ClusterArn"]
properties["cluster"] = cluster_prop
# Update Prefect API key secret ARN default if not set and we created one (treat None as empty)
api_key_secret_prop = properties.get("prefect_api_key_secret_arn", {})
current_default = api_key_secret_prop.get("default")
if (
current_default is None or not current_default
) and "PrefectApiKeySecretArnOutput" in stack_outputs:
api_key_secret_prop["default"] = stack_outputs[
"PrefectApiKeySecretArnOutput"
]
properties["prefect_api_key_secret_arn"] = api_key_secret_prop
# Update execution role ARN default if not set (treat None as empty)
execution_role_prop = properties.get("execution_role_arn", {})
current_default = execution_role_prop.get("default")
if (
current_default is None or not current_default
) and "TaskExecutionRoleArn" in stack_outputs:
execution_role_prop["default"] = stack_outputs["TaskExecutionRoleArn"]
properties["execution_role_arn"] = execution_role_prop
# Update network configuration subnets default if not set
network_config_prop = properties.get("network_configuration", {})
network_config_props = network_config_prop.get("properties", {})
awsvpc_config_prop = network_config_props.get("awsvpcConfiguration", {})
awsvpc_config_props = awsvpc_config_prop.get("properties", {})
subnets_prop = awsvpc_config_props.get("subnets", {})
current_subnets_default = subnets_prop.get("default")
if (
current_subnets_default is None or not current_subnets_default
) and "SubnetIds" in stack_outputs:
subnet_list = stack_outputs["SubnetIds"].split(",")
subnets_prop["default"] = subnet_list
awsvpc_config_props["subnets"] = subnets_prop
awsvpc_config_prop["properties"] = awsvpc_config_props
network_config_props["awsvpcConfiguration"] = awsvpc_config_prop
network_config_prop["properties"] = network_config_props
properties["network_configuration"] = network_config_prop
variables["properties"] = properties
# Update the work pool
update_data = WorkPoolUpdate(base_job_template=base_template)
client.update_work_pool(work_pool_name, update_data)
except Exception:
# Don't fail the deployment if work pool update fails
pass