Revision 5dd9d123

b/snf-cyclades-app/synnefo/api/servers.py
257 257
            raise faults.OverLimit("Server count limit exceeded for your account.")
258 258

  
259 259
        backend_allocator = BackendAllocator()
260
        backend = backend_allocator.allocate(flavor)
260
        backend = backend_allocator.allocate(request.user_uniq, flavor)
261 261

  
262 262
        if backend is None:
263 263
            log.error("No available backends for VM with flavor %s", flavor)
b/snf-cyclades-app/synnefo/app_settings/default/api.py
98 98
#
99 99
IGNORE_FLAVOR_DISK_SIZES = False
100 100

  
101
# Fixed mapping of user VMs to a specific backend.
102
# e.g. BACKEND_PER_USER = {'example@okeanos.grnet.gr': 2}
103
BACKEND_PER_USER = {}
104

  
101 105
# Quota
102 106
#
103 107
# Maximum number of VMs a user is allowed to have.
b/snf-cyclades-app/synnefo/logic/backend_allocator.py
31 31
import datetime
32 32
from django.utils import importlib
33 33

  
34
from synnefo import settings
34
from synnefo.settings import (BACKEND_ALLOCATOR_MODULE, BACKEND_REFRESH_MIN,
35
                              BACKEND_PER_USER)
35 36
from synnefo.db.models import Backend
36 37
from synnefo.logic.backend import update_resources
37 38
from synnefo.api.util import backend_public_networks
......
45 46
    """
46 47
    def __init__(self):
47 48
        self.strategy_mod =\
48
            importlib.import_module(settings.BACKEND_ALLOCATOR_MODULE)
49
            importlib.import_module(BACKEND_ALLOCATOR_MODULE)
49 50

  
50
    def allocate(self, flavor):
51
    def allocate(self, userid, flavor):
51 52
        """Allocate a vm of the specified flavor to a backend.
52 53

  
53 54
        Warning!!: An explicit commit is required after calling this function,
......
55 56
        function.
56 57

  
57 58
        """
59

  
60
        backend = None
61
        backend = get_backend_for_user(userid)
62
        if backend:
63
            return backend
64

  
58 65
        # Get the size of the vm
59 66
        disk = flavor_disk(flavor)
60 67
        ram = flavor.ram
......
138 145
    """
139 146

  
140 147
    now = datetime.datetime.now()
141
    delta = datetime.timedelta(minutes=settings.BACKEND_REFRESH_MIN)
148
    delta = datetime.timedelta(minutes=BACKEND_REFRESH_MIN)
142 149
    for b in backends:
143 150
        if now > b.updated + delta:
144 151
            log.debug("Updating resources of backend %r. Last Updated %r",
145 152
                      b, b.updated)
146 153
            update_resources(b)
154

  
155

  
156
def get_backend_for_user(userid):
157
    """Find fixed Backend for user based on BACKEND_PER_USER setting."""
158

  
159
    backend = BACKEND_PER_USER.get(userid)
160

  
161
    if not backend:
162
        return None
163

  
164
    try:
165
        try:
166
            backend_id = int(backend)
167
            return Backend.objects.get(id=backend_id)
168
        except ValueError:
169
            pass
170

  
171
        backend_name = str(backend)
172
        return Backend.objects.get(clustername=backend_name)
173
    except Backend.DoesNotExist:
174
        log.error("Invalid backend %s for user %s", backend, userid)

Also available in: Unified diff