- Timestamp:
- Aug 30, 2012, 6:43:39 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/gformat.py
r11326 r11426 13 13 # </Directory> 14 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 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 # 15 27 # Rick van der Zwet <info@rickvanderzwet.nl> 16 28 # … … 26 38 import copy 27 39 import glob 40 import make_network_kml 41 import math 42 import rdnap 28 43 import socket 29 44 import string 30 45 import subprocess 31 46 import time 32 import rdnap 33 import math 34 import make_network_kml 47 import urlparse 35 48 from pprint import pprint 36 49 from collections import defaultdict … … 94 107 UNKNOWN = 90 95 108 109 96 110 datadump_cache = {} 111 interface_list_cache = {} 112 rc_conf_local_cache = {} 113 def 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 97 120 98 121 NO_DHCP = 0 … … 527 550 528 551 529 interface_list_cache = {}530 552 def make_interface_list(datadump): 531 553 if interface_list_cache.has_key(datadump['autogen_item']): … … 609 631 ileiden_proxies = [] 610 632 normal_proxies = [] 611 rc_conf_local_cache = {}612 633 def generate_rc_conf_local(datadump): 613 634 """ Generate configuration file '/etc/rc.conf.local' """ … … 1108 1129 1109 1130 1110 def process_cgi_request( ):1131 def process_cgi_request(environ=os.environ): 1111 1132 """ When calling from CGI """ 1133 response_headers = [] 1134 content_type = 'text/plain' 1135 1112 1136 # 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]) 1136 1169 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 1156 1179 1157 1180 def get_realname(datadump): … … 1392 1415 1393 1416 1394 def is_text_request( ):1417 def is_text_request(environ=os.environ): 1395 1418 """ Find out whether we are calling from the CLI or any text based CLI utility """ 1396 1419 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'] 1398 1421 except KeyError: 1399 1422 return True … … 1848 1871 process_cgi_request() 1849 1872 1873 def 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] 1850 1882 1851 1883 if __name__ == "__main__":
Note:
See TracChangeset
for help on using the changeset viewer.