Skip to content

Commit

Permalink
Dockerize
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeiP committed Apr 29, 2024
1 parent 931b8d4 commit ee6b3da
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 4 deletions.
97 changes: 97 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: Docker

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

on:
push:
branches: [ "master" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "master" ]

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}


jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@6e04d228eb30da1757ee4e1dd75a0ec73a653e06 #v3.1.1
with:
cosign-release: 'v2.1.1'

# Set up BuildKit Docker container builder to be able to build
# multi-platform images and export cache
# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
# repository is public to avoid leaking data. If you would like to publish
# transparency data even for private images, pass --force to cosign below.
# https://github.com/sigstore/cosign
- name: Sign the published Docker image
if: ${{ github.event_name != 'pull_request' }}
env:
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
TAGS: ${{ steps.meta.outputs.tags }}
DIGEST: ${{ steps.build-and-push.outputs.digest }}
# This step uses the identity token to provision an ephemeral certificate
# against the sigstore community Fulcio instance.
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
env
*.pyc
config.py
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM python:3.7-bullseye

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
mariadb-client libmariadb-dev \
&& rm -rf /var/lib/apt/lists/*

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

EXPOSE 8000

CMD ["python", "run.py"]
4 changes: 2 additions & 2 deletions app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def upload():
# print(form.file.data.filename, file=sys.stderr)

# Add a row with the date and conversion type for each use for statistics.
with open("/srv/django/viiteaineisto/log.txt", "a") as statfile:
statfile.write(str(datetime.date.today()) + ";" + form.type.data + "\n")
# with open("/srv/django/viiteaineisto/log.txt", "a") as statfile:
# statfile.write(str(datetime.date.today()) + ";" + form.type.data + "\n")

# Apparently we need to save the contents to a file to open the stream
# in text mode for the csv reader. Create a tmp file to generate a
Expand Down
9 changes: 9 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os

ENV = os.getenv('ENVIRONMENT')
DEBUG = True
SECRET_KEY = os.getenv('SECRET_KEY')
UPLOAD_FOLDER = '/tmp'
SMTP_SERVER = 'localhost'
FEEDBACK_TO = os.getenv('FEEDBACK_TO')
EMAIL_FROM = os.getenv('EMAIL_FROM')
1 change: 1 addition & 0 deletions kubernetes/create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kubectl create secret generic hash-salt --from-literal="hash_salt=" --namespace=viiteaineisto
107 changes: 107 additions & 0 deletions kubernetes/flask.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
apiVersion: v1
kind: Namespace
metadata:
name: viiteaineisto
---
apiVersion: v1
kind: Service
metadata:
name: flask
namespace: viiteaineisto
labels:
app: flask
spec:
type: NodePort
ports:
- port: 80
nodePort: 30781
selector:
app: flask
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: flask-set
namespace: viiteaineisto
spec:
selector:
matchLabels:
app: flask
serviceName: flask
replicas: 1
updateStrategy:
type: RollingUpdate
# rollingUpdate:
# maxUnavailable: 0
# maxSurge: 1
template:
metadata:
namespace: viiteaineisto
labels:
app: flask
spec:
imagePullSecrets:
- name: regcred
containers:
- image: ghcr.io/ardcoras/viiteaineisto:master
name: flask
imagePullPolicy: Always
ports:
- containerPort: 30781
name: flask
env:
# - name: SMTP_HOST
# value: 'mail.ardcoras.fi'
# - name: SMTP_USER
# valueFrom:
# secretKeyRef:
# name: smtp
# key: user
# - name: SMTP_PASSWORD
# valueFrom:
# secretKeyRef:
# name: smtp
# key: password
- name: SECRET_KEY
valueFrom:
secretKeyRef:
name: secret-key
key: secret_key
- name: FEEDBACK_TO
value: '[email protected]'
- name: FEEDBACK_TO
value: '[email protected]'
- name: ENVIRONMENT
value: 'production'
# readinessProbe:
# initialDelaySeconds: 1
# periodSeconds: 15
# timeoutSeconds: 5
# successThreshold: 2
# failureThreshold: 2
# httpGet:
# host:
# scheme: HTTP
# path: /
# httpHeaders:
# - name: Host
# value: viiteaineisto.ardcoras.fi
# port: 80
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: flask-route
namespace: viiteaineisto
spec:
entryPoints:
- websecure
- web
routes:
- match: Host(`viiteaineisto.zeip.fi`)
kind: Rule
middlewares:
- name: headers-default@file
services:
- name: flask
port: 80
2 changes: 1 addition & 1 deletion run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from app import app

if __name__ == '__main__':
app.run()
app.run(host='0.0.0.0')

0 comments on commit ee6b3da

Please sign in to comment.