Statistics
| Branch: | Tag: | Revision:

root / ui / views.py @ 5bc1794f

History | View | Annotate | Download (9.5 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
TIMEOUT = settings.TIMEOUT
48
UPDATE_INTERVAL = settings.UPDATE_INTERVAL
49
IMAGE_ICONS = settings.IMAGE_ICONS
50

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

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

    
67
def machines(request):
68
    context = {'default_keywords': settings.DEFAULT_KEYWORDS}
69
    return template('machines', context)
70

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

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

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

    
83
def machines_console(request):
84
    host, port, password = ('','','')
85
    host = request.GET.get('host','')
86
    port = request.GET.get('port','')
87
    password = request.GET.get('password','')
88
    machine = request.GET.get('machine','')
89
    host_ip = request.GET.get('host_ip','')
90
    host_ip_v6 = request.GET.get('host_ip_v6','')
91
    context = {'host': host, 'port': port, 'password': password, 'machine': machine, 'host_ip': host_ip, 'host_ip_v6': host_ip_v6}
92
    return template('machines_console', context)
93

    
94

    
95
CONNECT_LINUX_LINUX_MESSAGE = _("""A direct connection to this machine can be established using the <a target="_blank"
96
href="http://en.wikipedia.org/wiki/Secure_Shell">SSH Protocol</a>.
97
To do so open a terminal and type the following at the prompt to connect to your machine:""")
98
CONNECT_LINUX_WINDOWS_MESSAGE = _("""A direct connection to this machine can be
99
established using <a target="_blank" href="http://en.wikipedia.org/wiki/Remote_Desktop_Services">Remote Desktop Service</a>.
100
To do so, open the following file with an appropriate remote desktop client.""")
101
CONNECT_LINUX_WINDOWS_SUBMESSAGE = _("""If you don't have one already
102
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>.""")
103

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

    
111

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

    
128
def machines_connect(request):
129
    ip_address = request.GET.get('ip_address','')
130
    operating_system = metadata_os = request.GET.get('os','')
131
    server_id = request.GET.get('srv', 0)
132
    host_os = request.GET.get('host_os','Linux').lower()
133
    username = request.GET.get('username', None)
134

    
135
    if operating_system != "windows":
136
        operating_system = "linux"
137

    
138
    # rdp param is set, the user requested rdp file
139
    if operating_system == 'windows' and request.GET.get("rdp", False): #check if we are on windows
140
        rdp_file = os.path.join(os.path.dirname(__file__), "static/") + 'synnefo-windows.rdp'
141
        connect_data = open(rdp_file, 'r').read()
142
        connect_data = connect_data + 'full address:s:' + ip_address + '\n'
143
        response = HttpResponse(connect_data, mimetype='application/x-rdp')
144

    
145
        # proper filename, use server id and ip address
146
        filename = "%d-%s.rdp" % (int(server_id), ip_address)
147
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
148
    else:
149
        # no rdp requested return json object with info on how to connect
150
        ssh = False
151
        if (operating_system != "windows"):
152
            ssh = True
153

    
154
        link_title = _("Remote desktop to %s") % ip_address
155
        link_url = "%s?ip_address=%s&os=%s&rdp=1&srv=%d" % (reverse("machines-connect"), ip_address, operating_system,
156
                int(server_id))
157

    
158
        user = username
159
        if not user:
160
            user = "root"
161
            if metadata_os.lower() in ['ubuntu', 'kubuntu', 'fedora']:
162
                user = "user"
163

    
164
        if (operating_system != "windows"):
165
            link_title = "ssh %s@%s" % (user, ip_address)
166
            link_url = None
167

    
168
            if host_os == "windows":
169
                link_title = ip_address
170

    
171
        # try to find a specific message
172
        try:
173
            connect_message = CONNECT_PROMT_MESSAGES[host_os][operating_system][0]
174
            subinfo = CONNECT_PROMT_MESSAGES[host_os][operating_system][1]
175
        except KeyError:
176
            connect_message = _("You are trying to connect from a %s machine to a %s machine") % (host_os, operating_system)
177
            subinfo = ""
178

    
179
        response_object = {
180
                'ip': ip_address,
181
                'os': operating_system,
182
                'ssh': ssh,
183
                'info': unicode(connect_message),
184
                'subinfo': unicode(subinfo),
185
                'link': {'title': unicode(link_title), 'url': link_url}
186
            }
187
        response = HttpResponse(json.dumps(response_object), mimetype='application/json')  #no windows, no rdp
188

    
189
    return response
190

    
191
FEEDBACK_CONTACTS = getattr(settings, "FEEDBACK_CONTACTS", [])
192
FEEDBACK_EMAIL_FROM = settings.FEEDBACK_EMAIL_FROM
193

    
194
def feedback_submit(request):
195
    message = request.POST.get("feedback-msg")
196
    data = request.POST.get("feedback-data")
197

    
198
    # default to True (calls from error pages)
199
    allow_data_send = request.POST.get("feedback-submit-data", True)
200

    
201
    mail_subject = _("Feedback from synnefo application")
202

    
203
    mail_context = {'message': message, 'data': data, 'allow_data_send': allow_data_send, 'request': request}
204
    mail_content = render_to_string("feedback_mail.txt", mail_context)
205

    
206
    if settings.DEBUG:
207
        print mail_subject, mail_content
208

    
209
    for email in FEEDBACK_CONTACTS:
210
        send_async(
211
                frm = FEEDBACK_EMAIL_FROM,
212
                to = "%s <%s>" % (email[0], email[1]),
213
                subject = mail_subject,
214
                body = mail_content
215
        )
216

    
217
    return HttpResponse("ok");
218

    
219
def images(request):
220
    context = {}
221
    return template('images', context)
222

    
223
def disks(request):
224
    context = {}
225
    return template('disks', context)
226

    
227
def networks(request):
228
    context = {}
229
    return template('networks', context)
230

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

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

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