Allow reconnecting to the same terminal session
[invirt/packages/invirt-web.git] / code / main.py
index 0f8ed77..77646c9 100755 (executable)
@@ -406,6 +406,28 @@ console will suffer artifacts.
                      hostname=cherrypy.request.local.name)
             return d
 
+        @cherrypy.expose
+        def at(self, machine_id, k=None, c=0, force=0):
+            machine = validation.Validate(cherrypy.request.login, cherrypy.request.state, machine_id=machine_id).machine
+            if machine_id in self.atsessions:
+                term = self.atsessions[machine_id]
+            else:
+                print >>sys.stderr, "spawning new session for terminal to ",machine_id
+                term = self.atsessions[machine_id] = self.atmulti.create(
+                    ["ssh", "-e","none", "-l", machine.name, config.console.hostname]
+                    )
+            if k:
+                self.atmulti.proc_write(term,k)
+            time.sleep(0.002)
+            dump=self.atmulti.dump(term,c,int(force))
+            cherrypy.response.headers['Content-Type']='text/xml'
+            if isinstance(dump,str):
+                return dump
+            else:
+                print "Removing session for", machine_id
+                del self.atsessions[machine_id]
+                return '<?xml version="1.0"?><idem></idem>'
+
     machine = MachineView()