Created
July 25, 2013 17:45
-
-
Save grahamg/6082075 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os, pwd, grp, subprocess | |
#Base directory of your django sites | |
wwwroot = "/var/www/" | |
#Create list of directories inside wwwroot | |
dirs = os.listdir(wwwroot) | |
#Default django project directory | |
djangoProject = "/app/" | |
#Files for django fcgi operations | |
files = { | |
"pid": "site.pid", | |
"socket": "site.sock" | |
} | |
#are the pid and sockets owned by nginx? | |
def check_ownership(file): | |
return pwd.getpwuid(os.stat(file).st_uid).pw_name | |
#convert given name to uid | |
def name_to_uid(name): | |
return pwd.getpwnam(name).pw_uid | |
#convert given group to gid | |
def name_to_gid(group): | |
return grp.getgrnam(group).gr_gid | |
#change ownership of given file to given user and group | |
def change_owner(file, user, group): | |
os.chown(file, user, group) | |
#kill given process | |
def kill_process(process): | |
try: | |
subprocess.call(["kill", process], stderr=subprocess.STDOUT, stdout=subprocess.PIPE) | |
except: | |
print "Couldn't find process for %s" % process | |
#start fcgi process | |
def startFCGI(pid, socket): | |
subprocess.call(["/var/www/control.gigenet.com/bin/python2.6","manage.py", "runfcgi", socket, pid], shell=False) | |
#loop through directories collected by os.listdir for wwroot variable | |
for dir in dirs: | |
workdir = wwwroot + dir + djangoProject | |
os.chdir(workdir) | |
socket = "socket=" + workdir + "%s" % files['socket'] | |
pid = "pidfile=" + workdir + "%s" % files['pid'] | |
try: | |
f = (workdir + files['pid']) | |
process = f.readlines()[0] | |
kill_process(process) | |
os.remove(files['pid']) | |
startFCGI(pid, socket) | |
f.close() | |
except: | |
startFCGI(pid, socket) | |
#verbose output when sites start | |
print "Starting %s" % dir | |
for file, value in files.iteritems(): | |
owner = check_ownership(workdir + value) | |
if owner != "nginx": | |
#find uid of nginx user | |
newOwner = name_to_uid("nginx") | |
#find gid of nginx group | |
newGroup = name_to_gid("nginx") | |
#change owner of file | |
change_owner(value, newOwner, newGroup) | |
else: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment