django-ex/README.md

222 lines
9.3 KiB
Markdown
Raw Normal View History

2015-05-29 17:19:22 +02:00
# Openshift quickstart: Django
This is a [Django](http://www.djangoproject.com) project that you can use as the starting point to develop your own and deploy it on an [OpenShift](https://github.com/openshift/origin) cluster.
2015-05-19 12:18:31 +02:00
It assumes you have access to an existing OpenShift installation.
2015-06-01 18:45:29 +02:00
2015-05-29 17:19:22 +02:00
## 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 .`
2015-06-01 13:12:20 +02:00
3. Update `project/settings.py` to configure `SECRET_KEY`, `DATABASE` and `STATIC_ROOT` entries
2015-06-01 10:43:14 +02:00
4. `./manage.py startapp welcome`, to create the welcome page's app
2015-05-29 17:19:22 +02:00
2015-06-01 13:12:20 +02:00
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
2015-05-29 17:19:22 +02:00
## 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/ - OpenShift-specific files
├── scripts - helper scripts
└── templates - application templates
requirements.txt - list of dependencies
```
2015-05-29 17:19:22 +02:00
## Local development
2015-05-19 12:18:31 +02:00
2015-05-29 17:19:22 +02:00
To run this project in your development machine, follow these steps:
2015-05-19 12:18:31 +02:00
1. (optional) Create and activate a [virtualenv](https://virtualenv.pypa.io/) (you may want to use [virtualenvwrapper](http://virtualenvwrapper.readthedocs.org/)).
2. Fork this repo and clone your fork:
2015-05-29 17:19:22 +02:00
`git clone https://github.com/openshift/django-ex.git`
2015-05-19 12:18:31 +02:00
3. Install dependencies:
2015-05-24 14:06:57 +02:00
`pip install -r requirements.txt`
4. Create a development database:
`./manage.py migrate`
2015-05-19 12:18:31 +02:00
5. If everything is alright, you should be able to start the Django development server:
2015-05-19 12:18:31 +02:00
2015-05-24 14:06:57 +02:00
`./manage.py runserver`
2015-05-19 12:18:31 +02:00
6. Open your browser and go to http://127.0.0.1:8000, you will be greeted with a welcome page.
2015-05-19 12:18:31 +02:00
2015-06-01 12:48:01 +02:00
## Deploying to OpenShift
2015-06-01 13:12:20 +02:00
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.
2015-06-01 18:45:29 +02:00
### Using an application template
The directory `openshift/templates/` contains OpenShift application templates that you can add to your OpenShift project with:
2015-06-01 12:48:01 +02:00
osc create -f openshift/templates/<TEMPLATE_NAME>.json
2015-06-01 12:48:01 +02:00
The template `django.json` contains just a minimal set of components to get your Django application into OpenShift.
2015-06-01 12:48:01 +02:00
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.
2015-06-01 12:48:01 +02:00
Alternatively, you can use the command line to create your new app:
osc new-app --template=<TEMPLATE_NAME> --param=GIT_REPOSITORY=...,...
Your application will be built and deployed automatically. If that doesn't happen, you can debug your build:
osc get builds
# take build name from the command above
osc build-logs <build-name>
And you can see information about your deployment too:
osc describe dc/django
In the web console, the overview tab shows you a service, by default called "django", that encapsulates all pods running your Django application. You can access your application by browsing to the service's IP address and port.
2015-06-01 12:48:01 +02:00
2015-06-01 18:45:29 +02:00
### 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:
```bash
$ 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.
```
You can access your application by browsing to the service's IP address and port.
2015-06-01 12:48:01 +02:00
## Logs
2015-05-19 12:18:31 +02:00
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:
2015-05-29 17:19:22 +02:00
osc get pods # list all pods in your project
osc logs <pod-name>
2015-06-01 12:44:15 +02:00
This can be useful to observe the correct functioning of your application.
2015-06-01 12:44:15 +02:00
## Special environment variables
2015-06-01 12:44:15 +02:00
### 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](http://docs.gunicorn.org/en/latest/settings.html).
### 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](https://docs.djangoproject.com/en/1.8/ref/settings/#std:setting-SECRET_KEY).
## 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 `osc`.
In the future, the `osc` 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. Inpect the output of the command below to find the name of a pod that matches a given label:
osc get pods -l <your-label-selector>
2. Open a shell in the pod of your choice:
osc exec -p <pod-name> -it -- bash
3. Because of how `kubectl exec` and `osc exec` work right now, your current working directory is root (/). Change it to where your code lives:
cd $HOME
4. Because of how the images produced with CentOS and RHEL work currently, you need to manually enable any Software Collections you need to use:
source scl_source enable python33
5. Finally, execute any command that you need and exit the shell.
Related GitHub issues:
1. https://github.com/GoogleCloudPlatform/kubernetes/issues/8876
2. https://github.com/GoogleCloudPlatform/kubernetes/issues/7770
3. https://github.com/openshift/origin/issues/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=frontend POD_INDEX=2 ./run-in-container.sh ./manage.py shell
2015-05-19 12:18:31 +02:00
2015-05-29 17:19:22 +02:00
## Data persistence
2015-06-01 12:48:58 +02:00
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.
2015-06-01 12:49:15 +02:00
2015-06-01 13:12:20 +02:00
## 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](https://github.com/openshift/origin), or by filling an issue.
2015-06-01 12:49:15 +02:00
## License
This code is dedicated to the public domain to the maximum extent permitted by applicable law, pursuant to [CC0](http://creativecommons.org/publicdomain/zero/1.0/).