~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/console/service.py

ivle.webapp.console.service: Port www/apps/consoleservice to new framework.
    consoleservice now lives under /console/service, and actions are taken
    in the "ivle.op" query argument, not as the last segment of the path.
    Otherwise the interface is identical.
www/apps/console/console.js: Update to new consoleservice interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
'''
25
25
 
 
26
import cStringIO
 
27
import md5
26
28
import os
 
29
import random
27
30
import socket
 
31
import sys
 
32
import uuid
28
33
 
29
34
import cjson
30
35
import errno
31
36
 
32
 
import ivle.console
33
 
import ivle.chat
 
37
from ivle import (util, studpath, chat, console)
34
38
import ivle.conf
35
 
from ivle.webapp.base.rest import JSONRESTView, named_operation
 
39
from ivle.webapp.base.views import JSONRESTView, named_operation
36
40
 
37
41
# XXX: Should be RPC view, with actions in URL?
38
42
class ConsoleServiceRESTView(JSONRESTView):
45
49
 
46
50
        # Start the server
47
51
        jail_path = os.path.join(ivle.conf.jail_base, req.user.login)
48
 
        cons = ivle.console.Console(uid, jail_path, working_dir)
 
52
        cons = console.Console(uid, jail_path, working_dir)
49
53
 
50
54
        # Assemble the key and return it. Yes, it is double-encoded.
51
55
        return {'key': cjson.encode({"host": cons.host,
75
79
 
76
80
        msg = {'cmd':kind, 'text':text}
77
81
        try:
78
 
            json_response = ivle.chat.chat(host, port, msg, magic,decode=False)
 
82
            json_response = chat.chat(host, port, msg, magic, decode = False)
79
83
 
80
84
            # Snoop the response from python-console to check that it's valid
81
85
            try:
101
105
                raise socket.error, (enumber, estring)
102
106
        return response
103
107
 
 
108
def handle(req):
 
109
    """Handler for the Console Service AJAX backend application."""
 
110
    if len(req.path) > 0 and req.path[-1] == os.sep:
 
111
        path = req.path[:-1]
 
112
    else:
 
113
        path = req.path
 
114
    # The path determines which "command" we are receiving
 
115
    if req.path == "start":
 
116
        handle_start(req)
 
117
    elif req.path == "interrupt":
 
118
        handle_chat(req, kind='interrupt')
 
119
    elif req.path == "restart":
 
120
        handle_chat(req, kind='terminate')
 
121
    elif req.path == "chat":
 
122
        handle_chat(req)
 
123
    elif req.path == "block":
 
124
        handle_chat(req, kind="block")
 
125
    elif req.path == "flush":
 
126
        handle_chat(req, kind="flush")
 
127
    elif req.path == "inspect":
 
128
        handle_chat(req, kind="inspect")
 
129
    else:
 
130
        req.throw_error(req.HTTP_BAD_REQUEST)
 
131
 
 
132
 
104
133
 
105
134
def restart_console(uid, jail_path, working_dir, reason):
106
135
    """Tells the client that it must be issued a new console since the old 
108
137
    Returns the JSON response to be given to the client.
109
138
    """
110
139
    # Start a new console server console
111
 
    cons = ivle.console.Console(uid, jail_path, working_dir)
 
140
    cons = console.Console(uid, jail_path, working_dir)
112
141
 
113
142
    # Make a JSON object to tell the browser to restart its console client
114
143
    new_key = cjson.encode(
115
144
        {"host": cons.host, "port": cons.port, "magic": cons.magic})
116
 
 
117
 
    return {"restart": reason, "key": new_key.encode("hex")}
 
145
    json_restart = {
 
146
        "restart": reason,
 
147
        "key": new_key.encode("hex"),
 
148
    }
 
149
    
 
150
    return cjson.encode(json_restart)