root / ui / views.py @ bdf369a2
History | View | Annotate | Download (8.2 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 |
import os |
35 |
from django.conf import settings |
36 |
from django.utils.translation import gettext_lazy as _ |
37 |
from django.template import Context, loader |
38 |
from django.http import HttpResponse |
39 |
from django.utils.translation import get_language |
40 |
from django.utils import simplejson as json |
41 |
from django.shortcuts import render_to_response |
42 |
from django.core.urlresolvers import reverse |
43 |
|
44 |
TIMEOUT = settings.TIMEOUT |
45 |
UPDATE_INTERVAL = settings.UPDATE_INTERVAL |
46 |
IMAGE_ICONS = settings.IMAGE_ICONS |
47 |
|
48 |
def template(name, context): |
49 |
template_path = os.path.join(os.path.dirname(__file__), "templates/")
|
50 |
current_template = template_path + name + '.html'
|
51 |
t = loader.get_template(current_template) |
52 |
return HttpResponse(t.render(Context(context)))
|
53 |
|
54 |
def home(request): |
55 |
context = {'timeout': TIMEOUT,
|
56 |
'project': '+nefo', |
57 |
'request': request,
|
58 |
'current_lang': get_language() or 'en', |
59 |
'update_interval': UPDATE_INTERVAL,
|
60 |
'image_icons': IMAGE_ICONS,
|
61 |
'DEBUG': settings.DEBUG}
|
62 |
return template('home', context) |
63 |
|
64 |
def machines(request): |
65 |
context = {'default_keywords': settings.DEFAULT_KEYWORDS}
|
66 |
return template('machines', context) |
67 |
|
68 |
def machines_icon(request): |
69 |
context = {'default_keywords': settings.DEFAULT_KEYWORDS}
|
70 |
return template('machines_icon', context) |
71 |
|
72 |
def machines_list(request): |
73 |
context = {'default_keywords': settings.DEFAULT_KEYWORDS}
|
74 |
return template('machines_list', context) |
75 |
|
76 |
def machines_single(request): |
77 |
context = {'default_keywords': settings.DEFAULT_KEYWORDS}
|
78 |
return template('machines_single', context) |
79 |
|
80 |
def machines_console(request): |
81 |
host, port, password = ('','','') |
82 |
host = request.GET.get('host','') |
83 |
port = request.GET.get('port','') |
84 |
password = request.GET.get('password','') |
85 |
machine = request.GET.get('machine','') |
86 |
host_ip = request.GET.get('host_ip','') |
87 |
context = {'host': host, 'port': port, 'password': password, 'machine': machine, 'host_ip': host_ip} |
88 |
return template('machines_console', context) |
89 |
|
90 |
|
91 |
CONNECT_LINUX_LINUX_MESSAGE = _("""A direct connection to this machine can be established using the <a target="_blank"
|
92 |
href="http://en.wikipedia.org/wiki/Secure_Shell">SSH Protocol</a>.
|
93 |
To do so open a terminal and type the following at the prompt to connect to your machine:""")
|
94 |
CONNECT_LINUX_WINDOWS_MESSAGE = _("""A direct connection to this machine can be
|
95 |
established using <a target="_blank" href="http://en.wikipedia.org/wiki/Remote_Desktop_Services">Remote Desktop Service</a>.
|
96 |
To do so, open the following file with an appropriate remote desktop client.""")
|
97 |
CONNECT_LINUX_WINDOWS_SUBMESSAGE = _("""If you don't have one already
|
98 |
installed, we suggest the use of <a target="_blank" href="http://sourceforge.net/projects/tsclient/files/tsclient/tsclient-unstable/tsclient-2.0.1.tar.bz2/download">tsclient</a>.""")
|
99 |
|
100 |
CONNECT_WINDOWS_LINUX_MESSAGE = _("""A direct connection to this machine can be established using the <a target="_blank"
|
101 |
href="http://en.wikipedia.org/wiki/Secure_Shell">SSH Protocol</a>.
|
102 |
Open an ssh client such as PuTTY to connect to your machine at IP:""")
|
103 |
CONNECT_WINDOWS_LINUX_SUBMESSAGE = _("""If you do not have an ssh client already installed,
|
104 |
<a target="_blank" href="http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe">Download PuTTY</a>""")
|
105 |
CONNECT_WINDOWS_WINDOWS_MESSAGE = _("Trying to connect from windows to windows")
|
106 |
|
107 |
|
108 |
# info/subinfo for all os combinations
|
109 |
#
|
110 |
# [0] info gets displayed on top of the message box
|
111 |
# [1] subinfo gets displayed on the bottom as extra info
|
112 |
# provided to the user when needed
|
113 |
CONNECT_PROMT_MESSAGES = { |
114 |
'linux': {
|
115 |
'linux': [CONNECT_LINUX_LINUX_MESSAGE, ""], |
116 |
'windows': [CONNECT_LINUX_WINDOWS_MESSAGE, CONNECT_LINUX_WINDOWS_SUBMESSAGE]
|
117 |
}, |
118 |
'windows': {
|
119 |
'linux': [CONNECT_WINDOWS_LINUX_MESSAGE, CONNECT_WINDOWS_LINUX_SUBMESSAGE],
|
120 |
'windows': [CONNECT_WINDOWS_WINDOWS_MESSAGE, ""] |
121 |
} |
122 |
} |
123 |
|
124 |
def machines_connect(request): |
125 |
ip_address = request.GET.get('ip_address','') |
126 |
operating_system = request.GET.get('os','') |
127 |
server_id = request.GET.get('srv', 0) |
128 |
host_os = request.GET.get('host_os','Linux').lower() |
129 |
|
130 |
if operating_system != "windows": |
131 |
operating_system = "linux"
|
132 |
|
133 |
# rdp param is set, the user requested rdp file
|
134 |
if operating_system == 'windows' and request.GET.get("rdp", False): #check if we are on windows |
135 |
rdp_file = os.path.join(os.path.dirname(__file__), "static/") + 'synnefo-windows.rdp' |
136 |
connect_data = open(rdp_file, 'r').read() |
137 |
connect_data = connect_data + 'full address:s:' + ip_address + '\n' |
138 |
response = HttpResponse(connect_data, mimetype='application/x-rdp')
|
139 |
|
140 |
# proper filename, use server id and ip address
|
141 |
filename = "%d-%s.rdp" % (int(server_id), ip_address) |
142 |
response['Content-Disposition'] = 'attachment; filename=%s' % filename |
143 |
else:
|
144 |
# no rdp requested return json object with info on how to connect
|
145 |
ssh = False
|
146 |
if (operating_system != "windows"): |
147 |
ssh = True
|
148 |
|
149 |
link_title = _("Remote desktop to %s") % ip_address
|
150 |
link_url = "%s?ip_address=%s&os=%s&rdp=1&srv=%d" % (reverse("machines-connect"), ip_address, operating_system, |
151 |
int(server_id))
|
152 |
|
153 |
if (operating_system != "windows"): |
154 |
link_title = "ssh root@%s" % ip_address
|
155 |
link_url = None
|
156 |
|
157 |
if host_os == "windows": |
158 |
link_title = ip_address |
159 |
|
160 |
# try to find a specific message
|
161 |
try:
|
162 |
connect_message = CONNECT_PROMT_MESSAGES[host_os][operating_system][0]
|
163 |
subinfo = CONNECT_PROMT_MESSAGES[host_os][operating_system][1]
|
164 |
except KeyError: |
165 |
connect_message = _("You are trying to connect from a %s machine to a %s machine") % (host_os, operating_system)
|
166 |
subinfo = ""
|
167 |
|
168 |
response_object = { |
169 |
'ip': ip_address,
|
170 |
'os': operating_system,
|
171 |
'ssh': ssh,
|
172 |
'info': unicode(connect_message), |
173 |
'subinfo': unicode(subinfo), |
174 |
'link': {'title': unicode(link_title), 'url': link_url} |
175 |
} |
176 |
response = HttpResponse(json.dumps(response_object), mimetype='application/json') #no windows, no rdp |
177 |
|
178 |
return response
|
179 |
|
180 |
|
181 |
def images(request): |
182 |
context = {} |
183 |
return template('images', context) |
184 |
|
185 |
def disks(request): |
186 |
context = {} |
187 |
return template('disks', context) |
188 |
|
189 |
def networks(request): |
190 |
context = {} |
191 |
return template('networks', context) |
192 |
|
193 |
def files(request): |
194 |
context = {} |
195 |
return template('files', context) |
196 |
|
197 |
def desktops(request): |
198 |
context = {} |
199 |
return template('desktops', context) |
200 |
|
201 |
def apps(request): |
202 |
context = {} |
203 |
return template('apps', context) |