Statistics
| Branch: | Tag: | Revision:

root / ui / views.py @ c0f6fb49

History | View | Annotate | Download (9.2 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
    context = {'host': host, 'port': port, 'password': password, 'machine': machine, 'host_ip': host_ip}
91
    return template('machines_console', context)
92

    
93

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

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

    
110

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

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

    
133
    if operating_system != "windows":
134
        operating_system = "linux"
135

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

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

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

    
156
        if (operating_system != "windows"):
157
            link_title = "ssh root@%s" % ip_address
158
            link_url = None
159

    
160
            if host_os == "windows":
161
                link_title = ip_address
162

    
163
        # try to find a specific message
164
        try:
165
            connect_message = CONNECT_PROMT_MESSAGES[host_os][operating_system][0]
166
            subinfo = CONNECT_PROMT_MESSAGES[host_os][operating_system][1]
167
        except KeyError:
168
            connect_message = _("You are trying to connect from a %s machine to a %s machine") % (host_os, operating_system)
169
            subinfo = ""
170

    
171
        response_object = {
172
                'ip': ip_address,
173
                'os': operating_system,
174
                'ssh': ssh,
175
                'info': unicode(connect_message),
176
                'subinfo': unicode(subinfo),
177
                'link': {'title': unicode(link_title), 'url': link_url}
178
            }
179
        response = HttpResponse(json.dumps(response_object), mimetype='application/json')  #no windows, no rdp
180

    
181
    return response
182

    
183
FEEDBACK_CONTACTS = getattr(settings, "FEEDBACK_CONTACTS", [])
184
FEEDBACK_EMAIL_FROM = settings.FEEDBACK_EMAIL_FROM
185

    
186
def feedback_submit(request):
187
    message = request.POST.get("feedback-msg")
188
    data = request.POST.get("feedback-data")
189

    
190
    # default to True (calls from error pages)
191
    allow_data_send = request.POST.get("feedback-submit-data", True)
192

    
193
    mail_subject = _("Feedback from synnefo application")
194

    
195
    mail_context = {'message': message, 'data': data, 'allow_data_send': allow_data_send, 'request': request}
196
    mail_content = render_to_string("feedback_mail.txt", mail_context)
197

    
198
    if settings.DEBUG:
199
        print mail_subject, mail_content
200

    
201
    for email in FEEDBACK_CONTACTS:
202
        send_async(
203
                frm = FEEDBACK_EMAIL_FROM,
204
                to = "%s <%s>" % (email[0], email[1]),
205
                subject = mail_subject,
206
                body = mail_content
207
        )
208

    
209
    return HttpResponse("ok");
210

    
211
def images(request):
212
    context = {}
213
    return template('images', context)
214

    
215
def disks(request):
216
    context = {}
217
    return template('disks', context)
218

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

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

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

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