Skip to content

Commit 6ee8d33

Browse files
authored
Merge pull request #6162 from mozilla/local-dev-docker-compose
feat(local): add docker compose for easier local setup
2 parents 969cb3c + 7682bd0 commit 6ee8d33

File tree

5 files changed

+159
-9
lines changed

5 files changed

+159
-9
lines changed

.docker/postgres/init-db.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE USER blurts WITH ENCRYPTED PASSWORD 'blurts';
2+
CREATE DATABASE blurts WITH OWNER 'blurts';
3+
CREATE DATABASE "test-blurts" WITH OWNER 'blurts';
4+
5+
ALTER DEFAULT privileges IN SCHEMA public GRANT ALL ON tables to blurts;

.docker/pubsub/setup_pubsub.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
# Ensure the emulator host is set
4+
PUBSUB_HOST=$1
5+
PUBSUB_PORT=$2
6+
PROJECT_ID=$3
7+
8+
PUBSUB_EMULATOR_HOST="${PUBSUB_HOST}:${PUBSUB_PORT}"
9+
10+
# Start PubSub emulator in the background
11+
gcloud beta emulators pubsub start --host-port=0.0.0.0:${PUBSUB_PORT} --project=${PROJECT_ID} &
12+
echo "Waiting for Pub/Sub emulator to be ready..."
13+
until curl -s "http://${PUBSUB_EMULATOR_HOST}/v1/projects/${PROJECT_ID}/schemas" > /dev/null; do
14+
echo "Pub/Sub emulator not ready yet..."
15+
sleep 2
16+
done
17+
echo "Pub/Sub emulator is ready!"
18+
echo "Initializing Pub/Sub emulator..."
19+
20+
create_topic_and_subscription () {
21+
if [ $# -ne 2 ]; then
22+
echo "create_topic_and_subscription takes 2 arguments"
23+
exit 1
24+
fi
25+
if [ -z "$1" ]; then
26+
echo "create_topic_and_subscription requires non-empty topic"
27+
exit 1
28+
fi
29+
if [ -z "$2" ]; then
30+
echo "create_topic_and_subscription requires non-empty subscription"
31+
exit 1
32+
fi
33+
34+
topic=$1
35+
subscription=$2
36+
echo "Creating topic '$topic' with subscription '$subscription'..."
37+
38+
# Create the topic using REST API instead of gcloud
39+
curl -s -X PUT "${PUBSUB_EMULATOR_HOST}/v1/projects/${PROJECT_ID}/topics/${topic}"
40+
41+
# Create the subscription using REST API instead of gcloud
42+
curl -s -X PUT "${PUBSUB_EMULATOR_HOST}/v1/projects/${PROJECT_ID}/subscriptions/${subscription}" \
43+
-H "Content-Type: application/json" \
44+
-d "{\"topic\": \"projects/${PROJECT_ID}/topics/${topic}\"}"
45+
}
46+
47+
create_topic_and_subscription hibp-breaches hibp-cron
48+
49+
echo "Pub/Sub initialization completed."
50+
touch /tmp/startup.done
51+
52+
# Keep emulator running
53+
wait

.env.local.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,6 @@ DATA_BROKER_REMOVAL_ESTIMATES_DATA=[]
9494
GCP_PUBSUB_PROJECT_ID=your-project-name
9595
GCP_PUBSUB_TOPIC_NAME=hibp-breaches
9696
GCP_PUBSUB_SUBSCRIPTION_NAME=hibp-cron
97-
PUBSUB_EMULATOR_HOST=localhost:8085
97+
PUBSUB_HOST=localhost
98+
PUBSUB_PORT=8085
99+
PUBSUB_EMULATOR_HOST="${PUBSUB_HOST}:${PUBSUB_PORT}"

README.md

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ We track commits that are largely style/formatting via `.git-blame-ignore-revs`.
4747

4848
### Database
4949

50-
To create the database tables ...
50+
To create the database tables you have two options: manually, or using docker-compose
51+
52+
#### Manual setup
5153

5254
1. Create the `blurts` database:
5355

@@ -69,6 +71,28 @@ To create the database tables ...
6971
npm run db:migrate
7072
```
7173

74+
#### Via docker-compose
75+
76+
This will automatically provision the databases 'blurts' and 'test-blurts', and a user 'blurts' with password 'blurts'. The connection string in your .env.local file should be: "postgres://blurts:blurts@localhost:5432/blurts"
77+
78+
1. Ensure that you have an up-to-date .env.local file.
79+
80+
```$sh
81+
cp .env.local.example .env.local
82+
```
83+
84+
2. Start docker containers (this will stand up all services defined, including pubsub)
85+
86+
```sh
87+
docker compose --env-file .env.local up -d
88+
```
89+
90+
3. To tear down (this will delete stored data):
91+
92+
```sh
93+
docker compose --env-file .env.local down
94+
```
95+
7296
### Install
7397

7498
1. Clone and change to the directory:
@@ -140,24 +164,44 @@ To create the database tables ...
140164

141165
### PubSub
142166

143-
Monitor uses GCP PubSub for processing incoming breach data, this can be tested locally using an emulator: https://cloud.google.com/pubsub/docs/emulator
167+
Monitor uses GCP PubSub for processing incoming breach data, this can be tested locally using an emulator: <https://cloud.google.com/pubsub/docs/emulator>
168+
169+
You can run the emulator manually or via docker-compose.
144170

145-
#### Run the GCP PubSub emulator:
171+
#### Manual Setup
172+
173+
##### Run the GCP PubSub emulator
146174

147175
```sh
148176
gcloud beta emulators pubsub start --project=your-project-name
149177
```
150178

151179
(Set `your-project-name` as the value for `GCP_PUBSUB_PROJECT_ID` in your `.env.local`.)
152180

153-
### In a different shell, set the environment to point at the emulator and run Monitor in dev mode:
181+
#### Docker Compose Setup
182+
183+
This will automatically provision a pubsub topic named 'hibp-breaches' with a subscription named 'hibp-cron'.
184+
185+
1. Ensure that you have an up-to-date .env.local file.
186+
187+
```$sh
188+
cp .env.local.example .env.local
189+
```
190+
191+
2. Start docker containers (this will stand up all services defined, including postgres)
192+
193+
```sh
194+
docker compose --env-file .env.local up -d
195+
```
196+
197+
### In a different shell, set the environment to point at the emulator and run Monitor in dev mode
154198

155199
```sh
156200
$(gcloud beta emulators pubsub env-init)
157201
npm run dev
158202
```
159203

160-
### Incoming WebHook requests from HIBP will be of the form:
204+
### Incoming WebHook requests from HIBP will be of the form
161205

162206
```sh
163207
curl -d '{ "breachName": "000webhost", "hashPrefix": "test", "hashSuffixes": ["test"] }' \
@@ -168,7 +212,7 @@ curl -d '{ "breachName": "000webhost", "hashPrefix": "test", "hashSuffixes": ["t
168212
This emulates HIBP notifying our API that a new breach was found. Our API will
169213
then add it to the (emulated) pubsub queue.
170214

171-
### This pubsub queue will be consumed by this cron job, which is responsible for looking up and emailing impacted users:
215+
### This pubsub queue will be consumed by this cron job, which is responsible for looking up and emailing impacted users
172216

173217
```sh
174218
NODE_ENV="development" npm run dev:cron:breach-alerts
@@ -181,7 +225,7 @@ Monitor generates multiple emails that get sent to subscribers. To preview or te
181225
### Mozilla accounts ("FxA", formerly known as Firefox accounts)
182226

183227
The repo comes with a development FxA oauth app pre-configured in `.env`, which
184-
should work fine running the app on http://localhost:6060. You'll need to get
228+
should work fine running the app on <http://localhost:6060>. You'll need to get
185229
the `OAUTH_CLIENT_SECRET` value from a team member or someone in #fxmonitor-engineering.
186230

187231
## Testing
@@ -216,7 +260,7 @@ To test this part of Monitor:
216260

217261
k6 is used for load testing.
218262

219-
See https://grafana.com/docs/k6/latest/get-started/running-k6/ for more information.
263+
See <https://grafana.com/docs/k6/latest/get-started/running-k6/> for more information.
220264

221265
##### HIBP breach alerts
222266

docker-compose.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
services:
2+
postgres:
3+
image: postgres:latest
4+
ports:
5+
- "5432:5432"
6+
volumes:
7+
- "./.docker/postgres:/docker-entrypoint-initdb.d:delegated,z"
8+
environment:
9+
POSTGRES_PASSWORD: password
10+
POSTGRES_USER: postgres
11+
PGUSER: postgres
12+
POSTGRES_DB: postgres
13+
command: ["-c", "log_statement=all"]
14+
healthcheck:
15+
test: ["CMD-SHELL", "pg_isready"]
16+
interval: 1s
17+
timeout: 5s
18+
retries: 10
19+
start_period: 80s
20+
networks:
21+
- shared
22+
23+
pubsub:
24+
image: google/cloud-sdk:529.0.0
25+
platform: linux/amd64
26+
entrypoint: ["/bin/bash", "-c"]
27+
command:
28+
- |
29+
/pubsub-init/setup_pubsub.sh ${PUBSUB_HOST} ${PUBSUB_PORT} ${GCP_PUBSUB_PROJECT_ID}
30+
ports:
31+
- "${PUBSUB_PORT}:${PUBSUB_PORT}"
32+
volumes:
33+
- "./.docker/pubsub:/pubsub-init:delegated,z"
34+
healthcheck:
35+
test: ["CMD-SHELL", "test -f /tmp/startup.done"]
36+
interval: 15s
37+
timeout: 10s
38+
retries: 20
39+
start_period: 30s
40+
start_interval: 10s
41+
networks:
42+
- shared
43+
44+
networks:
45+
shared:
46+
name: shared

0 commit comments

Comments
 (0)