best way to setup a webhook to restart apache for a django server
I first tried to use django and then django-webhooks to call a shell script that restarts the server. This didn't work, because the webpage hangs when the server restart is called, as django is reloaded.
Then I used fastcgi and python alone to create a URL that calls the shell script. I know the python script works when I run it on the server, but not when it is run from the URL.
Apache is setup as:
<VirtualHost *:80>
ServerName webhooks.myserver.com
DocumentRoot /home/ubuntu/web/common/www
<Directory />
Options FollowSymLinks +ExecCGI
AllowOverride All
</Directory>
<Files post.py>
SetHandler fastcgi-script
</Files>
FastCgiServer /home/ubuntu/web/common/www/post.py -processes 2 -socket /tmp/fcgi.sock
</VirtualHost>
The python code called by apache is:
#!/usr/bin/python
import fcgi, warnings, os, subprocess
BASE_DIR = os.getcwd()
def app(environ, start_response):
cmd = "sudo %s/../deploy/postwebhook.sh >> /var/log/votizen/webhooks_run.log 2>> /var/log/votizen/webhooks_error.log &" % BASE_DIR
warnings.warn("Running cmd=%s" % cmd)
bufsize = -1
PIPE = subprocess.PIPE
subprocess.Popen(cmd, shell=isinstance(cmd, basestring),
bufsize=bufsize, stdin=PIPE, stdout=PIPE,
stderr=PIPE, close_fds=True)
warnings.warn("Post deployment webhook completed")
start_response('200 OK', [('Content-Type', 'text/html')])
return('Hello World!')
fcgi.WSGIServer(app, bindAddress = '/tmp/fcgi开发者_StackOverflow中文版.sock').run()
And the shell script is:
#!/bin/bash
# restart the apache server
echo ' '
echo 'post webhooks started'
date '+%H:%M:%S %d-%m-%y'
apache2ctl -t; sudo /etc/init.d/apache2 stop; sudo /etc/init.d/apache2 start
# todo: check if apache failed
# copy media files for apps
echo "moving SC to S3"
python /home/ubuntu/web/corporate/manage.py sync_media_s3 -p sc
date '+%H:%M:%S %d-%m-%y'
echo 'post webhooks completed'
I'm not seeing any errors in the apache logs and the access log shows that the triggering URL is being called. However, I only see the python warnings the first time the URL is called after a restart and it never actually restarts the server.
I'm using webfaction, and the following script works for me:
import time
import os
BASE_DIR = "/path/to/your/app/"
def stopit(app):
os.popen( os.path.join(BASE_DIR, "apache2", "bin", "stop") )
def startit(app):
os.popen( os.path.join(BASE_DIR, "apache2", "bin", "start") )
def restart(app):
stopit(app)
time.sleep(1)
startit(app)
The "stop" script then looks like this:
#!/usr/local/bin/python
import os
lines = os.popen('ps -u username -o pid,command').readlines()
running = False
for line in lines:
if '/path/to/app/apache2/conf/httpd.conf' in line:
running = True
proc_id = line.split()[0]
os.system('kill %s 2> /dev/null' % proc_id)
if not running:
print "Not running"
else:
print "Stopped"
The "start" script:
/path/to/app/apache2/bin/httpd -f /path/to/app/apache2/conf/httpd.conf
I didn't use django, but with simple python following code works for me.
import os
os.system('apachectl -k restart')
精彩评论