Repository navigation
Expand file tree
/
Copy pathjustfile
More file actions
254 lines (225 loc) · 10.3 KB
/
Copy pathjustfile
File metadata and controls
254 lines (225 loc) · 10.3 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
# Local development: `just` lists the recipes, `just setup` prepares a checkout or a worktree.
# Each worktree has a slot (DEV_SLOT in .env.local): its ports are the defaults + 100 × slot, and its
# containers and volumes live in their own Compose project, so worktrees run side by side.
set shell := ["bash", "-euo", "pipefail", "-c"]
import? 'ee/dev.just'
local_edition := `awk -F= '$1 == "METEROID_EDITION" { v = $2 } END { print v }' .env.local 2>/dev/null || true`
local_slot := `awk -F= '$1 == "DEV_SLOT" { v = $2 } END { print v }' .env.local 2>/dev/null || true`
default_edition := if path_exists(justfile_directory() / 'ee/dev.just') == 'true' { 'enterprise' } else { 'oss' }
edition := env('METEROID_EDITION', if local_edition != '' { local_edition } else { default_edition })
just := quote(just_executable()) + ' --justfile ' + quote(justfile()) + ' --working-directory .'
compose := 'docker compose -f docker/develop/docker-compose.yml --env-file .env'
[private]
default:
@{{ just }} --list --unsorted
# Prepares this checkout: .env, web dependencies and the generated web clients.
[group('setup')]
setup: env
pnpm install --dir modules/web
@{{ just }} _proto-{{ edition }}
@echo "Ready ({{ edition }}). Next: just dev"
# Writes .env from .env.example, the edition's settings, this worktree's ports and .env.local (last wins).
[group('setup')]
env slot=local_slot:
#!/usr/bin/env bash
set -euo pipefail
slot="{{ if slot == '' { '0' } else { slot } }}"
[[ "$slot" =~ ^[0-9]$ ]] || { echo "slot must be 0-9, got '$slot'" >&2; exit 1; }
if [[ "$slot" != "{{ local_slot }}" ]]; then
touch .env.local
sed -i.bak '/^DEV_SLOT=/d' .env.local && rm -f .env.local.bak
echo "DEV_SLOT=$slot" >> .env.local
fi
o=$((slot * 100))
project=meteroid; [[ "$slot" == 0 ]] || project="meteroid-$slot"
web=$((5173 + o)); api=$((50061 + o)); rest=$((8084 + o)); metering=$((50062 + o))
tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT
cat > "$tmp/slot.env" <<EOF
DEV_SLOT=$slot
COMPOSE_PROJECT_NAME=$project
POSTGRES_PORT=$((5432 + o))
REDIS_PORT=$((6379 + o))
MINIO_PORT=$((9002 + o))
MINIO_CONSOLE_PORT=$((9001 + o))
CLICKHOUSE_HTTP_PORT=$((8123 + o))
CLICKHOUSE_TCP_PORT=$((9000 + o))
KAFKA_PORT=$((9092 + o))
REDPANDA_CONSOLE_PORT=$((8085 + o))
PGHERO_PORT=$((8080 + o))
MAILHOG_PORT=$((8025 + o))
WEB_PORT=$web
METEROID_API_PORT=$api
METEROID_REST_PORT=$rest
DATABASE_URL=postgres://\${DATABASE_USER}:\${DATABASE_PASSWORD}@localhost:$((5432 + o))/\${DATABASE_NAME}?sslmode=disable
METEROID_API_LISTEN_ADDRESS=0.0.0.0:$api
METEROID_API_EXTERNAL_URL=http://127.0.0.1:$api
METEROID_REST_API_LISTEN_ADDRESS=0.0.0.0:$rest
METEROID_REST_API_EXTERNAL_URL=http://127.0.0.1:$rest
METERING_API_LISTEN_ADDRESS=0.0.0.0:$metering
METERING_API_EXTERNAL_URL=http://127.0.0.1:$metering
METEROID_PUBLIC_URL=http://localhost:$web
REDIS_URL=redis://localhost:$((6379 + o))
KAFKA_BOOTSTRAP_SERVERS=127.0.0.1:$((9092 + o))
CLICKHOUSE_HTTP_ADDRESS=http://127.0.0.1:$((8123 + o))
CLICKHOUSE_TCP_ADDRESS=127.0.0.1:$((9000 + o))
OBJECT_STORE_URI=file:///tmp/$project/object-store
EOF
sources=$({{ just }} _env-sources-{{ edition }})
if [[ -f .env ]] && ! head -1 .env | grep -q '^# Generated by `just env`'; then
if [[ -e .env.local ]] && grep -qv '^DEV_SLOT=' .env.local; then
mv .env .env.bak
echo "Moved the hand-written .env to .env.bak: put what you need from it in .env.local." >&2
else
awk -v slotfile="$tmp/slot.env" '
FILENAME == slotfile { managed[substr($0, 1, index($0, "=") - 1)] = 1; next }
pass == 1 { if (match($0, /^[A-Za-z_][A-Za-z0-9_]*=/)) base[substr($0, 1, RLENGTH - 1)] = substr($0, RLENGTH + 1); next }
match($0, /^[A-Za-z_][A-Za-z0-9_]*=/) {
k = substr($0, 1, RLENGTH - 1)
if (!(k in managed) && (!(k in base) || base[k] != substr($0, RLENGTH + 1))) print
}
' "$tmp/slot.env" pass=1 $sources pass=2 .env >> .env.local
mv .env .env.bak
echo "Moved the hand-written .env to .env.bak and its changed settings to .env.local." >&2
fi
fi
{
echo '# Generated by `just env`: edit .env.local (overrides) or the .env.example files, not this file.'
awk '
function expand(v, name, n) {
while (n++ < 50 && match(v, /\$\{[A-Za-z_][A-Za-z0-9_]*\}/)) {
name = substr(v, RSTART + 2, RLENGTH - 3)
v = substr(v, 1, RSTART - 1) val[name] substr(v, RSTART + RLENGTH)
}
return v
}
pass == 1 { if (match($0, /^[A-Za-z_][A-Za-z0-9_]*=/)) val[substr($0, 1, RLENGTH - 1)] = substr($0, RLENGTH + 1); next }
FNR == 1 { print ""; print "# ---- " FILENAME }
match($0, /^[A-Za-z_][A-Za-z0-9_]*=/) {
k = substr($0, 1, RLENGTH - 1)
if (!(k in done)) { done[k] = 1; print k "=" expand(val[k]) }
next
}
{ print }
' pass=1 $sources "$tmp/slot.env" .env.local pass=2 $sources "$tmp/slot.env" .env.local \
| sed "s|^# ---- $tmp/slot.env|# ---- worktree slot $slot (just env)|"
} > .env
echo "Wrote .env ({{ edition }}, slot $slot): web http://localhost:$web, postgres localhost:$((5432 + o)), compose project $project"
# Creates a worktree on a new or existing branch, with the next free slot and a copy of .env.local.
[group('worktree')]
worktree branch path='':
#!/usr/bin/env bash
set -euo pipefail
main=$(dirname "$(git rev-parse --path-format=absolute --git-common-dir)")
path="{{ path }}"; [[ -n "$path" ]] || { b="{{ branch }}"; path="$main-${b//\//-}"; }
used=" "
for wt in $(git worktree list --porcelain | awk '/^worktree / { print $2 }'); do
s=$(awk -F= '$1 == "DEV_SLOT" { v = $2 } END { print v }' "$wt/.env.local" 2>/dev/null || true)
used+="${s:-0} "
done
slot=""; for s in 1 2 3 4 5 6 7 8 9; do [[ "$used" == *" $s "* ]] || { slot=$s; break; }; done
[[ -n "$slot" ]] || { echo "All 9 worktree slots are taken (just slots)." >&2; exit 1; }
if git show-ref --quiet "refs/heads/{{ branch }}" || git show-ref --quiet "refs/remotes/origin/{{ branch }}"; then
git worktree add "$path" "{{ branch }}"
else
git worktree add -b "{{ branch }}" "$path"
fi
grep -v '^DEV_SLOT=' "$main/.env.local" > "$path/.env.local" 2>/dev/null || true
cd "$path" && {{ just }} env "$slot"
echo "Next: cd $path && just setup"
# Stops and deletes a worktree's containers and volumes, then the worktree.
[group('worktree')]
[confirm("Delete this worktree's containers, volumes and checkout?")]
worktree-remove path:
cd "{{ path }}" && {{ compose }} --profile '*' down -v --remove-orphans
git worktree remove "{{ path }}"
# Lists the worktrees, their slot, web port and running containers.
[group('worktree')]
slots:
#!/usr/bin/env bash
set -euo pipefail
printf '%-6s %-10s %-11s %s\n' SLOT WEB CONTAINERS WORKTREE
for wt in $(git worktree list --porcelain | awk '/^worktree / { print $2 }'); do
get() { awk -F= -v k="$1" '$1 == k { v = $2 } END { print v }' "$wt/.env" 2>/dev/null || true; }
project=$(get COMPOSE_PROJECT_NAME)
running=$([[ -n "$project" ]] && docker compose -p "$project" ps -q 2>/dev/null | wc -l | tr -d ' ' || echo -)
printf '%-6s %-10s %-11s %s\n' "$(get DEV_SLOT)" "$(get WEB_PORT)" "$running" "$wt"
done
# Starts the dependencies: Postgres, plus the profiles given (storage metering redis full pgbouncer pghero api-image).
[group('run')]
up *profiles: _dotenv
{{ compose }} {{ prepend('--profile=', profiles) }} up -d --wait
# Stops the dependencies, keeping their data.
[group('run')]
down: _dotenv
{{ compose }} --profile '*' down --remove-orphans
# Stops the dependencies and deletes their data.
[group('run')]
[confirm("Delete this worktree's database and other volumes?")]
nuke: _dotenv
{{ compose }} --profile '*' down -v --remove-orphans
# Follows the dependencies' logs.
[group('run')]
logs *services: _dotenv
{{ compose }} logs -f {{ services }}
# Opens psql on this worktree's database.
[group('run')]
psql: _dotenv
{{ compose }} exec meteroid-db psql -U "$({{ just }} _get DATABASE_USER)" "$({{ just }} _get DATABASE_NAME)"
# Runs the backend, all-in-one (api and workers). `{{ just }} api metering-server` also serves metering.
[group('run')]
api features='': _dotenv
{{ just }} _api-{{ edition }} {{ if features != '' { '--features ' + features } else { '' } }}
# Runs the web app's dev server.
[group('run')]
web: _dotenv
@{{ just }} _web-{{ edition }}
# Starts the dependencies, then the backend and the web app. `just dev metering-server` adds metering.
[group('run')]
dev features='': (up if features =~ 'metering' { 'metering' } else { '' })
#!/usr/bin/env bash
trap 'trap - EXIT; kill 0' INT TERM EXIT
{{ just }} api {{ features }} &
{{ just }} web &
wait
# Regenerates the web clients from the protos (Rust ones are generated by the builds).
[group('codegen')]
proto:
pnpm --dir modules/web/web-app run generate:proto
@{{ just }} _proto-{{ edition }}
# Regenerates the OpenAPI specs.
[group('codegen')]
openapi:
cargo run -p meteroid-oss-openapi
@{{ just }} _openapi-{{ edition }}
# Creates a Diesel migration.
[group('db')]
migration name:
diesel migration generate {{ name }}
.github/scripts/check-migration-versions.sh
# Applies the pending migrations and regenerates schema.rs (the backend also applies them at startup).
[group('db')]
migrate: _dotenv
diesel migration run
# Reverts the last migration and applies it again.
[group('db')]
migrate-redo: _dotenv
diesel migration redo
[private]
_dotenv:
@[[ -f .env && -z "$(find $({{ just }} _env-sources-{{ edition }}) .env.local -newer .env 2>/dev/null)" ]] || {{ just }} env
[private]
_get key:
@awk -F= -v k={{ key }} '$1 == k { sub(/^[^=]*=/, ""); v = $0 } END { print v }' .env
[private]
_env-sources-oss:
@echo .env.example
[private]
_api-oss *args:
cargo run -p meteroid-oss-standalone {{ args }}
[private]
_web-oss:
pnpm --dir modules/web/web-app dev
[private]
_proto-oss:
[private]
_openapi-oss: