Statistics
| Branch: | Tag: | Revision:

root / ui / views.py @ 404ccab2

History | View | Annotate | Download (11 kB)

1 a5bc3755 Vangelis Koukis
# Copyright 2011 GRNET S.A. All rights reserved.
2 8950a78f Christodoulos Psaltis
#
3 a5bc3755 Vangelis Koukis
# Redistribution and use in source and binary forms, with or
4 a5bc3755 Vangelis Koukis
# without modification, are permitted provided that the following
5 a5bc3755 Vangelis Koukis
# conditions are met:
6 8950a78f Christodoulos Psaltis
#
7 a5bc3755 Vangelis Koukis
#   1. Redistributions of source code must retain the above
8 a5bc3755 Vangelis Koukis
#      copyright notice, this list of conditions and the following
9 a5bc3755 Vangelis Koukis
#      disclaimer.
10 8950a78f Christodoulos Psaltis
#
11 a5bc3755 Vangelis Koukis
#   2. Redistributions in binary form must reproduce the above
12 a5bc3755 Vangelis Koukis
#      copyright notice, this list of conditions and the following
13 a5bc3755 Vangelis Koukis
#      disclaimer in the documentation and/or other materials
14 a5bc3755 Vangelis Koukis
#      provided with the distribution.
15 8950a78f Christodoulos Psaltis
#
16 a5bc3755 Vangelis Koukis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 a5bc3755 Vangelis Koukis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 a5bc3755 Vangelis Koukis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 a5bc3755 Vangelis Koukis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 a5bc3755 Vangelis Koukis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 a5bc3755 Vangelis Koukis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 a5bc3755 Vangelis Koukis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 a5bc3755 Vangelis Koukis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 a5bc3755 Vangelis Koukis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 a5bc3755 Vangelis Koukis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 a5bc3755 Vangelis Koukis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 a5bc3755 Vangelis Koukis
# POSSIBILITY OF SUCH DAMAGE.
28 8950a78f Christodoulos Psaltis
#
29 a5bc3755 Vangelis Koukis
# The views and conclusions contained in the software and
30 a5bc3755 Vangelis Koukis
# documentation are those of the authors and should not be
31 a5bc3755 Vangelis Koukis
# interpreted as representing official policies, either expressed
32 a5bc3755 Vangelis Koukis
# or implied, of GRNET S.A.
33 8950a78f Christodoulos Psaltis
#
34 edda4d30 Markos Gogoulos
import os
35 abf90954 Markos Gogoulos
from django.conf import settings
36 edda4d30 Markos Gogoulos
from django.utils.translation import gettext_lazy as _
37 b303ba05 Christodoulos Psaltis
from django.template import Context, loader
38 cc7c5902 Dimitris Moraitis
from django.http import HttpResponse
39 ec06b07c Dimitris Moraitis
from django.utils.translation import get_language
40 9c901aec Markos Gogoulos
from django.utils import simplejson as json
41 f533f224 Vangelis Koukis
from django.shortcuts import render_to_response
42 faf5ddde Kostas Papadimitriou
from django.template.loader import render_to_string
43 b687587e Kostas Papadimitriou
from django.core.urlresolvers import reverse
44 cc7c5902 Dimitris Moraitis
45 c90302ad Kostas Papadimitriou
from django.core.mail import send_mail
46 faf5ddde Kostas Papadimitriou
47 31f9a55b Kostas Papadimitriou
from django.http import Http404
48 31f9a55b Kostas Papadimitriou
49 abf90954 Markos Gogoulos
TIMEOUT = settings.TIMEOUT
50 7b03c69d Dimitris Moraitis
UPDATE_INTERVAL = settings.UPDATE_INTERVAL
51 f533f224 Vangelis Koukis
IMAGE_ICONS = settings.IMAGE_ICONS
52 dacd179a Kostas Papadimitriou
LOGOUT_URL = getattr(settings, "LOGOUT_URL", settings.LOGIN_URL)
53 e0dea91a Kostas Papadimitriou
SUGGESTED_FLAVORS = getattr(settings, "SUGGESTED_FLAVORS", {})
54 73e25ce2 Kostas Papadimitriou
SUGGESTED_ROLES = getattr(settings, "SUGGESTED_ROLES",
55 73e25ce2 Kostas Papadimitriou
        ["Database server", "File server", "Mail server", "Web server", "Proxy", "CI server", "Leeching machine"])
56 bcd451ea Kostas Papadimitriou
VM_IMAGE_COMMON_METADATA = getattr(settings, "VM_IMAGE_COMMON_METADATA", ["OS"])
57 48c07635 Kostas Papadimitriou
INVITATIONS_PER_PAGE = getattr(settings, "INVITATIONS_PER_PAGE", 10)
58 e646ebe5 Dimitris Moraitis
59 cc7c5902 Dimitris Moraitis
def template(name, context):
60 e646ebe5 Dimitris Moraitis
    template_path = os.path.join(os.path.dirname(__file__), "templates/")
61 b303ba05 Christodoulos Psaltis
    current_template = template_path + name + '.html'
62 b303ba05 Christodoulos Psaltis
    t = loader.get_template(current_template)
63 b303ba05 Christodoulos Psaltis
    return HttpResponse(t.render(Context(context)))
64 cc7c5902 Dimitris Moraitis
65 cc7c5902 Dimitris Moraitis
def home(request):
66 e646ebe5 Dimitris Moraitis
    context = {'timeout': TIMEOUT,
67 a226ff9f Christodoulos Psaltis
               'project': '+nefo',
68 e646ebe5 Dimitris Moraitis
               'request': request,
69 f533f224 Vangelis Koukis
               'current_lang': get_language() or 'en',
70 7b03c69d Dimitris Moraitis
               'update_interval': UPDATE_INTERVAL,
71 0e8e87ac Kostas Papadimitriou
               'image_icons': IMAGE_ICONS,
72 dacd179a Kostas Papadimitriou
               'logout_redirect': LOGOUT_URL,
73 e0dea91a Kostas Papadimitriou
               'suggested_flavors': json.dumps(SUGGESTED_FLAVORS),
74 73e25ce2 Kostas Papadimitriou
               'suggested_roles': json.dumps(SUGGESTED_ROLES),
75 bcd451ea Kostas Papadimitriou
               'vm_image_common_metadata': json.dumps(VM_IMAGE_COMMON_METADATA),
76 48c07635 Kostas Papadimitriou
               'invitations_per_page': INVITATIONS_PER_PAGE,
77 0e8e87ac Kostas Papadimitriou
               'DEBUG': settings.DEBUG}
78 cc7c5902 Dimitris Moraitis
    return template('home', context)
79 cc7c5902 Dimitris Moraitis
80 43df43aa Markos Gogoulos
def machines(request):
81 f533f224 Vangelis Koukis
    context = {'default_keywords': settings.DEFAULT_KEYWORDS}
82 43df43aa Markos Gogoulos
    return template('machines', context)
83 42f67a2a Dimitris Moraitis
84 f533f224 Vangelis Koukis
def machines_icon(request):
85 f533f224 Vangelis Koukis
    context = {'default_keywords': settings.DEFAULT_KEYWORDS}
86 f533f224 Vangelis Koukis
    return template('machines_icon', context)
87 f533f224 Vangelis Koukis
88 f533f224 Vangelis Koukis
def machines_list(request):
89 f533f224 Vangelis Koukis
    context = {'default_keywords': settings.DEFAULT_KEYWORDS}
90 f533f224 Vangelis Koukis
    return template('machines_list', context)
91 f533f224 Vangelis Koukis
92 f533f224 Vangelis Koukis
def machines_single(request):
93 f533f224 Vangelis Koukis
    context = {'default_keywords': settings.DEFAULT_KEYWORDS}
94 f533f224 Vangelis Koukis
    return template('machines_single', context)
95 f533f224 Vangelis Koukis
96 f533f224 Vangelis Koukis
def machines_console(request):
97 473d37cc Markos Gogoulos
    host, port, password = ('','','')
98 6912c72b Markos Gogoulos
    host = request.GET.get('host','')
99 6912c72b Markos Gogoulos
    port = request.GET.get('port','')
100 6912c72b Markos Gogoulos
    password = request.GET.get('password','')
101 f533f224 Vangelis Koukis
    machine = request.GET.get('machine','')
102 f533f224 Vangelis Koukis
    host_ip = request.GET.get('host_ip','')
103 f277a1cd Kostas Papadimitriou
    host_ip_v6 = request.GET.get('host_ip_v6','')
104 f277a1cd Kostas Papadimitriou
    context = {'host': host, 'port': port, 'password': password, 'machine': machine, 'host_ip': host_ip, 'host_ip_v6': host_ip_v6}
105 f533f224 Vangelis Koukis
    return template('machines_console', context)
106 e646ebe5 Dimitris Moraitis
107 8d08f18a Kostas Papadimitriou
def js_tests(request):
108 8d08f18a Kostas Papadimitriou
    return template('tests', {})
109 6f4c1371 Kostas Papadimitriou
110 bdf369a2 Kostas Papadimitriou
CONNECT_LINUX_LINUX_MESSAGE = _("""A direct connection to this machine can be established using the <a target="_blank"
111 bdf369a2 Kostas Papadimitriou
href="http://en.wikipedia.org/wiki/Secure_Shell">SSH Protocol</a>.
112 bdf369a2 Kostas Papadimitriou
To do so open a terminal and type the following at the prompt to connect to your machine:""")
113 bdf369a2 Kostas Papadimitriou
CONNECT_LINUX_WINDOWS_MESSAGE = _("""A direct connection to this machine can be
114 bdf369a2 Kostas Papadimitriou
established using <a target="_blank" href="http://en.wikipedia.org/wiki/Remote_Desktop_Services">Remote Desktop Service</a>.
115 bdf369a2 Kostas Papadimitriou
To do so, open the following file with an appropriate remote desktop client.""")
116 bdf369a2 Kostas Papadimitriou
CONNECT_LINUX_WINDOWS_SUBMESSAGE = _("""If you don't have one already
117 bdf369a2 Kostas Papadimitriou
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>.""")
118 bdf369a2 Kostas Papadimitriou
CONNECT_WINDOWS_LINUX_MESSAGE = _("""A direct connection to this machine can be established using the <a target="_blank"
119 bdf369a2 Kostas Papadimitriou
href="http://en.wikipedia.org/wiki/Secure_Shell">SSH Protocol</a>.
120 ae748a9d Kostas Papadimitriou
Open an ssh client such as PuTTY and type the following:""")
121 bdf369a2 Kostas Papadimitriou
CONNECT_WINDOWS_LINUX_SUBMESSAGE = _("""If you do not have an ssh client already installed,
122 bdf369a2 Kostas Papadimitriou
<a target="_blank" href="http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe">Download PuTTY</a>""")
123 6f4c1371 Kostas Papadimitriou
124 65ecc5e4 Kostas Papadimitriou
CONNECT_WINDOWS_WINDOWS_MESSAGE = _("""A direct connection to this machine can be
125 65ecc5e4 Kostas Papadimitriou
established using Remote Desktop. Click on the following link, and if asked open it using "Remote Desktop Connection" """)
126 65ecc5e4 Kostas Papadimitriou
CONNECT_WINDOWS_WINDOWS_SUBMESSAGE = _("""Save this file to disk for future use""")
127 bdf369a2 Kostas Papadimitriou
128 bdf369a2 Kostas Papadimitriou
# info/subinfo for all os combinations
129 bdf369a2 Kostas Papadimitriou
#
130 bdf369a2 Kostas Papadimitriou
# [0] info gets displayed on top of the message box
131 bdf369a2 Kostas Papadimitriou
# [1] subinfo gets displayed on the bottom as extra info
132 bdf369a2 Kostas Papadimitriou
# provided to the user when needed
133 65ecc5e4 Kostas Papadimitriou
CONNECT_PROMPT_MESSAGES = {
134 6f4c1371 Kostas Papadimitriou
    'linux': {
135 bdf369a2 Kostas Papadimitriou
            'linux': [CONNECT_LINUX_LINUX_MESSAGE, ""],
136 bdf369a2 Kostas Papadimitriou
            'windows': [CONNECT_LINUX_WINDOWS_MESSAGE, CONNECT_LINUX_WINDOWS_SUBMESSAGE]
137 6f4c1371 Kostas Papadimitriou
        },
138 6f4c1371 Kostas Papadimitriou
    'windows': {
139 bdf369a2 Kostas Papadimitriou
            'linux': [CONNECT_WINDOWS_LINUX_MESSAGE, CONNECT_WINDOWS_LINUX_SUBMESSAGE],
140 65ecc5e4 Kostas Papadimitriou
            'windows': [CONNECT_WINDOWS_WINDOWS_MESSAGE, CONNECT_WINDOWS_WINDOWS_SUBMESSAGE]
141 6f4c1371 Kostas Papadimitriou
        }
142 6f4c1371 Kostas Papadimitriou
    }
143 6f4c1371 Kostas Papadimitriou
144 65ecc5e4 Kostas Papadimitriou
# retrieve domain prefix from settings
145 65ecc5e4 Kostas Papadimitriou
DOMAIN_PREFIX = getattr(settings, 'MACHINE_DOMAIN_PREFIX', getattr(settings, 'BACKEND_PREFIX_ID', ""))
146 65ecc5e4 Kostas Papadimitriou
147 65ecc5e4 Kostas Papadimitriou
# domain template string
148 65ecc5e4 Kostas Papadimitriou
DOMAIN_TPL = "%s%%s" % DOMAIN_PREFIX
149 65ecc5e4 Kostas Papadimitriou
150 fd70c1db Markos Gogoulos
def machines_connect(request):
151 fd70c1db Markos Gogoulos
    ip_address = request.GET.get('ip_address','')
152 5bc1794f Kostas Papadimitriou
    operating_system = metadata_os = request.GET.get('os','')
153 bdf369a2 Kostas Papadimitriou
    server_id = request.GET.get('srv', 0)
154 6f4c1371 Kostas Papadimitriou
    host_os = request.GET.get('host_os','Linux').lower()
155 5bc1794f Kostas Papadimitriou
    username = request.GET.get('username', None)
156 65ecc5e4 Kostas Papadimitriou
    domain = request.GET.get("domain", DOMAIN_TPL % int(server_id))
157 6f4c1371 Kostas Papadimitriou
158 406bf0d9 Kostas Papadimitriou
    # guess host os
159 319091c7 Kostas Papadimitriou
    if host_os != "windows":
160 319091c7 Kostas Papadimitriou
        host_os = 'linux'
161 319091c7 Kostas Papadimitriou
162 406bf0d9 Kostas Papadimitriou
    # guess username
163 406bf0d9 Kostas Papadimitriou
    if not username:
164 406bf0d9 Kostas Papadimitriou
        username = "root"
165 406bf0d9 Kostas Papadimitriou
166 406bf0d9 Kostas Papadimitriou
        if metadata_os.lower() in ['ubuntu', 'kubuntu', 'fedora']:
167 406bf0d9 Kostas Papadimitriou
            username = "user"
168 406bf0d9 Kostas Papadimitriou
169 406bf0d9 Kostas Papadimitriou
        if metadata_os.lower() == "windows":
170 406bf0d9 Kostas Papadimitriou
            username = "Administrator"
171 406bf0d9 Kostas Papadimitriou
172 406bf0d9 Kostas Papadimitriou
    # operating system provides ssh access
173 406bf0d9 Kostas Papadimitriou
    ssh = False
174 6f4c1371 Kostas Papadimitriou
    if operating_system != "windows":
175 6f4c1371 Kostas Papadimitriou
        operating_system = "linux"
176 406bf0d9 Kostas Papadimitriou
        ssh = True
177 6f4c1371 Kostas Papadimitriou
178 bdf369a2 Kostas Papadimitriou
    # rdp param is set, the user requested rdp file
179 406bf0d9 Kostas Papadimitriou
    # check if we are on windows
180 406bf0d9 Kostas Papadimitriou
    if operating_system == 'windows' and request.GET.get("rdp", False):
181 406bf0d9 Kostas Papadimitriou
182 406bf0d9 Kostas Papadimitriou
        # UI sent domain info (from vm metadata) use this
183 406bf0d9 Kostas Papadimitriou
        # otherwise use our default snf-<vm_id> domain
184 406bf0d9 Kostas Papadimitriou
        rdp_context = {
185 406bf0d9 Kostas Papadimitriou
                'username': username,
186 406bf0d9 Kostas Papadimitriou
                'domain': domain,
187 406bf0d9 Kostas Papadimitriou
                'ip_address': ip_address
188 406bf0d9 Kostas Papadimitriou
        }
189 406bf0d9 Kostas Papadimitriou
190 406bf0d9 Kostas Papadimitriou
        rdp_file_data = render_to_string("synnefo-windows.rdp", rdp_context)
191 406bf0d9 Kostas Papadimitriou
        response = HttpResponse(rdp_file_data, mimetype='application/x-rdp')
192 bdf369a2 Kostas Papadimitriou
193 bdf369a2 Kostas Papadimitriou
        # proper filename, use server id and ip address
194 bdf369a2 Kostas Papadimitriou
        filename = "%d-%s.rdp" % (int(server_id), ip_address)
195 bdf369a2 Kostas Papadimitriou
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
196 fd70c1db Markos Gogoulos
    else:
197 bdf369a2 Kostas Papadimitriou
        link_title = _("Remote desktop to %s") % ip_address
198 406bf0d9 Kostas Papadimitriou
        link_url = "%s?ip_address=%s&os=%s&rdp=1&srv=%d&username=%s&domain=%s" % (
199 406bf0d9 Kostas Papadimitriou
                reverse("machines-connect"), ip_address, operating_system,int(server_id), username, domain)
200 5bc1794f Kostas Papadimitriou
201 b687587e Kostas Papadimitriou
        if (operating_system != "windows"):
202 406bf0d9 Kostas Papadimitriou
            link_title = "ssh %s@%s" % (username, ip_address)
203 bdf369a2 Kostas Papadimitriou
            link_url = None
204 bdf369a2 Kostas Papadimitriou
205 bdf369a2 Kostas Papadimitriou
            if host_os == "windows":
206 406bf0d9 Kostas Papadimitriou
                link_title = "%s@%s" % (username, ip_address)
207 b687587e Kostas Papadimitriou
208 6f4c1371 Kostas Papadimitriou
        # try to find a specific message
209 6f4c1371 Kostas Papadimitriou
        try:
210 65ecc5e4 Kostas Papadimitriou
            connect_message = CONNECT_PROMPT_MESSAGES[host_os][operating_system][0]
211 65ecc5e4 Kostas Papadimitriou
            subinfo = CONNECT_PROMPT_MESSAGES[host_os][operating_system][1]
212 6f4c1371 Kostas Papadimitriou
        except KeyError:
213 6f4c1371 Kostas Papadimitriou
            connect_message = _("You are trying to connect from a %s machine to a %s machine") % (host_os, operating_system)
214 bdf369a2 Kostas Papadimitriou
            subinfo = ""
215 6f4c1371 Kostas Papadimitriou
216 b687587e Kostas Papadimitriou
        response_object = {
217 b687587e Kostas Papadimitriou
                'ip': ip_address,
218 b687587e Kostas Papadimitriou
                'os': operating_system,
219 b687587e Kostas Papadimitriou
                'ssh': ssh,
220 6f4c1371 Kostas Papadimitriou
                'info': unicode(connect_message),
221 bdf369a2 Kostas Papadimitriou
                'subinfo': unicode(subinfo),
222 b687587e Kostas Papadimitriou
                'link': {'title': unicode(link_title), 'url': link_url}
223 b687587e Kostas Papadimitriou
            }
224 b687587e Kostas Papadimitriou
        response = HttpResponse(json.dumps(response_object), mimetype='application/json')  #no windows, no rdp
225 6f4c1371 Kostas Papadimitriou
226 fd70c1db Markos Gogoulos
    return response
227 fd70c1db Markos Gogoulos
228 faf5ddde Kostas Papadimitriou
FEEDBACK_CONTACTS = getattr(settings, "FEEDBACK_CONTACTS", [])
229 faf5ddde Kostas Papadimitriou
FEEDBACK_EMAIL_FROM = settings.FEEDBACK_EMAIL_FROM
230 faf5ddde Kostas Papadimitriou
231 faf5ddde Kostas Papadimitriou
def feedback_submit(request):
232 31f9a55b Kostas Papadimitriou
    if not request.method == "POST":
233 31f9a55b Kostas Papadimitriou
        raise Http404
234 31f9a55b Kostas Papadimitriou
235 faf5ddde Kostas Papadimitriou
    message = request.POST.get("feedback-msg")
236 faf5ddde Kostas Papadimitriou
    data = request.POST.get("feedback-data")
237 faf5ddde Kostas Papadimitriou
238 faf5ddde Kostas Papadimitriou
    # default to True (calls from error pages)
239 faf5ddde Kostas Papadimitriou
    allow_data_send = request.POST.get("feedback-submit-data", True)
240 faf5ddde Kostas Papadimitriou
241 31f9a55b Kostas Papadimitriou
    mail_subject = unicode(_("Feedback from synnefo application"))
242 faf5ddde Kostas Papadimitriou
243 faf5ddde Kostas Papadimitriou
    mail_context = {'message': message, 'data': data, 'allow_data_send': allow_data_send, 'request': request}
244 faf5ddde Kostas Papadimitriou
    mail_content = render_to_string("feedback_mail.txt", mail_context)
245 faf5ddde Kostas Papadimitriou
246 c90302ad Kostas Papadimitriou
    send_mail(mail_subject, mail_content, FEEDBACK_EMAIL_FROM,
247 c90302ad Kostas Papadimitriou
            dict(FEEDBACK_CONTACTS).values(), fail_silently=False)
248 faf5ddde Kostas Papadimitriou
249 8d08f18a Kostas Papadimitriou
    return HttpResponse('{"status":"send"}');
250 fd70c1db Markos Gogoulos
251 e646ebe5 Dimitris Moraitis
def images(request):
252 ec06b07c Dimitris Moraitis
    context = {}
253 cc7c5902 Dimitris Moraitis
    return template('images', context)
254 cc7c5902 Dimitris Moraitis
255 cc7c5902 Dimitris Moraitis
def disks(request):
256 ec06b07c Dimitris Moraitis
    context = {}
257 cc7c5902 Dimitris Moraitis
    return template('disks', context)
258 cc7c5902 Dimitris Moraitis
259 cc7c5902 Dimitris Moraitis
def networks(request):
260 ec06b07c Dimitris Moraitis
    context = {}
261 cc7c5902 Dimitris Moraitis
    return template('networks', context)
262 0dedf601 Christodoulos Psaltis
263 f533f224 Vangelis Koukis
def files(request):
264 0dedf601 Christodoulos Psaltis
    context = {}
265 0dedf601 Christodoulos Psaltis
    return template('files', context)
266 0dedf601 Christodoulos Psaltis
267 0dedf601 Christodoulos Psaltis
def desktops(request):
268 0dedf601 Christodoulos Psaltis
    context = {}
269 0dedf601 Christodoulos Psaltis
    return template('desktops', context)
270 0dedf601 Christodoulos Psaltis
271 0dedf601 Christodoulos Psaltis
def apps(request):
272 0dedf601 Christodoulos Psaltis
    context = {}
273 0dedf601 Christodoulos Psaltis
    return template('apps', context)