|
1 | 1 | import os |
2 | 2 |
|
3 | | -from aws_cdk import ( |
4 | | - core, |
5 | | - aws_ecs as ecs, |
6 | | -) |
| 3 | +from aws_cdk import core, aws_ecs as ecs, aws_cloudformation as cloudformation |
7 | 4 |
|
| 5 | +# These tasks are executed from manual GitLab CI jobs. The cluster is |
| 6 | +# specified with: |
| 7 | +# `aws ecs run-task --cluster ${ENVIRONMENT}-${APP_NAME}-cluster [...]` |
| 8 | +# TODO: consider making this more DRY |
8 | 9 |
|
9 | | -class BackendTasks(core.Construct): |
10 | | - def __init__( |
11 | | - self, |
12 | | - scope: core.Construct, |
13 | | - id: str, |
14 | | - image: ecs.AssetImage, |
15 | | - cluster: ecs.ICluster, |
16 | | - environment_variables: core.Construct, |
17 | | - full_app_name: str, |
18 | | - **kwargs, |
19 | | - ) -> None: |
| 10 | + |
| 11 | +class BackendTasksStack(cloudformation.NestedStack): |
| 12 | + def __init__(self, scope: core.Construct, id: str, **kwargs,) -> None: |
20 | 13 | super().__init__( |
21 | 14 | scope, id, **kwargs, |
22 | 15 | ) |
23 | 16 |
|
| 17 | + # migrate |
24 | 18 | self.migrate_task = ecs.FargateTaskDefinition( |
25 | | - self, "MigrateTask", family=f"{full_app_name}-migrate" |
| 19 | + self, "MigrateTask", family=f"{scope.full_app_name}-migrate" |
26 | 20 | ) |
27 | 21 |
|
| 22 | + for secret in [scope.variables.django_secret_key, scope.rds.db_secret]: |
| 23 | + secret.grant_read(self.migrate_task.task_role) |
| 24 | + |
28 | 25 | self.migrate_task.add_container( |
29 | 26 | "MigrateCommand", |
30 | | - image=image, |
31 | | - environment=environment_variables.regular_variables, |
32 | | - secrets=environment_variables.secret_variables, |
| 27 | + image=scope.image, |
| 28 | + environment=scope.variables.regular_variables, |
| 29 | + secrets=scope.variables.secret_variables, |
33 | 30 | command=["python3", "manage.py", "migrate", "--no-input"], |
34 | 31 | logging=ecs.LogDrivers.aws_logs(stream_prefix="MigrateCommand"), |
35 | 32 | ) |
36 | 33 |
|
| 34 | + # collectstatic |
37 | 35 | self.collectstatic_task = ecs.FargateTaskDefinition( |
38 | | - self, "CollecstaticTask", family=f"{full_app_name}-collectstatic" |
| 36 | + self, |
| 37 | + "CollecstaticTask", |
| 38 | + family=f"{scope.full_app_name}-collectstatic", |
39 | 39 | ) |
40 | 40 |
|
| 41 | + scope.backend_assets_bucket.grant_read_write( |
| 42 | + self.collectstatic_task.task_role |
| 43 | + ) |
| 44 | + |
| 45 | + for secret in [scope.variables.django_secret_key, scope.rds.db_secret]: |
| 46 | + secret.grant_read(self.collectstatic_task.task_role) |
| 47 | + |
41 | 48 | self.collectstatic_task.add_container( |
42 | 49 | "CollecstaticCommand", |
43 | | - image=image, |
44 | | - environment=environment_variables.regular_variables, |
45 | | - secrets=environment_variables.secret_variables, |
| 50 | + image=scope.image, |
| 51 | + environment=scope.variables.regular_variables, |
| 52 | + secrets=scope.variables.secret_variables, |
46 | 53 | command=["python3", "manage.py", "collectstatic", "--no-input"], |
47 | 54 | logging=ecs.LogDrivers.aws_logs( |
48 | 55 | stream_prefix="CollectstaticCommand" |
49 | 56 | ), |
50 | 57 | ) |
51 | 58 |
|
| 59 | + # createsuperuser |
52 | 60 | self.create_superuser_task = ecs.FargateTaskDefinition( |
53 | 61 | self, |
54 | 62 | "CreateSuperuserTask", |
55 | | - family=f"{full_app_name}-create-superuser", |
| 63 | + family=f"{scope.full_app_name}-create-superuser", |
56 | 64 | ) |
57 | 65 |
|
| 66 | + for secret in [scope.variables.django_secret_key, scope.rds.db_secret]: |
| 67 | + secret.grant_read(self.create_superuser_task.task_role) |
| 68 | + |
58 | 69 | self.create_superuser_task.add_container( |
59 | 70 | "CreateSuperuserCommand", |
60 | | - image=image, |
61 | | - environment=environment_variables.regular_variables, |
62 | | - secrets=environment_variables.secret_variables.update( |
| 71 | + image=scope.image, |
| 72 | + environment=scope.variables.regular_variables, |
| 73 | + secrets=scope.variables.secret_variables.update( |
63 | 74 | { |
64 | 75 | "SUPERUSER_PASSWORD": os.environ.get( |
65 | 76 | "SUPERUSER_PASSWORD", "password" |
|
0 commit comments