-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjustfile
More file actions
201 lines (161 loc) · 5.95 KB
/
justfile
File metadata and controls
201 lines (161 loc) · 5.95 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
# List root recipes plus split CI/deploy recipe files.
_default:
@just --list
@printf '\nCI recipes (`just --justfile justfile.ci --list`):\n'
@just --justfile justfile.ci --list
@printf '\nDeploy recipes (`just --justfile justfile.deploy --list`):\n'
@just --justfile justfile.deploy --list
PROJECT_DIR := justfile_directory()
LAMBDA_DIR := "lambdas"
FRONTEND_DIR := "frontend"
# Delete local git branches whose upstream refs have gone away.
git-tidy:
#!/usr/bin/env bash
git fetch --prune
for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do
git branch -d $branch
done
terraform-tidy:
#!/usr/bin/env bash
set -euo pipefail
TARGET_DIR="{{justfile_directory()}}/infra/live"
echo "Cleaning in: $TARGET_DIR"
# Remove .terragrunt-cache directories
find "$TARGET_DIR" -type d -name ".terragrunt-cache" -prune -exec rm -rf {} +
# Remove .terraform.lock.hcl files
find "$TARGET_DIR" -type f -name ".terraform.lock.hcl" -exec rm -f {} +
echo "Done."
# Create and push a new branch from the latest `main`.
branch name:
#!/usr/bin/env bash
git fetch origin
git checkout main
git pull origin
git checkout -b {{ name }}
git push -u origin {{ name }}
# Run Terraform and Terragrunt formatting locally.
format:
#!/usr/bin/env bash
terraform fmt -recursive
terragrunt hclfmt
# Run a Terragrunt operation for one environment/module pair.
tg env module op:
#!/usr/bin/env bash
cd {{justfile_directory()}}/infra/live/{{env}}/{{module}} ; terragrunt {{op}}
# Run a Terragrunt operation across all live stacks.
tg-all op:
#!/usr/bin/env bash
cd {{justfile_directory()}}/infra/live
terragrunt run-all {{op}}
# Open an ECS Exec shell in the worker debug container.
worker-debug-shell env:
#!/usr/bin/env bash
set -euo pipefail
if ! command -v session-manager-plugin >/dev/null 2>&1; then
echo "❌ session-manager-plugin is not installed or not on PATH."
exit 1
fi
aws_region="${AWS_REGION:-eu-west-2}"
project_name="$(basename "{{PROJECT_DIR}}")"
cluster_name="{{env}}-${project_name}-cluster"
service_name="ecs-worker"
container_name="${service_name}-debug"
database_cluster_identifier="${project_name}-{{env}}-app-aurora"
credentials_secret_id="$(
aws rds describe-db-clusters \
--region "$aws_region" \
--db-cluster-identifier "$database_cluster_identifier" \
--query 'DBClusters[0].MasterUserSecret.SecretArn' \
--output text
)"
credentials_json="$(
aws secretsmanager get-secret-value \
--secret-id "$credentials_secret_id" \
--region "$aws_region" \
--query 'SecretString' \
--output text
)"
db_user="$(printf '%s' "$credentials_json" | jq -r '.username')"
db_password="$(printf '%s' "$credentials_json" | jq -r '.password')"
escaped_db_user="${db_user//\'/\'\"\'\"\'}"
escaped_db_password="${db_password//\'/\'\"\'\"\'}"
task_arn="$(
aws ecs list-tasks \
--region "$aws_region" \
--cluster "$cluster_name" \
--service-name "$service_name" \
--query 'taskArns[0]' \
--output text
)"
if [[ -z "$task_arn" || "$task_arn" == "None" ]]; then
echo "❌ No running task found for service ${service_name} in cluster ${cluster_name}."
exit 1
fi
echo "🔌 Opening ECS Exec shell to ${container_name} in ${service_name}..."
aws ecs execute-command \
--region "$aws_region" \
--cluster "$cluster_name" \
--task "$task_arn" \
--container "$container_name" \
--interactive \
--command "/bin/sh -lc 'export PGUSER='\''${escaped_db_user}'\''; export DB_USER='\''${escaped_db_user}'\''; export PGPASSWORD='\''${escaped_db_password}'\''; exec /bin/sh'"
# Create or update a readonly Cognito user in the target environment.
cognito-create-readonly-user env email password:
#!/usr/bin/env bash
set -euo pipefail
aws_region="${AWS_REGION:-eu-west-2}"
project_name="$(basename "{{PROJECT_DIR}}")"
user_pool_id="$(
aws cognito-idp list-user-pools \
--region "$aws_region" \
--max-results 60 \
--query "UserPools[?Name=='${project_name}-{{env}}-users'].Id | [0]" \
--output text
)"
if [[ -z "$user_pool_id" || "$user_pool_id" == "None" ]]; then
echo "❌ Could not find Cognito user pool ${project_name}-{{env}}-users."
exit 1
fi
user_exists="$(
aws cognito-idp admin-get-user \
--region "$aws_region" \
--user-pool-id "$user_pool_id" \
--username "{{email}}" \
--query 'Username' \
--output text 2>/dev/null || true
)"
if [[ -z "$user_exists" || "$user_exists" == "None" ]]; then
aws cognito-idp admin-create-user \
--region "$aws_region" \
--user-pool-id "$user_pool_id" \
--username "{{email}}" \
--user-attributes Name=email,Value="{{email}}" Name=email_verified,Value=true \
--message-action SUPPRESS >/dev/null
fi
aws cognito-idp admin-set-user-password \
--region "$aws_region" \
--user-pool-id "$user_pool_id" \
--username "{{email}}" \
--password "{{password}}" \
--permanent >/dev/null
aws cognito-idp admin-add-user-to-group \
--region "$aws_region" \
--user-pool-id "$user_pool_id" \
--username "{{email}}" \
--group-name readonly >/dev/null
echo "✅ Ensured readonly Cognito user {{email}} exists in {{env}}."
# Publish a message directly to an SNS topic.
sns-publish:
#!/usr/bin/env bash
set -euo pipefail
if [[ -z "${TOPIC_ARN:-}" ]]; then
echo "❌ TOPIC_ARN environment variable is not set."
exit 1
fi
if [[ -z "${MESSAGE:-}" ]]; then
echo "❌ MESSAGE environment variable is not set."
exit 1
fi
aws sns publish \
--topic-arn "$TOPIC_ARN" \
--message "$MESSAGE"