Changeset 11426 in genesis


Ignore:
Timestamp:
Aug 30, 2012, 6:43:39 AM (12 years ago)
Author:
rick
Message:

Some rocket science to hack wsgi support info gformat, this speedsup processing a lot when requesting a lot of config files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/gformat.py

    r11326 r11426  
    1313#  </Directory>
    1414#
     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 processes=2 threads=25
     19# WSGISocketPrefix run/wsgi
     20#
     21# <Directory /var/www/cgi-bin>
     22#   WSGIProcessGroup gformat
     23# </Directory>
     24# WSGIScriptAlias /hello /var/www/cgi-bin/genesis/tools/gformat.py
     25#
     26#
    1527# Rick van der Zwet <info@rickvanderzwet.nl>
    1628#
     
    2638import copy
    2739import glob
     40import make_network_kml
     41import math
     42import rdnap
    2843import socket
    2944import string
    3045import subprocess
    3146import time
    32 import rdnap
    33 import math
    34 import make_network_kml
     47import urlparse
    3548from pprint import pprint
    3649from collections import defaultdict
     
    94107UNKNOWN = 90
    95108
     109
    96110datadump_cache = {}
     111interface_list_cache = {}
     112rc_conf_local_cache = {}
     113def clear_cache():
     114  ''' Poor mans cache implementation '''
     115  global datadump_cache, interface_list_cache, rc_conf_local_cache
     116  datadump_cache = {}
     117  interface_list_cache = {}
     118  rc_conf_local_cache = {}
     119 
    97120
    98121NO_DHCP = 0
     
    527550
    528551
    529 interface_list_cache = {}
    530552def make_interface_list(datadump):
    531553  if interface_list_cache.has_key(datadump['autogen_item']):
     
    609631ileiden_proxies = []
    610632normal_proxies = []
    611 rc_conf_local_cache = {}
    612633def generate_rc_conf_local(datadump):
    613634  """ Generate configuration file '/etc/rc.conf.local' """
     
    11081129
    11091130
    1110 def process_cgi_request():
     1131def process_cgi_request(environ=os.environ):
    11111132  """ When calling from CGI """
     1133  response_headers = []
     1134  content_type = 'text/plain'
     1135
    11121136  # Update repository if requested
    1113   form = cgi.FieldStorage()
    1114   if form.getvalue("action") == "update":
    1115     print "Refresh: 5; url=."
    1116     print "Content-type:text/plain\r\n\r\n",
    1117     print "[INFO] Updating subverion, please wait..."
    1118     print subprocess.Popen(['svn', 'cleanup', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
    1119     print subprocess.Popen(['svn', 'up', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
    1120     print "[INFO] All done, redirecting in 5 seconds"
    1121     sys.exit(0)
    1122 
    1123 
    1124   base_uri = os.environ['PATH_INFO']
    1125   uri = base_uri.strip('/').split('/')
    1126 
    1127   output = "Template Holder"
    1128   content_type='text/plain'
    1129   if base_uri.endswith('/create/network.kml'):
    1130       content_type='application/vnd.google-earth.kml+xml'
    1131       output = make_network_kml.make_graph()
    1132   elif not uri[0]:
    1133     if is_text_request():
    1134       content_type = 'text/plain'
    1135       output = '\n'.join(get_hostlist())
     1137  form = urlparse.parse_qs(environ['QUERY_STRING'])
     1138  if form.has_key("action") and "update" in form["action"]:
     1139    output = "[INFO] Updating subverion, please wait...\n"
     1140    output += subprocess.Popen(['svn', 'cleanup', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
     1141    output += subprocess.Popen(['svn', 'up', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
     1142    output += "[INFO] All done, redirecting in 5 seconds"
     1143    response_headers += [
     1144      ('Refresh', '5; url=.'),
     1145    ]
     1146    clear_cache()
     1147  else:
     1148    base_uri = environ['PATH_INFO']
     1149    uri = base_uri.strip('/').split('/')
     1150
     1151    output = "Template Holder"
     1152    if base_uri.endswith('/create/network.kml'):
     1153        content_type='application/vnd.google-earth.kml+xml'
     1154        output = make_network_kml.make_graph()
     1155    elif not uri[0]:
     1156      if is_text_request(environ):
     1157        output = '\n'.join(get_hostlist())
     1158      else:
     1159        content_type = 'text/html'
     1160        output = generate_title(get_hostlist())
     1161    elif len(uri) == 1:
     1162      if is_text_request(environ):
     1163        output = generate_node(uri[0])
     1164      else:
     1165        content_type = 'text/html'
     1166        output = generate_node_overview(uri[0])
     1167    elif len(uri) == 2:
     1168      output = generate_config(uri[0], uri[1])
    11361169    else:
    1137       content_type = 'text/html'
    1138       output = generate_title(get_hostlist())
    1139   elif len(uri) == 1:
    1140     if is_text_request():
    1141       content_type = 'text/plain'
    1142       output = generate_node(uri[0])
    1143     else:
    1144       content_type = 'text/html'
    1145       output = generate_node_overview(uri[0])
    1146   elif len(uri) == 2:
    1147     content_type = 'text/plain'
    1148     output = generate_config(uri[0], uri[1])
    1149   else:
    1150     assert False, "Invalid option"
    1151 
    1152   print "Content-Type: %s" % content_type
    1153   print "Content-Length: %s" % len(output)
    1154   print ""
    1155   print output
     1170      assert False, "Invalid option"
     1171
     1172  # Return response
     1173  response_headers += [
     1174    ('Content-type', content_type),
     1175    ('Content-Length', str(len(output))),
     1176  ]
     1177  return(response_headers, str(output))
     1178
    11561179
    11571180def get_realname(datadump):
     
    13921415
    13931416
    1394 def is_text_request():
     1417def is_text_request(environ=os.environ):
    13951418  """ Find out whether we are calling from the CLI or any text based CLI utility """
    13961419  try:
    1397     return os.environ['HTTP_USER_AGENT'].split()[0] in ['curl', 'fetch', 'wget']
     1420    return environ['HTTP_USER_AGENT'].split()[0] in ['curl', 'fetch', 'wget']
    13981421  except KeyError:
    13991422    return True
     
    18481871    process_cgi_request()
    18491872
     1873def application(environ, start_response):
     1874  status = '200 OK'
     1875  response_headers, output = process_cgi_request(environ)
     1876  start_response(status, response_headers)
     1877
     1878  # Debugging only
     1879  # output = 'wsgi.multithread = %s' % repr(environ['wsgi.multithread'])
     1880  # soutput += '\nwsgi.multiprocess = %s' % repr(environ['wsgi.multiprocess'])
     1881  return [output]
    18501882
    18511883if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.