fastcgi: Also kill trac and wipi processes.
[matthijs/servers/drsnuggles.git] / etc / init.d / fastcgi
1 #!/usr/bin/python
2 import os
3 import re
4 import pwd
5 import grp
6 import shutil
7 import stat
8
9 ROOT_DIR="/data/www"
10
11 # SITES = [(sitename, application_list)]
12 # application_list = [application_name, (application_name, command, ...)]
13 # Here, sitename is the name of the site. This folder name should exist below ROOT_DIR and
14 # is also used below SOCKET_DIR. The site name is also translated to a user and
15 # group name by replacing dots by dashes and prepending USER_PREFIX and
16 # GROUP_PREFIX.
17 #
18 # application_list specifies the applications to start for this site. These can
19 # be generic (when only application_name is given), in which case the command
20 # is looked up in APPLICATIONS using the application_name. For a site-specific
21 # application, command is the command that should be run. It will be prefixed
22 # with the site's root dir, if is not an absolute path.
23
24  
25 SITES=[
26         ('stderr.nl',             ['php', ('trac', 'applications/trac/trac.fcgi')]),
27 #       ('stdin.nl',              ['php']),
28         ('stdout.nl',             ['php']),
29         ('ninniach.nl',           ['php']),
30         ('blues-brothers.eu',     ['php']),
31         ('evolution-events.nl',   ['php', ('wipi', 'applications/wipi/wipi.fcgi'), ('hunternet', 'applications/lexnet/manage.py runfcgi')]), #, ('xerxes', 'applications/xerxes/manage.py runfcgi')]),
32 #       ('stdio.flexvps.nl',      ['php']),
33 #       ('foresightsecurity.nl',  ['php']),
34 ]
35
36 # Generic applications that can be run for any site
37 # Maps application_name to application_command. application_command will be
38 # prefixed with the site's root dir, if it is not an absolute path.
39 APPLICATIONS={"php": "/usr/bin/php-cgi"}
40
41 # Kill these procs before starting new ones. Only processes of these names that
42 # are run by the sites in SITES are killed. This is a bit hackish, we should
43 # really be using pidfiles...
44 KILL_PROCS=['php-cgi', 'manage.py', 'trac.fcgi', 'wipi.fcgi']
45
46 ## ABSOLUTE path to the spawn-fcgi binary
47 SPAWNFCGI="/usr/bin/spawn-fcgi"
48
49 ## Dir in which to create the UNIX sockets to listen on
50 SOCKET_DIR="%s/var/fcgi" % (ROOT_DIR)
51
52 ## number of PHP children to spawn
53 PHP_FCGI_CHILDREN=2
54
55 ## maximum number of requests a single PHP process can serve before it is restarted
56 PHP_FCGI_MAX_REQUESTS=1000
57
58 # The user to run as, will be prefixed to the sitename
59 USER_PREFIX="httpd-"
60 # The group to run as.
61 SCRIPT_GROUP="httpd-users"
62 # The group that should be able to use the sockets created
63 HTTPD_GROUP="www-data"
64
65 # Will be postfixed to the site's root and exported in the PHPRC variable.
66 PHPRC_DIR="conf"
67
68 #### END OF CONFIG ####
69
70 for (site, apps) in SITES:
71         site_name = re.sub('\.', '-', site)
72
73         ## switch to the following user / group
74         user_id    = "%s%s" % (USER_PREFIX, site_name)
75         
76         # Find the site dir
77         site_dir   = os.path.join(ROOT_DIR, site)
78         socket_dir = os.path.join(SOCKET_DIR, site_name)
79
80         # Pass the site dir to all fastcgi processes
81         os.environ['SITE_DIR'] = site_dir
82
83         if not site_dir:
84                 raise Exception("Site dir does not exist: %s" % (site_dir))
85         
86         
87         # Kill existing processes first
88         for procname in KILL_PROCS:
89                 os.system('killall --user %s %s' % (user_id, procname))
90
91         # Remove old sockets
92         if os.path.exists(socket_dir): 
93                 shutil.rmtree(socket_dir)
94
95         # Create dir for sockets. Make owning group root and set group write
96         # permissions, so the mask field in the acl will not block out anything.
97         os.makedirs(socket_dir)
98         os.chown(socket_dir, pwd.getpwnam(user_id)[2], grp.getgrnam(HTTPD_GROUP)[2])
99         #os.chmod(socket_dir, stat.S_IRWXU)
100         
101         for app in apps:
102                 # Unpack app tuple or lookup app command in APPLICATIONS
103                 if isinstance(app, tuple):
104                         if len(app) == 2:
105                                 (app_name, app_command) = app
106                         else:
107                                 raise Exception("Wrong number of elements in site tuple: %s", app)
108                 else:
109                         app_name = app
110                         app_command = APPLICATIONS[app_name]
111         
112                 # Prefix with site dir if not an absolute path
113                 if not os.path.isabs(app_command):
114                         app_command = os.path.join(site_dir, app_command)
115
116                 # Create socket filename
117                 socket = os.path.join(socket_dir, app_name)
118
119                 # Build the command
120                 # TODO: Wrap this in env to clear up the environment
121                 spawnfcgi = '%s -s "%s" -u "%s" -g "%s"' % (SPAWNFCGI, socket, user_id, SCRIPT_GROUP)
122                 fcgiapp = ' -- %s' % (app_command)
123
124                 if app_name == 'php':
125                         os.environ['PHP_FCGI_MAX_REQUESTS'] = str(PHP_FCGI_MAX_REQUESTS)
126                         phprc = os.path.join(site_dir, PHPRC_DIR, 'php.ini')
127                         if os.path.exists(phprc):
128                                 #os.environ['PHPRC'] = phprc
129                                 fcgiapp   += ' -c %s' % (phprc)
130                         spawnfcgi += ' -C %s' % (PHP_FCGI_CHILDREN)
131
132
133                 print spawnfcgi + fcgiapp
134                 os.system(spawnfcgi + fcgiapp)
135
136                 # Ensure www-data can write to the socket :-S
137                 # Spawn-fcgi explicitely chmods the socket after creation, very
138                 # annoying
139                 os.chmod(socket, stat.S_IRWXU | stat.S_IRWXG)