Revision 4cf8adf8 api/actions.py

b/api/actions.py
38 38
    """Arrange for an OOB console of the specified type
39 39

  
40 40
    This method arranges for an OOB console of the specified type.
41
    Only "vnc" type consoles are supported for now.
41
    Only consoles of type "vnc" are supported for now.
42
    
42 43
    It uses a running instance of vncauthproxy to setup proper
43 44
    VNC forwarding with a random password, then returns the necessary
44 45
    VNC connection info to the caller.
45 46

  
47
    JSON Request: {
48
        "console": {
49
            "type": "vnc"
50
        }
51
    }
52

  
53
    JSON Reply: {
54
        "vnc": {
55
            "host": "fqdn_here",
56
            "port": a_port_here,
57
            "password": "a_password_here"
58
        }
59
    }
60

  
46 61
    """
47 62
    # Normal Response Code: 200
48 63
    # Error Response Codes: computeFault (400, 500),
......
55 70
    #                       overLimit (413)
56 71
    try:
57 72
        console_type = args.get('type', '')
58
        if console_type != 'VNC':
59
            raise BadRequest(message="type can only be 'VNC'")
73
        if console_type != 'vnc':
74
            raise BadRequest(message="type can only be 'vnc'")
60 75
    except KeyError:
61 76
        raise BadRequest()
62 77

  
63 78
    # Use RAPI to get VNC console information for this instance
64 79
    if get_rsapi_state(vm) != 'ACTIVE':
65 80
        raise BadRequest(message="Server not in ACTIVE state")
66
    console_data = rapi.GetInstanceConsole(vm.backend_id)
81
    if settings.TEST:
82
        console_data = { 'kind': 'vnc', 'host': 'ganeti_node', 'port': 1000 }
83
    else:
84
        console_data = rapi.GetInstanceConsole(vm.backend_id)
67 85
    if console_data['kind'] != 'vnc':
68 86
        raise ServiceUnavailable()
69 87

  
70 88
    # Let vncauthproxy decide on the source port.
71 89
    # The alternative: static allocation, e.g.
72
    # sport = console_data['port'] - 1000]
90
    # sport = console_data['port'] - 1000
73 91
    sport = 0
74 92
    daddr = console_data['host']
75 93
    dport = console_data['port']
76 94
    passwd = random_password()
77 95

  
78 96
    try:
79
        fwd = request_vnc_forwarding(sport, daddr, dport, passwd)
97
        if settings.TEST:
98
            fwd = { 'source_port': 1234, 'status': 'OK' }
99
        else:
100
            fwd = request_vnc_forwarding(sport, daddr, dport, passwd)
80 101
        if fwd['status'] != "OK":
81
            raise ServiceUnavailable("Could not allocate VNC console port")
102
            raise ServiceUnavailable()
82 103
        vnc = { 'host': getfqdn(), 'port': fwd['source_port'], 'password': passwd }
83 104
    except Exception:
84
        #raise ServiceUnavailable("Could not allocate VNC console port")
85
        raise
105
        raise ServiceUnavailable("Could not allocate VNC console port")
86 106

  
87 107
    # Format to be reviewed by [verigak], FIXME
88 108
    if request.serialization == 'xml':
......
90 110
        data = render_to_string('vnc.xml', {'vnc': vnc})
91 111
    else:
92 112
        mimetype = 'application/json'
93
        data = json.dumps(vnc)
113
        data = json.dumps({'vnc': vnc})
94 114

  
95 115
    return HttpResponse(data, mimetype=mimetype, status=200)
96 116

  

Also available in: Unified diff