Coder Social home page Coder Social logo

django-ex's Introduction

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.

django-ex's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-ex's Issues

Git Push not reflecting changes

Git push doesn't reflect the new addition and changes in local branch. How is that possible ?

List directories on openshift

VIEWS_BASE_DIR welcome templates welcome
['index.html']

On local
VIEWS_BASE_DIR welcome templates welcome
['index.html', 'index0.html', 'indexa.html', 'LBase', 'Portfolio', 'services.html', 'sidebar.html']

Dead links

Both master and 1.11.x branches contains a lot of dead links to OpenShift documentation. Also, it would be nice to test this project with the lastest OpenShift.

Any volunteers?

Whitenoise requirement?

I noticed that whitenoise is in the requirements.txt file, and it in the wsgy.py file, but in reading the docs from whitenoise, it doesn't seem to match the django setup.

Is this correct?

Why is APP_CONFIG defined in the template?

I just spent 2hrs figuring out why my app_config values in my .s2i/environment setup didn't work, and it turns out that the template has a blank APP_CONFIG variable.

Is this there for a reason? It seems to collide with the .s2i documentation, or rather it's spirit.

add new build environment variable to allow this template to be easily deployed in air gapped environments

When deploying this template or any python based application in an air gapped, disconnected or non standard environment where the custom pypi is non standard the build pod fails to build because it does not trust the certificate of the provided custom pypi server. The way we have been able to get around this has been to add a build environment variable named PIP_TRUSTED_HOST and set its value to the FQDN or IP of the custom server. Adding alongside the PIP_INDEX_URL will allow for easier deployment. In standard environment that variable would not be needed.

Log python

I tried to debug my app but I don't find python log with
rhc tail -a project
Or when I got to /var/lib/openshift/id/app-root/logs/python.log file I don't find more log than displayed at screen

How can I see all my python output console ?

Met AttributeError: 'NoneType' object has no attribute 'upper'

  1. Login openshift and create project for the user

$ osc login --server=$master -u $user -p $password
$ osc new-project test

  1. Create imageStream

$ osc create -f image-streams-centos7.json
(the file "image-streams-centos7.json" is same with https://github.com/openshift/origin/blob/master/examples/image-streams/image-streams-centos7.json)

  1. Login openshift in web console

  2. Create application in console

"Create.." -> Input repo url "https://github.com/openshift/django-ex" -> "Next" -> Select a builder image "python:latest" -> finish create

  1. Start a build in webconsole

Browse -> Builds -> "Start Build"

  1. Wait build complete and check the log of pod

$ osc get pod
$ osc log django-ex-1-y6hc1

_log messages as below_****

  • python manage.py migrate --noinput
    Traceback (most recent call last):
    File "manage.py", line 10, in
    execute_from_command_line(sys.argv)
    File "/opt/openshift/src/.local/lib/python3.3/site-packages/django/core/management/init.py", line 338, in execute_from_command_line
    utility.execute()
    File "/opt/openshift/src/.local/lib/python3.3/site-packages/django/core/management/init.py", line 303, in execute
    settings.INSTALLED_APPS
    File "/opt/openshift/src/.local/lib/python3.3/site-packages/django/conf/init.py", line 48, in getattr
    self._setup(name)
    File "/opt/openshift/src/.local/lib/python3.3/site-packages/django/conf/init.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
    File "/opt/openshift/src/.local/lib/python3.3/site-packages/django/conf/init.py", line 92, in init
    mod = importlib.import_module(self.SETTINGS_MODULE)
    File "/opt/rh/python33/root/usr/lib64/python3.3/importlib/init.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    File "", line 1586, in _gcd_import
    File "", line 1567, in _find_and_load
    File "", line 1534, in _find_and_load_unlocked
    File "", line 586, in _check_name_wrapper
    File "", line 1024, in load_module
    File "", line 1005, in load_module
    File "", line 562, in module_for_loader_wrapper
    File "", line 870, in _load_module
    File "", line 313, in _call_with_frames_removed
    File "/opt/openshift/src/project/settings.py", line 87, in
    'default': database.config()
    File "/opt/openshift/src/project/database.py", line 14, in config
    service_name = os.getenv('DATABASE_SERVICE_NAME').upper()
    AttributeError: 'NoneType' object has no attribute 'upper'

manage.py import error

When running

oc new-app --file https://raw.githubusercontent.com/openshift/django-ex/master/openshift/templates/django.json

on the latest RHEL or CentOS devenv, I'm noticing the following error in the build:

I0302 20:15:44.120427       1 common.go:134] Running post commit hook with image hwlzr/django-example-1:a2264c0c ...
Traceback (most recent call last):
  File "./manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
F0302 20:15:45.168126 1 builder.go:204] Error: build error: container "openshift_s2i-build_django-example-1_hwlzr_post-commit" returned non-zero exit code: 1

The client machine is Fedora 23 x64, with
oc v1.1.3-232-gf39a5a8
kubernetes v1.2.0-alpha.7-703-gbc4550d
etcd 2.2.5

I will try this evening to test against the lastest Fedora 'origin', @csrwng indicated some success with that.

Branch 2.2.x sqlite version problem

I have tried to deploy branch 2.2.x to OpenShift with sqlite and the build ended with an error.

Build works fine, but when pod starts it ends in a CrashLoopBackOff state. I can see in the logs:

django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).

It seems that the default builder image for Python 3.6 has old sqlite version...

Django version in requirements.txt

Django 1.8.X is the LTS version and has support until at least April 2018 why not update the requirements.txt to:

django==1.8.1 # now
django<=1.9

Static folder

I tried to configure my static folder without success.
In the setting file you have STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')but I don't find this folder on the repo

Someone can help me to configure my static folder ?

Error: pg_config executable not found.

While installing with pip install -r requirements.txt on OS X El Capitan I get an error:

(OPENSHIFT_DJANGO) @MyMac[~/_GITHUB/django-ex]$ pip install -r requirements.txt
Collecting django<1.9,>=1.8 (from -r requirements.txt (line 1))
  Using cached Django-1.8.17-py2.py3-none-any.whl
Collecting django-debug-toolbar==1.5 (from -r requirements.txt (line 2))
  Using cached django_debug_toolbar-1.5-py2.py3-none-any.whl
Collecting gunicorn==19.4.5 (from -r requirements.txt (line 3))
  Using cached gunicorn-19.4.5-py2.py3-none-any.whl
Collecting psycopg2==2.6.1 (from -r requirements.txt (line 4))
  Using cached psycopg2-2.6.1.tar.gz
    Complete output from command python setup.py egg_info:
    running egg_info
    creating pip-egg-info/psycopg2.egg-info
    writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
    writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
    writing pip-egg-info/psycopg2.egg-info/PKG-INFO
    writing manifest file 'pip-egg-info/psycopg2.egg-info/SOURCES.txt'
    warning: manifest_maker: standard file '-c' not found
    
    Error: pg_config executable not found.
    
    Please add the directory containing pg_config to the PATH
    or specify the full executable path with the option:
    
        python setup.py build_ext --pg-config /path/to/pg_config build ...
    
    or with the pg_config option in 'setup.cfg'.
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/nr/46qp2z_s3wx14pwzl5s_dy6w0000gn/T/pip-build-hkjjeb8f/psycopg2/

Found "SyntaxError: invalid syntax" while deploying through openshift/python-27-centos7 registry

Found the below error in build logs of django-ex

File "/tmp/pip-build-UPwlfC/gunicorn/gunicorn/workers/_gaiohttp.py", line 78
yield from self.wsgi.close()
^
SyntaxError: invalid syntax

Steps to reproduce:

  1. Create a new project (oc new-project python-test)
  2. Deploy the new app (oc new-app openshift/python-27-centos7~https://github.com/openshift/django-ex)
  3. Go to the build logs (oc logs -f bc/django-ex)
  4. Build is successful but in the build logs found an error as above

changing group not permitted, failed to kill container

Post-processed 'debug_toolbar/img/close_hover.png' as 'debug_toolbar/img/close_hover.2592d7057d2c.png'

79 static files copied to '/opt/app-root/src/staticfiles', 79 post-processed.
chgrp: changing group of ‘/opt/app-root/lib/python3.5/struct.py’: Operation not permitted
chgrp: changing group of ‘/opt/app-root/lib/python3.5/tempfile.py’: Operation not permitted
chgrp: changing group of ‘/opt/app-root/lib/python3.5/codecs.py’: Operation not permitted

warning: Failed to kill container "ac4d9f3e61a1873bc0742f44243e0c4b8d463d01bf543d15017fe952c9cd972a": Error response from daemon: {"message":"Cannot kill container ac4d9f3e61a1873bc0742f44243e0c4b8d463d01bf543d15017fe952c9cd972a: Container ac4d9f3e61a1873bc0742f44243e0c4b8d463d01bf543d15017fe952c9cd972a is not running"}

pg_config executable not found

When trying to spin up the application locally on Mac OS X, I got the error:

Collecting psycopg2==2.6.1 (from -r requirements.txt (line 4))
  Downloading psycopg2-2.6.1.tar.gz (371kB)
    100% |████████████████████████████████| 378kB 15kB/s 
    Complete output from command python setup.py egg_info:
    running egg_info
    creating pip-egg-info/psycopg2.egg-info
    writing pip-egg-info/psycopg2.egg-info/PKG-INFO
    writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
    writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
    writing manifest file 'pip-egg-info/psycopg2.egg-info/SOURCES.txt'
    Error: pg_config executable not found.
    
    Please add the directory containing pg_config to the PATH
    or specify the full executable path with the option:
    
        python setup.py build_ext --pg-config /path/to/pg_config build ...
    
    or with the pg_config option in 'setup.cfg'.

I solved this by installing the following homebrew bottle.

$ brew install postgresql

Can't connect to service with dashes in name

When the environment variable DATABASE_SERVICE_NAME contains dashes, database.py doesn't convert the dashes (-) into underscores (_) (code). This causes Django to fail to connect to the database.

For example, if my Openshift service is named foo-1, the corresponding environment variables are FOO_1_SERVICE_HOST and FOO_1_SERVICE_PORT. However, database.py tries to get the environment variables FOO-1_SERVICE_HOST and FOO-1_SERVICE_PORT (code), which aren't defined environment variables. Thus, HOST and PORT are set to None.

Hello, I tried to run the code, but it shows

Hello, I tried to run the code, but it shows:
django.core.exceptions.ImproperlyConfigured: WSGI application 'application' could not be loaded; Error importing module: 'application doesn't look like a module path'

Add notes in documentation about tests triggered by build.

A section should be added to the documentation about how tests are being triggered by a "postCommit" in the build configuration. It is important to highlight it because its presence can break builds in a confusing way if people copy this repo as an example and change around how databases are setup and remove the fallback to SQLite.

Worst case would be someone who changed the code such that database information was available during the build phase. In this case they could run the tests against an external production database, which could be really bad as could be destructive of data.

So better to highlight what is being done with running tests during the build and how it is done.

This issue came about because a real world user did remove the fallback to SQLite and so all their builds were then failing and they had no idea.

Local development errors on macOS with postgresql 10.0

Local development does not work on macOS with postgresql 10.0:

$ brew info postgresql | head -1
postgresql: stable 10.0 (bottled), HEAD
$ pip install -r requirements.txt
[...]
Collecting psycopg2==2.6.1 (from -r requirements.txt (line 4))
  Downloading psycopg2-2.6.1.tar.gz (371kB)
    100% |████████████████████████████████| 378kB 2.2MB/s
    Complete output from command python setup.py egg_info:
    running egg_info
    creating pip-egg-info/psycopg2.egg-info
    writing pip-egg-info/psycopg2.egg-info/PKG-INFO
    writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
    writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
    writing manifest file 'pip-egg-info/psycopg2.egg-info/SOURCES.txt'
    warning: manifest_maker: standard file '-c' not found

    Error: could not determine PostgreSQL version from '10.0'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/9_/k7p0mlkj41n03hf7kf85sXXXXXXXXX/T/pip-build-on90xxxx/psycopg2/

We should specify the latest version of psycopg2.

Changes for django 2.1 support

In starting to build the django 2.x PR for this repo, i ran into a basic issue, django 2.1+ requires SQlite 3.8 or higher, and looks like 3.7 is installed on the python S2I builds.

django 2.0 runs fine with minor changes.

Need SQlite 3.8 installed for 2.1 to run :)

./run-in-container

Need to check t this.

./run-in-container.sh is not needed since there is no such file in the app-root.
Just run manage.py createsuperuser from the terminal of a running pod.

Met "can't open file 'app.py': [Errno 2] No such file or directory"

  1. Login openshift and create project for the user

$ osc login --server=$master -u $user -p $password
$ osc new-project test

  1. Create imageStream

$ osc create -f image-streams-centos7.json
(the file "image-streams-centos7.json" is same with https://github.com/openshift/origin/blob/master/examples/image-streams/image-streams-centos7.json)

  1. Login openshift in web console

  2. Create application in console

"Create.." -> Input repo url "https://github.com/openshift/django-ex" -> "Next" -> Select a builder image "python:latest" -> finish create

  1. Start a build in webconsole

Browse -> Builds -> "Start Build"

  1. Wait build to complete and keep an eye on the log of pod

$ osc get pod
$ osc logs django-ex-1-c4x9k

_log messages as below_****

python manage.py migrate --noinput
Operations to perform:
Synchronize unmigrated apps: messages, staticfiles, debug_toolbar
Apply all migrations: sessions, auth, admin, welcome, contenttypes
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying sessions.0001_initial... OK
Applying welcome.0001_initial... OK
Metadata-Version: 2.0
Name: gunicorn
Version: 19.3.0
Summary: WSGI HTTP Server for UNIX
Home-page: http://gunicorn.org
Author: Benoit Chesneau
Author-email: [email protected]
License: MIT
Location: /opt/openshift/src/.local/lib/python3.3/site-packages
Requires:
python: can't open file 'app.py': [Errno 2] No such file or directory

allowed_hosts

In Openshift2 I used to set allowed_hosts to an environment variable called OPENSHIFT_APP_DNS, but now I notice that you have used * in it's place.

Do we think this is safe since we are in a dedicated container now?

Just wanted to check.

thanks,

Upgrade to Django 2.2

I've tried to use django 2.2, but the collect static portion of the deploy no longer runs properly.

Different database password in Django and PostgreSQL containers

In the current example template, the Django and PostgreSQL containers are in separate DeploymentConfig's. I have a CI pipeline that processes the template, and since I leave the DATABASE_PASSWORD parameter blank (intentional), it is generated every time the pipeline runs. However, this doesn't trigger a ConfigChange in either DeploymentConfig because the environment variables are secret references (it seems like this would be the obvious thing to do). The Django container does get re-built as part of my CI pipeline and triggers an ImageChange, so the result is that I end up with a Django container that has a different database password in its environment variables from my PostgreSQL container.

Has anyone figured out how to solve this problem? I thought maybe if we put both containers in the same DeploymentConfig or have both the containers be restarted when the Django image changes... but neither seems ideal. Is there a way to specify an annotation that results in the Deployments being recreated when the Secret is updated?

Django Admin

I want to do createsuperuser ( python manage.py createsuperuser )on remote pod. So i did it through oc rsh pod_name .

But, whenever i push the code to repository, the admin settings are changing. That means I have to re-run the oc rsh pod_name command

How can I avoid this repeating procedure ?

Unable to start django

Hello - I just tried deploying this to OpenShift and could not get the django pod to start up. The postgresql has no issues. Any ideas what may be causing this? Thanks.

NAME                             READY     STATUS             RESTARTS   AGE
django-psql-persistent-1-build   0/1       Completed          0          26m
django-psql-persistent-1-jtg64   0/1       CrashLoopBackOff   7          18m
postgresql-1-cqlp2               1/1       Running            0          26m

This is the error from the logs.


---> Migrating database ...
--
  | Operations to perform:
  | Apply all migrations: admin, auth, contenttypes, sessions, welcome
  | Running migrations:
  | No migrations to apply.
  | ---> Serving application with gunicorn (wsgi) ...
  | [2019-08-28 18:18:09 +0000] [1] [INFO] Starting gunicorn 19.4.5
  | [2019-08-28 18:18:09 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080  (1)
  | [2019-08-28 18:18:09 +0000] [1] [INFO] Using worker: sync
  | [2019-08-28 18:18:09 +0000] [41] [INFO] Booting worker with pid: 41
  | [2019-08-28 18:18:10 +0000] [42] [INFO] Booting worker with pid: 42
  | [2019-08-28 18:18:10 +0000] [43] [INFO] Booting worker with pid: 43
  | [2019-08-28 18:18:10 +0000] [45] [INFO] Booting worker with pid: 45
  | [2019-08-28 18:18:10 +0000] [44] [INFO] Booting worker with pid: 44
  | [2019-08-28 18:18:11 +0000] [46] [INFO] Booting worker with pid: 46
  | [2019-08-28 18:18:11 +0000] [47] [INFO] Booting worker with pid: 47
  | [2019-08-28 18:18:12 +0000] [49] [INFO] Booting worker with pid: 49
  | [2019-08-28 18:18:12 +0000] [48] [INFO] Booting worker with pid: 48
  | [2019-08-28 18:18:12 +0000] [51] [INFO] Booting worker with pid: 51
  | [2019-08-28 18:18:13 +0000] [50] [INFO] Booting worker with pid: 50
  | [2019-08-28 18:18:40 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:41)
  | [2019-08-28 18:18:40 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:42)
  | [2019-08-28 18:18:40 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:43)
  | [2019-08-28 18:18:40 +0000] [1] [INFO] Handling signal: term
  | [2019-08-28 18:18:40 +0000] [42] [INFO] Worker exiting (pid: 42)
  | [2019-08-28 18:18:41 +0000] [41] [ERROR] Exception in worker process:
  | Traceback (most recent call last):
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/workers/base.py", line 176, in handle_abort
  | sys.exit(1)
  | SystemExit: 1
  |  
  | The above exception was the direct cause of the following exception:
  |  
  | Traceback (most recent call last):
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/arbiter.py", line 515, in spawn_worker
  | worker.init_process()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/workers/base.py", line 122, in init_process
  | self.load_wsgi()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/workers/base.py", line 130, in load_wsgi
  | self.wsgi = self.app.wsgi()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
  | self.callable = self.load()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
  | return self.load_wsgiapp()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
  | return util.import_app(self.app_uri)
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/util.py", line 357, in import_app
  | __import__(module)
  | File "/opt/app-root/src/wsgi.py", line 16, in <module>
  | application = get_wsgi_application()
  | File "/opt/app-root/lib/python3.6/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
  | django.setup(set_prefix=False)
  | File "/opt/app-root/lib/python3.6/site-packages/django/__init__.py", line 20, in setup
  | from django.utils.log import configure_logging
  | File "/opt/app-root/lib/python3.6/site-packages/django/utils/log.py", line 8, in <module>
  | from django.core import mail
  | File "/opt/app-root/lib/python3.6/site-packages/django/core/mail/__init__.py", line 11, in <module>
  | from django.core.mail.message import (
  | File "/opt/app-root/lib/python3.6/site-packages/django/core/mail/message.py", line 12, in <module>
  | from email.mime.base import MIMEBase
  | File "/opt/rh/rh-python36/root/usr/lib64/python3.6/email/mime/base.py", line 9, in <module>
  | import email.policy
  | File "/opt/rh/rh-python36/root/usr/lib64/python3.6/email/policy.py", line 9, in <module>
  | from email.contentmanager import raw_data_manager
  | File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  | File "<frozen importlib._bootstrap>", line 951, in _find_and_load_unlocked
  | File "<frozen importlib._bootstrap>", line 894, in _find_spec
  | File "<frozen importlib._bootstrap_external>", line 1157, in find_spec
  | File "<frozen importlib._bootstrap_external>", line 1129, in _get_spec
  | File "<frozen importlib._bootstrap_external>", line 1271, in find_spec
  | File "<frozen importlib._bootstrap_external>", line 96, in _path_isfile
  | File "<frozen importlib._bootstrap_external>", line 88, in _path_is_mode_type
  | File "<frozen importlib._bootstrap_external>", line 82, in _path_stat
  | SystemError: PyEval_EvalFrameEx returned a result with an error set
  | Traceback (most recent call last):
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/workers/base.py", line 176, in handle_abort
  | sys.exit(1)
  | SystemExit: 1
  |  
  | The above exception was the direct cause of the following exception:
  |  
  | Traceback (most recent call last):
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/arbiter.py", line 515, in spawn_worker
  | worker.init_process()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/workers/base.py", line 122, in init_process
  | self.load_wsgi()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/workers/base.py", line 130, in load_wsgi
  | self.wsgi = self.app.wsgi()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
  | self.callable = self.load()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
  | return self.load_wsgiapp()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
  | return util.import_app(self.app_uri)
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/util.py", line 357, in import_app
  | __import__(module)
  | File "/opt/app-root/src/wsgi.py", line 16, in <module>
  | application = get_wsgi_application()
  | File "/opt/app-root/lib/python3.6/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
  | django.setup(set_prefix=False)
  | File "/opt/app-root/lib/python3.6/site-packages/django/__init__.py", line 20, in setup
  | from django.utils.log import configure_logging
  | File "/opt/app-root/lib/python3.6/site-packages/django/utils/log.py", line 8, in <module>
  | from django.core import mail
  | File "/opt/app-root/lib/python3.6/site-packages/django/core/mail/__init__.py", line 11, in <module>
  | from django.core.mail.message import (
  | File "/opt/app-root/lib/python3.6/site-packages/django/core/mail/message.py", line 12, in <module>
  | from email.mime.base import MIMEBase
  | File "/opt/rh/rh-python36/root/usr/lib64/python3.6/email/mime/base.py", line 9, in <module>
  | import email.policy
  | File "/opt/rh/rh-python36/root/usr/lib64/python3.6/email/policy.py", line 9, in <module>
  | from email.contentmanager import raw_data_manager
  | File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  | File "<frozen importlib._bootstrap>", line 951, in _find_and_load_unlocked
  | File "<frozen importlib._bootstrap>", line 894, in _find_spec
  | File "<frozen importlib._bootstrap_external>", line 1157, in find_spec
  | File "<frozen importlib._bootstrap_external>", line 1129, in _get_spec
  | File "<frozen importlib._bootstrap_external>", line 1271, in find_spec
  | File "<frozen importlib._bootstrap_external>", line 96, in _path_isfile
  | File "<frozen importlib._bootstrap_external>", line 88, in _path_is_mode_type
  | File "<frozen importlib._bootstrap_external>", line 82, in _path_stat
  | SystemError: PyEval_EvalFrameEx returned a result with an error set
  | [2019-08-28 18:18:41 +0000] [41] [INFO] Worker exiting (pid: 41)
  | [2019-08-28 18:18:42 +0000] [43] [ERROR] Exception in worker process:
  | Traceback (most recent call last):
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/arbiter.py", line 515, in spawn_worker
  | worker.init_process()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/workers/base.py", line 122, in init_process
  | self.load_wsgi()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/workers/base.py", line 130, in load_wsgi
  | self.wsgi = self.app.wsgi()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
  | self.callable = self.load()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
  | return self.load_wsgiapp()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
  | return util.import_app(self.app_uri)
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/util.py", line 357, in import_app
  | __import__(module)
  | File "/opt/app-root/src/wsgi.py", line 16, in <module>
  | application = get_wsgi_application()
  | File "/opt/app-root/lib/python3.6/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
  | django.setup(set_prefix=False)
  | File "/opt/app-root/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
  | apps.populate(settings.INSTALLED_APPS)
  | File "/opt/app-root/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
  | app_config.import_models()
  | File "/opt/app-root/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
  | self.models_module = import_module(models_module_name)
  | File "/opt/app-root/lib64/python3.6/importlib/__init__.py", line 126, in import_module
  | return _bootstrap._gcd_import(name[level:], package, level)
  | File "/opt/app-root/lib/python3.6/site-packages/django/contrib/auth/models.py", line 4, in <module>
  | from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  | File "/opt/app-root/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
  | class AbstractBaseUser(models.Model):
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/models/base.py", line 124, in __new__
  | new_class.add_to_class('_meta', Options(meta, app_label))
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/models/base.py", line 325, in add_to_class
  | value.contribute_to_class(cls, name)
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/models/options.py", line 214, in contribute_to_class
  | self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__
  | return getattr(connections[DEFAULT_DB_ALIAS], item)
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/utils.py", line 211, in __getitem__
  | backend = load_backend(db['ENGINE'])
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/utils.py", line 115, in load_backend
  | return import_module('%s.base' % backend_name)
  | File "/opt/app-root/lib64/python3.6/importlib/__init__.py", line 126, in import_module
  | return _bootstrap._gcd_import(name[level:], package, level)
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 21, in <module>
  | import psycopg2 as Database
  | File "/opt/app-root/lib/python3.6/site-packages/psycopg2/__init__.py", line 50, in <module>
  | from psycopg2._psycopg import (                     # noqa
  | SystemError: initialization of _psycopg raised unreported exception
  | Traceback (most recent call last):
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/arbiter.py", line 515, in spawn_worker
  | worker.init_process()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/workers/base.py", line 122, in init_process
  | self.load_wsgi()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/workers/base.py", line 130, in load_wsgi
  | self.wsgi = self.app.wsgi()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
  | self.callable = self.load()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
  | return self.load_wsgiapp()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
  | return util.import_app(self.app_uri)
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/util.py", line 357, in import_app
  | __import__(module)
  | File "/opt/app-root/src/wsgi.py", line 16, in <module>
  | application = get_wsgi_application()
  | File "/opt/app-root/lib/python3.6/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
  | django.setup(set_prefix=False)
  | File "/opt/app-root/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
  | apps.populate(settings.INSTALLED_APPS)
  | File "/opt/app-root/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
  | app_config.import_models()
  | File "/opt/app-root/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
  | self.models_module = import_module(models_module_name)
  | File "/opt/app-root/lib64/python3.6/importlib/__init__.py", line 126, in import_module
  | return _bootstrap._gcd_import(name[level:], package, level)
  | File "/opt/app-root/lib/python3.6/site-packages/django/contrib/auth/models.py", line 4, in <module>
  | from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  | File "/opt/app-root/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
  | class AbstractBaseUser(models.Model):
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/models/base.py", line 124, in __new__
  | new_class.add_to_class('_meta', Options(meta, app_label))
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/models/base.py", line 325, in add_to_class
  | value.contribute_to_class(cls, name)
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/models/options.py", line 214, in contribute_to_class
  | self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__
  | return getattr(connections[DEFAULT_DB_ALIAS], item)
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/utils.py", line 211, in __getitem__
  | backend = load_backend(db['ENGINE'])
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/utils.py", line 115, in load_backend
  | return import_module('%s.base' % backend_name)
  | File "/opt/app-root/lib64/python3.6/importlib/__init__.py", line 126, in import_module
  | return _bootstrap._gcd_import(name[level:], package, level)
  | File "/opt/app-root/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 21, in <module>
  | import psycopg2 as Database
  | File "/opt/app-root/lib/python3.6/site-packages/psycopg2/__init__.py", line 50, in <module>
  | from psycopg2._psycopg import (                     # noqa
  | SystemError: initialization of _psycopg raised unreported exception
  | [2019-08-28 18:18:42 +0000] [43] [INFO] Worker exiting (pid: 43)
  | Traceback (most recent call last):
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/arbiter.py", line 199, in run
  | handler()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/arbiter.py", line 234, in handle_term
  | raise StopIteration
  | StopIteration
  |  
  | During handling of the above exception, another exception occurred:
  |  
  | Traceback (most recent call last):
  | File "/opt/app-root/bin/gunicorn", line 11, in <module>
  | sys.exit(run())
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 74, in run
  | WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/base.py", line 192, in run
  | super(Application, self).run()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/app/base.py", line 72, in run
  | Arbiter(self).run()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/arbiter.py", line 202, in run
  | self.halt()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/arbiter.py", line 302, in halt
  | self.stop()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/arbiter.py", line 347, in stop
  | time.sleep(0.1)
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/arbiter.py", line 219, in handle_chld
  | self.reap_workers()
  | File "/opt/app-root/lib/python3.6/site-packages/gunicorn/arbiter.py", line 464, in reap_workers
  | raise HaltServer(reason, self.WORKER_BOOT_ERROR)
  | gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>


Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.