Statistics
| Branch: | Tag: | Revision:

root / ui / views.py @ 319091c7

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

    
139
    if host_os != "windows":
140
        host_os = 'linux'
141

    
142
    if operating_system != "windows":
143
        operating_system = "linux"
144

    
145
    # rdp param is set, the user requested rdp file
146
    if operating_system == 'windows' and request.GET.get("rdp", False): #check if we are on windows
147
        rdp_file = os.path.join(os.path.dirname(__file__), "static/") + 'synnefo-windows.rdp'
148
        connect_data = open(rdp_file, 'r').read()
149
        connect_data = connect_data + 'full address:s:' + ip_address + '\n'
150
        response = HttpResponse(connect_data, mimetype='application/x-rdp')
151

    
152
        # proper filename, use server id and ip address
153
        filename = "%d-%s.rdp" % (int(server_id), ip_address)
154
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
155
    else:
156
        # no rdp requested return json object with info on how to connect
157
        ssh = False
158
        if (operating_system != "windows"):
159
            ssh = True
160

    
161
        link_title = _("Remote desktop to %s") % ip_address
162
        link_url = "%s?ip_address=%s&os=%s&rdp=1&srv=%d" % (reverse("machines-connect"), ip_address, operating_system,
163
                int(server_id))
164

    
165
        user = username
166
        if not user:
167
            user = "root"
168
            if metadata_os.lower() in ['ubuntu', 'kubuntu', 'fedora']:
169
                user = "user"
170

    
171
        if (operating_system != "windows"):
172
            link_title = "ssh %s@%s" % (user, ip_address)
173
            link_url = None
174

    
175
            if host_os == "windows":
176
                link_title = "%s@%s" % (user, ip_address)
177

    
178
        # try to find a specific message
179
        try:
180
            connect_message = CONNECT_PROMT_MESSAGES[host_os][operating_system][0]
181
            subinfo = CONNECT_PROMT_MESSAGES[host_os][operating_system][1]
182
        except KeyError:
183
            connect_message = _("You are trying to connect from a %s machine to a %s machine") % (host_os, operating_system)
184
            subinfo = ""
185

    
186
        response_object = {
187
                'ip': ip_address,
188
                'os': operating_system,
189
                'ssh': ssh,
190
                'info': unicode(connect_message),
191
                'subinfo': unicode(subinfo),
192
                'link': {'title': unicode(link_title), 'url': link_url}
193
            }
194
        response = HttpResponse(json.dumps(response_object), mimetype='application/json')  #no windows, no rdp
195

    
196
    return response
197

    
198
FEEDBACK_CONTACTS = getattr(settings, "FEEDBACK_CONTACTS", [])
199
FEEDBACK_EMAIL_FROM = settings.FEEDBACK_EMAIL_FROM
200

    
201
def feedback_submit(request):
202

    
203
    if not request.method == "POST":
204
        raise Http404
205

    
206
    message = request.POST.get("feedback-msg")
207
    data = request.POST.get("feedback-data")
208

    
209
    # default to True (calls from error pages)
210
    allow_data_send = request.POST.get("feedback-submit-data", True)
211

    
212
    mail_subject = unicode(_("Feedback from synnefo application"))
213

    
214
    mail_context = {'message': message, 'data': data, 'allow_data_send': allow_data_send, 'request': request}
215
    mail_content = render_to_string("feedback_mail.txt", mail_context)
216

    
217
    if settings.DEBUG:
218
        print mail_subject, mail_content
219

    
220
    for email in FEEDBACK_CONTACTS:
221
        send_async(
222
                frm = FEEDBACK_EMAIL_FROM,
223
                to = "%s <%s>" % (email[0], email[1]),
224
                subject = mail_subject,
225
                body = mail_content
226
        )
227

    
228
    return HttpResponse("ok");
229

    
230
def images(request):
231
    context = {}
232
    return template('images', context)
233

    
234
def disks(request):
235
    context = {}
236
    return template('disks', context)
237

    
238
def networks(request):
239
    context = {}
240
    return template('networks', context)
241

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

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

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