Thursday 15 May 2014

python - Deploying a local django app using openshift -



python - Deploying a local django app using openshift -

i've built webapp using django. in order host i'm trying utilize openshift having difficulty in getting working. there seems lack of step steps this. far have git working fine, app works on local dev environment , i've created app on openshift.

following url on openshift 1 time created standard page of "welcome openshift app".

i've followed https://developers.openshift.com/en/python-getting-started.html#step1 seek changing wsgi.py file. changed hello world, pushed , yet still openshift default page.

is there comprehensive resource anywhere getting local django apps , running on openshift? of can find on google illustration apps aren't useful have mine built.

many times happened me and, since had mount @ to the lowest degree 5 applications, had create own lifecycle:

don't utilize django cartridge, python 2.7 cartridge. using django cart. , trying update django version brings many headaches, not included if scratch.

clone repository via git. yourproject and...

# git clone yourrepo@rhcloud.com:app.git yourproject <- replace actual openshift repo address yourproject/ +---wsgi.py +---setup.py *---.openshift/ (with contents - omit them now)

make virtualenv brand-new repository cloned local machine. activate , install django via pip , dependencies need (e.g. new pillow package, mysql database package, ...). create django project there. say, yourdjproject. edit create, alongside, wsgi/static directory empty, dummy, file (e.g. .gitkeep - name convention: can utilize name want).

#assuming have virtualenv-wrapper installed , set-up mkvirtualenv myenvironment workon myenvironment pip install django[==x.y[.z]] #select version; optional. #creating project within git repository cd path/to/yourproject/ django-admin.py startproject yourjdproject . #creating dummy wsgi/static directory collectstatic mkdir -p wsgi/static touch wsgi/static/.gitkeep

create django app there. say, yourapp. include in project.

you have (django 1.7):

yourproject/ +---wsgi/ | +---static/ | +---.gitkeep +---wsgi.py +---setup.py +---.openshift/ (with contents - omit them now) +---yourdjproject/ | +----__init__.py | +----urls.py | +----settings.py | +----wsgi.py +---+yourapp/ +----__init__.py +----models.py +----views.py +----tests.py +----migrations +---__init__.py

set django application you'd (i not detail here). remember include dependencies installed, in setup.py file accordingly (this reply not place describe why, setup.py bundle installer , openshift uses reinstall app on each deploy, maintain date dependencies).

create migrations models.

edit openshift-given wsgi script follows. including django wsgi application after including virtualenv (openshift creates 1 python cartridges), pythonpath set up.

#!/usr/bin/python import os virtenv = os.environ['openshift_python_dir'] + '/virtenv/' virtualenv = os.path.join(virtenv, 'bin/activate_this.py') try: execfile(virtualenv, dict(__file__=virtualenv)) except ioerror: pass yourdjproject.wsgi import application

edit hooks in .openshift/action_hooks automatically perform db sincronization , media management:

build hook

#!/bin/bash #this .openshift/action/hooks/build #remember create +x openshift can run it. if [ ! -d ${openshift_data_dir}media ]; mkdir -p ${openshift_data_dir}media fi ln -snf ${openshift_data_dir}media $openshift_repo_dir/wsgi/static/media ######################### end of file

deploy hook

#!/bin/bash #this 1 deploy hook .openshift/action_hooks/deploy source $openshift_homedir/python/virtenv/bin/activate cd $openshift_repo_dir echo "executing 'python manage.py migrate'" python manage.py migrate echo "executing 'python manage.py collectstatic --noinput'" python manage.py collectstatic --noinput ########################### end of file

now have wsgi ready, pointing django wsgi import, , have scripts running. time consider locations static , media files used in such scripts. edit django settings tell did want such files:

static_url = '/static/' media_url = '/media/' static_root = os.path.join(base_dir, 'wsgi', 'static') media_root = os.path.join(base_dir, 'wsgi', 'static', 'media') staticfiles_dirs = (os.path.join(base_dir, 'yourjdproject', 'static'),) template_dirs = (os.path.join(base_dir, 'yourjdproject', 'templates'),)

create sample view, sample model, sample migration, , force everything.

edit remember set right settings consider both environments can test , run in local environment , in openshift (usually, involve having local_settings.py, optionally imported if file exists, omit part , set in same file). please read file conciously since things like yourlocaldbname are values must set accordingly:

""" django settings yourdjproject project. more info on file, see https://docs.djangoproject.com/en/1.7/topics/settings/ total list of settings , values, see https://docs.djangoproject.com/en/1.7/ref/settings/ """ # build paths within project this: os.path.join(base_dir, ...) import os base_dir = os.path.dirname(os.path.dirname(__file__)) on_openshift = false if 'openshift_repo_dir' in os.environ: on_openshift = true # quick-start development settings - unsuitable production # see https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ # security warning: maintain secret key used in production secret! secret_key = '60e32dn-za#y=x!551tditnset(o9b@2bkh1)b$hn&0$ec5-j7' # application definition installed_apps = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'yourapp', #more apps here ) middleware_classes = ( 'django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.common.commonmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware', 'django.contrib.auth.middleware.sessionauthenticationmiddleware', 'django.contrib.messages.middleware.messagemiddleware', ) root_urlconf = 'yourdjproject.urls' wsgi_application = 'yourdjproject.wsgi.application' # database # https://docs.djangoproject.com/en/1.7/ref/settings/#databases if on_openshift: debug = true template_debug = false allowed_hosts = ['*'] databases = { 'default': { 'engine': 'django.db.backends.mysql', 'name': 'youropenshiftgenerateddatabasename', 'user': os.getenv('openshift_mysql_db_username'), 'password': os.getenv('openshift_mysql_db_password'), 'host': os.getenv('openshift_mysql_db_host'), 'port': os.getenv('openshift_mysql_db_port'), } } else: debug = true template_debug = true allowed_hosts = [] databases = { 'default': { 'engine': 'django.db.backends.mysql', #if want utilize mysql 'name': 'yourlocaldbname', 'user': 'yourlocalusername', 'password': 'yourlocaluserpassword', 'host': 'yourlocaldbhost', 'port': '3306', #this case mysql } } # internationalization # https://docs.djangoproject.com/en/1.7/topics/i18n/ language_code = 'yr-lc' time_zone = 'your/timezone/here' use_i18n = true use_l10n = true use_tz = true # static files (css, javascript, images) # https://docs.djangoproject.com/en/1.7/howto/static-files/ static_url = '/static/' media_url = '/media/' static_root = os.path.join(base_dir, 'wsgi', 'static') media_root = os.path.join(base_dir, 'wsgi', 'static', 'media') staticfiles_dirs = (os.path.join(base_dir, 'yourdjproject', 'static'),) template_dirs = (os.path.join(base_dir, 'yourdjproject', 'templates'),)

git add, commit, push, enjoy.

cd path/to/yourproject/ git add together . git commit -m "your message" git force origin master # command take long # git enjoy

this should help mount django application, , took me lot of time standarize process. enjoy , don't hesitate on contacting me via comment if goes wrong

edit (for same problem don't expect find reply in post's comments): remember if edit build or deploy hook files under windows , force files, fly server 0644 permissions, since windows not back upwards permission scheme unix has, , has no way assign permissions since these files not have extension. you notice because scripts not executed when deploying. seek deploy files only unix-based systems.

python django git openshift

No comments:

Post a Comment