You've deployed the PyRunner.jar extension to Traffic Manager so that you can run Python code (see PyRunner.jar: Running Python code in Traffic Manager). What's the easiest way to deploy the Python code on Traffic Manager?
The following script makes this easy. It runs a quick syntax check against the Python code, then uses the REST API (Tech Tip: Using the RESTful Control API with Python - Overview) to PUT the python file in the conf/extra part of the Traffic Manager configuration.
The source of the publish script:
#!/usr/bin/python
import requests import sys import py_compile
# FIXME: these need to be correct for your deployment url = 'https://stingray:9070/api/tm/1.0/config/active/' auth = ( 'admin', 'admin' ) file = sys.argv[1] # Syntax-check script before uploading try: py_compile.compile( file, '/dev/null', file, True ) except Exception, e: print "Compilation check failed:", e sys.exit( 1 ) src=open( file, 'r' ).read() # Deploy to Stingray client = requests.Session() client.auth = auth client.verify = 0 try: response = client.put( url+'extra/'+file, data = src ) except Exception, e: print "Error: Unable to connect to " + url + ": ", e sys.exit( 1 ) print "Uploaded " + file
Save this script on your development environment, check the URL and auth parameters, and make the script executable (chmod +x).
Upload the PyRunner.jar file to Traffic Manager, as per the instructions in PyRunner.jar: Running Python code in Traffic Manager.
Create a simple TrafficScript rule and associate it with your virtual server:
java.run( "PyRunner", "vars.py" );
Create a python file named vars.py
from javax.servlet.http import HttpServlet class vars(HttpServlet): def doGet(self, request, response): reqsText = ' %30s: %s\n' % ( "URL", request.getRequestURL() ) reqsText += ' %30s: %s\n' % ( "URI", request.getRequestURI() ) reqsText += ' %30s: %s\n' % ( "Query String", request.getQueryString() ) headText = '' names = request.getHeaderNames() for n in names: headText += ' %30s: %s\n' % ( n, request.getHeader( n ) ) paramText = '' names = request.getParameterNames() for n in names: paramText += ' %30s: %s\n' % ( n, request.getParameter( n ) ) attrText = '' names = request.getAttributeNames() for n in names: attrText += ' %30s: %s\n' % ( n, request.getAttribute( n ) ) attrText += ' %30s: %s\n' % ( "args", request.getAttribute( "args" ) ) toClient = response.getWriter() response.setContentType ("text/html") htmlOut = ''' <html><head><title>vars.py</title><body> <h3>Request</h3><pre>%s</pre> <h3>Headers</h3><pre>%s</pre> <h3>Parameters</h3><pre>%s</pre> <h3>Attributes</h3><pre>%s</pre> ''' % ( reqsText, headText, paramText, attrText ) toClient.println(htmlOut)
Upload it using publish.py:
$ ./publish.py vars.py Uploaded vars.py
Then test it out!
The key advantage of this technique is that it makes publishing Python/Jython code very quick and easy. Traffic Manager will notice that the Python code has changed and re-load it immediately, giving you a quick test cycle.