2010-09-15 4 views
2

J'ai d'abord essayé d'utiliser django puis django-webhooks pour appeler un script shell qui redémarre le serveur. Cela n'a pas fonctionné, car la page Web se bloque lorsque le redémarrage du serveur est appelé, car django est rechargé. Puis j'ai utilisé fastcgi et python seul pour créer une URL qui appelle le script shell. Je sais que le script python fonctionne quand je l'exécute sur le serveur, mais pas quand il est exécuté à partir de l'URL.meilleure façon de configurer un webhook pour redémarrer apache pour un serveur django

Apache est configuré comme:

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

Le code python appelé par apache:

#!/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()

Et le script shell est:

#!/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'

Je ne vois aucune Les erreurs dans les journaux Apache et le journal d'accès indiquent que l'URL de déclenchement est appelée. Cependant, je vois seulement les avertissements de python la première fois que l'URL est appelée après un redémarrage et il ne redémarre jamais réellement le serveur.

Répondre

0

J'utilise webfaction, et le script suivant fonctionne pour moi:

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) 

Le script "stop" ressemble alors à ceci:

#!/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" 

Le script "start":

/path/to/app/apache2/bin/httpd -f /path/to/app/apache2/conf/httpd.conf 
+0

Ryan, Cela ne fonctionne pas pour moi. Ne servent pas ce script via Apache? Ce que je vois est quand Apache s'arrête, le script entier s'arrête de fonctionner, et par conséquent Apache ne redémarre jamais. –

+0

L'exécution du script "start" ne fonctionne pas? Vous devrez peut-être attendre un peu plus d'une seconde pour qu'Apache se ferme. La méthode robuste serait de vérifier votre liste de processus ... –

0

Je n'ai pas utilisé django, mais avec le code python simple qui suit fonctionne pour moi.

import os 
os.system('apachectl -k restart') 
Questions connexes