-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
185 lines (163 loc) · 5.45 KB
/
Makefile
File metadata and controls
185 lines (163 loc) · 5.45 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
SHELL := /bin/bash
ENV=source .env &&
PROJECT_NAME=beancount
# The registry is presumed to be docker.io, which is the implicit default
DOCKER_ACCOUNT=jdevries3133
ifdef GITHUB_SHA
TAG=$(GITHUB_SHA)
else
TAG=$(shell git rev-parse HEAD)
endif
CONTAINER_QUALNAME=$(DOCKER_ACCOUNT)/$(PROJECT_NAME)
CONTAINER_EXACT_REF=$(DOCKER_ACCOUNT)/$(PROJECT_NAME):$(TAG)
.PHONY: _start-db
.PHONY: _stop-db
.PHONY: backup-prod
.PHONY: bootstrap
.PHONY: build
.PHONY: build-container
.PHONY: check
.PHONY: debug-container
.PHONY: deploy
.PHONY: dev
.PHONY: proxy-prod-db
.PHONY: push-container
.PHONY: push-container
.PHONY: setup
.PHONY: shell-db
.PHONY: sqlx
.PHONY: watch-db
check: setup
ifdef CI
pnpm run build
endif
ifndef CI
@# Locally, we want to ensure that `cargo sqlx prepare` was run, otherwise
@# the build will fail in CI. So, we'll run an offline build as part of
@# our checks
SQLX_OFFLINE=true cargo build --features production
endif
./scripts/lint_dbg.sh
./scripts/lint_todo.sh
cargo clippy --features production -- -D warnings
cargo fmt --check
cargo test
sqlx:
cargo sqlx prepare -- --features production
build: setup
pnpm run build
cargo build --release
setup:
./scripts/download_htmx.sh
ifdef CI
npm i -g pnpm
endif
[[ ! -d node_modules ]] \
&& pnpm install \
|| true
ifndef CI
@# we only want the `.env` file locally in practice. We never run the app
@# in CI (yet). The problem with having the `.env` file in CI is that
@# sqlx will pickup on the `DATABASE_URL` environment variable and try
@# to talk to a datbase that isn't there, causing compilation to fail.
@# See also https://github.com/launchbadge/sqlx/blob/540baf7df55a372cb79d8636d02b1361a495b344/sqlx-cli/README.md#force-building-in-offline-mode
[[ ! -f .env ]] && cp env-template .env || true
endif
dev: setup
npx concurrently --names 'tailwind,cargo,stripe,proxy-prod-db' \
'pnpm run dev' \
"cargo watch -x 'run --features \"live_reload stripe use_stripe_test_instance localhost_base_url\"'" \
'make proxy-stripe-webhook' \
'make proxy-prod-db'
bootstrap: setup _stop-db
SQLX_OFFLINE=true cargo build
make _start-db
@sleep 1 # give the DB time to startup
@echo "===================================================================="
@echo "Bootstrap complete! The app is running now, but you need to stop it"
@echo "and run 'make dev' to get live-reloading started."
@echo "===================================================================="
$(ENV) ./target/debug/calcount
deploy:
ifdef CI
terraform init
endif
terraform apply -auto-approve
# Spawn a development database from a backup of the production database at
# $HOME/calcount-backup.sql
db-from-backup: _stop-db
$(ENV) docker run \
--name $(PROJECT_NAME) \
-e POSTGRES_DATABASE="$$POSTGRES_DB" \
-e POSTGRES_USER="$$POSTGRES_USER" \
-e POSTGRES_PASSWORD="$$POSTGRES_PASSWORD" \
-v $$HOME/calcount-backup.sql:/docker-entrypoint-initdb.d/initdb.sql \
-p 5432:5432 \
-d \
postgres:15
_start-db:
$(ENV) docker run \
--name $(PROJECT_NAME) \
-e POSTGRES_DB="$$POSTGRES_DB" \
-e POSTGRES_USER="$$POSTGRES_USER" \
-e POSTGRES_PASSWORD="$$POSTGRES_PASSWORD" \
-p 5432:5432 \
-d \
postgres:15
_stop-db:
docker kill $(PROJECT_NAME) || true
docker rm $(PROJECT_NAME) || true
watch-db:
docker logs -f $(PROJECT_NAME)
shell-db:
$(ENV) PGPASSWORD=$$POSTGRES_PASSWORD \
psql -U "$$POSTGRES_USER" -h 0.0.0.0 $$POSTGRES_DB
prod-shell-db:
kubectl exec \
-it \
-n $(PROJECT_NAME) \
pod/db-postgresql-0 \
-- /bin/sh -c 'psql postgresql://$(PROJECT_NAME):$$POSTGRES_PASSWORD@127.0.0.1:5432/$(PROJECT_NAME)'
# Port-forward the production database to your local port 5433 (as to not
# collide with your local PostgreSQL server).
proxy-prod-db:
kubectl -n $(PROJECT_NAME) port-forward service/db-postgresql 5433:5432
proxy-stripe-webhook:
stripe listen --forward-to localhost:8000/stripe-webhook
backup-prod:
kubectl exec \
-n $(PROJECT_NAME) \
pod/db-postgresql-0 \
-- /bin/sh -c 'pg_dump postgresql://$(PROJECT_NAME):$$POSTGRES_PASSWORD@127.0.0.1:5432/$(PROJECT_NAME)' \
> ~/Desktop/$(PROJECT_NAME)_backups/backup-$(shell date '+%m-%d-%Y__%H:%M:%S').sql
build-container: setup
pnpm run build
rustup target add x86_64-unknown-linux-musl
cargo build \
--release \
--target x86_64-unknown-linux-musl \
--features production
docker buildx build --load --platform linux/amd64 -t $(CONTAINER_EXACT_REF) .
# Run the above container locally, such that it can talk to the local
# PostgreSQL database launched by `make _start-db`. We expect here that the
# local database is already running and the container has already been built.
#
# Note: on macOS, you need to change the database host to "host.docker.internal"
# to allow the container to talk to the local PostgreSQL instance running inside
# Docker. On Linux, you can add the --net=host flag to the invocation of
# `docker run` below, to make PostgreSQL at localhost:5432 visible to the
# container.
debug-container:
$(ENV) docker run \
-e RUST_BACKTRACE=1 \
-e DATABASE_URL="$$DATABASE_URL" \
-e SESSION_SECRET="$$SESSION_SECRET" \
-e OPENAI_API_KEY="$$OPENAI_API_KEY" \
-e STRIPE_API_KEY="$$STRIPE_API_KEY" \
-e STRIPE_WEBHOOK_SIGNING_SECRET="$$STRIPE_WEBHOOK_SIGNING_SECRET" \
-e SMTP_EMAIL_USERNAME="$$SMTP_EMAIL_USERNAME" \
-e SMTP_EMAIL_PASSWORD="$$SMTP_EMAIL_PASSWORD" \
-p 8000:8000 \
$(CONTAINER_EXACT_REF)
push-container: build-container
docker push $(CONTAINER_EXACT_REF)