I am trying to deploy a Django app on openshift (python3.3, django1.7, Openshift 2.1).
I need to set the OPENSHIFT_PYTHON_WSGI_APPLICATION to point to an alternative wsgi.py location.
I have tried using the pre_build script to set the variable, using the following commands:
export OPENSHIFT_PYTHON_WSGI_APPLICATION="$OPENSHIFT_REPO_DIR"geartest4/wsgi.py
echo "-------> $OPENSHIFT_PYTHON_WSGI_APPLICATION"
I can see during the git push that the pre_build script sets the variable correctly. The echo shows the correct path as expected. However wsgi.py does not launch and I get:
CLIENT_ERROR: WSGI application was not found
When I immediately ssh into the gear and check the environment variable I see that OPENSHIFT_PYTHON_WSGI_APPLICATION="" is not set.
If I set the variable manually from my workstation using rhc set-env OPENSHIFT_PYTHON_WSGI_APPLICATION=/var/lib/openshift/gear_name/bla/bla then the variable sticks, the wsgi server launches, and the app works fine.
The problem is that I don't want to use rhc set-env because that means I have to hardwire the gear name in the path. This becomes a problem when I want to do scaling with multiple gears.
Anyone have any ideas on how to set the variable and make stick?
The environment variable OPENSHIFT_PYTHON_WSGI_APPLICATION can be set to a relative path like this:
rhc env set OPENSHIFT_PYTHON_WSGI_APPLICATION=wsgi/wsgi.py
The openshift cartridge openshift-django17 by jfmatth uses this approach, too.
Related
I've been trying to understand how it's possible to launch a flask project by running flask run.
What actually happens behind the scenes? How is it possible to actually launch the app using the flask keyword? I got as far as understanding that it is based on the Click library (https://palletsprojects.com/p/click/) but I still don't understand what happens step by step (the internals).
If someone could explain that would be appreciated. Thank you!
To launch a flask application, you can use the command flask run. But how does it work?
Before running any flask application, flask needs to be told how to import it by setting certain environment variables. On your terminal, you run these commands in their order:
(venv) $ export FLASK_APP=app.py
(venv) $ flask run
What is app.py? This is the entry point of your application. In this file, you probably have:
from app import app
# Here, you application instance is being imported.
# The application instance is where you defined your flask app.
Alternatively, this file may have:
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
After the server initializes it will wait for client connections. The output from flask run indicates that the server is running on IP address 127.0.0.1, which is always the address of your own computer. This address is so common that is also has a simpler name that you may have seen before: localhost.
Applications deployed on production web servers typically listen on port 443, or sometimes 80 if they do not implement encryption, but access to these ports require administration rights. Since this application is running in a development environment, Flask uses the freely available port 5000.
To access you application on your web browser, paste this URL:
http://localhost:5000/
There are other environment variables that flask can use:
FLASK_ENV (sets your environment, either to development or production)
FLASK_DEBUG (enables/disables debugging)
Before running flask run, you will run need to add the other environment variables in your terminal:
(venv) $ export FLASK_APP=app.py
(venv) $ export FLASK_ENV=development
(venv) $ export FLASK_DEBUG=True
(venv) $ flask run
Now, you will have specified that your application is running on a development server, and you have enabled flask debugging features.
Every time you want to see the changes in your application, you will need to restart your server by running the same commands in your terminal.
Starting with version 1.0, Flask allows you to register environment variables that you want to be automatically imported when you run the flask command.
It is recommended that you store flask environment variables (these are the ones needed to run your application) in a file called .flaskenv in your project's root directory.
(venv) $ touch .flaskenv
Then update this file with your environment variables:
# .flaskenv
FLASK_APP=app.py
FLASK_ENV=development
FLASK_DEBUG=True
To implement this option, you will need the packege python-dotenv:
(venv)$ pip3 install python-dotenv
This is optional, but it makes it a lot easier rather than you having to memorize all environment variables which you pass via the terminal.
To run your flask app using this option, you will only need:
(venv)$ flask run
I am trying to learn flask and for the same created a small program. When I try to run it I am getting the below error. flask run Error-
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
if I try to change the file name to app.py it runs smooth but when the file name is flaskblog.py it isn't working.
I tried setting up the environment variables as suggested in the blog-> How to set environment variables in PyCharm? but nothing worked.
Screenshots attached.
content of environment variable file
I was able to resolve this by setting up the environment variable on the terminal itself.
set FLASK_APP=flaskblog.py
and then it ran like smoothly.
Open Settings -> Tools -> Terminal and there set up the environment variable FLASK_APP=app.py. Next, perform restart of PyCharm.
I'm trying to get a Python 2.7, Django 1.7 web gear up and running.
I have hot_deploy activated.
However, after setting my required env vars (via rhc), and I see them set in the gear ('env | grep MY_VAR' is OK), when running the WSGI script the vars are NOT SET.
os.environ['MY_VAR'] yields KeyError. Is this somehow related to hot_deploy?
You probably just need to stop & start (not restart) your application via the rhc command line so that your python environment can pick them up.
I have been researching docker and understand almost everything I have read so far. I have built a few images, linked containers together, mounted volumes, and even got a sample django app running.
The one thing I can not wrap my head around is setting up a development environment. The whole point of docker is to be able to take your environment anywhere so that everything you do is portable and consistent. If I am running a django app in production being served by gunicorn for example, I need to restart the server in order for my code changes to take affect; this is not ideal when you are working on your project in your local laptop environment. If I make a change to my models or views I don't want to have to attach to the container, stop gunicorn, and then restart it every time I make a code change.
I am also not sure how I would run management commands. python manage.py syncdb would require me to get inside of the container and run commands. I also use south to manage data and schema migrations python manage.py migrate. How are others dealing with this issue?
Debugging is another issue. Would I have to somehow get all my logs to save somewhere so I can look at things? I usually just look at the django development server's output to see errors and prints.
It seems that I would have to make a special dev-environment container that had a bunch of workarounds; that seems like it completely defeats the purpose of the tool in the first place though. Any suggestions?
Update after doing more research:
Thanks for the responses. They set me on the right path.
I ended up discovering fig http://www.fig.sh/ It let's you orchestrate the linking and mounting of volumes, you can run commands. fig run container_name python manage.py syncdb . It seems pretty nice and I have been able to set up my dev environment using it.
Made a diagram of how I set it up using vagrant https://www.vagrantup.com/.
I just run
fig up
in the same directory as my fig.yml file and it does everything needed to link the containers and start the server. I am just running the development server when working on my mac so that it restarts when I change python code.
At my current gig we setup a bash script called django_admin. You run it like so:
django_admin <management command>
Example:
django_admin syncdb
The script looks something like this:
docker run -it --rm \
-e PYTHONPATH=/var/local \
-e DJANGO_ENVIRON=LOCAL \
-e LC_ALL=en_US.UTF-8 \
-e LANG=en_US.UTF-8 \
-v /src/www/run:/var/log \
-v /src/www:/var/local \
--link mysql:db \
localhost:5000/www:dev /var/local/config/local/django-admin $#
I'm guessing you could also hook something up like this to manage.py
I normally wrap my actual CMD in a script that launches a bash shell. Take a look at Docker-Jetty container as an example. The final two lines in the script are:
/opt/jetty/bin/jetty.sh restart
bash
This will start jetty and then open a shell.
Now I can use the following command to enter a shell inside the container and run any commands or look at logs. Once I am done I can use Ctrl-p + Ctrl-q to detach from the container.
docker attach CONTAINER_NAME
I've tried to create a twill test that changes the proxy server settings of 2 different tests. I need to trigger this change in runtime without relaunching the test script.
I've tried to use the "http_proxy" environment variable by setting os.environ["HTTP_PROXY"], but it's only changes the proxy setting for the first test, and does not works on the second and third tests.
Could you please suggest a way to change twill's proxy settings on runtime ?
Set the proxy environment variable before you run the twill script.
sh/ksh/bash
export HTTP_PROXY=blah:8080
csh
setenv HTTP_PROXY blah:8080
It's worth nothing, this should work by setting os.environ['http_proxy'], but it might not if you set it after you import twill. Twill may be checking this once on startup? The only 100% safe way I would imagine is exporting the variable so that all further child processes will get it as their environment.