Skip to content

Commit

Permalink
Merge pull request #5 from dlcs/feature/cleanup-handler
Browse files Browse the repository at this point in the history
Create a cleanup handler for the varnish cache to provide 'ban' functionality
  • Loading branch information
JackLewis-digirati authored Aug 11, 2023
2 parents 29f9fb2 + db2212a commit afcb20a
Show file tree
Hide file tree
Showing 16 changed files with 643 additions and 51 deletions.
53 changes: 53 additions & 0 deletions .github/actions/docker-build-and-push/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Docker Build & Push
description: Composite GitHub Action to build and push Docker images to the DLCS GitHub Packages repositories.

inputs:
image-name:
description: "Name of the image to push to the GHCR repository."
required: true
dockerfile:
description: "The Dockerfile to build and push."
required: true
context:
description: "The context to use when building the Dockerfile."
required: true
github-token:
description: "The GitHub token used when interacting with GCHR."
required: true

runs:
using: "composite"
steps:
- id: checkout
uses: actions/checkout@v2
- id: docker-setup-buildx
uses: docker/setup-buildx-action@v2
with:
driver-opts: |
image=moby/buildkit:v0.10.6
- id: docker-meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/dlcs/${{ inputs.image-name }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,enable=true,prefix=,format=long
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
- id: docker-login
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ inputs.github-token }}
- id: docker-build-push
uses: docker/build-push-action@v4
with:
context: ${{ inputs.context }}
file: ${{ inputs.dockerfile }}
builder: ${{ steps.docker-setup-buildx.outputs.name }}
tags: ${{ steps.docker-meta.outputs.tags }}
labels: ${{ steps.docker-meta.outputs.labels }}
push: ${{ github.actor != 'dependabot[bot]' }}
54 changes: 17 additions & 37 deletions .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,25 @@ on:
- master

jobs:
build-push:
build-push-varnish:
runs-on: ubuntu-latest

steps:
- name: Check out code
id: checkout
uses: actions/checkout@v2

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
- uses: actions/checkout@v3
- uses: ./.github/actions/docker-build-and-push
name: build and push
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
image-name: "dlcs-varnish"
dockerfile: "Dockerfile"
context: "."
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Docker meta
id: docker_meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/dlcs/dlcs-varnish
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,enable=true,prefix=,format=long
- name: Build and push
id: docker_build
uses: docker/build-push-action@v4
build-push-dlcs-varnish-cleanup:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/docker-build-and-push
with:
context: .
builder: ${{ steps.buildx.outputs.name }}
push: true
labels: ${{ steps.docker_meta.outputs.labels }}
tags: ${{ steps.docker_meta.outputs.tags }}


image-name: "dlcs-varnish-cleanup"
dockerfile: "varnish-cleanup/Dockerfile"
context: "./varnish-cleanup"
github-token: ${{ secrets.GITHUB_TOKEN }}
165 changes: 165 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
Scripts/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
pyvenv.cfg

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

# General
*.exe
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ RUN pip install awscli
COPY start.sh /start.sh
RUN chmod +x /start.sh

WORKDIR /usr/app/src
COPY varnish-cleanup/requirements.txt ./
RUN pip install -r requirements.txt

COPY varnish-cleanup/cleanup_handler.py ./
COPY varnish-cleanup/app ./app

ENV VARNISH_PORT 80
EXPOSE 80

Expand Down
46 changes: 39 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

Docker image using disk-backed Varnish instance for caching.

On startup it uses the AWS CLI to copy vcl file from location specified by `S3_VCL_FILE` environment variable.
On startup it uses the AWS CLI to copy vcl file from location specified by `S3_VCL_FILE` environment variable.
Optionally, a local file can also be configured for development purposes

## Configuration

The following environment files are expected:

The following environment settings are expected:
* `S3_VCL_FILE` - The location of a vcl file to use. Expected S3Uri as it is used by [aws s3 cp](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html) command.
* `VARNISH_CACHE_FOLDER` - Folder where disk backed cache is stored.
* `VARNISH_CACHE_SIZE` - Size of cache.
* `VARNISH_PORT` - Which port Varnish should listen on (defaults to 80)
* `AWS_PROFILE` - Required to run locally
* `INCOMING_QUEUE` - the name of the queue that the cleanup handler listens to

The following configuration is optional:

* `VARNISH_ADDRESS` - The location of varnish used by the cleanup handler. Defaults to localhost
* `AWS_REGION` - The AWS region. Defaults to eu-west-1
* `USE_LOCAL_CONFIG` - Whether to use a local config file over S3.

*NOTE:* using `USE_LOCAL_CONFIG` requires a `mount`to be added to the `docker run` containing the VCL
## Running

```bash
Expand All @@ -21,10 +29,34 @@ docker build -t dlcs-varnish:local .

# run
docker run -it --rm \
--env AWS_ACCESS_KEY_ID='xxx' \
--env AWS_SECRET_ACCESS_KEY='xxx' \
--env S3_VCL_FILE='s3://my-bucket/varnish-config.vcl' \
--env VARNISH_CACHE_FOLDER='/path/to/folder' \
--env VARNISH_CACHE_SIZE='100M'
--env VARNISH_CACHE_SIZE='100M' \
--env-file='/path/to/env' \
{REQUIRED FOR LOCAL RUNNING}--volume $HOME\.aws\credentials:/root/.aws/credentials:ro \
{OPTIONAL}--mount type=bind,source=.\etc\default.vcl,target=/mnt/varnish/default.vcl \
dlcs-varnish:local
```
# varnish-cleanup

Additionally, there is a standalone docker container for the cleanup handler.

## Configuration

Required:
* `AWS_PROFILE` - Required to run locally

Optional:
* `VARNISH_ADDRESS` - The location of varnish used by the cleanup handler. Defaults to localhost
* `AWS_REGION` - The region used by the cleanup handler. Defaults to eu-west-1

```bash
# build
docker build -t dlcs-varnish-cleanup:local ./varnish-cleanup

# run
docker run -it --rm \
--env-file='/path/to/env'
{REQUIRED FOR LOCAL RUNNING}--volume=$HOME\.aws\credentials:/root/.aws/credentials:ro
dlcs-varnish:local
```
Loading

0 comments on commit afcb20a

Please sign in to comment.