Skip to content
Snippets Groups Projects
Commit 1cd4e667 authored by nimrod's avatar nimrod
Browse files

Migrate from platform.sh to docker-compose.

parent 7b30f815
No related branches found
No related tags found
No related merge requests found
.git*
*.py[cod]
static/
......@@ -109,3 +109,5 @@ dmypy.json
# Pyre type checker
.pyre/
static/
# This file describes an application. You can have multiple applications
# in the same project.
#
# See https://docs.platform.sh/user_guide/reference/platform-app-yaml.html
# The name of this app. Must be unique within a project.
name: 'app'
# The runtime the application uses.
type: 'python:3.7'
# The build-time dependencies of the app.
dependencies:
python3:
pipenv: '2018.10.13'
# The relationships of the application with services or other applications.
#
# The left-hand side is the name of the relationship as it will be exposed
# to the application in the PLATFORM_RELATIONSHIPS variable. The right-hand
# side is in the form `<service name>:<endpoint name>`.
relationships:
database: "postgresqldb:postgresql"
# The configuration of app when it is exposed to the web.
web:
# Whether your app should speak to the webserver via TCP or Unix socket
# https://docs.platform.sh/configuration/app-containers.html#upstream
upstream:
socket_family: unix
# Commands are run once after deployment to start the application process.
commands:
start: "gunicorn -w 4 -b unix:$SOCKET myapp.wsgi:application"
locations:
"/":
passthru: true
"/static":
root: "static"
expires: 1h
allow: true
# The size of the persistent disk of the application (in MB).
disk: 512
# Set a local R/W mount for logs
mounts:
'logs':
source: local
source_path: logs
# The hooks executed at various points in the lifecycle of the application.
hooks:
# The build hook runs before the application is deployed, and is useful for
# assembling the codebase.
build: |
pipenv install --system --deploy
mkdir logs
python manage.py collectstatic
rm -rf logs
deploy: |
python manage.py migrate
# The routes of the project.
#
# Each route describes how an incoming URL is going to be processed by Platform.sh.
#
# See https://docs.platform.sh/user_guide/reference/routes-yaml.html
"https://{default}/":
type: upstream
upstream: "app:http"
"https://www.{default}/":
type: redirect
to: "https://{default}/"
# The services of the project.
#
# Each service listed will be deployed in its own container as part of your
# Platform.sh project.
#
# See https://docs.platform.sh/user_guide/reference/services-yaml.html
postgresqldb:
type: postgresql:9.6
disk: 1024
FROM kennethreitz/pipenv
COPY . /app
RUN mkdir logs && \
python3 manage.py collectstatic && \
rm -rf logs
CMD gunicorn --bind=0.0.0.0:8000 myapp.wsgi:application
# Django 2 LTS template for Platform.sh
This project provides a starter kit for Django projects hosted on Platform.sh. It is primarily an example, although could be used as the starting point for a real project.
Notice specifically the `settings.py` where we read some of the environment variables and configure Django
to connect to the correct database, and run in Debug mode when not running the `master` branch.
In this example we are running Django with Gunicorn. You can check-out other examples to see it run with
other application servers.
## Starting a new project
To start a new project based on this template, follow these 3 simple steps:
1. Clone this repository locally. You may optionally remove the `origin` remote or remove the `.git` directory and re-init the project if you want a clean history.
2. Create a new project through the Platform.sh user interface and select "Import an existing project" when prompted.
3. Run the provided Git commands to add a Platform.sh remote and push the code to the Platform.sh repository.
That's it! You now have a working "hello world" level project you can build on.
## Using as a reference
You can also use this repository as a reference for your own projects, and borrow whatever code is needed. The most important parts are the `.platform.app.yaml` file and the `.platform` directory.
# Igentify assignment
---
version: '3'
services:
postgres:
image: postgres:alpine
environment: &environment
POSTGRES_PASSWORD: password
POSTGRES_DB: myapp
myapp:
build:
context: ./
environment: *environment
depends_on:
- postgres
ports:
- 8000:8000
command: |
/bin/sh -c "
while ! nc -z postgres 5432; do sleep 1; done
python3 manage.py migrate
gunicorn --bind=0.0.0.0:8000 myapp.wsgi:application"
#!/usr/bin/env python
#!/usr/bin/env python3
import os
import sys
......
......@@ -74,17 +74,6 @@ TEMPLATES = [
WSGI_APPLICATION = 'myapp.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
......@@ -122,42 +111,14 @@ USE_TZ = True
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# Import some Platform.sh settings from the environment.
app_dir = os.getenv('PLATFORM_APP_DIR')
if app_dir:
STATIC_ROOT = os.path.join(app_dir, 'static')
entropy = os.getenv('PLATFORM_PROJECT_ENTROPY')
if entropy:
SECRET_KEY = entropy
routes = os.getenv('PLATFORM_ROUTES')
if routes:
routes = json.loads(base64.b64decode(routes).decode('utf-8'))
app_name = os.getenv('PLATFORM_APPLICATION_NAME')
for url, route in routes.items():
host = urlparse(url).netloc
if (host not in ALLOWED_HOSTS and route['type'] == 'upstream'
and route['upstream'] == app_name):
ALLOWED_HOSTS.append(host)
relationships = os.getenv('PLATFORM_RELATIONSHIPS')
if relationships:
relationships = json.loads(base64.b64decode(relationships).decode('utf-8'))
db_settings = relationships['database'][0]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': db_settings['path'],
'USER': db_settings['username'],
'PASSWORD': db_settings['password'],
'HOST': db_settings['host'],
'PORT': db_settings['port'],
'NAME': 'myapp',
'USER': os.getenv('POSTGRES_USER', 'postgres'),
'PASSWORD': os.getenv('POSTGRES_PASSWORD'),
'HOST': os.getenv('POSTGRES_HOST', 'postgres'),
},
'sqlite': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment