Bug 1082363 - New Python directory structure and wsgi.py
Summary: New Python directory structure and wsgi.py
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: OpenShift Online
Classification: Red Hat
Component: Image
Version: 1.x
Hardware: All
OS: All
unspecified
medium
Target Milestone: ---
: ---
Assignee: Vojtech Vitek
QA Contact: libra bugs
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2014-03-30 19:08 UTC by David O'Connor
Modified: 2015-03-04 23:58 UTC (History)
4 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2014-04-04 14:13:10 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)
Default wsgi.py (39.57 KB, text/plain)
2014-03-30 19:08 UTC, David O'Connor
no flags Details
Applicaiton file (895 bytes, text/x-python)
2014-04-01 20:24 UTC, David O'Connor
no flags Details
app.py (1.25 KB, text/plain)
2014-04-01 20:25 UTC, David O'Connor
no flags Details
Python 3.3 - app.py (1.51 KB, text/x-python-script)
2014-04-04 14:16 UTC, Vojtech Vitek
no flags Details
app.py (39.90 KB, text/plain)
2014-04-04 18:03 UTC, fatfantasma
no flags Details


Links
System ID Private Priority Status Summary Last Updated
Red Hat Bugzilla 1081190 0 low CLOSED OSError: [Errno 98] Address already in use - Cannot start own server because an httpd instance already exists 2021-02-22 00:41:40 UTC

Internal Links: 1081190

Description David O'Connor 2014-03-30 19:08:30 UTC
Created attachment 880443 [details]
Default wsgi.py

Description of problem:
The updated method of creating Python apps on Openshift doesn't appear to work, based on available information. Update description is here: https://www.openshift.com/blogs/openshift-online-march-2014-release-blog

The key seems to be in wsgi.py. Note that the code in the (new) default wsgi.py under 'if __name__ == '__main':' does not appear to actually get run. Instead, application() is run instead. It's possible that entering something (undocumented) in wsgi.py might fix the file, but without documentation or examples, it's also likely that this is a bug.


How reproducible:
Easy-Peasy. Is that how you spell peasy? I've never typed it before.


Steps to Reproduce:
1. Create new Openshift  App with Python 3.3 cartridge
2. Load Django (or other Python 3 content) in repo directory
3. Upload

Actual results:
Welcome webpage


Expected results:
Python-powered webpage

Additional info:
If this is not a bug, a working example of wsgi.py, or a working Github example would be all that's required to demonstrate it.



Related Stack overflow post:
http://stackoverflow.com/questions/22383501/openshift-oserror-errno-98-cant-update-server

Related bug:
https://www.openshift.com/blogs/openshift-online-march-2014-release-blog

Comment 1 fatfantasma 2014-04-01 18:58:46 UTC
This is exactly the same problem I have.  I have not been able to get any new python apps working since the March 14 update.  I see that @Michal Fojtik is not on this bug report with a "keywords': upcoming release.  

Is that a sign that they are fixing this March 14 buggy update?

Comment 2 Michal Fojtik 2014-04-01 19:36:49 UTC
fatfantasma: can you please describe the 'any new python apps are not working'? Is this just the case of python-3.3 with wsgi? Were they working before?

Comment 3 Michal Fojtik 2014-04-01 19:42:24 UTC
Guys can you please elaborate on what you think should be the *right* structure for Python 3.x applications that will work for you?

The 'application' is being run by wsgi. You can also use 'app.py' to start your own server and bypass the Apache/WSGI (see docker-registry for example[1]).

[1] https://github.com/mfojtik/openshift-docker-registry/blob/master/app.py

Comment 4 David O'Connor 2014-04-01 20:24:41 UTC
Created attachment 881517 [details]
Applicaiton file

Comment 5 David O'Connor 2014-04-01 20:25:08 UTC
Created attachment 881518 [details]
app.py

Comment 6 David O'Connor 2014-04-01 20:26:37 UTC
I've attached the old application and app.py files from the old directory, which seem to be the equivalent of  wsgi.py.



The *right* structure, per the March 14 article and creating a new app would be the standard Django files, 'wsgi.py', 'setup.py', and '.openshift/'

Comment 7 David O'Connor 2014-04-01 20:27:23 UTC
And 'requirements.txt'.

Comment 8 fatfantasma 2014-04-02 00:46:17 UTC
I think I know what is going on here now.  I't a combination of poor documentation and a bug on Openshift's part.  Please correct me if I am wrong.

First, referring to the openshift-online-march-2014-release-blog
https://wopenshift-online-march-2014-release-blogww.openshift.com/blogs/

It suggests (or at least I took it that way. Plus there is the bug which still exists that compounds the issue ) that developers are now supposed to use wsgi.py as the new 'entry point' for their applications and not to use app.py anymore.  This is further exasperated by not having an 'app.py' file in a newly created python openshift app(not sure about other cartridges) reinforcing the new wsgi.py method.

It looks like that Openshift has created two entry points now.  Both 'wsgi.py' and 'app.py'.  Both cannot exist at the same time.

If the app.py file exists it is actually executed.  If the wsgi.py file exists the function 'application' in 'wsgi.p' is executed.  

You should use app.py if you want to start your own server and use wsgi.py if you want to use the default openshift (mod_wsgi) server. I have no done the latter so don't know much about it.

Now for the BUG!!!!!!!!!!!!!!!!!!!!!!!!!

If you use app.py for your entry point and do a PUSH to openshift the existing server does not receive a STOP command and hence you get the 

 [Errno 98] Address already in use

Currently, to work around this issue you have to do a 'rhc app-force-stop <app>' to shut everything down and then do your push.  The push should issue all the correct stop commands when using 'app.py'.  IT DOES NOT!

This is where everything went awry on stackoverflow because some people found that using 'wsgi.py' solved the problem.

I have verified the above as my app is now running again (minus the PUSH bug)

So, I believe better documentation is in order by Openshift and the PUSH/Stop current server BUG needs to be fixed.

So what do you think?

Comment 9 Vojtech Vitek 2014-04-02 16:21:21 UTC
@David, @fatfantasma, thank you both for your great feedback. Much appreciated!

@fatfantasma, sounds like you found the root cause of the bug you're hitting. I'm going to investigate why httpd process is not being killed on stop(), when the app.py file exist and is chosen as a server instead of apache/mod_wsgi.

Even though app.py was brought to OpenShift Python 3.3 from the community cartridge and it *still works* as an alternative server to apache/mod_wsgi, it is still not documented. This is something we're going to address in the upcoming releases, together with research for other alternative servers that we might support in the future. Any further feedback on this would be also appreciated. Feel free to take a look at https://trello.com/c/5qhdqEuk and comment on this Trello card.

Comment 10 fatfantasma 2014-04-02 21:39:13 UTC
Great.  I look forward to the bug being fixed.  When fixed, do you push it immediately or what for a major release?

Comment 11 Vojtech Vitek 2014-04-04 12:55:25 UTC
@fatfantasma, @David, here's the docs:
http://openshift.github.io/documentation-latest/oo_cartridge_guide.html#python

Comment 12 fatfantasma 2014-04-04 13:09:03 UTC
Thanks. I see no mention of 'app.py' in the docs for people that want to start their own server. Also, can you give us an update to the 'stop' bug when pushing to openshift?

Comment 13 Vojtech Vitek 2014-04-04 13:13:33 UTC
@David, I tried the source codes from provided attachments and I hit the following error:

> Traceback (most recent call last):
>   File "app.py", line 34, in <module>
>     zapp = imp.load_source('application', 'wsgi/application')
>   File "/opt/rh/python33/root/usr/lib64/python3.3/imp.py", line 114, in load_source
>     _LoadSourceCompatibility(name, pathname, file).load_module(name)
>   File "<frozen importlib._bootstrap>", line 586, in _check_name_wrapper
>   File "<frozen importlib._bootstrap>", line 1024, in load_module
>   File "<frozen importlib._bootstrap>", line 1005, in load_module
>   File "<frozen importlib._bootstrap>", line 562, in module_for_loader_wrapper
>   File "<frozen importlib._bootstrap>", line 855, in _load_module
>   File "<frozen importlib._bootstrap>", line 950, in get_code
>   File "<frozen importlib._bootstrap>", line 1043, in path_stats
> FileNotFoundError: [Errno 2] No such file or directory: 'wsgi/application'

Your app.py referenced non-existing file. You'd just need to edit the 'wsgi/application' path to the correct one .. 'application' in your case (or 'wsgi.py' as seen from the template repository layout):

> diff --git a/app.py b/app.py
> index 0a016d2..f1c6342 100644
> --- a/app.py
> +++ b/app.py
> @@ -31,7 +31,7 @@ def run_simple_httpd_server(app, ip, port=8080):
>  if __name__ == '__main__':
>     ip   = os.environ['OPENSHIFT_PYTHON_IP']
>     port = int(os.environ['OPENSHIFT_PYTHON_PORT'])
> -   zapp = imp.load_source('application', 'wsgi/application')
> +   zapp = imp.load_source('application', 'application')

Comment 14 Vojtech Vitek 2014-04-04 14:05:34 UTC
@fatfantasma I can't actually reproduce your bug with stop(). Can you please provide the exact steps to reproduce this? Can you also run `ps aux' command while ssh'd into your gear once you hit the problem? Logs would be also appreciated (you can get them using `rhc tail <app>' command).


My steps:

1. Create Python 3.3 app

$ rhc app-create py33 python-3.3
> ..
> Creating application 'py33' ... done
> Your application 'py33' is now available.
> ..

# git push something to see the remote messages
$ cd py33 && touch README && git add README && git commit -amREADME && git push
> remote: Running setup.py script..
> ...
> remote: Starting Python 3.3 cartridge (Apache+mod_wsgi)
> remote: Application directory "/" selected as DocumentRoot
> remote: Application "wsgi.py" selected as default WSGI entry point


2. Check the app from the Web

Working template "Welcome to Openshift" appeared, meaning that Apache/mod_WSGI successfully loaded the default 'wsgi.py' file.

$ rhc ssh py33
$ ps aux
> Listed processes (snip):
> /usr/sbin/httpd
> /usr/sbin/rotatelogs


3. Add 'app.py' file to the application:

- The 'app.py' file to be attached below.
- Note that 'app.py' is still undocumented feature that came from Python 3.3 community cartridge. We'll eventually support it officially, see https://trello.com/c/5qhdqEuk.

$ git add app.py && git commit -am"Adding app.py" && git push
> remote: Stopping Python 3.3 cartridge
> remote: Running setup.py script..
> remote: Starting Python 3.3 cartridge (app.py server)

4. Check the app from the Web

Working template "Welcome to Openshift" appeared, meaning that app.py server was successfully started and loaded the 'wsgi.py' file.

$ rhc ssh py33
$ ps aux
> Listed processes (snip):
> python -u app.py

Comment 15 Vojtech Vitek 2014-04-04 14:13:10 UTC
@fatfantasma Let's focus on the stop/bind issue in Bug 1081190.

@David, could you please take a look at working 'app.py' example at Bug 1081190 Comment 6? I'm closing this as NOTABUG for now. Feel free to reopen this bug if you still think there's a bug or regression (apart of from what is described in Bug 1081190). Thank you.

Comment 16 Vojtech Vitek 2014-04-04 14:16:43 UTC
Created attachment 882738 [details]
Python 3.3 - app.py

Working 'app.py' file for Python 3.3, just for reference.

Comment 17 fatfantasma 2014-04-04 18:03:39 UTC
Created attachment 882808 [details]
app.py

Comment 18 fatfantasma 2014-04-04 18:05:02 UTC
@Vojtech


Here are the steps to reproduce OSError: [Errno 98] Address already in use.

1. Create python 3.3 app
2. Clone app to PC
3. Delete wsgi.py
4. Add in my app.py
5. commit and push

At this point you will get the error or not depending if the server is running before.

If not error.  Just make another commit and push.

The error is

Traceback (most recent call last):
  File "app.py", line 299, in <module>
    server = make_server(ip, port, application)                      #Serve from our local server
  File "/opt/rh/python33/root/usr/lib64/python3.3/wsgiref/simple_server.py", line 146, in make_server
    server = server_class((host, port), handler_class)
  File "/opt/rh/python33/root/usr/lib64/python3.3/socketserver.py", line 430, in __init__
    self.server_bind()
  File "/opt/rh/python33/root/usr/lib64/python3.3/wsgiref/simple_server.py", line 50, in server_bind
    HTTPServer.server_bind(self)
  File "/opt/rh/python33/root/usr/lib64/python3.3/http/server.py", line 135, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/opt/rh/python33/root/usr/lib64/python3.3/socketserver.py", line 441, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use




At this point if you do a "rhc app-force-stop <app>" and do another commit and push the app will run correctly.  This must be done for every new commit and push or you will get the error.

Comment 19 Vojtech Vitek 2014-04-04 18:24:56 UTC
@fatfantasma moving Comment 18 to Bug 1081190.


Note You need to log in before you can comment on or make changes to this bug.