Commits

Ian Bicking committed 9ce09ef

Several improvements to silver serve when using PHP

Comments (0)

Files changed (2)

silverlining/commands/php-devel-server.conf.tmpl

 <IfModule mpm_worker_module>
     StartServers          1
     MinSpareThreads      25
-    MaxSpareThreads      75 
+    MaxSpareThreads      75
     ThreadLimit          64
     ThreadsPerChild      25
     MaxClients          150
 ## Python/WSGI setup:
 RewriteEngine on
 ## Uncomment if you want to understand what is happening here:
-#RewriteLog /var/log/apache2/rewrite.log
+#RewriteLog {{tempdir}}/rewrite.log
 #RewriteLogLevel 5
 
 RewriteCond %{HTTP_HOST} ^([^:]*)
 RewriteRule . - [E=SILVER_FUNCS:{{silver_funcs}}]
 
 {{for prefix in path_prefixes}}
-RewriteCond ./{{prefix}}%{REQUEST_URI} -f
+RewriteCond {{prefix}}%{REQUEST_URI} -f
 RewriteRule (.*/)index.html $1 [L,R=permanent]
-RewriteCond ./{{prefix}}%{REQUEST_URI} -d
+RewriteCond {{prefix}}%{REQUEST_URI} -d
 RewriteCond %{REQUEST_URI} [^/]$
-RewriteCond ./{{prefix}}%{REQUEST_URI}/index.html -f
+RewriteCond {{prefix}}%{REQUEST_URI}/index.html -f
 RewriteRule (.*) $1/ [L,R=permanent]
-RewriteCond ./{{prefix}}%{REQUEST_URI} -f
-RewriteRule !\.php$ ./{{prefix}}%{REQUEST_URI} [L]
+RewriteCond {{prefix}}%{REQUEST_URI} -f
+RewriteRule !\.php$ {{prefix}}%{REQUEST_URI} [L]
 {{endfor}}
 
 RewriteRule . {{mgr_scripts}}/master-runner.php [L]

silverlining/commands/serve.py

 import sys
 import subprocess
 import glob
-import tempfile
+import shlex
+import urlparse
+import urllib
 from cmdutils import CommandError
 from tempita import Template
 from paste import httpserver
 def serve_php(config, appconfig):
     apache_config_tmpl = Template.from_filename(
         os.path.join(os.path.dirname(__file__), 'php-devel-server.conf.tmpl'))
-    path_prefixes = ['./static', appconfig.php_root]
-    if appconfig.writable_root_location:
+    path_prefixes = [os.path.join(appconfig.app_dir, 'static'),
+                     appconfig.php_root]
+    if appconfig.writable_root_location != '/dev/null':
         path_prefixes.append(appconfig.writable_root_location
                              + '/%{ENV:SILVER_HOSTNAME}')
         path_prefixes.append(appconfig.writable_root_location)
-    tempdir = os.path.join(config.args.dir, '.apache')
+    tempdir = os.path.join(os.path.abspath(config.args.dir), '.apache')
     if not os.path.exists(tempdir):
         os.makedirs(tempdir)
     includes = glob.glob('/etc/apache2/mods-enabled/*.load')
     exe_name = search_path(['apache2', 'apache', 'httpd'])
     config.logger.notify('Serving on http://localhost:8080')
     ## FIXME: -X would also be an alternative to -DFOREGROUND; not sure which is better
+    ## FIXME: this logic (and call_script()) repeats stuff in devel-server.py:
+    for url in appconfig.update_fetch:
+        if url.startswith('script:'):
+            script = url[len('script:'):]
+            print 'Running update script %s' % script
+            call_script(appconfig, script)
+        else:
+            print 'Fetching update URL %s' % url
+            url = urlparse.urljoin('http://localhost:8080', url)
+            r = urllib.urlopen(url)
+            ## FIXME: handle non-200 status
+            body = r.read()
+            if body:
+                sys.stdout.write(body)
+                if not body.endswith('\n'):
+                    sys.stdout.write('\n')
+                sys.stdout.flush()
     run([exe_name, '-f', conf_file,
          '-d', config.args.dir, '-DFOREGROUND'])
 
 
+def call_script(app_config, script):
+    run([sys.executable, os.path.join(os.path.dirname(os.path.dirname(__file__)),
+                                      'mgr-scripts', 'call-script.py'),
+         app_config.app_dir] + shlex.split(script))
+
+
 def _turn_sigterm_into_systemexit():
     """
     Attempts to turn a SIGTERM exception into a SystemExit exception.