• Stars
    star
    247
  • Rank 164,085 (Top 4 %)
  • Language
    HTML
  • Created over 9 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Django Example

Openshift quickstart: Django

This is a Django project that you can use as the starting point to develop your own and deploy it on an OpenShift cluster.

NOTE: This is the oldest example of Django application for specific Django LTS version. If you want a newer version, check the alternative branches.

The steps in this document assume that you have access to an OpenShift deployment that you can deploy applications on.

What has been done for you

This is a minimal Django 1.11 project. It was created with these steps:

  1. Create a virtualenv
  2. Manually install Django and other dependencies
  3. pip freeze > requirements.txt
  4. django-admin startproject project .
  5. Update project/settings.py to configure SECRET_KEY, DATABASE and STATIC_ROOT entries
  6. ./manage.py startapp welcome, to create the welcome page's app

From this initial state you can:

  • create new Django apps
  • remove the welcome app
  • rename the Django project
  • update settings to suit your needs
  • install more Python libraries and add them to the requirements.txt file

Special files in this repository

Apart from the regular files created by Django (project/*, welcome/*, manage.py), this repository contains:

openshift/         - OpenShift-specific files
β”œβ”€β”€ scripts        - helper scripts
└── templates      - application templates

requirements.txt   - list of dependencies

Warnings

Please be sure to read the following warnings and considerations before running this code on your local workstation, shared systems, or production environments.

Database configuration

The sample application code and templates in this repository contain database connection settings and credentials that rely on being able to use sqlite.

Automatic test execution

The sample application code and templates in this repository contain scripts that automatically execute tests via the postCommit hook. These tests assume that they are being executed against a local test sqlite database. If alternate database credentials are supplied to the build, the tests could make undesirable changes to that database.

Local development

To run this project in your development machine, follow these steps:

  1. (optional) Create and activate a virtualenv (you may want to use virtualenvwrapper).

  2. Ensure that the executable pg_config is available on your machine. You can check this using which pg_config. If not, install the dependency with one of the following.

  • macOS: brew install postgresql using Homebrew
  • Ubuntu: sudo apt-get install libpq-dev
  • Others

Note: pg_config is not needed. You can use sqlite instead.

  1. Fork this repo and clone your fork. Make sure that you are in the right branch.

  2. Install dependencies:

    pip install -r requirements.txt

  3. Create a development database:

    ./manage.py migrate

  4. If everything is alright, you should be able to start the Django development server:

    ./manage.py runserver

  5. Open your browser and go to http://127.0.0.1:8000, you will be greeted with a welcome page.

Deploying to OpenShift

To follow the next steps, you need to be logged in to an OpenShift cluster and have an OpenShift project where you can work on.

Using an application template

The directory openshift/templates/ contains OpenShift application templates that you can add to your OpenShift project with:

oc create -f openshift/templates/<TEMPLATE_NAME>.json

The template django.json contains just a minimal set of components to get your Django application into OpenShift.

The template django-postgresql.json contains all of the components from django.json, plus a PostgreSQL database service and an Image Stream for the Python base image. For simplicity, the PostgreSQL database in this template uses ephemeral storage and, therefore, is not production ready.

After adding your templates, you can go to your OpenShift web console, browse to your project and click the create button. Create a new app from one of the templates that you have just added.

Adjust the parameter values to suit your configuration. Most times you can just accept the default values, however you will probably want to set the GIT_REPOSITORY parameter to point to your fork and the DATABASE_* parameters to match your database configuration.

Alternatively, you can use the command line to create your new app, assuming your OpenShift deployment has the default set of ImageStreams defined. Instructions for installing the default ImageStreams are available here. If you are defining the set of ImageStreams now, remember to pass in the proper cluster-admin credentials and to create the ImageStreams in the 'openshift' namespace:

oc new-app openshift/templates/django.json -p SOURCE_REPOSITORY_URL=<your repository location>

Your application will be built and deployed automatically. If that doesn't happen, you can debug your build:

oc get builds
# take build name from the command above
oc logs build/<build-name>

And you can see information about your deployment too:

oc describe dc/django-example

In the web console, the overview tab shows you a service, by default called "django-example", that encapsulates all pods running your Django application. You can access your application by browsing to the service's IP address and port. You can determine these by running

oc get svc

Without an application template

Templates give you full control of each component of your application. Sometimes your application is simple enough and you don't want to bother with templates. In that case, you can let OpenShift inspect your source code and create the required components automatically for you:

$ oc new-app centos/python-35-centos7~https://github.com/sclorg/django-ex
imageStreams/python-35-centos7
imageStreams/django-ex
buildConfigs/django-ex
deploymentConfigs/django-ex
services/django-ex
A build was created - you can run `oc start-build django-ex` to start it.
Service "django-ex" created at 172.30.16.213 with port mappings 8080.

You can access your application by browsing to the service's IP address and port.

Logs

By default your Django application is served with gunicorn and configured to output its access log to stderr. You can look at the combined stdout and stderr of a given pod with this command:

oc get pods         # list all pods in your project
oc logs <pod-name>

This can be useful to observe the correct functioning of your application.

Special environment variables

APP_CONFIG

You can fine tune the gunicorn configuration through the environment variable APP_CONFIG that, when set, should point to a config file as documented here.

DJANGO_SECRET_KEY

When using one of the templates provided in this repository, this environment variable has its value automatically generated. For security purposes, make sure to set this to a random string as documented here.

One-off command execution

At times you might want to manually execute some command in the context of a running application in OpenShift. You can drop into a Python shell for debugging, create a new user for the Django Admin interface, or perform any other task.

You can do all that by using regular CLI commands from OpenShift. To make it a little more convenient, you can use the script openshift/scripts/run-in-container.sh that wraps some calls to oc. In the future, the oc CLI tool might incorporate changes that make this script obsolete.

Here is how you would run a command in a pod specified by label:

  1. Inspect the output of the command below to find the name of a pod that matches a given label:

     oc get pods -l <your-label-selector>
    
  2. Open a shell in the pod of your choice. Because of how the images produced with CentOS and RHEL work currently, we need to wrap commands with bash to enable any Software Collections that may be used (done automatically inside every bash shell).

     oc exec -p <pod-name> -it -- bash
    
  3. Finally, execute any command that you need and exit the shell.

Related GitHub issues:

  1. kubernetes/kubernetes#8876
  2. openshift/origin#2001

The wrapper script combines the steps above into one. You can use it like this:

./run-in-container.sh ./manage.py migrate          # manually migrate the database
                                                   # (done for you as part of the deployment process)
./run-in-container.sh ./manage.py createsuperuser  # create a user to access Django Admin
./run-in-container.sh ./manage.py shell            # open a Python shell in the context of your app

If your Django pods are labeled with a name other than "django", you can use:

POD_NAME=name ./run-in-container.sh ./manage.py check

If there is more than one replica, you can also specify a POD by index:

POD_INDEX=1 ./run-in-container.sh ./manage.py shell

Or both together:

POD_NAME=django-example POD_INDEX=2 ./run-in-container.sh ./manage.py shell

Data persistence

You can deploy this application without a configured database in your OpenShift project, in which case Django will use a temporary SQLite database that will live inside your application's container, and persist only until you redeploy your application.

After each deploy you get a fresh, empty, SQLite database. That is fine for a first contact with OpenShift and perhaps Django, but sooner or later you will want to persist your data across deployments.

To do that, you should add a properly configured database server or ask your OpenShift administrator to add one for you. Then use oc env to update the DATABASE_* environment variables in your DeploymentConfig to match your database settings.

Redeploy your application to have your changes applied, and open the welcome page again to make sure your application is successfully connected to the database server.

Looking for help

If you get stuck at some point, or think that this document needs further details or clarification, you can give feedback and look for help using the channels mentioned in the OKD repo, or by filing an issue.

License

This code is dedicated to the public domain to the maximum extent permitted by applicable law, pursuant to CC0.

More Repositories

1

s2i-python-container

Python container images based on Red Hat Software Collections and intended for OpenShift and general usage, that provide a platform for building and running Python applications. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
260
star
2

s2i-nodejs-container

NodeJS images based on Red Hat Software Collections and intended for OpenShift and general usage, that provide a platform for building and running NodeJS applications. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
165
star
3

postgresql-container

PostgreSQL container images based on Red Hat Software Collections and intended for OpenShift and general usage. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
163
star
4

mysql-container

MySQL container images based on Red Hat Software Collections and intended for OpenShift and general usage. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
128
star
5

s2i-php-container

PHP container images based on Red Hat Software Collections and intended for OpenShift and general usage, that provide a platform for building and running PHP applications. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
108
star
6

nginx-container

Nginx high-performance HTTP server and reverse proxy container images based on Red Hat Software Collections and intended for OpenShift and general usage. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
91
star
7

s2i-base-container

OpenShift base images
Dockerfile
86
star
8

rhscl-dockerfiles

DEPRECATED AND NOT UPDATED set of dockerfiles for various Software Collection packages.
Shell
80
star
9

softwarecollections

Software Collections Management Website and Utils
Python
66
star
10

centos-release-scl

yum Configs and basic docs for Software Collections as delivered via the CentOS SCLo SIG.
Shell
62
star
11

cakephp-ex

CakePHP Example
PHP
60
star
12

s2i-ruby-container

Ruby container images based on Red Hat Software Collections and intended for OpenShift and general usage, that provide a platform for building and running Ruby applications. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
55
star
13

mongodb-container

MongoDB container images based on Red Hat Software Collections and intended for OpenShift and general usage. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
50
star
14

httpd-container

Apache HTTP container images based on Red Hat Software Collections and intended for OpenShift and general usage. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
47
star
15

devtoolset-container

Devtoolset container images based on Red Hat Software Collections, that provide a platform for building and running C and C++ applications. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
44
star
16

golang-container

Golang container image sources
Shell
43
star
17

rails-ex

Ruby Rails Example
HTML
35
star
18

httpd-ex

An example application repository for the s2i httpd builder image
HTML
32
star
19

mariadb-container

MariaDB container images based on Red Hat Software Collections and intended for OpenShift and general usage. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
31
star
20

ruby-ex

Ruby
30
star
21

redis-container

Redis container images based on Red Hat Software Collections and intended for OpenShift and general usage. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
25
star
22

dancer-ex

Perl Dancer Example
Perl
23
star
23

nginx-ex

An example application repository for the s2i nginx builder image
HTML
23
star
24

container-common-scripts

Shell
20
star
25

welcome

Welcome page with basic overview around the sclorg organization, aka what all one can find here.
16
star
26

s2i-perl-container

Perl container images based on Red Hat Software Collections and intended for OpenShift and general usage, that provide a platform for building and running Perl applications. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
16
star
27

scl-utils

Tool to setup and run software from Software Collection environment
C
15
star
28

testing-farm-as-github-action

GitHub Action to execute tests by Testing Farm and update Pull Request status
TypeScript
13
star
29

scl-examples

Set of spec files following the best practices for Software Collections
Shell
8
star
30

varnish-container

Varnish HTTP Cache container images based on Red Hat Software Collections and intended for OpenShift and general usage. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
7
star
31

spec2scl

Python
7
star
32

golang-ex

A sample app that is built using the s2i golang builder
Go
7
star
33

cassandra-container

Cassandra container images based on Software Collections and intended for OpenShift and general usage. Currently only CentOS based image is available. The Apache Cassandra database is the right choice when you need scalability and high availability without compromising performance.
Shell
6
star
34

rhscl-rebuild-recipes

This repository is intended to gather recipes to rebuild Software Collections we have now in RHSCL.
5
star
35

ansible-tests

This repository contains ansible tests for rhscl containers
Shell
4
star
36

rpm-list-builder

RPM List Builder helps you to build a list of defined RPM packages including Software Collection from the recipe file
Python
4
star
37

passenger-container

Phusion Passenger container images based on Red Hat Software Collections and intended for OpenShift and general usage. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
4
star
38

ror-container

Container images based on Red Hat Software Collections and intended for OpenShift and general usage, that provide a platform for building and running Ruby on Rails applications. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Shell
4
star
39

scltests

Simple tests designed to keep up with regressions in scl-utils
Python
3
star
40

llvm-container

LLVM container image sources
Shell
3
star
41

sclo-ci-tests

Scripts for testing SCLo builds from cbs.centos.org
Shell
2
star
42

postgresql

PostgreSQL packaged for software collections
Shell
2
star
43

s2i-light

This is a podman-compatible lightweight re-implementation of the original source-to-image.
Shell
2
star
44

rhscl2dockerfile

DEPRECATED: Simple script to generate Software collections Dockerfiles
Shell
2
star
45

buildpacks

Cloud Native Buildpacks and Builders based on Red Hat Universal Base Image
Shell
1
star
46

container-workflow-tool

Python
1
star
47

memcached

Container for memcached - high-performance, distributed memory object caching system
Python
1
star
48

container-ci-suite

This repos is used for testing RHSCL containers
Python
1
star
49

helm-charts

Python
1
star
50

betka-fedmsg

Bot for transfering fedmsg events to celery tasks.
Python
1
star
51

betka

Python
1
star
52

rust-container

Rust container image sources
Shell
1
star