Commit 656dd5af authored by ∞'s avatar 💻

Add django example

parent 0bae7ad8
Pipeline #7215 failed
build:
image: docker:dind
stage: build
script:
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA -t $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME .
review-deploy:
image: yourlabs/ansible
stage: test
environment:
name: test/$CI_COMMIT_REF_NAME
url: http://${CI_ENVIRONMENT_SLUG}.xdevops.ci.yourlabs.io
on_stop: review-stop
before_script:
- mkdir -p ~/.ssh; echo "$ssh_key" > ~/.ssh/id_rsa; echo "$ssh_fingerprints" > ~/.ssh/known_hosts; chmod 700 ~/.ssh; chmod 600 ~/.ssh/*
script:
- export HOST=${CI_ENVIRONMENT_SLUG}.xdevops.ci.yourlabs.io
- bigsudo
yourlabs.compose
image=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
pull=no
compose=docker-compose.yml,docker-compose.ephemeral.yml
project=$CI_ENVIRONMENT_SLUG
lifetime=300
xdevops@ci.yourlabs.io
except:
refs:
- master
review-stop:
stage: test
image: yourlabs/python
script:
- docker-compose -p $CI_ENVIRONMENT_SLUG down -v --remove-orphans
- docker-compose -p $CI_ENVIRONMENT_SLUG rm -fsv
when: manual
environment:
action: stop
name: test/$CI_COMMIT_REF_NAME
except:
refs:
- master
deploy:
image: yourlabs/python
stage: deploy
environment:
name: demo
url: https://xdevops.ci.yourlabs.io
before_script:
mkdir -p ~/.ssh; echo "$ssh_key" > ~/.ssh/id_rsa; echo "$ssh_fingerprints" > ~/.ssh/known_hosts; chmod 700 ~/.ssh; chmod 600 ~/.ssh/*
script:
- export HOST=xdevops.ci.yourlabs.io
- export PROTO=https
- bigsudo
yourlabs.compose
image=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
pull=no
compose=docker-compose.yml,docker-compose.persistent.yml
home=/home/xdevops
xdevops@ci.yourlabs.io
only:
refs:
- master
- staging
FROM alpine:edge
ENV DJANGO_SETTINGS_MODULE=xdevops.settings
ENV UWSGI_MODULE=xdevops.wsgi:application
ENV STATIC_URL=/static/ STATIC_ROOT=/app/static
ENV PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1
EXPOSE 8000
RUN apk update && apk --no-cache upgrade && apk --no-cache add gettext shadow python3 py3-pip py3-psycopg2 uwsgi-python uwsgi-http uwsgi-spooler dumb-init bash git curl
RUN useradd -md /app app
WORKDIR /app
COPY . /app/
RUN pip install django
USER app
RUN xdevops collectstatic --noinput
RUN ls -l /app/static
RUN find /app/static
RUN gzip -k -6 $(find /app/static -type f)
CMD /usr/bin/dumb-init bash -c "until djcli dbcheck; do sleep 1; done && xdevops migrate --noinput && uwsgi \
--spooler=/app/spool \
--spooler-processes 3 \
--http-socket=0.0.0.0:8000 \
--chdir=/app \
--spooler-chdir=/app \
--plugin=python3,http \
--module=xdevops.wsgi:application \
--http-keepalive \
--harakiri=120 \
--max-requests=100 \
--master \
--workers=12 \
--processes=6 \
--log-5xx \
--vacuum \
--enable-threads \
--post-buffering=8192 \
--ignore-sigpipe \
--ignore-write-errors \
--disable-write-exception \
--mime-file /etc/mime.types \
--thunder-lock \
--offload-threads '%k' \
--static-map $STATIC_URL=$STATIC_ROOT \
--static-gzip-all"
version: "3.5"
services:
django:
networks:
- default
- web
environment:
- HOST
- PROTO=http
labels:
- "traefik.enable=true"
- "traefik.port=8000"
- "traefik.docker.network=web"
- "traefik.http.routers.xdevops-${CI_COMMIT_REF_SLUG}.entryPoints=web"
- "traefik.http.routers.xdevops-${CI_COMMIT_REF_SLUG}.rule=Host(`$HOST`)"
postgres:
networks:
- default
networks:
web:
external: true
version: "3.5"
services:
django:
restart: always
environment:
- HOST
- PROTO=https
networks:
- web
- default
labels:
- "traefik.enable=true"
- "traefik.port=8000"
- "traefik.docker.network=web"
- "traefik.http.routers.xdevops-${CI_COMMIT_REF_SLUG}.entryPoints=web,websecure"
- "traefik.http.routers.xdevops-${CI_COMMIT_REF_SLUG}.rule=Host(`$HOST`)"
- "traefik.http.routers.xdevops-${CI_COMMIT_REF_SLUG}.tls=true"
- "traefik.http.routers.xdevops-${CI_COMMIT_REF_SLUG}.tls.certResolver=leresolver"
postgres:
restart: always
volumes:
- ./data/postgres:/var/lib/postgresql/data
labels:
- "io.yourlabs.compose.mkdir=./dump,./data/postgres:999:999:0700"
networks:
web:
external: true
version: '3.5'
services:
git:
image: yourlabs/yourlab-git
labels:
- "traefik.enable=true"
postgres:
image: postgres
environment:
- POSTGRES_DB=xdevops
- POSTGRES_USER=xdevops
- POSTGRES_PASSWORD=xdevops
volumes:
- /var/lib/postgresql/data
django:
image: yourlabs/yourlab
environment:
- DB_URI=postgres
- DB_NAME=xdevops
- DB_USER=xdevops
- DB_PASS=xdevops
- CI_COMMIT_REF_SLUG
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'xdevops.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
"""
ASGI config for xdevops project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'xdevops.settings')
application = get_asgi_application()
"""
Django settings for xdevops project.
Generated by 'django-admin startproject' using Django 3.0.5.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2dej42t)b=wqrczfkfqth2_uqpdvz7s2-=s$j2gc2#(3rew_sa'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = [os.getenv('HOST', '*')]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'xdevops.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'xdevops.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/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/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
"""xdevops URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
"""
WSGI config for xdevops project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'xdevops.settings')
application = get_wsgi_application()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment