No description
Find a file
Rodolfo Carvalho 3ae50da49f Fix spacing
2015-06-01 18:45:29 +02:00
.sti/bin Run django migrations during deploy 2015-05-22 14:03:13 +02:00
openshift Add application template without database 2015-06-01 18:43:32 +02:00
project Automatically plug database via env vars 2015-05-29 15:52:28 +02:00
scripts Update application template 2015-06-01 18:37:01 +02:00
welcome Generalize div's id 2015-06-01 10:43:44 +02:00
.gitignore Add Django project 2015-05-19 12:19:44 +02:00
gunicorn_conf.py Add config to send access log to file 2015-05-22 17:10:19 +02:00
manage.py Add Django project 2015-05-19 12:19:44 +02:00
README.md Fix spacing 2015-06-01 18:45:29 +02:00
requirements.txt Add Django Debug Toolbar 2015-05-24 13:09:31 +02:00

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.

It assumes you have access to an existing OpenShift installation.

What has been done for you

This is a minimal Django 1.8 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

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. Fork this repo and clone your fork:

    git clone https://github.com/openshift/django-ex.git

  3. Install dependencies:

    pip install -r requirements.txt

  4. Create a development database:

    ./manage.py migrate

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

    ./manage.py runserver

  6. 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/ contains OpenShift application template files that you can add you your OpenShift project with:

osc create -f openshift/<TEMPLATE_NAME>.json

Now you can go to your OpenShift web console and create a new app from one of the templates that you have just added. After adjusting your preferences (or accepting the defaults), your application will be built and deployed.

You will probably want to set the GIT_REPOSITORY parameter to point to your fork.

Alternatively, you can use the command line to create your new app:

osc new-app --template=<TEMPLATE_NAME> --param=GIT_REPOSITORY=...,...

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

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:

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

Special files in this repository

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

.sti/
└── bin/           - scripts used by source-to-image
    ├── assemble   - executed to produce a Docker image with your code and dependencies during build
    └── run        - executed to start your app during deployment

openshift/         - application templates for OpenShift

scripts/           - helper scripts to automate some tasks

gunicorn_conf.py   - configuration for the gunicorn HTTP server

requirements.txt   - list of dependencies

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 osc 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 OpenShift Origin repo, or by filling an issue.

License

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