Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / views.py @ 04a1b675

History | View | Annotate | Download (19.7 kB)

1 a5bc3755 Vangelis Koukis
# Copyright 2011 GRNET S.A. All rights reserved.
2 8950a78f Christodoulos Psaltis
#
3 a5bc3755 Vangelis Koukis
# Redistribution and use in source and binary forms, with or
4 a5bc3755 Vangelis Koukis
# without modification, are permitted provided that the following
5 a5bc3755 Vangelis Koukis
# conditions are met:
6 8950a78f Christodoulos Psaltis
#
7 a5bc3755 Vangelis Koukis
#   1. Redistributions of source code must retain the above
8 a5bc3755 Vangelis Koukis
#      copyright notice, this list of conditions and the following
9 a5bc3755 Vangelis Koukis
#      disclaimer.
10 8950a78f Christodoulos Psaltis
#
11 a5bc3755 Vangelis Koukis
#   2. Redistributions in binary form must reproduce the above
12 a5bc3755 Vangelis Koukis
#      copyright notice, this list of conditions and the following
13 a5bc3755 Vangelis Koukis
#      disclaimer in the documentation and/or other materials
14 a5bc3755 Vangelis Koukis
#      provided with the distribution.
15 8950a78f Christodoulos Psaltis
#
16 a5bc3755 Vangelis Koukis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 a5bc3755 Vangelis Koukis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 a5bc3755 Vangelis Koukis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 a5bc3755 Vangelis Koukis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 a5bc3755 Vangelis Koukis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 a5bc3755 Vangelis Koukis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 a5bc3755 Vangelis Koukis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 a5bc3755 Vangelis Koukis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 a5bc3755 Vangelis Koukis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 a5bc3755 Vangelis Koukis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 a5bc3755 Vangelis Koukis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 a5bc3755 Vangelis Koukis
# POSSIBILITY OF SUCH DAMAGE.
28 8950a78f Christodoulos Psaltis
#
29 a5bc3755 Vangelis Koukis
# The views and conclusions contained in the software and
30 a5bc3755 Vangelis Koukis
# documentation are those of the authors and should not be
31 a5bc3755 Vangelis Koukis
# interpreted as representing official policies, either expressed
32 a5bc3755 Vangelis Koukis
# or implied, of GRNET S.A.
33 8950a78f Christodoulos Psaltis
#
34 258235f4 Kostas Papadimitriou
35 edda4d30 Markos Gogoulos
import os
36 258235f4 Kostas Papadimitriou
37 abf90954 Markos Gogoulos
from django.conf import settings
38 edda4d30 Markos Gogoulos
from django.utils.translation import gettext_lazy as _
39 b303ba05 Christodoulos Psaltis
from django.template import Context, loader
40 cc7c5902 Dimitris Moraitis
from django.http import HttpResponse
41 ec06b07c Dimitris Moraitis
from django.utils.translation import get_language
42 9c901aec Markos Gogoulos
from django.utils import simplejson as json
43 f533f224 Vangelis Koukis
from django.shortcuts import render_to_response
44 faf5ddde Kostas Papadimitriou
from django.template.loader import render_to_string
45 b687587e Kostas Papadimitriou
from django.core.urlresolvers import reverse
46 c90302ad Kostas Papadimitriou
from django.core.mail import send_mail
47 31f9a55b Kostas Papadimitriou
from django.http import Http404
48 0d0466bf Kostas Papadimitriou
from django.template import RequestContext
49 31f9a55b Kostas Papadimitriou
50 07d104d8 Kostas Papadimitriou
from synnefo.util.version import get_component_version
51 7970aa87 Kostas Papadimitriou
52 04a1b675 Christos Stavrakakis
from snf_django.lib.astakos import get_user
53 02f0cd19 Kostas Papadimitriou
54 07d104d8 Kostas Papadimitriou
SYNNEFO_JS_LIB_VERSION = get_component_version('app')
55 30b6f316 Kostas Papadimitriou
56 30b6f316 Kostas Papadimitriou
# api configuration
57 30b6f316 Kostas Papadimitriou
COMPUTE_API_URL = getattr(settings, 'COMPUTE_API_URL', '/api/v1.1')
58 30b6f316 Kostas Papadimitriou
59 66be390b Kostas Papadimitriou
# UI preferences settings
60 66be390b Kostas Papadimitriou
TIMEOUT = getattr(settings, "TIMEOUT", 10000)
61 1cc6e94f Kostas Papadimitriou
UPDATE_INTERVAL = getattr(settings, "UI_UPDATE_INTERVAL", 5000)
62 6de7fde5 Kostas Papadimitriou
CHANGES_SINCE_ALIGNMENT = getattr(settings, "UI_CHANGES_SINCE_ALIGNMENT", 0)
63 1cc6e94f Kostas Papadimitriou
UPDATE_INTERVAL_INCREASE = getattr(settings, "UI_UPDATE_INTERVAL_INCREASE", 500)
64 1cc6e94f Kostas Papadimitriou
UPDATE_INTERVAL_INCREASE_AFTER_CALLS_COUNT = getattr(settings,
65 140d08d0 Kostas Papadimitriou
                                "UI_UPDATE_INTERVAL_INCREASE_AFTER_CALLS_COUNT",
66 140d08d0 Kostas Papadimitriou
                                3)
67 1cc6e94f Kostas Papadimitriou
UPDATE_INTERVAL_FAST = getattr(settings, "UI_UPDATE_INTERVAL_FAST", 2500)
68 1cc6e94f Kostas Papadimitriou
UPDATE_INTERVAL_MAX = getattr(settings, "UI_UPDATE_INTERVAL_MAX", 10000)
69 515b3e04 Kostas Papadimitriou
QUOTAS_UPDATE_INTERVAL = getattr(settings, "UI_QUOTAS_UPDATE_INTERVAL", 10000)
70 30b6f316 Kostas Papadimitriou
71 66be390b Kostas Papadimitriou
# predefined values settings
72 ebf4619d Kostas Papadimitriou
VM_IMAGE_COMMON_METADATA = getattr(settings, "UI_VM_IMAGE_COMMON_METADATA", ["OS", "users"])
73 66be390b Kostas Papadimitriou
SUGGESTED_FLAVORS_DEFAULT = {}
74 140d08d0 Kostas Papadimitriou
SUGGESTED_FLAVORS = getattr(settings, "VM_CREATE_SUGGESTED_FLAVORS",
75 140d08d0 Kostas Papadimitriou
                            SUGGESTED_FLAVORS_DEFAULT)
76 140d08d0 Kostas Papadimitriou
SUGGESTED_ROLES_DEFAULT = ["Database server", "File server", "Mail server",
77 140d08d0 Kostas Papadimitriou
                           "Web server", "Proxy"]
78 140d08d0 Kostas Papadimitriou
SUGGESTED_ROLES = getattr(settings, "VM_CREATE_SUGGESTED_ROLES",
79 140d08d0 Kostas Papadimitriou
                          SUGGESTED_ROLES_DEFAULT)
80 30b6f316 Kostas Papadimitriou
IMAGE_ICONS = settings.IMAGE_ICONS
81 03639c6d Kostas Papadimitriou
IMAGE_DELETED_TITLE = getattr(settings, 'UI_IMAGE_DELETED_TITLE',
82 fdc32267 Vangelis Koukis
                            '(deleted)')
83 03639c6d Kostas Papadimitriou
IMAGE_DELETED_SIZE_TITLE = getattr(settings, 'UI_IMAGE_DELETED_SIZE_TITLE',
84 03639c6d Kostas Papadimitriou
                            '(none)')
85 66be390b Kostas Papadimitriou
86 556dcd88 Kostas Papadimitriou
SUPPORT_SSH_OS_LIST = getattr(settings, "UI_SUPPORT_SSH_OS_LIST",)
87 556dcd88 Kostas Papadimitriou
OS_CREATED_USERS = getattr(settings, "UI_OS_DEFAULT_USER_MAP")
88 85f1cd1e Kostas Papadimitriou
LOGOUT_URL = getattr(settings, "UI_LOGOUT_URL", '/im/authenticate')
89 85f1cd1e Kostas Papadimitriou
LOGIN_URL = getattr(settings, "UI_LOGIN_URL", '/im/login')
90 85f1cd1e Kostas Papadimitriou
AUTH_COOKIE_NAME = getattr(settings, "UI_AUTH_COOKIE_NAME", 'synnefo_user')
91 30b6f316 Kostas Papadimitriou
92 66be390b Kostas Papadimitriou
# UI behaviour settings
93 66be390b Kostas Papadimitriou
DELAY_ON_BLUR = getattr(settings, "UI_DELAY_ON_BLUR", True)
94 66be390b Kostas Papadimitriou
UPDATE_HIDDEN_VIEWS = getattr(settings, "UI_UPDATE_HIDDEN_VIEWS", False)
95 66be390b Kostas Papadimitriou
HANDLE_WINDOW_EXCEPTIONS = getattr(settings, "UI_HANDLE_WINDOW_EXCEPTIONS", True)
96 66be390b Kostas Papadimitriou
SKIP_TIMEOUTS = getattr(settings, "UI_SKIP_TIMEOUTS", 1)
97 30b6f316 Kostas Papadimitriou
98 258235f4 Kostas Papadimitriou
# Additional settings
99 258235f4 Kostas Papadimitriou
VM_NAME_TEMPLATE = getattr(settings, "VM_CREATE_NAME_TPL", "My {0} server")
100 3796f345 Kostas Papadimitriou
VM_HOSTNAME_FORMAT = getattr(settings, "UI_VM_HOSTNAME_FORMAT",
101 3796f345 Kostas Papadimitriou
                                    'snf-%(id)s.vm.okeanos.grnet.gr')
102 f5e97e7f Kostas Papadimitriou
103 f5e97e7f Kostas Papadimitriou
if isinstance(VM_HOSTNAME_FORMAT, basestring):
104 f5e97e7f Kostas Papadimitriou
  VM_HOSTNAME_FORMAT =  VM_HOSTNAME_FORMAT % {'id':'{0}'}
105 f5e97e7f Kostas Papadimitriou
106 258235f4 Kostas Papadimitriou
MAX_SSH_KEYS_PER_USER = getattr(settings, "USERDATA_MAX_SSH_KEYS_PER_USER")
107 258235f4 Kostas Papadimitriou
FLAVORS_DISK_TEMPLATES_INFO = getattr(settings, "UI_FLAVORS_DISK_TEMPLATES_INFO", {})
108 1faf0b9c Kostas Papadimitriou
SYSTEM_IMAGES_OWNERS = getattr(settings, "UI_SYSTEM_IMAGES_OWNERS", {})
109 28958b8e Kostas Papadimitriou
CUSTOM_IMAGE_HELP_URL = getattr(settings, "UI_CUSTOM_IMAGE_HELP_URL", None)
110 30b6f316 Kostas Papadimitriou
111 7970aa87 Kostas Papadimitriou
# MEDIA PATHS
112 46990631 Kostas Papadimitriou
UI_MEDIA_URL = getattr(settings, "UI_MEDIA_URL",
113 46990631 Kostas Papadimitriou
                    "%ssnf-%s/" % (settings.MEDIA_URL, SYNNEFO_JS_LIB_VERSION))
114 7970aa87 Kostas Papadimitriou
UI_SYNNEFO_IMAGES_URL = getattr(settings,
115 46990631 Kostas Papadimitriou
                    "UI_SYNNEFO_IMAGES_URL", UI_MEDIA_URL + "images/")
116 7970aa87 Kostas Papadimitriou
UI_SYNNEFO_CSS_URL = getattr(settings,
117 46990631 Kostas Papadimitriou
                    "UI_SYNNEFO_CSS_URL", UI_MEDIA_URL + "css/")
118 c62c6b91 Kostas Papadimitriou
UI_SYNNEFO_JS_URL = getattr(settings,
119 46990631 Kostas Papadimitriou
                    "UI_SYNNEFO_JS_URL", UI_MEDIA_URL + "js/")
120 c62c6b91 Kostas Papadimitriou
UI_SYNNEFO_JS_LIB_URL = getattr(settings,
121 c62c6b91 Kostas Papadimitriou
                    "UI_SYNNEFO_JS_LIB_URL", UI_SYNNEFO_JS_URL + "lib/")
122 c62c6b91 Kostas Papadimitriou
UI_SYNNEFO_JS_WEB_URL = getattr(settings,
123 c62c6b91 Kostas Papadimitriou
                    "UI_SYNNEFO_JS_WEB_URL",
124 c62c6b91 Kostas Papadimitriou
                    UI_SYNNEFO_JS_URL + "ui/web/")
125 c62c6b91 Kostas Papadimitriou
126 30b6f316 Kostas Papadimitriou
# extensions
127 30b6f316 Kostas Papadimitriou
ENABLE_GLANCE = getattr(settings, 'UI_ENABLE_GLANCE', True)
128 30b6f316 Kostas Papadimitriou
GLANCE_API_URL = getattr(settings, 'UI_GLANCE_API_URL', '/glance')
129 1e882dd7 Kostas Papadimitriou
DIAGNOSTICS_UPDATE_INTERVAL = getattr(settings,
130 1e882dd7 Kostas Papadimitriou
                'UI_DIAGNOSTICS_UPDATE_INTERVAL', 2000)
131 d965e6dc Kostas Papadimitriou
132 126a01f2 Kostas Papadimitriou
# network settings
133 b7d38981 Dimitris Aragiorgis
DEFAULT_NETWORK_TYPES = {'MAC_FILTERED': 'mac-filtering', 'PHYSICAL_VLAN': 'physical-vlan'}
134 126a01f2 Kostas Papadimitriou
NETWORK_TYPES = getattr(settings,
135 126a01f2 Kostas Papadimitriou
                    'UI_NETWORK_AVAILABLE_NETWORK_TYPES', DEFAULT_NETWORK_TYPES)
136 126a01f2 Kostas Papadimitriou
DEFAULT_NETWORK_SUBNETS = ['10.0.0.0/24', '192.168.1.1/24']
137 126a01f2 Kostas Papadimitriou
NETWORK_SUBNETS = getattr(settings,
138 126a01f2 Kostas Papadimitriou
                    'UI_NETWORK_AVAILABLE_SUBNETS', DEFAULT_NETWORK_SUBNETS)
139 126a01f2 Kostas Papadimitriou
NETWORK_DUPLICATE_NICS = getattr(settings,
140 126a01f2 Kostas Papadimitriou
                    'UI_NETWORK_ALLOW_DUPLICATE_VM_NICS', False)
141 126a01f2 Kostas Papadimitriou
NETWORK_STRICT_DESTROY = getattr(settings,
142 126a01f2 Kostas Papadimitriou
                    'UI_NETWORK_STRICT_DESTROY', False)
143 126a01f2 Kostas Papadimitriou
NETWORK_ALLOW_MULTIPLE_DESTROY = getattr(settings,
144 126a01f2 Kostas Papadimitriou
                    'UI_NETWORK_ALLOW_MULTIPLE_DESTROY', False)
145 29cf98c9 Kostas Papadimitriou
AUTOMATIC_NETWORK_RANGE_FORMAT = getattr(settings,
146 29cf98c9 Kostas Papadimitriou
                                         'UI_AUTOMATIC_NETWORK_RANGE_FORMAT',
147 29cf98c9 Kostas Papadimitriou
                                        "192.168.%d.0/24").replace("%d", "{0}")
148 9555268a Kostas Papadimitriou
GROUP_PUBLIC_NETWORKS = getattr(settings, 'UI_GROUP_PUBLIC_NETWORKS', True)
149 40e5d2d2 Kostas Papadimitriou
GROUPED_PUBLIC_NETWORK_NAME = getattr(settings, 'UI_GROUPED_PUBLIC_NETWORK_NAME', 'Internet')
150 126a01f2 Kostas Papadimitriou
151 8f53cfa3 Kostas Papadimitriou
USER_CATALOG_URL = getattr(settings, 'UI_USER_CATALOG_URL', '/user_catalogs')
152 5af5920c Kostas Papadimitriou
FEEDBACK_POST_URL = getattr(settings, 'UI_FEEDBACK_POST_URL', '/feedback')
153 8f53cfa3 Kostas Papadimitriou
TRANSLATE_UUIDS = not getattr(settings, 'TRANSLATE_UUIDS', False)
154 8f53cfa3 Kostas Papadimitriou
155 0d0466bf Kostas Papadimitriou
def template(name, request, context):
156 e646ebe5 Dimitris Moraitis
    template_path = os.path.join(os.path.dirname(__file__), "templates/")
157 b303ba05 Christodoulos Psaltis
    current_template = template_path + name + '.html'
158 b303ba05 Christodoulos Psaltis
    t = loader.get_template(current_template)
159 7970aa87 Kostas Papadimitriou
    media_context = {
160 7970aa87 Kostas Papadimitriou
       'UI_MEDIA_URL': UI_MEDIA_URL,
161 7970aa87 Kostas Papadimitriou
       'SYNNEFO_JS_URL': UI_SYNNEFO_JS_URL,
162 7970aa87 Kostas Papadimitriou
       'SYNNEFO_JS_LIB_URL': UI_SYNNEFO_JS_LIB_URL,
163 7970aa87 Kostas Papadimitriou
       'SYNNEFO_JS_WEB_URL': UI_SYNNEFO_JS_WEB_URL,
164 7970aa87 Kostas Papadimitriou
       'SYNNEFO_IMAGES_URL': UI_SYNNEFO_IMAGES_URL,
165 7970aa87 Kostas Papadimitriou
       'SYNNEFO_CSS_URL': UI_SYNNEFO_CSS_URL,
166 7970aa87 Kostas Papadimitriou
       'SYNNEFO_JS_LIB_VERSION': SYNNEFO_JS_LIB_VERSION,
167 7970aa87 Kostas Papadimitriou
       'DEBUG': settings.DEBUG
168 7970aa87 Kostas Papadimitriou
    }
169 7970aa87 Kostas Papadimitriou
    context.update(media_context)
170 0d0466bf Kostas Papadimitriou
    return HttpResponse(t.render(RequestContext(request, context)))
171 cc7c5902 Dimitris Moraitis
172 cc7c5902 Dimitris Moraitis
def home(request):
173 e646ebe5 Dimitris Moraitis
    context = {'timeout': TIMEOUT,
174 a226ff9f Christodoulos Psaltis
               'project': '+nefo',
175 e646ebe5 Dimitris Moraitis
               'request': request,
176 f533f224 Vangelis Koukis
               'current_lang': get_language() or 'en',
177 30b6f316 Kostas Papadimitriou
               'compute_api_url': json.dumps(COMPUTE_API_URL),
178 8f53cfa3 Kostas Papadimitriou
               'user_catalog_url': json.dumps(USER_CATALOG_URL),
179 5af5920c Kostas Papadimitriou
               'feedback_post_url': json.dumps(FEEDBACK_POST_URL),
180 8f53cfa3 Kostas Papadimitriou
               'translate_uuids': json.dumps(TRANSLATE_UUIDS),
181 258235f4 Kostas Papadimitriou
                # update interval settings
182 7b03c69d Dimitris Moraitis
               'update_interval': UPDATE_INTERVAL,
183 1cc6e94f Kostas Papadimitriou
               'update_interval_increase': UPDATE_INTERVAL_INCREASE,
184 1cc6e94f Kostas Papadimitriou
               'update_interval_increase_after_calls': UPDATE_INTERVAL_INCREASE_AFTER_CALLS_COUNT,
185 1cc6e94f Kostas Papadimitriou
               'update_interval_fast': UPDATE_INTERVAL_FAST,
186 1cc6e94f Kostas Papadimitriou
               'update_interval_max': UPDATE_INTERVAL_MAX,
187 6de7fde5 Kostas Papadimitriou
               'changes_since_alignment': CHANGES_SINCE_ALIGNMENT,
188 515b3e04 Kostas Papadimitriou
               'quotas_update_interval': QUOTAS_UPDATE_INTERVAL,
189 0e8e87ac Kostas Papadimitriou
               'image_icons': IMAGE_ICONS,
190 dacd179a Kostas Papadimitriou
               'logout_redirect': LOGOUT_URL,
191 85f1cd1e Kostas Papadimitriou
               'login_redirect': LOGIN_URL,
192 85f1cd1e Kostas Papadimitriou
               'auth_cookie_name': AUTH_COOKIE_NAME,
193 e0dea91a Kostas Papadimitriou
               'suggested_flavors': json.dumps(SUGGESTED_FLAVORS),
194 73e25ce2 Kostas Papadimitriou
               'suggested_roles': json.dumps(SUGGESTED_ROLES),
195 bcd451ea Kostas Papadimitriou
               'vm_image_common_metadata': json.dumps(VM_IMAGE_COMMON_METADATA),
196 5764e310 Kostas Papadimitriou
               'synnefo_version': SYNNEFO_JS_LIB_VERSION,
197 66be390b Kostas Papadimitriou
               'delay_on_blur': json.dumps(DELAY_ON_BLUR),
198 66be390b Kostas Papadimitriou
               'update_hidden_views': json.dumps(UPDATE_HIDDEN_VIEWS),
199 66be390b Kostas Papadimitriou
               'handle_window_exceptions': json.dumps(HANDLE_WINDOW_EXCEPTIONS),
200 66be390b Kostas Papadimitriou
               'skip_timeouts': json.dumps(SKIP_TIMEOUTS),
201 9c191b95 Kostas Papadimitriou
               'vm_name_template': json.dumps(VM_NAME_TEMPLATE),
202 d965e6dc Kostas Papadimitriou
               'flavors_disk_templates_info': json.dumps(FLAVORS_DISK_TEMPLATES_INFO),
203 47276ec2 Kostas Papadimitriou
               'support_ssh_os_list': json.dumps(SUPPORT_SSH_OS_LIST),
204 47276ec2 Kostas Papadimitriou
               'os_created_users': json.dumps(OS_CREATED_USERS),
205 c72a830d Kostas Papadimitriou
               'userdata_keys_limit': json.dumps(MAX_SSH_KEYS_PER_USER),
206 30b6f316 Kostas Papadimitriou
               'use_glance': json.dumps(ENABLE_GLANCE),
207 1faf0b9c Kostas Papadimitriou
               'glance_api_url': json.dumps(GLANCE_API_URL),
208 03639c6d Kostas Papadimitriou
               'system_images_owners': json.dumps(SYSTEM_IMAGES_OWNERS),
209 28958b8e Kostas Papadimitriou
               'custom_image_help_url': CUSTOM_IMAGE_HELP_URL,
210 03639c6d Kostas Papadimitriou
               'image_deleted_title': json.dumps(IMAGE_DELETED_TITLE),
211 03639c6d Kostas Papadimitriou
               'image_deleted_size_title': json.dumps(IMAGE_DELETED_SIZE_TITLE),
212 126a01f2 Kostas Papadimitriou
               'network_suggested_subnets': json.dumps(NETWORK_SUBNETS),
213 126a01f2 Kostas Papadimitriou
               'network_available_types': json.dumps(NETWORK_TYPES),
214 126a01f2 Kostas Papadimitriou
               'network_allow_duplicate_vm_nics': json.dumps(NETWORK_DUPLICATE_NICS),
215 126a01f2 Kostas Papadimitriou
               'network_strict_destroy': json.dumps(NETWORK_STRICT_DESTROY),
216 1e882dd7 Kostas Papadimitriou
               'network_allow_multiple_destroy': json.dumps(NETWORK_ALLOW_MULTIPLE_DESTROY),
217 29cf98c9 Kostas Papadimitriou
               'automatic_network_range_format': json.dumps(AUTOMATIC_NETWORK_RANGE_FORMAT),
218 40e5d2d2 Kostas Papadimitriou
               'grouped_public_network_name': json.dumps(GROUPED_PUBLIC_NETWORK_NAME),
219 9555268a Kostas Papadimitriou
               'group_public_networks': json.dumps(GROUP_PUBLIC_NETWORKS),
220 3796f345 Kostas Papadimitriou
               'diagnostics_update_interval': json.dumps(DIAGNOSTICS_UPDATE_INTERVAL),
221 f5e97e7f Kostas Papadimitriou
               'vm_hostname_format': json.dumps(VM_HOSTNAME_FORMAT)
222 126a01f2 Kostas Papadimitriou
    }
223 0d0466bf Kostas Papadimitriou
    return template('home', request, context)
224 cc7c5902 Dimitris Moraitis
225 f533f224 Vangelis Koukis
def machines_console(request):
226 473d37cc Markos Gogoulos
    host, port, password = ('','','')
227 6912c72b Markos Gogoulos
    host = request.GET.get('host','')
228 6912c72b Markos Gogoulos
    port = request.GET.get('port','')
229 6912c72b Markos Gogoulos
    password = request.GET.get('password','')
230 f533f224 Vangelis Koukis
    machine = request.GET.get('machine','')
231 f533f224 Vangelis Koukis
    host_ip = request.GET.get('host_ip','')
232 f277a1cd Kostas Papadimitriou
    host_ip_v6 = request.GET.get('host_ip_v6','')
233 140d08d0 Kostas Papadimitriou
    context = {'host': host, 'port': port, 'password': password,
234 140d08d0 Kostas Papadimitriou
               'machine': machine, 'host_ip': host_ip, 'host_ip_v6': host_ip_v6}
235 0d0466bf Kostas Papadimitriou
    return template('machines_console', request, context)
236 e646ebe5 Dimitris Moraitis
237 cbf7b384 Kostas Papadimitriou
def user_quota(request):
238 2425992b Kostas Papadimitriou
    try:
239 2425992b Kostas Papadimitriou
        get_user(request, settings.ASTAKOS_URL, usage=True)
240 2425992b Kostas Papadimitriou
    except TypeError:
241 2425992b Kostas Papadimitriou
        # astakos client backwards compatibility
242 2425992b Kostas Papadimitriou
        get_user(request, settings.ASTAKOS_URL)
243 2425992b Kostas Papadimitriou
244 bda242f9 Georgios D. Tsoukalas
    if request.user and 'usage' in request.user:
245 367f2916 Kostas Papadimitriou
        response = json.dumps(request.user['usage'])
246 367f2916 Kostas Papadimitriou
    else:
247 367f2916 Kostas Papadimitriou
        # hmmm, old astakos ???
248 367f2916 Kostas Papadimitriou
        # try to mimic astakos response using cyclades quota settings
249 367f2916 Kostas Papadimitriou
        networks_limit_for_user = \
250 367f2916 Kostas Papadimitriou
            settings.NETWORKS_USER_QUOTA.get(request.user_uniq,
251 367f2916 Kostas Papadimitriou
                    settings.MAX_NETWORKS_PER_USER)
252 367f2916 Kostas Papadimitriou
        vms_limit_for_user = \
253 367f2916 Kostas Papadimitriou
            settings.VMS_USER_QUOTA.get(request.user_uniq,
254 367f2916 Kostas Papadimitriou
                    settings.MAX_NETWORKS_PER_USER)
255 367f2916 Kostas Papadimitriou
        usage = [{'name':'cyclades.vm',
256 367f2916 Kostas Papadimitriou
                  'maxValue': vms_limit_for_user
257 367f2916 Kostas Papadimitriou
                 },
258 367f2916 Kostas Papadimitriou
                 {'name':'cyclades.network.private',
259 367f2916 Kostas Papadimitriou
                  'maxValue': networks_limit_for_user
260 367f2916 Kostas Papadimitriou
                 }]
261 367f2916 Kostas Papadimitriou
        response = json.dumps(usage);
262 2425992b Kostas Papadimitriou
263 367f2916 Kostas Papadimitriou
    return HttpResponse(response, mimetype="application/json")
264 cbf7b384 Kostas Papadimitriou
265 8d08f18a Kostas Papadimitriou
def js_tests(request):
266 0d0466bf Kostas Papadimitriou
    return template('tests', request, {})
267 6f4c1371 Kostas Papadimitriou
268 bdf369a2 Kostas Papadimitriou
CONNECT_LINUX_LINUX_MESSAGE = _("""A direct connection to this machine can be established using the <a target="_blank"
269 bdf369a2 Kostas Papadimitriou
href="http://en.wikipedia.org/wiki/Secure_Shell">SSH Protocol</a>.
270 bdf369a2 Kostas Papadimitriou
To do so open a terminal and type the following at the prompt to connect to your machine:""")
271 bdf369a2 Kostas Papadimitriou
CONNECT_LINUX_WINDOWS_MESSAGE = _("""A direct connection to this machine can be
272 bdf369a2 Kostas Papadimitriou
established using <a target="_blank" href="http://en.wikipedia.org/wiki/Remote_Desktop_Services">Remote Desktop Service</a>.
273 17ab1a48 Vangelis Koukis
To do so, open the following file with an appropriate remote desktop client.
274 99a005df Kostas Papadimitriou
""")
275 ab3e9eb8 Kostas Papadimitriou
CONNECT_LINUX_WINDOWS_SUBMESSAGE = _("""If you don't have a Remote Desktop client already installed,
276 ab3e9eb8 Kostas Papadimitriou
we suggest the use of <a target="_blank"
277 99a005df Kostas Papadimitriou
href="http://sourceforge.net/projects/tsclient/files/latest/download?source=files">tsclient</a>.<br
278 99a005df Kostas Papadimitriou
/><span class="important">IMPORTANT: It may take up to 15 minutes for your Windows VM to become available
279 99a005df Kostas Papadimitriou
after its creation.</span>""")
280 bdf369a2 Kostas Papadimitriou
CONNECT_WINDOWS_LINUX_MESSAGE = _("""A direct connection to this machine can be established using the <a target="_blank"
281 bdf369a2 Kostas Papadimitriou
href="http://en.wikipedia.org/wiki/Secure_Shell">SSH Protocol</a>.
282 ae748a9d Kostas Papadimitriou
Open an ssh client such as PuTTY and type the following:""")
283 bdf369a2 Kostas Papadimitriou
CONNECT_WINDOWS_LINUX_SUBMESSAGE = _("""If you do not have an ssh client already installed,
284 bdf369a2 Kostas Papadimitriou
<a target="_blank" href="http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe">Download PuTTY</a>""")
285 6f4c1371 Kostas Papadimitriou
286 65ecc5e4 Kostas Papadimitriou
CONNECT_WINDOWS_WINDOWS_MESSAGE = _("""A direct connection to this machine can be
287 17ab1a48 Vangelis Koukis
established using Remote Desktop. Click on the following link, and if asked
288 17ab1a48 Vangelis Koukis
open it using "Remote Desktop Connection".
289 3796f345 Kostas Papadimitriou
<br /><span class="important">IMPORTANT: It may take up to 15 minutes for your Windows VM to become available
290 3796f345 Kostas Papadimitriou
after its creation.</span>""")
291 3796f345 Kostas Papadimitriou
CONNECT_WINDOWS_WINDOWS_SUBMESSAGE = _("""Save this file to disk for future use.""")
292 bdf369a2 Kostas Papadimitriou
293 bdf369a2 Kostas Papadimitriou
# info/subinfo for all os combinations
294 bdf369a2 Kostas Papadimitriou
#
295 bdf369a2 Kostas Papadimitriou
# [0] info gets displayed on top of the message box
296 bdf369a2 Kostas Papadimitriou
# [1] subinfo gets displayed on the bottom as extra info
297 bdf369a2 Kostas Papadimitriou
# provided to the user when needed
298 65ecc5e4 Kostas Papadimitriou
CONNECT_PROMPT_MESSAGES = {
299 6f4c1371 Kostas Papadimitriou
    'linux': {
300 bdf369a2 Kostas Papadimitriou
            'linux': [CONNECT_LINUX_LINUX_MESSAGE, ""],
301 140d08d0 Kostas Papadimitriou
            'windows': [CONNECT_LINUX_WINDOWS_MESSAGE,
302 3796f345 Kostas Papadimitriou
                        CONNECT_LINUX_WINDOWS_SUBMESSAGE],
303 3796f345 Kostas Papadimitriou
            'ssh_message': "ssh %(user)s@%(hostname)s"
304 3796f345 Kostas Papadimitriou
    },
305 6f4c1371 Kostas Papadimitriou
    'windows': {
306 140d08d0 Kostas Papadimitriou
            'linux': [CONNECT_WINDOWS_LINUX_MESSAGE,
307 140d08d0 Kostas Papadimitriou
                      CONNECT_WINDOWS_LINUX_SUBMESSAGE],
308 140d08d0 Kostas Papadimitriou
            'windows': [CONNECT_WINDOWS_WINDOWS_MESSAGE,
309 3796f345 Kostas Papadimitriou
                        CONNECT_WINDOWS_WINDOWS_SUBMESSAGE],
310 3796f345 Kostas Papadimitriou
            'ssh_message': "%(user)s@%(hostname)s"
311 3796f345 Kostas Papadimitriou
    },
312 3796f345 Kostas Papadimitriou
}
313 3796f345 Kostas Papadimitriou
314 3796f345 Kostas Papadimitriou
APPEND_CONNECT_PROMPT_MESSAGES = getattr(settings, 'UI_CONNECT_PROMPT_MESSAGES',
315 3796f345 Kostas Papadimitriou
                                       {})
316 3796f345 Kostas Papadimitriou
for k, v in APPEND_CONNECT_PROMPT_MESSAGES.iteritems():
317 3796f345 Kostas Papadimitriou
    CONNECT_PROMPT_MESSAGES[k].update(v)
318 6f4c1371 Kostas Papadimitriou
319 65ecc5e4 Kostas Papadimitriou
# retrieve domain prefix from settings
320 140d08d0 Kostas Papadimitriou
DOMAIN_PREFIX = getattr(settings, 'MACHINE_DOMAIN_PREFIX', getattr(settings,
321 140d08d0 Kostas Papadimitriou
                        'BACKEND_PREFIX_ID', ""))
322 65ecc5e4 Kostas Papadimitriou
323 65ecc5e4 Kostas Papadimitriou
# domain template string
324 65ecc5e4 Kostas Papadimitriou
DOMAIN_TPL = "%s%%s" % DOMAIN_PREFIX
325 65ecc5e4 Kostas Papadimitriou
326 fd70c1db Markos Gogoulos
def machines_connect(request):
327 fd70c1db Markos Gogoulos
    ip_address = request.GET.get('ip_address','')
328 3796f345 Kostas Papadimitriou
    hostname = request.GET.get('hostname','')
329 5bc1794f Kostas Papadimitriou
    operating_system = metadata_os = request.GET.get('os','')
330 bdf369a2 Kostas Papadimitriou
    server_id = request.GET.get('srv', 0)
331 6f4c1371 Kostas Papadimitriou
    host_os = request.GET.get('host_os','Linux').lower()
332 5bc1794f Kostas Papadimitriou
    username = request.GET.get('username', None)
333 65ecc5e4 Kostas Papadimitriou
    domain = request.GET.get("domain", DOMAIN_TPL % int(server_id))
334 6f4c1371 Kostas Papadimitriou
335 406bf0d9 Kostas Papadimitriou
    # guess host os
336 319091c7 Kostas Papadimitriou
    if host_os != "windows":
337 319091c7 Kostas Papadimitriou
        host_os = 'linux'
338 319091c7 Kostas Papadimitriou
339 406bf0d9 Kostas Papadimitriou
    # guess username
340 406bf0d9 Kostas Papadimitriou
    if not username:
341 406bf0d9 Kostas Papadimitriou
        username = "root"
342 406bf0d9 Kostas Papadimitriou
343 406bf0d9 Kostas Papadimitriou
        if metadata_os.lower() in ['ubuntu', 'kubuntu', 'fedora']:
344 406bf0d9 Kostas Papadimitriou
            username = "user"
345 406bf0d9 Kostas Papadimitriou
346 406bf0d9 Kostas Papadimitriou
        if metadata_os.lower() == "windows":
347 406bf0d9 Kostas Papadimitriou
            username = "Administrator"
348 406bf0d9 Kostas Papadimitriou
349 406bf0d9 Kostas Papadimitriou
    # operating system provides ssh access
350 406bf0d9 Kostas Papadimitriou
    ssh = False
351 6f4c1371 Kostas Papadimitriou
    if operating_system != "windows":
352 6f4c1371 Kostas Papadimitriou
        operating_system = "linux"
353 406bf0d9 Kostas Papadimitriou
        ssh = True
354 6f4c1371 Kostas Papadimitriou
355 bdf369a2 Kostas Papadimitriou
    # rdp param is set, the user requested rdp file
356 406bf0d9 Kostas Papadimitriou
    # check if we are on windows
357 406bf0d9 Kostas Papadimitriou
    if operating_system == 'windows' and request.GET.get("rdp", False):
358 406bf0d9 Kostas Papadimitriou
359 406bf0d9 Kostas Papadimitriou
        # UI sent domain info (from vm metadata) use this
360 406bf0d9 Kostas Papadimitriou
        # otherwise use our default snf-<vm_id> domain
361 3796f345 Kostas Papadimitriou
        EXTRA_RDP_CONTENT = getattr(settings, 'UI_EXTRA_RDP_CONTENT', '')
362 3796f345 Kostas Papadimitriou
        if callable(EXTRA_RDP_CONTENT):
363 3796f345 Kostas Papadimitriou
            extra_rdp_content = EXTRA_RDP_CONTENT(server_id, ip_address,
364 3796f345 Kostas Papadimitriou
                                                  hostname, username)
365 3796f345 Kostas Papadimitriou
        else:
366 3796f345 Kostas Papadimitriou
            extra_rdp_content = EXTRA_RDP_CONTENT % {
367 3796f345 Kostas Papadimitriou
                    'server_id':server_id,
368 3796f345 Kostas Papadimitriou
                    'ip_address': ip_address,
369 3796f345 Kostas Papadimitriou
                    'hostname': hostname,
370 3796f345 Kostas Papadimitriou
                    'user': username
371 3796f345 Kostas Papadimitriou
                  }
372 3796f345 Kostas Papadimitriou
373 406bf0d9 Kostas Papadimitriou
        rdp_context = {
374 406bf0d9 Kostas Papadimitriou
                'username': username,
375 406bf0d9 Kostas Papadimitriou
                'domain': domain,
376 3796f345 Kostas Papadimitriou
                'ip_address': ip_address,
377 f5e97e7f Kostas Papadimitriou
                'hostname': hostname,
378 3796f345 Kostas Papadimitriou
                'extra_content': extra_rdp_content
379 406bf0d9 Kostas Papadimitriou
        }
380 406bf0d9 Kostas Papadimitriou
381 406bf0d9 Kostas Papadimitriou
        rdp_file_data = render_to_string("synnefo-windows.rdp", rdp_context)
382 406bf0d9 Kostas Papadimitriou
        response = HttpResponse(rdp_file_data, mimetype='application/x-rdp')
383 bdf369a2 Kostas Papadimitriou
384 bdf369a2 Kostas Papadimitriou
        # proper filename, use server id and ip address
385 3796f345 Kostas Papadimitriou
        filename = "%d-%s.rdp" % (int(server_id), hostname)
386 bdf369a2 Kostas Papadimitriou
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
387 fd70c1db Markos Gogoulos
    else:
388 3796f345 Kostas Papadimitriou
        ssh_message = CONNECT_PROMPT_MESSAGES['linux'].get('ssh_message')
389 3796f345 Kostas Papadimitriou
        if host_os == 'windows':
390 3796f345 Kostas Papadimitriou
            ssh_message = CONNECT_PROMPT_MESSAGES['windows'].get('ssh_message')
391 3796f345 Kostas Papadimitriou
        if callable(ssh_message):
392 3796f345 Kostas Papadimitriou
            link_title = ssh_message(server_id, ip_address, hostname, username)
393 3796f345 Kostas Papadimitriou
        else:
394 3796f345 Kostas Papadimitriou
            link_title = ssh_message % {
395 3796f345 Kostas Papadimitriou
                    'server_id':server_id,
396 3796f345 Kostas Papadimitriou
                    'ip_address': ip_address,
397 3796f345 Kostas Papadimitriou
                    'hostname': hostname,
398 3796f345 Kostas Papadimitriou
                    'user': username
399 3796f345 Kostas Papadimitriou
                  }
400 b687587e Kostas Papadimitriou
        if (operating_system != "windows"):
401 bdf369a2 Kostas Papadimitriou
            link_url = None
402 bdf369a2 Kostas Papadimitriou
403 3796f345 Kostas Papadimitriou
        else:
404 3796f345 Kostas Papadimitriou
            link_title = _("Remote desktop to %s") % ip_address
405 3796f345 Kostas Papadimitriou
            link_url = "%s?ip_address=%s&os=%s&rdp=1&srv=%d&username=%s&domain=%s&hostname=%s" % (
406 3796f345 Kostas Papadimitriou
                    reverse("ui_machines_connect"), ip_address,
407 3796f345 Kostas Papadimitriou
                    operating_system, int(server_id), username, domain, hostname)
408 b687587e Kostas Papadimitriou
409 6f4c1371 Kostas Papadimitriou
        # try to find a specific message
410 6f4c1371 Kostas Papadimitriou
        try:
411 65ecc5e4 Kostas Papadimitriou
            connect_message = CONNECT_PROMPT_MESSAGES[host_os][operating_system][0]
412 65ecc5e4 Kostas Papadimitriou
            subinfo = CONNECT_PROMPT_MESSAGES[host_os][operating_system][1]
413 6f4c1371 Kostas Papadimitriou
        except KeyError:
414 140d08d0 Kostas Papadimitriou
            connect_message = _("You are trying to connect from a %s "
415 140d08d0 Kostas Papadimitriou
                                "machine to a %s machine") % (host_os, operating_system)
416 bdf369a2 Kostas Papadimitriou
            subinfo = ""
417 6f4c1371 Kostas Papadimitriou
418 b687587e Kostas Papadimitriou
        response_object = {
419 b687587e Kostas Papadimitriou
                'ip': ip_address,
420 b687587e Kostas Papadimitriou
                'os': operating_system,
421 b687587e Kostas Papadimitriou
                'ssh': ssh,
422 6f4c1371 Kostas Papadimitriou
                'info': unicode(connect_message),
423 bdf369a2 Kostas Papadimitriou
                'subinfo': unicode(subinfo),
424 b687587e Kostas Papadimitriou
                'link': {'title': unicode(link_title), 'url': link_url}
425 b687587e Kostas Papadimitriou
            }
426 140d08d0 Kostas Papadimitriou
        response = HttpResponse(json.dumps(response_object),
427 140d08d0 Kostas Papadimitriou
                                mimetype='application/json')  #no windows, no rdp
428 6f4c1371 Kostas Papadimitriou
429 fd70c1db Markos Gogoulos
    return response