Statistics
| Branch: | Tag: | Revision:

root / ui / views.py @ 406bf0d9

History | View | Annotate | Download (10 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

    
108
CONNECT_WINDOWS_LINUX_MESSAGE = _("""A direct connection to this machine can be established using the <a target="_blank"
109
href="http://en.wikipedia.org/wiki/Secure_Shell">SSH Protocol</a>.
110
Open an ssh client such as PuTTY and type the following:""")
111
CONNECT_WINDOWS_LINUX_SUBMESSAGE = _("""If you do not have an ssh client already installed,
112
<a target="_blank" href="http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe">Download PuTTY</a>""")
113
CONNECT_WINDOWS_WINDOWS_MESSAGE = _("Trying to connect from windows to windows")
114

    
115

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

    
132
def machines_connect(request):
133
    ip_address = request.GET.get('ip_address','')
134
    operating_system = metadata_os = request.GET.get('os','')
135
    server_id = request.GET.get('srv', 0)
136
    host_os = request.GET.get('host_os','Linux').lower()
137
    username = request.GET.get('username', None)
138
    domain = request.GET.get("domain", "snf-%d" % int(server_id))
139

    
140
    # guess host os
141
    if host_os != "windows":
142
        host_os = 'linux'
143

    
144
    # guess username
145
    if not username:
146
        username = "root"
147

    
148
        if metadata_os.lower() in ['ubuntu', 'kubuntu', 'fedora']:
149
            username = "user"
150

    
151
        if metadata_os.lower() == "windows":
152
            username = "Administrator"
153

    
154
    # operating system provides ssh access
155
    ssh = False
156
    if operating_system != "windows":
157
        operating_system = "linux"
158
        ssh = True
159

    
160
    # rdp param is set, the user requested rdp file
161
    # check if we are on windows
162
    if operating_system == 'windows' and request.GET.get("rdp", False):
163

    
164
        # UI sent domain info (from vm metadata) use this
165
        # otherwise use our default snf-<vm_id> domain
166
        rdp_context = {
167
                'username': username,
168
                'domain': domain,
169
                'ip_address': ip_address
170
        }
171

    
172
        rdp_file_data = render_to_string("synnefo-windows.rdp", rdp_context)
173
        response = HttpResponse(rdp_file_data, mimetype='application/x-rdp')
174

    
175
        # proper filename, use server id and ip address
176
        filename = "%d-%s.rdp" % (int(server_id), ip_address)
177
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
178
    else:
179
        link_title = _("Remote desktop to %s") % ip_address
180
        link_url = "%s?ip_address=%s&os=%s&rdp=1&srv=%d&username=%s&domain=%s" % (
181
                reverse("machines-connect"), ip_address, operating_system,int(server_id), username, domain)
182

    
183
        if (operating_system != "windows"):
184
            link_title = "ssh %s@%s" % (username, ip_address)
185
            link_url = None
186

    
187
            if host_os == "windows":
188
                link_title = "%s@%s" % (username, ip_address)
189

    
190
        # try to find a specific message
191
        try:
192
            connect_message = CONNECT_PROMT_MESSAGES[host_os][operating_system][0]
193
            subinfo = CONNECT_PROMT_MESSAGES[host_os][operating_system][1]
194
        except KeyError:
195
            connect_message = _("You are trying to connect from a %s machine to a %s machine") % (host_os, operating_system)
196
            subinfo = ""
197

    
198
        response_object = {
199
                'ip': ip_address,
200
                'os': operating_system,
201
                'ssh': ssh,
202
                'info': unicode(connect_message),
203
                'subinfo': unicode(subinfo),
204
                'link': {'title': unicode(link_title), 'url': link_url}
205
            }
206
        response = HttpResponse(json.dumps(response_object), mimetype='application/json')  #no windows, no rdp
207

    
208
    return response
209

    
210
FEEDBACK_CONTACTS = getattr(settings, "FEEDBACK_CONTACTS", [])
211
FEEDBACK_EMAIL_FROM = settings.FEEDBACK_EMAIL_FROM
212

    
213
def feedback_submit(request):
214

    
215
    if not request.method == "POST":
216
        raise Http404
217

    
218
    message = request.POST.get("feedback-msg")
219
    data = request.POST.get("feedback-data")
220

    
221
    # default to True (calls from error pages)
222
    allow_data_send = request.POST.get("feedback-submit-data", True)
223

    
224
    mail_subject = unicode(_("Feedback from synnefo application"))
225

    
226
    mail_context = {'message': message, 'data': data, 'allow_data_send': allow_data_send, 'request': request}
227
    mail_content = render_to_string("feedback_mail.txt", mail_context)
228

    
229
    if settings.DEBUG:
230
        print mail_subject, mail_content
231

    
232
    for email in FEEDBACK_CONTACTS:
233
        send_async(
234
                frm = FEEDBACK_EMAIL_FROM,
235
                to = "%s <%s>" % (email[0], email[1]),
236
                subject = mail_subject,
237
                body = mail_content
238
        )
239

    
240
    return HttpResponse("ok");
241

    
242
def images(request):
243
    context = {}
244
    return template('images', context)
245

    
246
def disks(request):
247
    context = {}
248
    return template('disks', context)
249

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

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

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

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