Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / views.py @ e76bada8

History | View | Annotate | Download (20.2 kB)

1
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33
#
34

    
35
import os
36

    
37
from django.conf import settings
38
from django.utils.translation import gettext_lazy as _
39
from django.template import loader
40
from django.http import HttpResponse
41
from django.utils.translation import get_language
42
from django.utils import simplejson as json
43
from synnefo_branding.utils import render_to_string
44
from django.core.urlresolvers import reverse
45
from django.template import RequestContext
46

    
47
from synnefo.util.version import get_component_version
48

    
49
from synnefo.ui import settings as uisettings
50

    
51
SYNNEFO_JS_LIB_VERSION = get_component_version('app')
52

    
53
# UI preferences settings
54
TIMEOUT = getattr(settings, "TIMEOUT", 10000)
55
UPDATE_INTERVAL = getattr(settings, "UI_UPDATE_INTERVAL", 5000)
56
CHANGES_SINCE_ALIGNMENT = getattr(settings, "UI_CHANGES_SINCE_ALIGNMENT", 0)
57
UPDATE_INTERVAL_INCREASE = getattr(settings, "UI_UPDATE_INTERVAL_INCREASE",
58
                                   500)
59
UPDATE_INTERVAL_INCREASE_AFTER_CALLS_COUNT = \
60
    getattr(settings, "UI_UPDATE_INTERVAL_INCREASE_AFTER_CALLS_COUNT", 3)
61
UPDATE_INTERVAL_FAST = getattr(settings, "UI_UPDATE_INTERVAL_FAST", 2500)
62
UPDATE_INTERVAL_MAX = getattr(settings, "UI_UPDATE_INTERVAL_MAX", 10000)
63

    
64
# predefined values settings
65
VM_IMAGE_COMMON_METADATA = \
66
    getattr(settings, "UI_VM_IMAGE_COMMON_METADATA", ["OS", "users"])
67
SUGGESTED_FLAVORS_DEFAULT = {}
68
SUGGESTED_FLAVORS = getattr(settings, "VM_CREATE_SUGGESTED_FLAVORS",
69
                            SUGGESTED_FLAVORS_DEFAULT)
70
SUGGESTED_ROLES_DEFAULT = ["Database server", "File server", "Mail server",
71
                           "Web server", "Proxy"]
72
SUGGESTED_ROLES = getattr(settings, "VM_CREATE_SUGGESTED_ROLES",
73
                          SUGGESTED_ROLES_DEFAULT)
74
IMAGE_ICONS = settings.IMAGE_ICONS
75
IMAGE_DELETED_TITLE = \
76
    getattr(settings, 'UI_IMAGE_DELETED_TITLE', '(deleted)')
77
IMAGE_DELETED_SIZE_TITLE = \
78
    getattr(settings, 'UI_IMAGE_DELETED_SIZE_TITLE', '(none)')
79

    
80
SUPPORT_SSH_OS_LIST = getattr(settings, "UI_SUPPORT_SSH_OS_LIST",)
81
OS_CREATED_USERS = getattr(settings, "UI_OS_DEFAULT_USER_MAP")
82
UNKNOWN_OS = getattr(settings, "UI_UNKNOWN_OS", "unknown")
83

    
84
AUTH_COOKIE_NAME = getattr(settings, "UI_AUTH_COOKIE_NAME", 'synnefo_user')
85

    
86
# never change window location. Helpful in development environments
87
AUTH_SKIP_REDIRECTS = getattr(settings, "UI_AUTH_SKIP_REDIRECTS", False)
88

    
89
# UI behaviour settings
90
DELAY_ON_BLUR = getattr(settings, "UI_DELAY_ON_BLUR", True)
91
UPDATE_HIDDEN_VIEWS = getattr(settings, "UI_UPDATE_HIDDEN_VIEWS", False)
92
HANDLE_WINDOW_EXCEPTIONS = \
93
    getattr(settings, "UI_HANDLE_WINDOW_EXCEPTIONS", True)
94
SKIP_TIMEOUTS = getattr(settings, "UI_SKIP_TIMEOUTS", 1)
95

    
96
# Additional settings
97
VM_NAME_TEMPLATE = getattr(settings, "VM_CREATE_NAME_TPL", "My {0} server")
98
NO_FQDN_MESSAGE = getattr(settings, "UI_NO_FQDN_MESSAGE", "No available FQDN")
99

    
100
MAX_SSH_KEYS_PER_USER = getattr(settings, "USERDATA_MAX_SSH_KEYS_PER_USER")
101
FLAVORS_DISK_TEMPLATES_INFO = \
102
    getattr(settings, "UI_FLAVORS_DISK_TEMPLATES_INFO", {})
103
SYSTEM_IMAGES_OWNERS = getattr(settings, "UI_SYSTEM_IMAGES_OWNERS", {})
104
CUSTOM_IMAGE_HELP_URL = getattr(settings, "UI_CUSTOM_IMAGE_HELP_URL", None)
105

    
106
# MEDIA PATHS
107
UI_MEDIA_URL = \
108
    getattr(settings, "UI_MEDIA_URL",
109
            "%sui/static/snf/" % (settings.MEDIA_URL,))
110
UI_SYNNEFO_IMAGES_URL = \
111
    getattr(settings,
112
            "UI_SYNNEFO_IMAGES_URL", UI_MEDIA_URL + "images/")
113
UI_SYNNEFO_CSS_URL = \
114
    getattr(settings,
115
            "UI_SYNNEFO_CSS_URL", UI_MEDIA_URL + "css/")
116
UI_SYNNEFO_JS_URL = \
117
    getattr(settings,
118
            "UI_SYNNEFO_JS_URL", UI_MEDIA_URL + "js/")
119
UI_SYNNEFO_JS_LIB_URL = \
120
    getattr(settings,
121
            "UI_SYNNEFO_JS_LIB_URL", UI_SYNNEFO_JS_URL + "lib/")
122
UI_SYNNEFO_JS_WEB_URL = \
123
    getattr(settings, "UI_SYNNEFO_JS_WEB_URL", UI_SYNNEFO_JS_URL + "ui/web/")
124
UI_SYNNEFO_FONTS_BASE_URL = \
125
    getattr(settings,
126
            "UI_FONTS_BASE_URL", "//fonts.googleapis.com/")
127

    
128
# extensions
129
ENABLE_GLANCE = getattr(settings, 'UI_ENABLE_GLANCE', True)
130

    
131
DIAGNOSTICS_UPDATE_INTERVAL = \
132
    getattr(settings, 'UI_DIAGNOSTICS_UPDATE_INTERVAL', 2000)
133

    
134
# network settings
135
DEFAULT_NETWORK_TYPES = {'MAC_FILTERED': 'mac-filtering',
136
                         'PHYSICAL_VLAN': 'physical-vlan'}
137
NETWORK_TYPES = \
138
    getattr(settings,
139
            'UI_NETWORK_AVAILABLE_NETWORK_TYPES', DEFAULT_NETWORK_TYPES)
140
DEFAULT_NETWORK_SUBNETS = ['10.0.0.0/24', '192.168.1.1/24']
141
NETWORK_SUBNETS = \
142
    getattr(settings,
143
            'UI_NETWORK_AVAILABLE_SUBNETS', DEFAULT_NETWORK_SUBNETS)
144
NETWORK_DUPLICATE_NICS = \
145
    getattr(settings,
146
            'UI_NETWORK_ALLOW_DUPLICATE_VM_NICS', False)
147
NETWORK_STRICT_DESTROY = \
148
    getattr(settings,
149
            'UI_NETWORK_STRICT_DESTROY', False)
150
NETWORK_ALLOW_MULTIPLE_DESTROY = \
151
    getattr(settings,
152
            'UI_NETWORK_ALLOW_MULTIPLE_DESTROY', False)
153
AUTOMATIC_NETWORK_RANGE_FORMAT = getattr(settings,
154
                                         'UI_AUTOMATIC_NETWORK_RANGE_FORMAT',
155
                                         "192.168.%d.0/24").replace("%d",
156
                                                                    "{0}")
157
GROUP_PUBLIC_NETWORKS = getattr(settings, 'UI_GROUP_PUBLIC_NETWORKS', True)
158
GROUPED_PUBLIC_NETWORK_NAME = \
159
    getattr(settings, 'UI_GROUPED_PUBLIC_NETWORK_NAME', 'Internet')
160

    
161

    
162
DEFAULT_FORCED_SERVER_NETWORKS = \
163
    getattr(settings, "CYCLADES_FORCED_SERVER_NETWORKS", [])
164
FORCED_SERVER_NETWORKS = getattr(settings, "UI_FORCED_SERVER_NETWORKS",
165
                                 DEFAULT_FORCED_SERVER_NETWORKS)
166

    
167
DEFAULT_HOTPLUG_ENABLED = getattr(settings, "CYCLADES_GANETI_USE_HOTPLUG",
168
                                  True)
169
HOTPLUG_ENABLED = getattr(settings, "UI_HOTPLUG_ENABLED",
170
                          DEFAULT_HOTPLUG_ENABLED)
171

    
172
def template(name, request, context):
173
    template_path = os.path.join(os.path.dirname(__file__), "templates/")
174
    current_template = template_path + name + '.html'
175
    t = loader.get_template(current_template)
176
    media_context = {
177
        'UI_MEDIA_URL': UI_MEDIA_URL,
178
        'SYNNEFO_JS_URL': UI_SYNNEFO_JS_URL,
179
        'SYNNEFO_JS_LIB_URL': UI_SYNNEFO_JS_LIB_URL,
180
        'SYNNEFO_FONTS_BASE_URL': UI_SYNNEFO_FONTS_BASE_URL,
181
        'SYNNEFO_JS_WEB_URL': UI_SYNNEFO_JS_WEB_URL,
182
        'SYNNEFO_IMAGES_URL': UI_SYNNEFO_IMAGES_URL,
183
        'SYNNEFO_CSS_URL': UI_SYNNEFO_CSS_URL,
184
        'SYNNEFO_JS_LIB_VERSION': SYNNEFO_JS_LIB_VERSION,
185
        'DEBUG': settings.DEBUG
186
    }
187
    context.update(media_context)
188
    return HttpResponse(t.render(RequestContext(request, context)))
189

    
190

    
191
def home(request):
192
    context = {
193
        'timeout': TIMEOUT,
194
        'project': '+nefo',
195
        'request': request,
196
        'current_lang': get_language() or 'en',
197
        'compute_api_url': json.dumps(uisettings.COMPUTE_URL),
198
        'network_api_url': json.dumps(uisettings.NETWORK_URL),
199
        'user_catalog_url': json.dumps(uisettings.USER_CATALOG_URL),
200
        'feedback_post_url': json.dumps(uisettings.FEEDBACK_URL),
201
        'accounts_api_url': json.dumps(uisettings.ACCOUNT_URL),
202
        'logout_redirect': json.dumps(uisettings.LOGOUT_REDIRECT),
203
        'login_redirect': json.dumps(uisettings.LOGIN_URL),
204
        'glance_api_url': json.dumps(uisettings.GLANCE_URL),
205
        'translate_uuids': json.dumps(True),
206
        # update interval settings
207
        'update_interval': UPDATE_INTERVAL,
208
        'update_interval_increase': UPDATE_INTERVAL_INCREASE,
209
        'update_interval_increase_after_calls':
210
        UPDATE_INTERVAL_INCREASE_AFTER_CALLS_COUNT,
211
        'update_interval_fast': UPDATE_INTERVAL_FAST,
212
        'update_interval_max': UPDATE_INTERVAL_MAX,
213
        'changes_since_alignment': CHANGES_SINCE_ALIGNMENT,
214
        'image_icons': IMAGE_ICONS,
215
        'auth_cookie_name': AUTH_COOKIE_NAME,
216
        'auth_skip_redirects': json.dumps(AUTH_SKIP_REDIRECTS),
217
        'suggested_flavors': json.dumps(SUGGESTED_FLAVORS),
218
        'suggested_roles': json.dumps(SUGGESTED_ROLES),
219
        'vm_image_common_metadata': json.dumps(VM_IMAGE_COMMON_METADATA),
220
        'synnefo_version': SYNNEFO_JS_LIB_VERSION,
221
        'delay_on_blur': json.dumps(DELAY_ON_BLUR),
222
        'update_hidden_views': json.dumps(UPDATE_HIDDEN_VIEWS),
223
        'handle_window_exceptions': json.dumps(HANDLE_WINDOW_EXCEPTIONS),
224
        'skip_timeouts': json.dumps(SKIP_TIMEOUTS),
225
        'vm_name_template': json.dumps(VM_NAME_TEMPLATE),
226
        'flavors_disk_templates_info': json.dumps(FLAVORS_DISK_TEMPLATES_INFO),
227
        'support_ssh_os_list': json.dumps(SUPPORT_SSH_OS_LIST),
228
        'unknown_os': json.dumps(UNKNOWN_OS),
229
        'os_created_users': json.dumps(OS_CREATED_USERS),
230
        'userdata_keys_limit': json.dumps(MAX_SSH_KEYS_PER_USER),
231
        'use_glance': json.dumps(ENABLE_GLANCE),
232
        'system_images_owners': json.dumps(SYSTEM_IMAGES_OWNERS),
233
        'custom_image_help_url': CUSTOM_IMAGE_HELP_URL,
234
        'image_deleted_title': json.dumps(IMAGE_DELETED_TITLE),
235
        'image_deleted_size_title': json.dumps(IMAGE_DELETED_SIZE_TITLE),
236
        'network_suggested_subnets': json.dumps(NETWORK_SUBNETS),
237
        'network_available_types': json.dumps(NETWORK_TYPES),
238
        'forced_server_networks': json.dumps(FORCED_SERVER_NETWORKS),
239
        'network_allow_duplicate_vm_nics': json.dumps(NETWORK_DUPLICATE_NICS),
240
        'network_strict_destroy': json.dumps(NETWORK_STRICT_DESTROY),
241
        'network_allow_multiple_destroy':
242
        json.dumps(NETWORK_ALLOW_MULTIPLE_DESTROY),
243
        'automatic_network_range_format':
244
        json.dumps(AUTOMATIC_NETWORK_RANGE_FORMAT),
245
        'grouped_public_network_name': json.dumps(GROUPED_PUBLIC_NETWORK_NAME),
246
        'group_public_networks': json.dumps(GROUP_PUBLIC_NETWORKS),
247
        'hotplug_enabled': json.dumps(HOTPLUG_ENABLED),
248
        'diagnostics_update_interval': json.dumps(DIAGNOSTICS_UPDATE_INTERVAL),
249
        'no_fqdn_message': json.dumps(NO_FQDN_MESSAGE)
250
    }
251
    return template('home', request, context)
252

    
253

    
254
def machines_console(request):
255
    host, port, password = ('', '', '')
256
    host = request.GET.get('host', '')
257
    port = request.GET.get('port', '')
258
    password = request.GET.get('password', '')
259
    machine = request.GET.get('machine', '')
260
    host_ip = request.GET.get('host_ip', '')
261
    host_ip_v6 = request.GET.get('host_ip_v6', '')
262
    context = {'host': host, 'port': port, 'password': password,
263
               'machine': machine, 'host_ip': host_ip,
264
               'host_ip_v6': host_ip_v6}
265
    return template('machines_console', request, context)
266

    
267

    
268
def js_tests(request):
269
    return template('tests', request, {})
270

    
271

    
272
CONNECT_LINUX_LINUX_MESSAGE = \
273
    _("""A direct connection to this machine can be established using the
274
<a target="_blank" href="http://en.wikipedia.org/wiki/Secure_Shell">
275
SSH Protocol</a>.
276
To do so open a terminal and type the following at the prompt to connect
277
to your machine:""")
278
CONNECT_LINUX_WINDOWS_MESSAGE = \
279
    _("""A direct connection to this machine can be established using
280
<a target="_blank"
281
href="http://en.wikipedia.org/wiki/Remote_Desktop_Services">
282
Remote Desktop Service</a>.
283
To do so, open the following file with an appropriate \
284
remote desktop client.""")
285
CONNECT_LINUX_WINDOWS_SUBMESSAGE = \
286
    _("""If you don't have a Remote Desktop client already installed,
287
we suggest the use of <a target="_blank"
288
href=
289
"http://sourceforge.net/projects/tsclient/files/latest/download?source=files">
290
tsclient</a>.<br /><span class="important">IMPORTANT: It may take up to 15
291
minutes for your Windows VM to become available
292
after its creation.</span>""")
293
CONNECT_WINDOWS_LINUX_MESSAGE = \
294
    _("""A direct connection to this machine can be established using the
295
<a target="_blank"
296
href="http://en.wikipedia.org/wiki/Secure_Shell">SSH Protocol</a>.
297
Open an ssh client such as PuTTY and type the following:""")
298
CONNECT_WINDOWS_LINUX_SUBMESSAGE = \
299
    _("""If you do not have an ssh client already installed,
300
<a target="_blank"
301
href="http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe">
302
Download PuTTY</a>""")
303

    
304
CONNECT_WINDOWS_WINDOWS_MESSAGE = \
305
    _("""A direct connection to this machine can be
306
established using Remote Desktop. Click on the following link, and if asked
307
open it using "Remote Desktop Connection".
308
<br /><span class="important">
309
IMPORTANT: It may take up to 15 minutes for your Windows VM to become available
310
after its creation.</span>""")
311
CONNECT_WINDOWS_WINDOWS_SUBMESSAGE = \
312
    _("""Save this file to disk for future use.""")
313

    
314
# info/subinfo for all os combinations
315
#
316
# [0] info gets displayed on top of the message box
317
# [1] subinfo gets displayed on the bottom as extra info
318
# provided to the user when needed
319
CONNECT_PROMPT_MESSAGES = {
320
    'linux': {
321
        'linux': [CONNECT_LINUX_LINUX_MESSAGE, ""],
322
        'windows': [CONNECT_LINUX_WINDOWS_MESSAGE,
323
                    CONNECT_LINUX_WINDOWS_SUBMESSAGE],
324
        'ssh_message': "ssh %(user)s@%(hostname)s",
325
        'ssh_message_port': "ssh -p %(port)s %(user)s@%(hostname)s"
326

    
327
    },
328
    'windows': {
329
        'linux': [CONNECT_WINDOWS_LINUX_MESSAGE,
330
                  CONNECT_WINDOWS_LINUX_SUBMESSAGE],
331
        'windows': [CONNECT_WINDOWS_WINDOWS_MESSAGE,
332
                    CONNECT_WINDOWS_WINDOWS_SUBMESSAGE],
333
        'ssh_message': "%(user)s@%(hostname)s",
334
        'ssh_message_port': "%(user)s@%(hostname)s (port: %(port)s)"
335
    },
336
}
337

    
338
APPEND_CONNECT_PROMPT_MESSAGES = \
339
    getattr(settings, 'UI_CONNECT_PROMPT_MESSAGES', {})
340
for k, v in APPEND_CONNECT_PROMPT_MESSAGES.iteritems():
341
    CONNECT_PROMPT_MESSAGES[k].update(v)
342

    
343
# retrieve domain prefix from settings
344
DOMAIN_PREFIX = getattr(settings, 'MACHINE_DOMAIN_PREFIX', getattr(settings,
345
                        'BACKEND_PREFIX_ID', ""))
346

    
347
# domain template string
348
DOMAIN_TPL = "%s%%s" % DOMAIN_PREFIX
349

    
350

    
351
def machines_connect(request):
352
    ip_address = request.GET.get('ip_address', '')
353
    hostname = request.GET.get('hostname', '')
354
    operating_system = metadata_os = request.GET.get('os', '')
355
    server_id = request.GET.get('srv', 0)
356
    host_os = request.GET.get('host_os', 'Linux').lower()
357
    username = request.GET.get('username', None)
358
    domain = request.GET.get("domain", DOMAIN_TPL % int(server_id))
359
    ports = json.loads(request.GET.get('ports', '{}'))
360

    
361
    # guess host os
362
    if host_os != "windows":
363
        host_os = 'linux'
364

    
365
    # guess username
366
    if not username:
367
        username = "root"
368

    
369
        if metadata_os.lower() in ['ubuntu', 'kubuntu', 'fedora']:
370
            username = "user"
371

    
372
        if metadata_os.lower() == "windows":
373
            username = "Administrator"
374

    
375
    ssh_forward = ports.get("22", None)
376
    rdp_forward = ports.get("3389", None)
377

    
378
    # operating system provides ssh access
379
    ssh = False
380
    if operating_system != "windows":
381
        operating_system = "linux"
382
        ssh = True
383

    
384
    # rdp param is set, the user requested rdp file
385
    # check if we are on windows
386
    if operating_system == 'windows' and request.GET.get("rdp", False):
387
        port = '3389'
388
        if rdp_forward:
389
            hostname = rdp_forward.get('host', hostname)
390
            ip_address = rdp_forward.get('host', ip_address)
391
            port = str(rdp_forward.get('port', '3389'))
392

    
393
        extra_rdp_content = ''
394
        # UI sent domain info (from vm metadata) use this
395
        # otherwise use our default snf-<vm_id> domain
396
        EXTRA_RDP_CONTENT = getattr(settings, 'UI_EXTRA_RDP_CONTENT', '')
397
        if callable(EXTRA_RDP_CONTENT):
398
            extra_rdp_content = EXTRA_RDP_CONTENT(server_id, ip_address,
399
                                                  hostname, username)
400
        else:
401
            if EXTRA_RDP_CONTENT:
402
                extra_rdp_content = EXTRA_RDP_CONTENT % \
403
                    {
404
                        'server_id': server_id,
405
                        'ip_address': ip_address,
406
                        'hostname': hostname,
407
                        'user': username,
408
                        'port': port
409
                    }
410

    
411
        rdp_context = {
412
            'username': username,
413
            'domain': domain,
414
            'ip_address': ip_address,
415
            'hostname': hostname,
416
            'port': request.GET.get('port', port),
417
            'extra_content': extra_rdp_content
418
        }
419

    
420
        rdp_file_data = render_to_string("synnefo-windows.rdp", rdp_context)
421
        response = HttpResponse(rdp_file_data, mimetype='application/x-rdp')
422

    
423
        # proper filename, use server id and ip address
424
        filename = "%d-%s.rdp" % (int(server_id), hostname)
425
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
426
    else:
427
        message_key = "ssh_message"
428
        ip_address = ip_address
429
        hostname = hostname
430
        port = ''
431
        if ssh_forward:
432
            message_key = 'ssh_message_port'
433
            hostname = ssh_forward.get('host', hostname)
434
            ip_address = ssh_forward.get('host', ip_address)
435
            port = str(ssh_forward.get('port', '22'))
436

    
437
        ssh_message = CONNECT_PROMPT_MESSAGES['linux'].get(message_key)
438
        if host_os == 'windows':
439
            ssh_message = CONNECT_PROMPT_MESSAGES['windows'].get(message_key)
440
        if callable(ssh_message):
441
            link_title = ssh_message(server_id, ip_address, hostname, username)
442
        else:
443
            link_title = ssh_message % {
444
                'server_id': server_id,
445
                'ip_address': ip_address,
446
                'hostname': hostname,
447
                'user': username,
448
                'port': port
449
            }
450
        if (operating_system != "windows"):
451
            link_url = None
452

    
453
        else:
454
            link_title = _("Remote desktop to %s") % ip_address
455
            if rdp_forward:
456
                hostname = rdp_forward.get('host', hostname)
457
                ip_address = rdp_forward.get('host', ip_address)
458
                port = str(rdp_forward.get('port', '3389'))
459
                link_title = _("Remote desktop to %s (port %s)") % (ip_address,
460
                                                                    port)
461
            link_url = \
462
                "%s?ip_address=%s&os=%s&rdp=1&srv=%d&username=%s&domain=%s" \
463
                "&hostname=%s&port=%s" % (
464
                    reverse("ui_machines_connect"), ip_address,
465
                    operating_system, int(server_id), username,
466
                    domain, hostname, port)
467

    
468
        # try to find a specific message
469
        try:
470
            connect_message = \
471
                CONNECT_PROMPT_MESSAGES[host_os][operating_system][0]
472
            subinfo = CONNECT_PROMPT_MESSAGES[host_os][operating_system][1]
473
        except KeyError:
474
            connect_message = \
475
                _("You are trying to connect from a %s "
476
                  "machine to a %s machine") % (host_os, operating_system)
477
            subinfo = ""
478

    
479
        response_object = {
480
            'ip': ip_address,
481
            'os': operating_system,
482
            'ssh': ssh,
483
            'info': unicode(connect_message),
484
            'subinfo': unicode(subinfo),
485
            'link': {'title': unicode(link_title), 'url': link_url}
486
        }
487
        response = \
488
            HttpResponse(json.dumps(response_object),
489
                         mimetype='application/json')  # no windows, no rdp
490

    
491
    return response