Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (19.9 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
            "%ssnf-%s/" % (settings.MEDIA_URL, SYNNEFO_JS_LIB_VERSION))
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

    
125
# extensions
126
ENABLE_GLANCE = getattr(settings, 'UI_ENABLE_GLANCE', True)
127

    
128
DIAGNOSTICS_UPDATE_INTERVAL = \
129
    getattr(settings, 'UI_DIAGNOSTICS_UPDATE_INTERVAL', 2000)
130

    
131
# network settings
132
DEFAULT_NETWORK_TYPES = {'MAC_FILTERED': 'mac-filtering',
133
                         'PHYSICAL_VLAN': 'physical-vlan'}
134
NETWORK_TYPES = \
135
    getattr(settings,
136
            'UI_NETWORK_AVAILABLE_NETWORK_TYPES', DEFAULT_NETWORK_TYPES)
137
DEFAULT_NETWORK_SUBNETS = ['10.0.0.0/24', '192.168.1.1/24']
138
NETWORK_SUBNETS = \
139
    getattr(settings,
140
            'UI_NETWORK_AVAILABLE_SUBNETS', DEFAULT_NETWORK_SUBNETS)
141
NETWORK_DUPLICATE_NICS = \
142
    getattr(settings,
143
            'UI_NETWORK_ALLOW_DUPLICATE_VM_NICS', False)
144
NETWORK_STRICT_DESTROY = \
145
    getattr(settings,
146
            'UI_NETWORK_STRICT_DESTROY', False)
147
PRIVATE_NETWORKS_NIC_HOTPLUG = \
148
    getattr(settings,
149
            'UI_PRIVATE_NETWORK_NIC_HOTPLUG', 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

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

    
185

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

    
249

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

    
263

    
264
def js_tests(request):
265
    return template('tests', request, {})
266

    
267

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

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

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

    
323
    },
324
    'windows': {
325
        'linux': [CONNECT_WINDOWS_LINUX_MESSAGE,
326
                  CONNECT_WINDOWS_LINUX_SUBMESSAGE],
327
        'windows': [CONNECT_WINDOWS_WINDOWS_MESSAGE,
328
                    CONNECT_WINDOWS_WINDOWS_SUBMESSAGE],
329
        'ssh_message': "%(user)s@%(hostname)s",
330
        'ssh_message_port': "%(user)s@%(hostname)s (port: %(port)s)"
331
    },
332
}
333

    
334
APPEND_CONNECT_PROMPT_MESSAGES = \
335
    getattr(settings, 'UI_CONNECT_PROMPT_MESSAGES', {})
336
for k, v in APPEND_CONNECT_PROMPT_MESSAGES.iteritems():
337
    CONNECT_PROMPT_MESSAGES[k].update(v)
338

    
339
# retrieve domain prefix from settings
340
DOMAIN_PREFIX = getattr(settings, 'MACHINE_DOMAIN_PREFIX', getattr(settings,
341
                        'BACKEND_PREFIX_ID', ""))
342

    
343
# domain template string
344
DOMAIN_TPL = "%s%%s" % DOMAIN_PREFIX
345

    
346

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

    
357
    # guess host os
358
    if host_os != "windows":
359
        host_os = 'linux'
360

    
361
    # guess username
362
    if not username:
363
        username = "root"
364

    
365
        if metadata_os.lower() in ['ubuntu', 'kubuntu', 'fedora']:
366
            username = "user"
367

    
368
        if metadata_os.lower() == "windows":
369
            username = "Administrator"
370

    
371
    ssh_forward = ports.get("22", None)
372
    rdp_forward = ports.get("3389", None)
373

    
374
    # operating system provides ssh access
375
    ssh = False
376
    if operating_system != "windows":
377
        operating_system = "linux"
378
        ssh = True
379

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

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

    
407
        rdp_context = {
408
            'username': username,
409
            'domain': domain,
410
            'ip_address': ip_address,
411
            'hostname': hostname,
412
            'port': request.GET.get('port', port),
413
            'extra_content': extra_rdp_content
414
        }
415

    
416
        rdp_file_data = render_to_string("synnefo-windows.rdp", rdp_context)
417
        response = HttpResponse(rdp_file_data, mimetype='application/x-rdp')
418

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

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

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

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

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

    
487
    return response