From 88ae7f915843f1e9a4fa4dbac736285ef486876b Mon Sep 17 00:00:00 2001 From: Cedric Pollefeys Date: Mon, 2 Nov 2026 13:48:45 -0500 Subject: [PATCH] Add pricing v1, locked dependencies, and Ansible deployment files Co-Authored-By: Claude Opus 5.5 --- ansible/deploy.yml | 62 +++++++++++++++++++++++++++++++ ansible/inventory.ini | 3 ++ ansible/known_hosts | 1 + ansible/release.example.yml | 8 ++++ ansible/requirements.yml | 3 ++ ansible/templates/compose.yaml.j2 | 19 ++++++++++ pricing/.dockerignore | 4 ++ pricing/Dockerfile | 11 ++++++ pricing/app.py | 33 ++++++++++++++++ pricing/requirements.txt | 9 +++++ pricing/test_app.py | 25 +++++++++++++ 11 files changed, 178 insertions(+) create mode 100644 ansible/deploy.yml create mode 100644 ansible/inventory.ini create mode 100644 ansible/known_hosts create mode 100644 ansible/release.example.yml create mode 100644 ansible/requirements.yml create mode 100644 ansible/templates/compose.yaml.j2 create mode 100644 pricing/.dockerignore create mode 100644 pricing/Dockerfile create mode 100644 pricing/app.py create mode 100644 pricing/requirements.txt create mode 100644 pricing/test_app.py diff --git a/ansible/deploy.yml b/ansible/deploy.yml new file mode 100644 index 0000000..b0c4fa2 --- /dev/null +++ b/ansible/deploy.yml @@ -0,0 +1,62 @@ +- name: Deploy a selected immutable pricing release + hosts: delivery + become: true + vars: + project_dir: "/opt/startup/pricing-{{ target_env }}" + tasks: + - name: Validate release selection + ansible.builtin.assert: + that: + - target_env in ['staging', 'production'] + - "'@sha256:' in release_image" + - expected_version in ['v1', 'v2'] + - expected_total | int > 0 + - public_url.startswith('https://') + - proxy_network | length > 0 + - name: Create deployment directory + ansible.builtin.file: + path: "{{ project_dir }}" + state: directory + mode: '0750' + - name: Render selected Compose configuration + ansible.builtin.template: + src: compose.yaml.j2 + dest: "{{ project_dir }}/compose.yaml" + mode: '0640' + - name: Converge application without rebuilding + community.docker.docker_compose_v2: + project_src: "{{ project_dir }}" + project_name: "pricing-{{ target_env }}" + state: present + build: never + pull: missing + wait: true + wait_timeout: 90 + - name: Verify health through public TLS endpoint + ansible.builtin.uri: + url: "{{ public_url }}/health" + return_content: true + validate_certs: true + register: health + until: health.status == 200 and health.json.status == 'ok' + retries: 12 + delay: 5 + changed_when: false + - name: Read release identity + ansible.builtin.uri: + url: "{{ public_url }}/" + return_content: true + register: identity + changed_when: false + - name: Exercise business behavior + ansible.builtin.uri: + url: "{{ public_url }}/price?quantity=3" + return_content: true + register: price + changed_when: false + - name: Check release identity and calculation + ansible.builtin.assert: + that: + - identity.json.version == expected_version + - identity.json.commit == expected_commit + - price.json.total | int == expected_total | int diff --git a/ansible/inventory.ini b/ansible/inventory.ini new file mode 100644 index 0000000..e64194a --- /dev/null +++ b/ansible/inventory.ini @@ -0,0 +1,3 @@ +[delivery] +# 172.18.0.1 is the droplet host as seen from the delivery agent on gitea_gitea-net. +startup ansible_host=172.18.0.1 ansible_user=deploy ansible_ssh_common_args='-o UserKnownHostsFile=ansible/known_hosts -o StrictHostKeyChecking=yes' diff --git a/ansible/known_hosts b/ansible/known_hosts new file mode 100644 index 0000000..3a26bfe --- /dev/null +++ b/ansible/known_hosts @@ -0,0 +1 @@ +172.18.0.1 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPvkyH0+3FOyLD81nBpbyYomdRhwEUhEnrRLfp7/UQPV diff --git a/ansible/release.example.yml b/ansible/release.example.yml new file mode 100644 index 0000000..ec9d774 --- /dev/null +++ b/ansible/release.example.yml @@ -0,0 +1,8 @@ +# Copy to a release-specific file and replace EVERY placeholder. +target_env: staging +release_image: git.pollefeys.dev/seatbacktrivia/pricing@sha256:REPLACE_DIGEST +expected_version: v1 +expected_commit: REPLACE_FULL_GIT_COMMIT +expected_total: 30 +public_url: https://staging.pollefeys.dev +proxy_network: gitea_gitea-net diff --git a/ansible/requirements.yml b/ansible/requirements.yml new file mode 100644 index 0000000..5ccd5c4 --- /dev/null +++ b/ansible/requirements.yml @@ -0,0 +1,3 @@ +collections: + - name: community.docker + version: ">=4.8.0,<6.0.0" diff --git a/ansible/templates/compose.yaml.j2 b/ansible/templates/compose.yaml.j2 new file mode 100644 index 0000000..7c003c7 --- /dev/null +++ b/ansible/templates/compose.yaml.j2 @@ -0,0 +1,19 @@ +services: + app: + image: "{{ release_image }}" + restart: unless-stopped + read_only: true + tmpfs: + - /tmp + cap_drop: + - ALL + security_opt: + - no-new-privileges:true + networks: + edge: + aliases: + - "pricing-{{ target_env }}" +networks: + edge: + external: true + name: "{{ proxy_network }}" diff --git a/pricing/.dockerignore b/pricing/.dockerignore new file mode 100644 index 0000000..e1b2920 --- /dev/null +++ b/pricing/.dockerignore @@ -0,0 +1,4 @@ +__pycache__ +.venv +.git +*.pyc diff --git a/pricing/Dockerfile b/pricing/Dockerfile new file mode 100644 index 0000000..a380e8c --- /dev/null +++ b/pricing/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.13-slim@sha256:ae14d4ce8771606477a81446e7ef429edd6762305fc50c84507c1041fa8285d1 +ARG APP_COMMIT=development +ENV APP_COMMIT=$APP_COMMIT PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY app.py . +USER 10001:10001 +EXPOSE 8000 +HEALTHCHECK --interval=10s --timeout=3s --start-period=10s --retries=3 CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=2)" +CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "1", "--worker-tmp-dir", "/tmp", "app:app"] diff --git a/pricing/app.py b/pricing/app.py new file mode 100644 index 0000000..1ea1194 --- /dev/null +++ b/pricing/app.py @@ -0,0 +1,33 @@ +import os +from flask import Flask, jsonify, request + +app = Flask(__name__) +VERSION = "v1" +UNIT_PRICE = 10 +COMMIT = os.environ.get("APP_COMMIT", "development") + + +def total_price(quantity): + if quantity < 1 or quantity > 1000: + raise ValueError("quantity must be between 1 and 1000") + return UNIT_PRICE * quantity + + +@app.get("/") +def index(): + return jsonify(service="Startup Pricing", version=VERSION, commit=COMMIT) + + +@app.get("/health") +def health(): + return jsonify(status="ok", version=VERSION) + + +@app.get("/price") +def price(): + try: + quantity = int(request.args.get("quantity", "1")) + total = total_price(quantity) + except ValueError as error: + return jsonify(error=str(error)), 400 + return jsonify(quantity=quantity, total=total, currency="USD") diff --git a/pricing/requirements.txt b/pricing/requirements.txt new file mode 100644 index 0000000..81b6476 --- /dev/null +++ b/pricing/requirements.txt @@ -0,0 +1,9 @@ +blinker==1.9.0 +click==8.5.0 +Flask==3.1.3 +gunicorn==23.0.0 +itsdangerous==2.2.0 +Jinja2==3.1.6 +MarkupSafe==3.0.4 +packaging==26.3 +Werkzeug==3.1.9 diff --git a/pricing/test_app.py b/pricing/test_app.py new file mode 100644 index 0000000..40b4207 --- /dev/null +++ b/pricing/test_app.py @@ -0,0 +1,25 @@ +import unittest +from app import app, total_price + + +class PricingTests(unittest.TestCase): + def setUp(self): + self.client = app.test_client() + + def test_price(self): + self.assertEqual(total_price(3), 30) + response = self.client.get("/price?quantity=3") + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json["total"], 30) + + def test_invalid_quantity(self): + for value in ["0", "-1", "1001", "abc", "1.5"]: + self.assertEqual(self.client.get("/price?quantity=" + value).status_code, 400) + + def test_release_identity(self): + self.assertEqual(self.client.get("/").json["version"], "v1") + self.assertEqual(self.client.get("/health").json["status"], "ok") + + +if __name__ == "__main__": + unittest.main()