diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a67fa26 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +settings.cfg diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..cf9919e --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,49 @@ +name: Docker Image CI + +on: + push: + branches: [ "master" ] +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push-image: + permissions: + contents: read + packages: write + runs-on: ubuntu-latest + steps: + + - uses: actions/checkout@v3 + + # for multiplatform image support + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=sha + type=raw,value=latest,enable={{is_default_branch}} + + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + platforms: linux/amd64,linux/arm64 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ab42e63 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "githubPullRequests.ignoredPullRequestBranches": [ + "master" + ] +} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..be2349c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM python:3-alpine + +WORKDIR /app +COPY requirements.txt /app +RUN --mount=type=cache,target=/root/.cache/pip pip3 install -r requirements.txt + +# install gunicorn production server +RUN --mount=type=cache,target=/root/.cache/pipu pip3 install gunicorn + +COPY . /app + +# put your settings.cfg in here +RUN mkdir /config +RUN ln -s /config/settings.cfg /app/settings.cfg + +EXPOSE 8000 +CMD ["gunicorn", "-c", "gunicorn.py", "app:app"] diff --git a/gunicorn.py b/gunicorn.py new file mode 100644 index 0000000..39d7aa5 --- /dev/null +++ b/gunicorn.py @@ -0,0 +1,10 @@ + +import multiprocessing +import os + +bind = f"0.0.0.0:{os.getenv('PORT', '8000')}" +accesslog = "-" +access_log_format = "%(h)s %(l)s %(u)s %(t)s '%(r)s' %(s)s %(b)s '%(f)s' '%(a)s' in %(D)sµs" # noqa: E501 + +workers = int(os.getenv("WEB_CONCURRENCY", multiprocessing.cpu_count() * 2)) +threads = int(os.getenv("PYTHON_MAX_THREADS", 1)) diff --git a/manifests/manifest.yaml b/manifests/manifest.yaml new file mode 100644 index 0000000..c6407f5 --- /dev/null +++ b/manifests/manifest.yaml @@ -0,0 +1,51 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: mtapi-config +data: + settings.cfg: | + MTA_KEY = 'i-dont-think-this-requires-a-key-but-whatever' + STATIONS_FILE = './data/stations.json' + CROSS_ORIGIN = '*' + MAX_TRAINS=10 + MAX_MINUTES=30 + CACHE_SECONDS=60 + THREADED=True + DEBUG=False + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: mtapi +spec: + selector: + matchLabels: + app: mtapi + template: + metadata: + labels: + app: mtapi + spec: + containers: + - name: mtapi + image: ghcr.io/asg0451/mtapi:latest + env: + - name: WEB_CONCURRENCY + value: "4" + resources: + requests: + memory: "512Mi" + cpu: "500m" + limits: + memory: "1Gi" + cpu: "2" + ports: + - containerPort: 8000 + volumeMounts: + - name: mtapi-config + mountPath: /config + volumes: + - name: mtapi-config + configMap: + name: mtapi-config