git webhook fails - do you know why? - python

Background:
I have a bitbucket repo called DOSTUFF that includes a python script do_stuff.py. I edit it using Eclipse pydev on my local machine and push changes to bitbucket via git push origin master.
I cloned DOSTUFF to a pythonanywhere trial account without any issues.
Now, whenever I edit do_stuff.py locally and then git commit -m 'foo' & git push origin master them to bitbucket, I manually need to git pull from within pythonanywhere afterwards in order to see the edits in pythonanywhere. This is inefficient.
Objectives:
I want that my local (Eclipse) commits to bitbucket get pulled automatically to pythonanywhere once pushed from local to bitbucket. Apparently, webhooks are the way to go.
Challenges:
In order to do so, I followed this hint by specifying a webhook within bitbucket to pythonanywhere/user/me/webhook.py. Unfortunately, those instructions are minimalistic as they lack proper imports and don't elucidate as to why flask is necessary (I am not an expert).
webhook.py looks like this:
#!/usr/bin/python2.7
# -*- coding: utf-8 -*
import git
from flask import Flask, request
# Initiate flask instance
app = Flask(__name__)
#app.route('/webhook', methods=['POST'])
def webhook():
if request.method == 'POST':
repo = git.Repo('./DOSTUFF')
origin = repo.remotes.origin
repo.create_head('master',
origin.refs.master).set_tracking_branch(origin.refs.master).checkout()
origin.pull()
return '', 200
else:
return '', 400
if __name__ == '__main__':
app.run(port=5000,debug=True)
Now, when I git push from Eclipse to bitbucket, the commit(s) arrive at bitbucket but the code in pythonanywhere remains unchanged. In other words, webhook.py fails.
In contrast, when I run webhook.py from within pythonanywhere (bash console), I yield the following error:
* Serving Flask app "__main__" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
Traceback (most recent call last):
File "/home/ME/webhook.py", line 21, in <module>
app.run(port=5000,debug=True)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 943, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 795, in run_simple
s.bind(get_sockaddr(hostname, port, address_family))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
error: [Errno 98] Address already in use
Questions:
What's the root cause for this to fail?
How to properly configure a webhook that is necessary and sufficient to auto-git pull changes to pythonanywhere once pushed from local to bitbucket?

You're trying to start a server in a PythonAnywhere console, which will not work since traffic is not routed to console servers. Use a web app to create the server to listen for the web hooks.

Related

Making my Flask website HTTPS stops me from being able to connect

I am trying to run a Flask website using HTTPS. The website worked perfectly using HTTP, but once I added a certificate I can no longer connect to it.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return '<b>hello world!'
if __name__ == '__main__':
# The following line is one option I tried.
# app.run(ssl_context='adhoc', host='0.0.0.0', port='80')
# The following line is another option I tried.
# app.run(ssl_context=('cert.pem', 'key.pem'), host='0.0.0.0', port='80')
# The following line works totally fine, but is not HTTPS.
app.run('0.0.0.0', '80')
All the code is in a file called main.py which I am running on a Ubuntu machine with the command sudo -E python3 main.py.
Running with app.run('0.0.0.0', '80') gives me this output (which is normal, and I can connect):
* Serving Flask app 'main' (lazy loading)
* 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
* Running on all addresses (0.0.0.0)
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://127.0.0.1:80
* Running on http://192.168.1.34:80 (Press CTRL+C to quit)
Running with either other option gives me this output (which is normal, but I CAN'T connect):
* Serving Flask app 'main' (lazy loading)
* 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
* Running on all addresses (0.0.0.0)
WARNING: This is a development server. Do not use it in a production deployment.
* Running on https://127.0.0.1:80
* Running on https://192.168.1.34:80 (Press CTRL+C to quit)
Extra Information:
I was following a very helpful tutorial here: https://blog.miguelgrinberg.com/post/running-your-flask-application-over-https, and it seems like everything is working as intended (which is why I came to Stack Overflow to ask this question).
I have pyopenssl installed.
I ran openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365 and it executed fine. The cert.pem and key.pem files were (I assume) correctly generated.
SSL usually runs via port 443, so changing it to that might fix the problem. If you look closely in the tutorial, they haven't defined it at all and let Flask figure out where to run itself.
If your browser simply tells you that "Your connection is not private" or "Can't verify certificate" or something along those lines, you can usually skip that message. But should use something like Let's Encrypt if you wanna go public with your WebApp.

Deploying flask app on cpanel, INTERNAL SERVER ERROR when changing requesting other than base url

I want to deploy my flask-restx application on a shared hosting. Since I am beginner in deployment, I followed a video tutorial from youtube.
I did step by step by following this tutorial.
For those who do not want to go through the tutorial, I am writing the steps:
I created an application from the Python cPanel
Initial set up in Cpanel
Then I opened terminal and changed my venv and installed flask by "pip install flask"
Project Structure
filas_folder/
├──public
├──tmp
│ └──restart.txt
├──app.py
└──passenger_wsgi.py
app.py looks like
from flask import Flask
app = Flask(__name__)
#app.route("/")
def main_():
return "flask is running"
#app.route("/user")
def main_2():
return "user is running"
if __name__ == "__main__": app.run()
Restart app from cpanel
passenger.py looks like
import imp
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
wsgi = imp.load_source('wsgi', 'app.py')
application = wsgi.app
when I open www.example.com
flask is running
But when I open www.example.com/user
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster#example.com to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
My system has cloudlinux and uses apache server. This is not the first deployment. Many wordpress and static websites are running on the server.
I opened apache logs at /usr/local/apache/logs/error_log
I get the error "Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer" http://example.com/user"
Add the following to the top of your .htaccess file:
RewriteEngine on
RewriteRule ^http://%{HTTP_HOST}%{REQUEST_URI} [END,NE]
Got this info from: https://stackoverflow.com/a/63971427/10122266

Flask basic app using OpenShift's own suggested approach: permission denied for git push

I'm building a small Flask app that will have user login, and a few other pages that will allow the user to run queries against a database.
Using the Flask User basic app as an example, I put togther a skeleton of the app, and it is working fine locally. Then in an attempt to deploy at as my first Flask app on OpenShift, I follow the guide here (changing flaskapp.py to basic_app.py). For requirements.txt, I add in all the libraries that were required by Flask, Flask User, Flask Login, and some other libraries I will need such as lxml.
When I attempt git push, I get:
remote: Traceback (most recent call last):
remote: File "/var/lib/openshift/56c60ef92d5271a900000016/python/virtenv/bin/pip", line 12, in <module>
remote: load_entry_point('pip==1.4.1', 'console_scripts', 'pip')()
remote: File "/var/lib/openshift/56c60ef92d5271a900000016/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-packages/pip/__init__.py", line 148, in main
remote: return command.main(args[1:], options)
remote: File "/var/lib/openshift/56c60ef92d5271a900000016/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-packages/pip/basecommand.py", line 171, in main
remote: log_fp = open_logfile(log_fn, 'w')
remote: File "/var/lib/openshift/56c60ef92d5271a900000016/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-packages/pip/basecommand.py", line 200, in open_logfile
remote: os.makedirs(dirname)
remote: File "/var/lib/openshift/56c60ef92d5271a900000016/python/virtenv/lib64/python2.7/os.py", line 157, in makedirs
remote: mkdir(name, mode)
remote: OSError: [Errno 13] Permission denied: '/var/lib/openshift/56c60ef92d5271a900000016/.pip'
When running rhc app create prior to this, I did see the following:
Cloning into 'myflaskapp2'...
The authenticity of host 'myflaskapp2-pyderman.rhcloud.com ' can't be established.
RSA key fingerprint is ......
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'myflaskapp2-pyderman.rhcloud.com' (RSA) to the list of known hosts.
Your application 'myflaskapp2' is now available.
The fact that it finished with 'your application is now available' though would suggest that things are OK with with the SSH key.
I've taken a look at this post, but it doesn't shed any light, as I don't think I am running a "DIY" application (whatever that is).
Any help much appreciated.
I know is late but I faced the same problem yesterday and I was able to solve it.
This happens when there's already a socket in the port your app is using. What to do? SSH into you server, kill your app process and push again.
Another option is that you're using the wrong port. You should use the port and IP from the environment variables (if you are using Python: OPENSHIFT_PYTHON_IP and OPENSHIFT_PYTHON_PORT)
About your second question: If you used a custom cartridge or use diy-0.1 cartridge then your app is running over diy. If you used python-2.6, 2.7 or 3.3 then is not.

Getting internal server error on Flask App hosted on Azure

I've hosted a flask app on Azure, but there seems to be some problem with linking the WSGIHandler. It is a very simple bug. I can't seem to identify it.
The following is the error I'm getting in my Logs
Error occurred while reading WSGI handler:
Traceback (most recent call last):
File "D:\Python27\Scripts\wfastcgi.py", line 711, in main
env, handler = read_wsgi_handler(response.physical_path)
File "D:\Python27\Scripts\wfastcgi.py", line 568, in read_wsgi_handler
return env, get_wsgi_handler(handler_name)
File "D:\Python27\Scripts\wfastcgi.py", line 551, in get_wsgi_handler
raise ValueError('"%s" could not be imported' % handler_name)
ValueError: "App" could not be imported
StdOut:
StdErr:
ErrorCode Access is denied.
(0x5)
Here is my folder structure
myapplication
-- App
-- __init__.py
The contents of __init__.py is
from flask import Flask
# initialize the flask app
app = Flask(__name__)
print "init"
#app.route('/')
def hello():
return "hello world";
if __name__ == "__main__":
app.run()
I've configured the following App Settings in Azure Web App
PYTHONPATH = D:\home\site\wwwroot
WSGI_HANDLER = App.app
Per my understanding, your deployment is incompleteness, as Azure uses IIS to host python web sites on Web Apps Services, which needs a web.config to configure hander mapping and URL rewrite rules and some other settings.
To create and deploy a flask project on Azure Web Apps , we can generally follow the steps below:
1, On Azure manage portal, click NEW => COMPUTE => WEB APP =>FROM GALLERY at the bottom navigation, on the ADD WEB APP dialog, select Flask, name the site on the next dialog page. Now we have created a flask web site project.
We can type the endpoint on the browser to check the website, http://<your_site_name>.azurewebsite.net
2,On the web apps list, click the name we created above to get into the configuration page, click DASHBOARD, at the quick glance column, click Set up deployment from source control ,select Local Git repository. Now there is an additional tab named DEPLOYMENT beside the DASHBOARD tab. And in the DEPLOYMENT page there are steps of how to deploy your site by git. We can clone the project to local by the GIT URL provided on this page.
We can get more on this official article

Print to file Beanstalk Worker Tier (Python)

I asked something similar to this question and I haven't gotten any responses that help. So, I have decided to simplify things as much as I can with the following:
I have developed a python flask application and deployed to a beanstalk worker tier python environment. The issue is I can't figure out how to print or log or write anything anywhere. I need to debug this application and the only way I know how to do that is by printing to either the console or a log file to see exactly what is going on. When I run the application locally I can print to the console, write to files, and log with zero problems, it is just when I deploy it to the beanstalk environment that nothing happens. I have SSHed into the ec2 instance where I have application deployed and searched practically every file and I find that nothing was written by my python script anywhere.
This question probably seems absolutely stupid but can someone please provide me with an example of a python flask application that will run on a beanstalk worker environment that just prints "Hello World" to some file that I can find on the ec2 instance? Please include what should be written the requirements.txt file and any *.config files in the .ebextensions folder.
Thank You
Here is another simple python app that you can try. The one in the blog will work as well but this shows a minimal example of an app that prints messages received from SQS to a file on the EC2 instance.
Your app source folder should have the following files:
application.py
import os
import time
import flask
import json
application = flask.Flask(__name__)
start_time = time.time()
counter_file = '/tmp/worker_role.tmp'
#application.route('/', methods=['GET', 'POST'])
def hello_world():
if flask.request.method == 'POST':
with open(counter_file, 'a') as f:
f.write(flask.request.data + "\n")
return flask.Response(status=200)
if __name__ == '__main__':
application.run(host='0.0.0.0', debug=True)
requirements.txt
Flask==0.9
Werkzeug==0.8.3
.ebextensions/01-login.config
option_settings:
- namespace: aws:autoscaling:launchconfiguration
option_name: EC2KeyName
value: your-key-name
Launch a worker tier 1.1 environment with a Python 2.7 solution stack. I tested with (64bit Amazon Linux 2014.03 v1.0.4 running Python 2.7).
Wait for the environment to go green. After it goes green click on the queue URL as visible in the console. This will take you to the SQS console page. Right click on the queue and click on "Send a message". Then type the following message: {"hello" : "world"}.
SSH to the EC2 instance and open the file /tmp/worker_role.tmp. You should be able to see your message in this file.
Make sure you have IAM policies correctly configured for using Worker Role environments.
For more information on IAM policies refer this answer: https://stackoverflow.com/a/23942498/161628
There is a python+flask on beanstalk example on AWS Application Management Blog:
http://blogs.aws.amazon.com/application-management/post/Tx1Y8QSQRL1KQZC/Elastic-Beanstalk-Video-Tutorial-Worker-Tier
http://blogs.aws.amazon.com/application-management/post/Tx36JL4GPZR4G98/A-Sample-App-For-Startups
For the logging issues, i'd suggest:
Check your /var/log/eb-cfn-init.log (and other log files in this directory), if a .config command is failing you will see which and why there.
In your .config commands, output messages to a different log file so you see exactly where your bootstrap failed in your own file.
Add you application log file to EB Log Snapshots (/opt/elasticbeanstalk/tasks/taillogs.d/) and EB S3 log rotation (/opt/elasticbeanstalk/tasks/publishlogs.d/). See other files in these directories for examples.

Categories