root / snf-cyclades-app / synnefo / ui / views.py @ b1fb3aac
History | View | Annotate | Download (20.5 kB)
1 |
# Copyright 2011 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 urlparse import urlparse |
38 |
|
39 |
from django.conf import settings |
40 |
from django.utils.translation import gettext_lazy as _ |
41 |
from django.template import loader |
42 |
from django.http import HttpResponse |
43 |
from django.utils.translation import get_language |
44 |
from django.utils import simplejson as json |
45 |
from synnefo_branding.utils import render_to_string |
46 |
from django.core.urlresolvers import reverse |
47 |
from django.template import RequestContext |
48 |
from synnefo_branding import settings as snf_settings |
49 |
|
50 |
from synnefo.util.version import get_component_version |
51 |
from synnefo.lib import join_urls |
52 |
|
53 |
from snf_django.lib.astakos import get_user |
54 |
from synnefo import cyclades_settings |
55 |
|
56 |
SYNNEFO_JS_LIB_VERSION = get_component_version('app')
|
57 |
PROXY_USER_SERVICES = getattr(settings, 'CYCLADES_PROXY_USER_SERVICES', True) |
58 |
|
59 |
|
60 |
# resolve astakos base url to be used by ui
|
61 |
UI_ASTAKOS_BASE_HREF = cyclades_settings.ASTAKOS_BASE_URL |
62 |
if PROXY_USER_SERVICES:
|
63 |
UI_ASTAKOS_BASE_HREF = \ |
64 |
'/' + cyclades_settings.BASE_ASTAKOS_PROXY_PATH.lstrip('/') |
65 |
|
66 |
UI_ASTAKOS_ACCOUNTS_HREF = join_urls(UI_ASTAKOS_BASE_HREF, |
67 |
cyclades_settings.ASTAKOS_ACCOUNTS_PREFIX) |
68 |
|
69 |
UI_ASTAKOS_VIEWS_HREF = join_urls(UI_ASTAKOS_BASE_HREF, |
70 |
cyclades_settings.ASTAKOS_VIEWS_PREFIX) |
71 |
|
72 |
UI_ASTAKOS_KEYSTONE_HREF = join_urls(UI_ASTAKOS_BASE_HREF, |
73 |
cyclades_settings.ASTAKOS_KEYSTONE_PREFIX) |
74 |
|
75 |
# api configuration
|
76 |
api_path = join_urls(cyclades_settings.COMPUTE_PREFIX, 'v1.1').lstrip('/') |
77 |
api_path = '/' + join_urls(cyclades_settings.BASE_PATH, api_path).lstrip('/') |
78 |
COMPUTE_API_HREF = getattr(settings, 'COMPUTE_API_URL', api_path) |
79 |
|
80 |
# UI preferences settings
|
81 |
TIMEOUT = getattr(settings, "TIMEOUT", 10000) |
82 |
UPDATE_INTERVAL = getattr(settings, "UI_UPDATE_INTERVAL", 5000) |
83 |
CHANGES_SINCE_ALIGNMENT = getattr(settings, "UI_CHANGES_SINCE_ALIGNMENT", 0) |
84 |
UPDATE_INTERVAL_INCREASE = getattr(settings, "UI_UPDATE_INTERVAL_INCREASE", |
85 |
500)
|
86 |
UPDATE_INTERVAL_INCREASE_AFTER_CALLS_COUNT = \ |
87 |
getattr(settings, "UI_UPDATE_INTERVAL_INCREASE_AFTER_CALLS_COUNT", 3) |
88 |
UPDATE_INTERVAL_FAST = getattr(settings, "UI_UPDATE_INTERVAL_FAST", 2500) |
89 |
UPDATE_INTERVAL_MAX = getattr(settings, "UI_UPDATE_INTERVAL_MAX", 10000) |
90 |
|
91 |
# predefined values settings
|
92 |
VM_IMAGE_COMMON_METADATA = \ |
93 |
getattr(settings, "UI_VM_IMAGE_COMMON_METADATA", ["OS", "users"]) |
94 |
SUGGESTED_FLAVORS_DEFAULT = {} |
95 |
SUGGESTED_FLAVORS = getattr(settings, "VM_CREATE_SUGGESTED_FLAVORS", |
96 |
SUGGESTED_FLAVORS_DEFAULT) |
97 |
SUGGESTED_ROLES_DEFAULT = ["Database server", "File server", "Mail server", |
98 |
"Web server", "Proxy"] |
99 |
SUGGESTED_ROLES = getattr(settings, "VM_CREATE_SUGGESTED_ROLES", |
100 |
SUGGESTED_ROLES_DEFAULT) |
101 |
IMAGE_ICONS = settings.IMAGE_ICONS |
102 |
IMAGE_DELETED_TITLE = \ |
103 |
getattr(settings, 'UI_IMAGE_DELETED_TITLE', '(deleted)') |
104 |
IMAGE_DELETED_SIZE_TITLE = \ |
105 |
getattr(settings, 'UI_IMAGE_DELETED_SIZE_TITLE', '(none)') |
106 |
|
107 |
SUPPORT_SSH_OS_LIST = getattr(settings, "UI_SUPPORT_SSH_OS_LIST",) |
108 |
OS_CREATED_USERS = getattr(settings, "UI_OS_DEFAULT_USER_MAP") |
109 |
UNKNOWN_OS = getattr(settings, "UI_UNKNOWN_OS", "unknown") |
110 |
|
111 |
LOGOUT_PATH = join_urls(cyclades_settings.ASTAKOS_ACCOUNTS_PREFIX, |
112 |
getattr(settings, "UI_LOGOUT_PATH", 'authenticate')) |
113 |
LOGOUT_URL = join_urls(cyclades_settings.ASTAKOS_BASE_URL, LOGOUT_PATH) |
114 |
|
115 |
LOGIN_PATH = join_urls(cyclades_settings.ASTAKOS_VIEWS_PREFIX, |
116 |
getattr(settings, "UI_LOGIN_PATH", 'login')) |
117 |
LOGIN_URL = join_urls(cyclades_settings.ASTAKOS_BASE_URL, LOGIN_PATH) |
118 |
|
119 |
AUTH_COOKIE_NAME = getattr(settings, "UI_AUTH_COOKIE_NAME", 'synnefo_user') |
120 |
|
121 |
# UI behaviour settings
|
122 |
DELAY_ON_BLUR = getattr(settings, "UI_DELAY_ON_BLUR", True) |
123 |
UPDATE_HIDDEN_VIEWS = getattr(settings, "UI_UPDATE_HIDDEN_VIEWS", False) |
124 |
HANDLE_WINDOW_EXCEPTIONS = \ |
125 |
getattr(settings, "UI_HANDLE_WINDOW_EXCEPTIONS", True) |
126 |
SKIP_TIMEOUTS = getattr(settings, "UI_SKIP_TIMEOUTS", 1) |
127 |
|
128 |
# Additional settings
|
129 |
VM_NAME_TEMPLATE = getattr(settings, "VM_CREATE_NAME_TPL", "My {0} server") |
130 |
VM_HOSTNAME_FORMAT = getattr(settings, "UI_VM_HOSTNAME_FORMAT", |
131 |
'snf-%(id)s.vm.synnefo.org')
|
132 |
|
133 |
if isinstance(VM_HOSTNAME_FORMAT, basestring): |
134 |
VM_HOSTNAME_FORMAT = VM_HOSTNAME_FORMAT % {'id': '{0}'} |
135 |
|
136 |
MAX_SSH_KEYS_PER_USER = getattr(settings, "USERDATA_MAX_SSH_KEYS_PER_USER") |
137 |
FLAVORS_DISK_TEMPLATES_INFO = \ |
138 |
getattr(settings, "UI_FLAVORS_DISK_TEMPLATES_INFO", {}) |
139 |
SYSTEM_IMAGES_OWNERS = getattr(settings, "UI_SYSTEM_IMAGES_OWNERS", {}) |
140 |
CUSTOM_IMAGE_HELP_URL = getattr(settings, "UI_CUSTOM_IMAGE_HELP_URL", None) |
141 |
|
142 |
# MEDIA PATHS
|
143 |
UI_MEDIA_URL = \ |
144 |
getattr(settings, "UI_MEDIA_URL", |
145 |
"%ssnf-%s/" % (settings.MEDIA_URL, SYNNEFO_JS_LIB_VERSION))
|
146 |
UI_SYNNEFO_IMAGES_URL = \ |
147 |
getattr(settings,
|
148 |
"UI_SYNNEFO_IMAGES_URL", UI_MEDIA_URL + "images/") |
149 |
UI_SYNNEFO_CSS_URL = \ |
150 |
getattr(settings,
|
151 |
"UI_SYNNEFO_CSS_URL", UI_MEDIA_URL + "css/") |
152 |
UI_SYNNEFO_JS_URL = \ |
153 |
getattr(settings,
|
154 |
"UI_SYNNEFO_JS_URL", UI_MEDIA_URL + "js/") |
155 |
UI_SYNNEFO_JS_LIB_URL = \ |
156 |
getattr(settings,
|
157 |
"UI_SYNNEFO_JS_LIB_URL", UI_SYNNEFO_JS_URL + "lib/") |
158 |
UI_SYNNEFO_JS_WEB_URL = \ |
159 |
getattr(settings, "UI_SYNNEFO_JS_WEB_URL", UI_SYNNEFO_JS_URL + "ui/web/") |
160 |
|
161 |
# extensions
|
162 |
ENABLE_GLANCE = getattr(settings, 'UI_ENABLE_GLANCE', True) |
163 |
glance_path = join_urls(cyclades_settings.BASE_PATH, |
164 |
cyclades_settings.PLANKTON_PREFIX) |
165 |
glance_path = '/' + glance_path.lstrip('/') |
166 |
GLANCE_API_HREF = getattr(settings, 'UI_GLANCE_API_URL', glance_path) |
167 |
|
168 |
DIAGNOSTICS_UPDATE_INTERVAL = \ |
169 |
getattr(settings, 'UI_DIAGNOSTICS_UPDATE_INTERVAL', 2000) |
170 |
|
171 |
# network settings
|
172 |
DEFAULT_NETWORK_TYPES = {'MAC_FILTERED': 'mac-filtering', |
173 |
'PHYSICAL_VLAN': 'physical-vlan'} |
174 |
NETWORK_TYPES = \ |
175 |
getattr(settings,
|
176 |
'UI_NETWORK_AVAILABLE_NETWORK_TYPES', DEFAULT_NETWORK_TYPES)
|
177 |
DEFAULT_NETWORK_SUBNETS = ['10.0.0.0/24', '192.168.1.1/24'] |
178 |
NETWORK_SUBNETS = \ |
179 |
getattr(settings,
|
180 |
'UI_NETWORK_AVAILABLE_SUBNETS', DEFAULT_NETWORK_SUBNETS)
|
181 |
NETWORK_DUPLICATE_NICS = \ |
182 |
getattr(settings,
|
183 |
'UI_NETWORK_ALLOW_DUPLICATE_VM_NICS', False) |
184 |
NETWORK_STRICT_DESTROY = \ |
185 |
getattr(settings,
|
186 |
'UI_NETWORK_STRICT_DESTROY', False) |
187 |
NETWORK_ALLOW_MULTIPLE_DESTROY = \ |
188 |
getattr(settings,
|
189 |
'UI_NETWORK_ALLOW_MULTIPLE_DESTROY', False) |
190 |
AUTOMATIC_NETWORK_RANGE_FORMAT = getattr(settings,
|
191 |
'UI_AUTOMATIC_NETWORK_RANGE_FORMAT',
|
192 |
"192.168.%d.0/24").replace("%d", |
193 |
"{0}")
|
194 |
GROUP_PUBLIC_NETWORKS = getattr(settings, 'UI_GROUP_PUBLIC_NETWORKS', True) |
195 |
GROUPED_PUBLIC_NETWORK_NAME = \ |
196 |
getattr(settings, 'UI_GROUPED_PUBLIC_NETWORK_NAME', 'Internet') |
197 |
|
198 |
USER_CATALOG_PATH = getattr(settings, 'UI_USER_CATALOG_PATH', 'user_catalogs') |
199 |
USER_CATALOG_HREF = join_urls(UI_ASTAKOS_ACCOUNTS_HREF, USER_CATALOG_PATH) |
200 |
|
201 |
FEEDBACK_POST_PATH = getattr(settings, 'UI_FEEDBACK_POST_PATH', 'feedback') |
202 |
FEEDBACK_POST_HREF = join_urls(UI_ASTAKOS_ACCOUNTS_HREF, FEEDBACK_POST_PATH) |
203 |
|
204 |
|
205 |
ACCOUNTS_API_VERSION = getattr(settings, 'UI_ACCOUNTS_API_VERSION', '') |
206 |
ACCOUNTS_API_HREF = join_urls(UI_ASTAKOS_ACCOUNTS_HREF, ACCOUNTS_API_VERSION) |
207 |
|
208 |
TRANSLATE_UUIDS = not getattr(settings, 'TRANSLATE_UUIDS', False) |
209 |
|
210 |
|
211 |
def template(name, request, context): |
212 |
template_path = os.path.join(os.path.dirname(__file__), "templates/")
|
213 |
current_template = template_path + name + '.html'
|
214 |
t = loader.get_template(current_template) |
215 |
media_context = { |
216 |
'UI_MEDIA_URL': UI_MEDIA_URL,
|
217 |
'SYNNEFO_JS_URL': UI_SYNNEFO_JS_URL,
|
218 |
'SYNNEFO_JS_LIB_URL': UI_SYNNEFO_JS_LIB_URL,
|
219 |
'SYNNEFO_JS_WEB_URL': UI_SYNNEFO_JS_WEB_URL,
|
220 |
'SYNNEFO_IMAGES_URL': UI_SYNNEFO_IMAGES_URL,
|
221 |
'SYNNEFO_CSS_URL': UI_SYNNEFO_CSS_URL,
|
222 |
'SYNNEFO_JS_LIB_VERSION': SYNNEFO_JS_LIB_VERSION,
|
223 |
'DEBUG': settings.DEBUG
|
224 |
} |
225 |
context.update(media_context) |
226 |
return HttpResponse(t.render(RequestContext(request, context)))
|
227 |
|
228 |
|
229 |
def home(request): |
230 |
context = {'timeout': TIMEOUT,
|
231 |
'project': '+nefo', |
232 |
'request': request,
|
233 |
'current_lang': get_language() or 'en', |
234 |
'compute_api_url': json.dumps(COMPUTE_API_HREF),
|
235 |
'user_catalog_url': json.dumps(USER_CATALOG_HREF),
|
236 |
'feedback_post_url': json.dumps(FEEDBACK_POST_HREF),
|
237 |
'accounts_api_url': json.dumps(ACCOUNTS_API_HREF),
|
238 |
'translate_uuids': json.dumps(TRANSLATE_UUIDS),
|
239 |
# update interval settings
|
240 |
'update_interval': UPDATE_INTERVAL,
|
241 |
'update_interval_increase': UPDATE_INTERVAL_INCREASE,
|
242 |
'update_interval_increase_after_calls':
|
243 |
UPDATE_INTERVAL_INCREASE_AFTER_CALLS_COUNT, |
244 |
'update_interval_fast': UPDATE_INTERVAL_FAST,
|
245 |
'update_interval_max': UPDATE_INTERVAL_MAX,
|
246 |
'changes_since_alignment': CHANGES_SINCE_ALIGNMENT,
|
247 |
'image_icons': IMAGE_ICONS,
|
248 |
'logout_redirect': LOGOUT_URL,
|
249 |
'login_redirect': LOGIN_URL,
|
250 |
'auth_cookie_name': AUTH_COOKIE_NAME,
|
251 |
'suggested_flavors': json.dumps(SUGGESTED_FLAVORS),
|
252 |
'suggested_roles': json.dumps(SUGGESTED_ROLES),
|
253 |
'vm_image_common_metadata': json.dumps(VM_IMAGE_COMMON_METADATA),
|
254 |
'synnefo_version': SYNNEFO_JS_LIB_VERSION,
|
255 |
'delay_on_blur': json.dumps(DELAY_ON_BLUR),
|
256 |
'update_hidden_views': json.dumps(UPDATE_HIDDEN_VIEWS),
|
257 |
'handle_window_exceptions': json.dumps(HANDLE_WINDOW_EXCEPTIONS),
|
258 |
'skip_timeouts': json.dumps(SKIP_TIMEOUTS),
|
259 |
'vm_name_template': json.dumps(VM_NAME_TEMPLATE),
|
260 |
'flavors_disk_templates_info':
|
261 |
json.dumps(FLAVORS_DISK_TEMPLATES_INFO), |
262 |
'support_ssh_os_list': json.dumps(SUPPORT_SSH_OS_LIST),
|
263 |
'unknown_os': json.dumps(UNKNOWN_OS),
|
264 |
'os_created_users': json.dumps(OS_CREATED_USERS),
|
265 |
'userdata_keys_limit': json.dumps(MAX_SSH_KEYS_PER_USER),
|
266 |
'use_glance': json.dumps(ENABLE_GLANCE),
|
267 |
'glance_api_url': json.dumps(GLANCE_API_HREF),
|
268 |
'system_images_owners': json.dumps(SYSTEM_IMAGES_OWNERS),
|
269 |
'custom_image_help_url': CUSTOM_IMAGE_HELP_URL,
|
270 |
'image_deleted_title': json.dumps(IMAGE_DELETED_TITLE),
|
271 |
'image_deleted_size_title': json.dumps(IMAGE_DELETED_SIZE_TITLE),
|
272 |
'network_suggested_subnets': json.dumps(NETWORK_SUBNETS),
|
273 |
'network_available_types': json.dumps(NETWORK_TYPES),
|
274 |
'network_allow_duplicate_vm_nics':
|
275 |
json.dumps(NETWORK_DUPLICATE_NICS), |
276 |
'network_strict_destroy': json.dumps(NETWORK_STRICT_DESTROY),
|
277 |
'network_allow_multiple_destroy':
|
278 |
json.dumps(NETWORK_ALLOW_MULTIPLE_DESTROY), |
279 |
'automatic_network_range_format':
|
280 |
json.dumps(AUTOMATIC_NETWORK_RANGE_FORMAT), |
281 |
'grouped_public_network_name':
|
282 |
json.dumps(GROUPED_PUBLIC_NETWORK_NAME), |
283 |
'group_public_networks': json.dumps(GROUP_PUBLIC_NETWORKS),
|
284 |
'diagnostics_update_interval':
|
285 |
json.dumps(DIAGNOSTICS_UPDATE_INTERVAL), |
286 |
'vm_hostname_format': json.dumps(VM_HOSTNAME_FORMAT)
|
287 |
} |
288 |
return template('home', request, context) |
289 |
|
290 |
def machines_console(request): |
291 |
host, port, password = ('', '', '') |
292 |
host = request.GET.get('host', '') |
293 |
port = request.GET.get('port', '') |
294 |
password = request.GET.get('password', '') |
295 |
machine = request.GET.get('machine', '') |
296 |
host_ip = request.GET.get('host_ip', '') |
297 |
host_ip_v6 = request.GET.get('host_ip_v6', '') |
298 |
context = {'host': host, 'port': port, 'password': password, |
299 |
'machine': machine, 'host_ip': host_ip, 'host_ip_v6': host_ip_v6} |
300 |
return template('machines_console', request, context) |
301 |
|
302 |
|
303 |
def js_tests(request): |
304 |
return template('tests', request, {}) |
305 |
|
306 |
|
307 |
CONNECT_LINUX_LINUX_MESSAGE = \ |
308 |
_("""A direct connection to this machine can be established using the
|
309 |
<a target="_blank" href="http://en.wikipedia.org/wiki/Secure_Shell">
|
310 |
SSH Protocol</a>.
|
311 |
To do so open a terminal and type the following at the prompt to connect
|
312 |
to your machine:""")
|
313 |
CONNECT_LINUX_WINDOWS_MESSAGE = \ |
314 |
_("""A direct connection to this machine can be established using
|
315 |
<a target="_blank"
|
316 |
href="http://en.wikipedia.org/wiki/Remote_Desktop_Services">
|
317 |
Remote Desktop Service</a>.
|
318 |
To do so, open the following file with an appropriate remote desktop client.""")
|
319 |
CONNECT_LINUX_WINDOWS_SUBMESSAGE = \ |
320 |
_("""If you don't have a Remote Desktop client already installed,
|
321 |
we suggest the use of <a target="_blank"
|
322 |
href=
|
323 |
"http://sourceforge.net/projects/tsclient/files/latest/download?source=files">
|
324 |
tsclient</a>.<br /><span class="important">IMPORTANT: It may take up to 15
|
325 |
minutes for your Windows VM to become available
|
326 |
after its creation.</span>""")
|
327 |
CONNECT_WINDOWS_LINUX_MESSAGE = \ |
328 |
_("""A direct connection to this machine can be established using the
|
329 |
<a target="_blank"
|
330 |
href="http://en.wikipedia.org/wiki/Secure_Shell">SSH Protocol</a>.
|
331 |
Open an ssh client such as PuTTY and type the following:""")
|
332 |
CONNECT_WINDOWS_LINUX_SUBMESSAGE = \ |
333 |
_("""If you do not have an ssh client already installed,
|
334 |
<a target="_blank"
|
335 |
href="http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe">
|
336 |
Download PuTTY</a>""")
|
337 |
|
338 |
CONNECT_WINDOWS_WINDOWS_MESSAGE = \ |
339 |
_("""A direct connection to this machine can be
|
340 |
established using Remote Desktop. Click on the following link, and if asked
|
341 |
open it using "Remote Desktop Connection".
|
342 |
<br /><span class="important">
|
343 |
IMPORTANT: It may take up to 15 minutes for your Windows VM to become available
|
344 |
after its creation.</span>""")
|
345 |
CONNECT_WINDOWS_WINDOWS_SUBMESSAGE = \ |
346 |
_("""Save this file to disk for future use.""")
|
347 |
|
348 |
# info/subinfo for all os combinations
|
349 |
#
|
350 |
# [0] info gets displayed on top of the message box
|
351 |
# [1] subinfo gets displayed on the bottom as extra info
|
352 |
# provided to the user when needed
|
353 |
CONNECT_PROMPT_MESSAGES = { |
354 |
'linux': {
|
355 |
'linux': [CONNECT_LINUX_LINUX_MESSAGE, ""], |
356 |
'windows': [CONNECT_LINUX_WINDOWS_MESSAGE,
|
357 |
CONNECT_LINUX_WINDOWS_SUBMESSAGE], |
358 |
'ssh_message': "ssh %(user)s@%(hostname)s" |
359 |
}, |
360 |
'windows': {
|
361 |
'linux': [CONNECT_WINDOWS_LINUX_MESSAGE,
|
362 |
CONNECT_WINDOWS_LINUX_SUBMESSAGE], |
363 |
'windows': [CONNECT_WINDOWS_WINDOWS_MESSAGE,
|
364 |
CONNECT_WINDOWS_WINDOWS_SUBMESSAGE], |
365 |
'ssh_message': "%(user)s@%(hostname)s" |
366 |
}, |
367 |
} |
368 |
|
369 |
APPEND_CONNECT_PROMPT_MESSAGES = \ |
370 |
getattr(settings, 'UI_CONNECT_PROMPT_MESSAGES', {}) |
371 |
for k, v in APPEND_CONNECT_PROMPT_MESSAGES.iteritems(): |
372 |
CONNECT_PROMPT_MESSAGES[k].update(v) |
373 |
|
374 |
# retrieve domain prefix from settings
|
375 |
DOMAIN_PREFIX = getattr(settings, 'MACHINE_DOMAIN_PREFIX', getattr(settings, |
376 |
'BACKEND_PREFIX_ID', "")) |
377 |
|
378 |
# domain template string
|
379 |
DOMAIN_TPL = "%s%%s" % DOMAIN_PREFIX
|
380 |
|
381 |
|
382 |
def machines_connect(request): |
383 |
ip_address = request.GET.get('ip_address', '') |
384 |
hostname = request.GET.get('hostname', '') |
385 |
operating_system = metadata_os = request.GET.get('os', '') |
386 |
server_id = request.GET.get('srv', 0) |
387 |
host_os = request.GET.get('host_os', 'Linux').lower() |
388 |
username = request.GET.get('username', None) |
389 |
domain = request.GET.get("domain", DOMAIN_TPL % int(server_id)) |
390 |
|
391 |
# guess host os
|
392 |
if host_os != "windows": |
393 |
host_os = 'linux'
|
394 |
|
395 |
# guess username
|
396 |
if not username: |
397 |
username = "root"
|
398 |
|
399 |
if metadata_os.lower() in ['ubuntu', 'kubuntu', 'fedora']: |
400 |
username = "user"
|
401 |
|
402 |
if metadata_os.lower() == "windows": |
403 |
username = "Administrator"
|
404 |
|
405 |
# operating system provides ssh access
|
406 |
ssh = False
|
407 |
if operating_system != "windows": |
408 |
operating_system = "linux"
|
409 |
ssh = True
|
410 |
|
411 |
# rdp param is set, the user requested rdp file
|
412 |
# check if we are on windows
|
413 |
if operating_system == 'windows' and request.GET.get("rdp", False): |
414 |
|
415 |
# UI sent domain info (from vm metadata) use this
|
416 |
# otherwise use our default snf-<vm_id> domain
|
417 |
EXTRA_RDP_CONTENT = getattr(settings, 'UI_EXTRA_RDP_CONTENT', '') |
418 |
if callable(EXTRA_RDP_CONTENT): |
419 |
extra_rdp_content = EXTRA_RDP_CONTENT(server_id, ip_address, |
420 |
hostname, username) |
421 |
else:
|
422 |
extra_rdp_content = EXTRA_RDP_CONTENT % \ |
423 |
{ |
424 |
'server_id': server_id,
|
425 |
'ip_address': ip_address,
|
426 |
'hostname': hostname,
|
427 |
'user': username
|
428 |
} |
429 |
|
430 |
rdp_context = { |
431 |
'username': username,
|
432 |
'domain': domain,
|
433 |
'ip_address': ip_address,
|
434 |
'hostname': hostname,
|
435 |
'extra_content': extra_rdp_content
|
436 |
} |
437 |
|
438 |
rdp_file_data = render_to_string("synnefo-windows.rdp", rdp_context)
|
439 |
response = HttpResponse(rdp_file_data, mimetype='application/x-rdp')
|
440 |
|
441 |
# proper filename, use server id and ip address
|
442 |
filename = "%d-%s.rdp" % (int(server_id), hostname) |
443 |
response['Content-Disposition'] = 'attachment; filename=%s' % filename |
444 |
else:
|
445 |
ssh_message = CONNECT_PROMPT_MESSAGES['linux'].get('ssh_message') |
446 |
if host_os == 'windows': |
447 |
ssh_message = CONNECT_PROMPT_MESSAGES['windows'].get('ssh_message') |
448 |
if callable(ssh_message): |
449 |
link_title = ssh_message(server_id, ip_address, hostname, username) |
450 |
else:
|
451 |
link_title = ssh_message % { |
452 |
'server_id': server_id,
|
453 |
'ip_address': ip_address,
|
454 |
'hostname': hostname,
|
455 |
'user': username
|
456 |
} |
457 |
if (operating_system != "windows"): |
458 |
link_url = None
|
459 |
|
460 |
else:
|
461 |
link_title = _("Remote desktop to %s") % ip_address
|
462 |
link_url = \ |
463 |
"%s?ip_address=%s&os=%s&rdp=1&srv=%d&username=%s&domain=%s" \
|
464 |
"&hostname=%s" % (
|
465 |
reverse("ui_machines_connect"), ip_address,
|
466 |
operating_system, int(server_id), username,
|
467 |
domain, hostname) |
468 |
|
469 |
# try to find a specific message
|
470 |
try:
|
471 |
connect_message = \ |
472 |
CONNECT_PROMPT_MESSAGES[host_os][operating_system][0]
|
473 |
subinfo = CONNECT_PROMPT_MESSAGES[host_os][operating_system][1]
|
474 |
except KeyError: |
475 |
connect_message = \ |
476 |
_("You are trying to connect from a %s "
|
477 |
"machine to a %s machine") % (host_os, operating_system)
|
478 |
subinfo = ""
|
479 |
|
480 |
response_object = { |
481 |
'ip': ip_address,
|
482 |
'os': operating_system,
|
483 |
'ssh': ssh,
|
484 |
'info': unicode(connect_message), |
485 |
'subinfo': unicode(subinfo), |
486 |
'link': {'title': unicode(link_title), 'url': link_url} |
487 |
} |
488 |
response = \ |
489 |
HttpResponse(json.dumps(response_object), |
490 |
mimetype='application/json') # no windows, no rdp |
491 |
|
492 |
return response
|
493 |
|