Commit 78847db4 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Introduce per-user allocation of VMs to backends

Add BACKEND_PER_USER setting, a mapping from users to backends. This setting
can by used in order to bypass the default backend allocation process, and
allocate all the new VMs of a specific user to a specific Backend.
parent 5e0168c5
......@@ -257,7 +257,7 @@ def create_server(request):
raise faults.OverLimit("Server count limit exceeded for your account.")
backend_allocator = BackendAllocator()
backend = backend_allocator.allocate(flavor)
backend = backend_allocator.allocate(request.user_uniq, flavor)
if backend is None:
log.error("No available backends for VM with flavor %s", flavor)
......
......@@ -98,6 +98,10 @@ APPEND_SLASH = False
#
IGNORE_FLAVOR_DISK_SIZES = False
# Fixed mapping of user VMs to a specific backend.
# e.g. BACKEND_PER_USER = {'example@okeanos.grnet.gr': 2}
BACKEND_PER_USER = {}
# Quota
#
# Maximum number of VMs a user is allowed to have.
......
......@@ -31,7 +31,8 @@ import logging
import datetime
from django.utils import importlib
from synnefo import settings
from synnefo.settings import (BACKEND_ALLOCATOR_MODULE, BACKEND_REFRESH_MIN,
BACKEND_PER_USER)
from synnefo.db.models import Backend
from synnefo.logic.backend import update_resources
from synnefo.api.util import backend_public_networks
......@@ -45,9 +46,9 @@ class BackendAllocator():
"""
def __init__(self):
self.strategy_mod =\
importlib.import_module(settings.BACKEND_ALLOCATOR_MODULE)
importlib.import_module(BACKEND_ALLOCATOR_MODULE)
def allocate(self, flavor):
def allocate(self, userid, flavor):
"""Allocate a vm of the specified flavor to a backend.
Warning!!: An explicit commit is required after calling this function,
......@@ -55,6 +56,12 @@ class BackendAllocator():
function.
"""
backend = None
backend = get_backend_for_user(userid)
if backend:
return backend
# Get the size of the vm
disk = flavor_disk(flavor)
ram = flavor.ram
......@@ -138,9 +145,30 @@ def refresh_backends_stats(backends):
"""
now = datetime.datetime.now()
delta = datetime.timedelta(minutes=settings.BACKEND_REFRESH_MIN)
delta = datetime.timedelta(minutes=BACKEND_REFRESH_MIN)
for b in backends:
if now > b.updated + delta:
log.debug("Updating resources of backend %r. Last Updated %r",
b, b.updated)
update_resources(b)
def get_backend_for_user(userid):
"""Find fixed Backend for user based on BACKEND_PER_USER setting."""
backend = BACKEND_PER_USER.get(userid)
if not backend:
return None
try:
try:
backend_id = int(backend)
return Backend.objects.get(id=backend_id)
except ValueError:
pass
backend_name = str(backend)
return Backend.objects.get(clustername=backend_name)
except Backend.DoesNotExist:
log.error("Invalid backend %s for user %s", backend, userid)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment