Added "uuid" field to UserService so we set our own global unique

identifier for this service.
This will be used by uds actor
There is another field, unique_id, that is provided by ServiceProviders,
and that we do not control in any way it's format. This is used, for
example, by actor to locate it's own "uuid"
This commit is contained in:
Adolfo Gómez García 2014-09-16 02:38:40 +02:00
parent bbe2b7d1c2
commit fba0bf8631
5 changed files with 100 additions and 5 deletions

View File

@ -35,9 +35,12 @@ from __future__ import unicode_literals
from django.utils.translation import ugettext as _
from uds.core.util import Config
from uds.core.util.State import State
from uds.core.util import log
from uds.core.managers import cryptoManager
from uds.REST import Handler, AccessDenied, RequestError
from uds.models import UserService
import datetime
@ -79,6 +82,29 @@ class Actor(Handler):
if self._args[0] != actorKey.get(True):
raise AccessDenied('Invalid actor key')
def processRequest(self, clientIds, message, data):
logger.debug("Called message for id_ {0}, message \"{1}\" and data \"{2}\"".format(clientIds, message, data))
res = ""
try:
services = UserService.objects.filter(unique_id__in=clientIds, state__in=[State.USABLE, State.PREPARING])
if services.count() == 0:
res = ""
else:
inUse = services[0].in_use
res = services[0].getInstance().osmanager().process(services[0], message, data)
services = UserService.objects.filter(unique_id__in=clientIds, state__in=[State.USABLE, State.PREPARING])
if services.count() > 0 and services[0].in_use != inUse: # If state changed, log it
type_ = inUse and 'login' or 'logout'
uniqueId = services[0].unique_id
serviceIp = ''
username = ''
log.useLog(type_, uniqueId, serviceIp, username)
except Exception as e:
logger.error("Exception at message (client): {0}".format(e))
res = ""
logger.debug("Returning {0}".format(res))
return res
def get(self):
'''
Processes get requests
@ -98,7 +124,7 @@ class Actor(Handler):
except Exception:
data = ''
return clientIds, message, data
return self.processRequest(clientIds, message, data)
def post(self):
'''
@ -110,4 +136,4 @@ class Actor(Handler):
data = self._params[0]
return clientIds, message, data
return self.processRequest(clientIds, message, data)

View File

@ -39,6 +39,7 @@ from Crypto.Random import atfork
import hashlib
import array
import uuid
import datetime
import logging
@ -61,6 +62,7 @@ class CryptoManager(object):
def __init__(self):
self._rsa = RSA.importKey(settings.RSA_KEY)
self._namespace = uuid.UUID('627a37a5-e8db-431a-b783-73f7d20b4934')
self._counter = 0
@staticmethod
def manager():
@ -123,11 +125,14 @@ class CryptoManager(object):
return six.text_type(hashlib.sha1(value).hexdigest())
def uuid(self, obj):
def uuid(self, obj=None):
'''
Generates an uuid from obj.
Right now, obj must be an string
If obj is None, returns an uuid based on current datetime + counter
'''
if obj is None:
obj = six.text_type(datetime.datetime.now()) + six.text_type(self._counter)
self._counter += 1
if isinstance(obj, six.text_type):
obj = obj.decode('utf-8')

View File

@ -0,0 +1,14 @@
'''
Created on Sep 15, 2014
@author: dkmaster
'''
from __future__ import unicode_literals
from uds.core.managers import cryptoManager
def generateUuid():
'''
Generates a ramdom uuid for models default
'''
return cryptoManager().uuid().upper()

View File

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
from uds.core.util.model import generateUuid
def add_uuids(apps, schema_editor):
# We can't import the Person model directly as it may be a newer
# version than this migration expects. We use the historical version.
UserService = apps.get_model("uds", "UserService")
for us in UserService.objects.all():
us.uuid = generateUuid()
us.save()
def remove_uuids(apps, schema_editor):
pass
class Migration(migrations.Migration):
dependencies = [
('uds', '0002_auto_20140908_1344'),
]
operations = [
migrations.AddField(
model_name='userservice',
name='uuid',
field=models.CharField(default=None, max_length=36, unique=True, null=True),
preserve_default=True,
),
migrations.RunPython(
add_uuids,
remove_uuids
),
]

View File

@ -33,13 +33,14 @@
from __future__ import unicode_literals
__updated__ = '2014-04-24'
__updated__ = '2014-09-16'
from django.db import models
from django.db.models import signals
from uds.core.Environment import Environment
from uds.core.util import log
from uds.core.util.State import State
from uds.core.util.model import generateUuid
from uds.models.ServicesPool import DeployedService
from uds.models.ServicesPoolPublication import DeployedServicePublication
@ -80,6 +81,8 @@ class UserService(models.Model):
src_hostname = models.CharField(max_length=64, default='')
src_ip = models.CharField(max_length=15, default='')
uuid = models.CharField(max_length=36, default=None, null=True, unique=True)
cluster_node = models.CharField(max_length=128, default=None, blank=True, null=True, db_index=True)
# objects = LockingManager() This model is on an innoDb table, so we do not need the locking manager anymore
@ -92,6 +95,14 @@ class UserService(models.Model):
ordering = ('creation_date',)
app_label = 'uds'
# Override default save to add uuid
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
if self.uuid is None:
self.uuid = generateUuid()
return models.Model.save(self, force_insert=force_insert,
force_update=force_update, using=using,
update_fields=update_fields)
def getEnvironment(self):
'''
Returns an environment valid for the record this object represents.