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.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.