Statistics
| Branch: | Tag: | Revision:

root / ui / views.py @ f81d0548

History | View | Annotate | Download (10.4 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.template.loader import render_to_string
43
from django.core.urlresolvers import reverse
44

    
45
from synnefo.logic.email_send import send_async
46

    
47
from django.http import Http404
48

    
49
TIMEOUT = settings.TIMEOUT
50
UPDATE_INTERVAL = settings.UPDATE_INTERVAL
51
IMAGE_ICONS = settings.IMAGE_ICONS
52
LOGOUT_URL = getattr(settings, "LOGOUT_URL", settings.LOGIN_URL)
53

    
54
def template(name, context):
55
    template_path = os.path.join(os.path.dirname(__file__), "templates/")
56
    current_template = template_path + name + '.html'
57
    t = loader.get_template(current_template)
58
    return HttpResponse(t.render(Context(context)))
59

    
60
def home(request):
61
    context = {'timeout': TIMEOUT,
62
               'project': '+nefo',
63
               'request': request,
64
               'current_lang': get_language() or 'en',
65
               'update_interval': UPDATE_INTERVAL,
66
               'image_icons': IMAGE_ICONS,
67
               'logout_redirect': LOGOUT_URL,
68
               'DEBUG': settings.DEBUG}
69
    return template('home', context)
70

    
71
def machines(request):
72
    context = {'default_keywords': settings.DEFAULT_KEYWORDS}
73
    return template('machines', context)
74

    
75
def machines_icon(request):
76
    context = {'default_keywords': settings.DEFAULT_KEYWORDS}
77
    return template('machines_icon', context)
78

    
79
def machines_list(request):
80
    context = {'default_keywords': settings.DEFAULT_KEYWORDS}
81
    return template('machines_list', context)
82

    
83
def machines_single(request):
84
    context = {'default_keywords': settings.DEFAULT_KEYWORDS}
85
    return template('machines_single', context)
86

    
87
def machines_console(request):
88
    host, port, password = ('','','')
89
    host = request.GET.get('host','')
90
    port = request.GET.get('port','')
91
    password = request.GET.get('password','')
92
    machine = request.GET.get('machine','')
93
    host_ip = request.GET.get('host_ip','')
94
    host_ip_v6 = request.GET.get('host_ip_v6','')
95
    context = {'host': host, 'port': port, 'password': password, 'machine': machine, 'host_ip': host_ip, 'host_ip_v6': host_ip_v6}
96
    return template('machines_console', context)
97

    
98

    
99
CONNECT_LINUX_LINUX_MESSAGE = _("""A direct connection to this machine can be established using the <a target="_blank"
100
href="http://en.wikipedia.org/wiki/Secure_Shell">SSH Protocol</a>.
101
To do so open a terminal and type the following at the prompt to connect to your machine:""")
102
CONNECT_LINUX_WINDOWS_MESSAGE = _("""A direct connection to this machine can be
103
established using <a target="_blank" href="http://en.wikipedia.org/wiki/Remote_Desktop_Services">Remote Desktop Service</a>.
104
To do so, open the following file with an appropriate remote desktop client.""")
105
CONNECT_LINUX_WINDOWS_SUBMESSAGE = _("""If you don't have one already
106
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>.""")
107
CONNECT_WINDOWS_LINUX_MESSAGE = _("""A direct connection to this machine can be established using the <a target="_blank"
108
href="http://en.wikipedia.org/wiki/Secure_Shell">SSH Protocol</a>.
109
Open an ssh client such as PuTTY and type the following:""")
110
CONNECT_WINDOWS_LINUX_SUBMESSAGE = _("""If you do not have an ssh client already installed,
111
<a target="_blank" href="http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe">Download PuTTY</a>""")
112

    
113
CONNECT_WINDOWS_WINDOWS_MESSAGE = _("""A direct connection to this machine can be
114
established using Remote Desktop. Click on the following link, and if asked open it using "Remote Desktop Connection" """)
115
CONNECT_WINDOWS_WINDOWS_SUBMESSAGE = _("""Save this file to disk for future use""")
116

    
117
# info/subinfo for all os combinations
118
#
119
# [0] info gets displayed on top of the message box
120
# [1] subinfo gets displayed on the bottom as extra info
121
# provided to the user when needed
122
CONNECT_PROMPT_MESSAGES = {
123
    'linux': {
124
            'linux': [CONNECT_LINUX_LINUX_MESSAGE, ""],
125
            'windows': [CONNECT_LINUX_WINDOWS_MESSAGE, CONNECT_LINUX_WINDOWS_SUBMESSAGE]
126
        },
127
    'windows': {
128
            'linux': [CONNECT_WINDOWS_LINUX_MESSAGE, CONNECT_WINDOWS_LINUX_SUBMESSAGE],
129
            'windows': [CONNECT_WINDOWS_WINDOWS_MESSAGE, CONNECT_WINDOWS_WINDOWS_SUBMESSAGE]
130
        }
131
    }
132

    
133
# retrieve domain prefix from settings
134
DOMAIN_PREFIX = getattr(settings, 'MACHINE_DOMAIN_PREFIX', getattr(settings, 'BACKEND_PREFIX_ID', ""))
135

    
136
# domain template string
137
DOMAIN_TPL = "%s%%s" % DOMAIN_PREFIX
138

    
139
def machines_connect(request):
140
    ip_address = request.GET.get('ip_address','')
141
    operating_system = metadata_os = request.GET.get('os','')
142
    server_id = request.GET.get('srv', 0)
143
    host_os = request.GET.get('host_os','Linux').lower()
144
    username = request.GET.get('username', None)
145
    domain = request.GET.get("domain", DOMAIN_TPL % int(server_id))
146

    
147
    # guess host os
148
    if host_os != "windows":
149
        host_os = 'linux'
150

    
151
    # guess username
152
    if not username:
153
        username = "root"
154

    
155
        if metadata_os.lower() in ['ubuntu', 'kubuntu', 'fedora']:
156
            username = "user"
157

    
158
        if metadata_os.lower() == "windows":
159
            username = "Administrator"
160

    
161
    # operating system provides ssh access
162
    ssh = False
163
    if operating_system != "windows":
164
        operating_system = "linux"
165
        ssh = True
166

    
167
    # rdp param is set, the user requested rdp file
168
    # check if we are on windows
169
    if operating_system == 'windows' and request.GET.get("rdp", False):
170

    
171
        # UI sent domain info (from vm metadata) use this
172
        # otherwise use our default snf-<vm_id> domain
173
        rdp_context = {
174
                'username': username,
175
                'domain': domain,
176
                'ip_address': ip_address
177
        }
178

    
179
        rdp_file_data = render_to_string("synnefo-windows.rdp", rdp_context)
180
        response = HttpResponse(rdp_file_data, mimetype='application/x-rdp')
181

    
182
        # proper filename, use server id and ip address
183
        filename = "%d-%s.rdp" % (int(server_id), ip_address)
184
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
185
    else:
186
        link_title = _("Remote desktop to %s") % ip_address
187
        link_url = "%s?ip_address=%s&os=%s&rdp=1&srv=%d&username=%s&domain=%s" % (
188
                reverse("machines-connect"), ip_address, operating_system,int(server_id), username, domain)
189

    
190
        if (operating_system != "windows"):
191
            link_title = "ssh %s@%s" % (username, ip_address)
192
            link_url = None
193

    
194
            if host_os == "windows":
195
                link_title = "%s@%s" % (username, ip_address)
196

    
197
        # try to find a specific message
198
        try:
199
            connect_message = CONNECT_PROMPT_MESSAGES[host_os][operating_system][0]
200
            subinfo = CONNECT_PROMPT_MESSAGES[host_os][operating_system][1]
201
        except KeyError:
202
            connect_message = _("You are trying to connect from a %s machine to a %s machine") % (host_os, operating_system)
203
            subinfo = ""
204

    
205
        response_object = {
206
                'ip': ip_address,
207
                'os': operating_system,
208
                'ssh': ssh,
209
                'info': unicode(connect_message),
210
                'subinfo': unicode(subinfo),
211
                'link': {'title': unicode(link_title), 'url': link_url}
212
            }
213
        response = HttpResponse(json.dumps(response_object), mimetype='application/json')  #no windows, no rdp
214

    
215
    return response
216

    
217
FEEDBACK_CONTACTS = getattr(settings, "FEEDBACK_CONTACTS", [])
218
FEEDBACK_EMAIL_FROM = settings.FEEDBACK_EMAIL_FROM
219

    
220
def feedback_submit(request):
221

    
222
    if not request.method == "POST":
223
        raise Http404
224

    
225
    message = request.POST.get("feedback-msg")
226
    data = request.POST.get("feedback-data")
227

    
228
    # default to True (calls from error pages)
229
    allow_data_send = request.POST.get("feedback-submit-data", True)
230

    
231
    mail_subject = unicode(_("Feedback from synnefo application"))
232

    
233
    mail_context = {'message': message, 'data': data, 'allow_data_send': allow_data_send, 'request': request}
234
    mail_content = render_to_string("feedback_mail.txt", mail_context)
235

    
236
    if settings.DEBUG:
237
        print mail_subject, mail_content
238

    
239
    for email in FEEDBACK_CONTACTS:
240
        send_async(
241
                frm = FEEDBACK_EMAIL_FROM,
242
                to = "%s <%s>" % (email[0], email[1]),
243
                subject = mail_subject,
244
                body = mail_content
245
        )
246

    
247
    return HttpResponse("ok");
248

    
249
def images(request):
250
    context = {}
251
    return template('images', context)
252

    
253
def disks(request):
254
    context = {}
255
    return template('disks', context)
256

    
257
def networks(request):
258
    context = {}
259
    return template('networks', context)
260

    
261
def files(request):
262
    context = {}
263
    return template('files', context)
264

    
265
def desktops(request):
266
    context = {}
267
    return template('desktops', context)
268

    
269
def apps(request):
270
    context = {}
271
    return template('apps', context)