~ubuntu-branches/ubuntu/quantal/maas/quantal-updates

« back to all changes in this revision

Viewing changes to src/maasserver/middleware.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-04-17 23:44:46 UTC
  • mfrom: (1.1.12)
  • Revision ID: package-import@ubuntu.com-20120417234446-y3212quny2x1gft8
Tags: 0.1+bzr482+dfsg-0ubuntu1
* New upstream release (Fixes LP: #981103)
* debian/maas.postinst:
  - Make sure rabbitmq and postgresql are started on upgrade (LP: #981282)
  - Handle upgrades from any lower than 0.1+bzr462+dfsg-0ubuntu1 to
    correctly re-generate passwords, and not have db sync/migrate issues
    as config has changed upstream.
  - Correctly set Passwords for PSERV, otherwise it won't set new passwords.
* Allow MAAS_DEFAULT_URL reconfiguration. (LP: #980970)
  - debian/maas.config: Add reconfigure validation to correctly allow it,
    and ask a question.
  - debian/maas.postinst: Reconfigure DEFAULT_MAAS_URL as well as cobbler
    server and next_server for PXE/Provisioning.
  - debian/maas.templates: Add debconf question and update info.
* Do not lose MAAS_DEFAULT_URL settings on upgrade (LP: #984309)
* debian/maas.postinst:
  - Set cobbler password in between quotes (LP: #984427)
  - Do not change permissions to maas.log (LP: #980915)
* no longer use maas-cloudimg2ephemeral, but rather use premade images 
  at http://maas.ubuntu.com

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
4
4
from __future__ import (
 
5
    absolute_import,
5
6
    print_function,
6
7
    unicode_literals,
7
8
    )
12
13
__all__ = [
13
14
    "AccessMiddleware",
14
15
    "APIErrorsMiddleware",
 
16
    "ErrorsMiddleware",
15
17
    "ExceptionMiddleware",
16
18
    ]
17
19
 
25
27
import re
26
28
 
27
29
from django.conf import settings
 
30
from django.contrib import messages
 
31
from django.core.cache import cache
28
32
from django.core.exceptions import (
29
33
    PermissionDenied,
30
34
    ValidationError,
37
41
    HttpResponseRedirect,
38
42
    )
39
43
from django.utils.http import urlquote_plus
40
 
from maasserver.exceptions import MAASAPIException
 
44
from maasserver.exceptions import (
 
45
    ExternalComponentException,
 
46
    MAASAPIException,
 
47
    )
41
48
 
42
49
 
43
50
def get_relative_path(path):
94
101
                return None
95
102
 
96
103
 
 
104
PROFILES_CHECK_DONE_KEY = 'profile-check-done'
 
105
 
 
106
# The profiles check done by check_profiles_cached is only done at most once
 
107
# every PROFILE_CHECK_DELAY seconds for efficiency.
 
108
PROFILE_CHECK_DELAY = 2 * 60
 
109
 
 
110
 
 
111
def check_profiles_cached():
 
112
    """Check Cobbler's profiles. The check is actually done at most once every
 
113
    PROFILE_CHECK_DELAY seconds for performance reasons.
 
114
    """
 
115
    # Avoid circular imports.
 
116
    from maasserver.provisioning import check_profiles
 
117
    if not cache.get(PROFILES_CHECK_DONE_KEY, False):
 
118
        # Mark the profile check as done beforehand as the actual check
 
119
        # might raise an exception.
 
120
        cache.set(PROFILES_CHECK_DONE_KEY, True, PROFILE_CHECK_DELAY)
 
121
        check_profiles()
 
122
 
 
123
 
 
124
def clear_profiles_check_cache():
 
125
    """Force a profile check next time the MAAS server is accessed."""
 
126
    cache.delete(PROFILES_CHECK_DONE_KEY)
 
127
 
 
128
 
 
129
class ExternalComponentsMiddleware:
 
130
    """This middleware performs checks for external components (right
 
131
    now only Cobbler is checked) at regular intervals.
 
132
    """
 
133
    def process_request(self, request):
 
134
        # This middleware hijacks the request to perform checks.  Any
 
135
        # error raised during these checks should be caught to avoid
 
136
        # disturbing the handling of the request.  Proper error reporting
 
137
        # should be handled in the check method itself.
 
138
        try:
 
139
            check_profiles_cached()
 
140
        except Exception:
 
141
            pass
 
142
        return None
 
143
 
 
144
 
97
145
class ExceptionMiddleware:
98
146
    """Convert exceptions into appropriate HttpResponse responses.
99
147
 
163
211
    path_regex = settings.API_URL_REGEXP
164
212
 
165
213
 
 
214
class ErrorsMiddleware:
 
215
    """Handle ExternalComponentException exceptions in POST requests: add a
 
216
    message with the error string and redirect to the same page (using GET).
 
217
    """
 
218
 
 
219
    def process_exception(self, request, exception):
 
220
        should_process_exception = (
 
221
            request.method == 'POST' and
 
222
            isinstance(exception, ExternalComponentException))
 
223
        if should_process_exception:
 
224
            messages.error(request, unicode(exception))
 
225
            return HttpResponseRedirect(request.path)
 
226
        else:
 
227
            # Not an ExternalComponentException or not a POST request: do not
 
228
            # handle it.
 
229
            return None
 
230
 
 
231
 
166
232
class ExceptionLoggerMiddleware:
167
233
 
168
234
    def process_exception(self, request, exception):