Changeset 13935 in genesis


Ignore:
Timestamp:
Jul 6, 2017, 8:56:40 AM (8 years ago)
Author:
www
Message:

Add caching support for CGI output

WSCGI does not work well with internal structures and multiple calls, reverting
to standaard cache using a file-based pre-generated backend powered by Apache24
rewriting.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/gformat.py

    r13930 r13935  
    1 #!/usr/bin/env python
     1#!/usr/local/bin/python
    22#
    33# vim:ts=2:et:sw=2:ai
     
    77#
    88# Sample apache configuration (mind the AcceptPathInfo!)
    9 #  ScriptAlias /wleiden/config /usr/local/www/genesis/tools/gformat.py
    10 #  <Directory /usr/local/www/genesis>
    11 #    Allow from all
    12 #    AcceptPathInfo On
    13 #  </Directory>
    14 #
    15 # MUCH FASTER WILL IT BE with mod_wsgi, due to caches and avoiding loading all
    16 # the heavy template lifting all the time.
    17 #
    18 # WSGIDaemonProcess gformat threads=25
    19 # WSGISocketPrefix run/wsgi
     9# Alias /config /usr/local/www/config
     10# <Directory /usr/local/www/config>
     11#    AddHandler cgi-script .py
     12#    Require all granted
    2013#
    21 # <Directory /var/www/cgi-bin>
    22 #   WSGIProcessGroup gformat
     14#    RewriteEngine on
     15#    RewriteCond %{REQUEST_FILENAME} !-f
     16#    RewriteRule ^(.*)$ gformat.py/$1 [L,QSA]
     17#    Options +FollowSymlinks +ExecCGI
    2318# </Directory>
    24 # WSGIScriptAlias /hello /var/www/cgi-bin/genesis/tools/gformat.py
    2519#
    2620# Package dependencies list:
     
    9993  NODE_DIR = os.environ['CONFIGROOT']
    10094else:
    101   NODE_DIR = os.path.abspath(os.path.dirname(__file__)) + '/../nodes'
     95  NODE_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) + '/../nodes'
    10296__version__ = '$Id$'
     97
     98CACHE_DIR = os.path.abspath(os.path.dirname(__file__))
    10399
    104100files = [
     
    435431  return "\n".join(files)
    436432
    437 def generate_node_overview(host):
     433def generate_node_overview(host, datadump=False):
    438434  """ Print overview of all files available for node """
    439   datadump = get_yaml(host)
     435  if not datadump:
     436    datadump = get_yaml(host)
    440437  params = { 'host' : host }
    441438  output = "<em><a href='..'>Back to overview</a></em><hr />"
     
    443440  for cf in files:
    444441    params['cf'] = cf
    445     output += '<li><a href="%(host)s/%(cf)s">%(cf)s</a></li>\n' % params
     442    output += '<li><a href="%(cf)s">%(cf)s</a></li>\n' % params
    446443  output += "</ul>"
    447444
     
    459456          continue
    460457        params = { 'remote': remote, 'remote_ip' : ifacedump['ip'] }
    461         output += '<li><a href="%(remote)s">%(remote)s</a> -- %(remote_ip)s</li>\n' % params
     458        output += '<li><a href="../%(remote)s">%(remote)s</a> -- %(remote_ip)s</li>\n' % params
    462459  output += "</ul>"
    463460  output += "<h2>MOTD details:</h2><pre>" + generate_motd(datadump) + "</pre>"
     
    14891486    reload_cache()
    14901487  else:
    1491     base_uri = environ['PATH_INFO']
    1492     uri = base_uri.strip('/').split('/')
     1488    base_uri = environ['REQUEST_URI']
     1489    uri = base_uri.strip('/').split('/')[1:]
    14931490
    14941491    output = "Template Holder"
     
    14991496        content_type='application/json'
    15001497        output = make_network_kml.make_nodeplanner_json()
    1501     elif not uri[0]:
     1498    elif not uri:
    15021499      if is_text_request(environ):
    15031500        output = '\n'.join(get_hostlist())
     
    15101507      else:
    15111508        content_type = 'text/html'
    1512         output = generate_node_overview(uri[0])
     1509        output = open(os.path.join(CACHE_DIR, uri[0], 'index.html'), 'r').read()
    15131510    elif len(uri) == 2:
    15141511      output = generate_config(uri[0], uri[1])
     
    18121809  """Hard working sub"""
    18131810  # Allow easy hacking using the CLI
    1814   if not os.environ.has_key('PATH_INFO'):
     1811  if not os.environ.has_key('REQUEST_URI'):
    18151812    if len(sys.argv) < 2:
    18161813      usage()
     
    18691866         print generate_config(node, config, datadump)
    18701867    elif sys.argv[1] == "test-cgi":
    1871       os.environ['PATH_INFO'] = "/".join(sys.argv[2:])
     1868      os.environ['REQUEST_URI'] = "/".join(['config'] + sys.argv[2:])
    18721869      os.environ['SCRIPT_NAME'] = __file__
    18731870      response_headers, output = process_cgi_request()
     
    18821879          os.makedirs(items['wdir'])
    18831880        datadump = get_yaml(node)
     1881        f = open("%(wdir)s/index.html" % items, "w")
     1882        f.write(generate_node_overview(items['node'], datadump))
     1883        f.close()
    18841884        for config in files:
    18851885          items['config'] = config
Note: See TracChangeset for help on using the changeset viewer.