Skip to content

Commit a8203ac

Browse files
authored
feat: token writer service + refactor (#24)
PR replaces ssh+script based token writing with a token writer service which runs on Maxwell to write the token files on request, along with that the codebase is restructured into two packages (zwop and the token writer services). Additionally: - redid some of the state management/config steps, temporarily(?) making them worse with *plans* to improve on later - tidied up build/install process, removed unused scripts, etc... - updated container definition to build both packages and export them as stages which can be targeted - updated compose files to use this - updated github workflows to build and publish images - added the one line config change 🫠 to set the mymdc scope so that damnit credentials can be used Closes #16
2 parents 3770e11 + 5aed9ab commit a8203ac

63 files changed

Lines changed: 1200 additions & 934 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/build-image.yml

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
- main
77
release:
88
types: [published]
9+
pull_request:
10+
branches:
11+
- main
912

1013
env:
1114
REGISTRY: ghcr.io
@@ -21,6 +24,15 @@ jobs:
2124
steps:
2225
- name: Checkout repository
2326
uses: actions/checkout@v4
27+
with:
28+
fetch-depth: 0
29+
30+
- uses: astral-sh/setup-uv@v5
31+
32+
- name: Resolve version
33+
id: version
34+
working-directory: ./packages/zwop
35+
run: echo "app_version=$(uvx hatch version)" >> "$GITHUB_OUTPUT"
2436

2537
- name: Set up Docker Buildx
2638
uses: docker/setup-buildx-action@v3
@@ -32,25 +44,52 @@ jobs:
3244
username: ${{ github.actor }}
3345
password: ${{ secrets.GITHUB_TOKEN }}
3446

35-
- name: Extract metadata (tags, labels) for Docker
36-
id: meta
47+
- name: Extract metadata (tags, labels) for zwop
48+
id: meta_zwop
3749
uses: docker/metadata-action@v5
3850
with:
3951
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
4052
tags: |
4153
type=ref,event=branch
42-
type=semver,pattern={{version}}
43-
type=semver,pattern={{major}}.{{minor}}
44-
type=semver,pattern={{major}}
54+
type=ref,event=pr
55+
type=raw,value=${{ steps.version.outputs.app_version }}
56+
type=raw,value=staging,enable=${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
57+
type=raw,value=stable,enable=${{ github.event_name == 'release' }}
58+
59+
- name: Build zwop image
60+
uses: docker/build-push-action@v6
61+
with:
62+
context: .
63+
target: zwop
64+
push: true # ${{ github.event_name != 'pull_request' }}
65+
tags: ${{ steps.meta_zwop.outputs.tags }}
66+
labels: ${{ steps.meta_zwop.outputs.labels }}
67+
cache-from: type=gha
68+
cache-to: type=gha,mode=max
69+
build-args: |
70+
APP_VERSION=${{ steps.version.outputs.app_version }}
71+
72+
- name: Extract metadata (tags, labels) for zwop-tws
73+
id: meta_zwop_tws
74+
uses: docker/metadata-action@v5
75+
with:
76+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-tws
77+
tags: |
78+
type=ref,event=branch
79+
type=ref,event=pr
80+
type=raw,value=${{ steps.version.outputs.app_version }}
4581
type=raw,value=staging,enable=${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
4682
type=raw,value=stable,enable=${{ github.event_name == 'release' }}
4783
48-
- name: Build and push Docker image
84+
- name: Build zwop-tws image
4985
uses: docker/build-push-action@v6
5086
with:
5187
context: .
52-
push: true
53-
tags: ${{ steps.meta.outputs.tags }}
54-
labels: ${{ steps.meta.outputs.labels }}
88+
target: zwop-tws
89+
push: true # ${{ github.event_name != 'pull_request' }}
90+
tags: ${{ steps.meta_zwop_tws.outputs.tags }}
91+
labels: ${{ steps.meta_zwop_tws.outputs.labels }}
5592
cache-from: type=gha
56-
cache-to: type=gha,mode=max
93+
cache-to: type=gha,mode=max
94+
build-args: |
95+
APP_VERSION=${{ steps.version.outputs.app_version }}

.gitignore

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
id_ed25519
2-
id_ed25519.pub
31
migration
42
*.zuliprc
53
.env
64
.task
75
clients.json
86
zuliprc.json
97
node_modules
10-
src/zulip_write_only_proxy/_version.py
11-
src/zulip_write_only_proxy/frontend/static
8+
certs
9+
packages/_version.py
10+
packages/zwop/src/zwop/frontend/static
1211
zuliprc
1312

1413
# Byte-compiled / optimized / DLL files

Dockerfile

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,71 @@
11
# syntax=docker/dockerfile:1
2+
FROM python:3.13-slim AS base
3+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
24

3-
## Frontend
4-
FROM node:21-slim AS frontend
5-
ENV PNPM_HOME="/pnpm"
6-
ENV PATH="$PNPM_HOME:$PATH"
7-
RUN corepack enable
5+
ARG APP_VERSION=0.0.0.dev0
86

97
WORKDIR /app
108

11-
COPY --link ./package.json ./pnpm-lock.yaml ./tailwind.config.js ./
12-
13-
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
14-
15-
COPY ./src/zulip_write_only_proxy/frontend/templates/ \
16-
./src/zulip_write_only_proxy/frontend/templates/
17-
18-
RUN pnpm build
19-
20-
ADD --link https://unpkg.com/htmx.org@1.9.10/dist/htmx.js \
21-
https://unpkg.com/htmx.org@1.9.10/dist/htmx.min.js \
22-
./src/zulip_write_only_proxy/frontend/static/
9+
ENV \
10+
PYTHONOPTIMIZE=2 \
11+
SETUPTOOLS_SCM_PRETEND_VERSION=${APP_VERSION} \
12+
UV_CACHE_DIR=/opt/uv-cache/ \
13+
UV_COMPILE_BYTECODE=1 \
14+
UV_LINK_MODE=copy
2315

16+
RUN apt update && \
17+
apt install -y wget --no-install-recommends && \
18+
rm -rf /var/lib/apt/lists/*
2419

25-
## Server
26-
FROM python:3.14-slim
27-
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
2820

29-
WORKDIR /app
21+
FROM base AS dev
3022

3123
ENV \
32-
PYTHONOPTIMIZE=2 \
33-
UV_COMPILE_BYTECODE=1 \
34-
UV_LINK_MODE=copy \
35-
UV_CACHE_DIR=/opt/uv-cache/
24+
PNPM_HOME="/pnpm" \
25+
PATH="/pnpm:$PATH"
3626

37-
RUN apt update && apt install -y openssh-client wget git && rm -rf /var/lib/apt/lists/*
27+
RUN apt update && \
28+
apt install -y node-corepack --no-install-recommends && \
29+
corepack enable && \
30+
rm -rf /var/lib/apt/lists/*
3831

39-
COPY --link ./pyproject.toml ./uv.lock /app/
32+
COPY --link ./pyproject.toml ./uv.lock ./.python-version /app/
4033

4134
RUN --mount=type=cache,target=/opt/uv-cache/ \
42-
uv sync --locked --no-install-project
35+
uv sync --frozen --no-install-workspace
4336

4437
COPY --link README.md ./
45-
COPY --link src ./src
38+
COPY --link packages ./packages
39+
40+
RUN --mount=type=cache,id=pnpm,target=/pnpm/store cd packages/zwop \
41+
pnpm install --frozen-lockfile
4642

4743
RUN --mount=type=cache,target=/opt/uv-cache/ \
48-
--mount=type=bind,source=.git,target=/app/.git \
4944
uv sync --locked
5045

51-
COPY --link --from=frontend /app/src/zulip_write_only_proxy/frontend \
52-
/app/src/zulip_write_only_proxy/frontend
5346

54-
EXPOSE 8000
47+
FROM dev AS build
48+
49+
RUN --mount=type=cache,target=/opt/uv-cache/ \
50+
--mount=type=cache,id=pnpm,target=/pnpm/store \
51+
uv build --all-packages
52+
53+
54+
FROM base AS zwop-tws
55+
RUN --mount=type=bind,from=build,source=/app/dist,target=/dist \
56+
--mount=type=cache,target=/opt/uv-cache/ \
57+
uv pip install --system /dist/zwop_tws-*.whl
58+
59+
CMD ["zwop-tws"]
60+
61+
62+
FROM base AS zwop
63+
RUN --mount=type=bind,from=build,source=/app/dist,target=/dist \
64+
--mount=type=cache,target=/opt/uv-cache/ \
65+
uv pip install --system /dist/zwop-*.whl /dist/zwop_tws-*.whl
5566

56-
ENV ZWOP_ADDRESS=http://0.0.0.0:8000
67+
CMD ["zwop"]
5768

58-
CMD ["uv", "run", "-m", "zulip_write_only_proxy.main"]
5969

60-
HEALTHCHECK --start-interval=1s --start-period=30s --interval=60s \
61-
CMD wget http://0.0.0.0:8000/api/health || exit 1
70+
# Use dev as base so that default target is dev
71+
FROM dev

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Here is an example configuration, placed in `.vscode/launch.json`:
136136
"request": "launch",
137137
"module": "uvicorn",
138138
"args": [
139-
"zulip_write_only_proxy.main:app",
139+
"zwop.main:app",
140140
"--port",
141141
"8000",
142142
],

Taskfile.yml

Lines changed: 0 additions & 41 deletions
This file was deleted.

compose.override.yml

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
21
name: zwop-dev
32

43
services:
54
server:
65
environment:
76
- "ZWOP_TOKEN_WRITER__ZWOP_URL=http://localhost:8000/${COMPOSE_PROJECT_NAME}"
8-
develop:
9-
watch:
10-
- action: sync
11-
path: ./src/zulip_write_only_proxy
12-
target: /app/src/zulip_write_only_proxy
13-
ignore:
14-
- __pycache__/
15-
- "*.pyc"
16-
- ".pytest_cache/"
17-
- action: sync+restart
18-
path: ./pyproject.toml
19-
target: /app/pyproject.toml
20-
- action: sync+restart
21-
path: ./src/zulip_write_only_proxy/main.py
22-
target: /app/src/zulip_write_only_proxy/main.py
23-
restart: never
7+
- "ZWOP_TOKEN_WRITER__URL=https://tws:8443/"
8+
- "ZWOP_ADDRESS=http://0.0.0.0:8000"
9+
10+
tws:
11+
build:
12+
context: .
13+
target: zwop-tws
14+
volumes:
15+
- ./certs:/app/certs
16+
env_file:
17+
- .env
2418

2519
traefik:
2620
image: "traefik:latest"

compose.prod.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ include:
66
services:
77
server:
88
image: ghcr.io/european-xfel/zulip-write-only-proxy:stable
9-
pull_policy: "every_10m"

compose.staging.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ networks:
99

1010
services:
1111
server:
12-
build: .
1312
environment:
1413
- "ZWOP_TOKEN_WRITER__ZWOP_URL=https://exfldadev01.desy.de/${COMPOSE_PROJECT_NAME}"
1514
networks:
1615
- proxy
17-
restart: unless-stopped
16+
restart: unless-stopped

compose.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
services:
22
server:
3-
build: .
3+
build:
4+
context: .
5+
target: zwop
46
volumes:
5-
- ./config:/config
7+
- ./config:/app/config
8+
- ./certs:/app/certs
69
env_file:
710
- .env
811
environment:
912
- "ZWOP_PROXY_ROOT=/${COMPOSE_PROJECT_NAME}"
1013
- "ZWOP_PROXY_HEADERS=true"
1114
- "ZWOP_FORWARDED_ALLOW_IPS=*"
15+
- "ZWOP_ADDRESS=http://0.0.0.0:8000"
1216
labels:
1317
- "traefik.enable=true"
1418
- "traefik.http.middlewares.${COMPOSE_PROJECT_NAME}-stripprefix.stripprefix.prefixes=/${COMPOSE_PROJECT_NAME}"

package.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)