VNC: Catch the error when the VM is off and there is no host
[invirt/packages/invirt-web.git] / code / main.py
index c8444e8..7af4ecc 100755 (executable)
@@ -6,12 +6,12 @@ import cPickle
 import cgi
 import datetime
 import hmac
+import random
 import sha
 import simplejson
 import sys
 import time
 import urllib
-import random
 from StringIO import StringIO
 
 def revertStandardError():
@@ -41,6 +41,8 @@ import cache_acls
 from webcommon import InvalidInput, CodeError, State
 import controls
 from getafsgroups import getAfsGroupMembers
+import invirt.config
+invirt_config = invirt.config.load()
 
 def pathSplit(path):
     if path.startswith('/'):
@@ -255,6 +257,12 @@ def vnc(username, state, path, fields):
     token = {'data': pickled_data, 'digest': m.digest()}
     token = cPickle.dumps(token)
     token = base64.urlsafe_b64encode(token)
+    host = controls.listHost(machine)
+    if host:
+        port = 10003 + [config_host["hostname"] for config_host in invirt_config["hosts"]
+            ].index(controls.listHost(machine))
+    else:
+        port = 5900 # dummy
 
     status = controls.statusInfo(machine)
     has_vnc = hasVnc(status)
@@ -264,6 +272,7 @@ def vnc(username, state, path, fields):
              has_vnc=has_vnc,
              machine=machine,
              hostname=state.environ.get('SERVER_NAME', 'localhost'),
+             port=port,
              authtoken=token)
     return templates.vnc(searchList=[d])
 
@@ -691,14 +700,12 @@ class App:
                 headers.update(new_headers)
             e = revertStandardError()
             if e:
-                if isinstance(output, basestring):
-                    sys.stderr = StringIO()
-                    x = str(output)
-                    print >> sys.stderr, x
-                    print >> sys.stderr, 'XXX'
-                    print >> sys.stderr, e
-                    raise Exception()
-                output.addError(e)
+                if hasattr(output, 'addError'):
+                    output.addError(e)
+                else:
+                    # This only happens on redirects, so it'd be a pain to get
+                    # the message to the user.  Maybe in the response is useful.
+                    output = output + '\n\nstderr:\n' + e
             output_string =  str(output)
             checkpoint.checkpoint('output as a string')
         except Exception, err: