Skip to content

Commit

Permalink
PEP-008 compat.
Browse files Browse the repository at this point in the history
  • Loading branch information
apollo13 committed Apr 14, 2011
1 parent 953fb54 commit ad3acd9
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 54 deletions.
1 change: 0 additions & 1 deletion mountain/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from mountain.core import registration

11 changes: 8 additions & 3 deletions mountain/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
from django.contrib import admin, messages
from django.utils.simplejson import dumps

from mountain.core.models import *
from mountain.core.models import Computer, Company, Message
from mountain.core.utils import hash_types


def force_resync(modeladmin, request, queryset):
for comp in queryset:
Message.objects.create(computer=comp, message=dumps({
# TODO: Fix operation-id
'type': 'resynchronize', 'operation-id': 1}))
messages.info(request, 'Queued resyncronisation for the selected computers.')
messages.info(request,
'Queued resyncronisation for the selected computers.')


def confirm_computer(modeladmin, request, queryset):
for comp in queryset:
plugins = comp.company.activated_plugins.values_list('identifier', flat=True).order_by('identifier')
Message.objects.create(computer=comp, message=dumps({
'type': 'registration-done'}))
queryset.update(confirmed=True)
messages.info(request, 'Queued confirmation for the selected computers.')


def set_intervals(modelsamdin, request, queryset):
for comp in queryset:
Message.objects.create(computer=comp, message=dumps({
Expand All @@ -28,11 +31,13 @@ def set_intervals(modelsamdin, request, queryset):
'urgent-exchange': 10}))
messages.info(request, 'Queued set-intervals for the selected computers.')


class ComputerAdmin(admin.ModelAdmin):
actions = [force_resync, confirm_computer, set_intervals]
exclude = ('client_accepted_types_hash',)
readonly_fields = ('confirmed',)


class CompanyAdmin(admin.ModelAdmin):
exclude = ('activated_plugins_hash',)

Expand Down
21 changes: 14 additions & 7 deletions mountain/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from taggit.managers import TaggableManager


class AcceptedTypes(models.Model):
"""This models holds the message types which are supported by this server.
They are selectable through the company model, so each customer can
Expand All @@ -14,14 +15,19 @@ class AcceptedTypes(models.Model):
def __unicode__(self):
return self.identifier


class Company(models.Model):
"""Company is used to define administrator and the activated plugins.
"""
verbose_name = models.CharField(_('verbose name'), max_length=255)
account_name = models.CharField(_('account name'), max_length=255, unique=True)
registration_password = models.CharField(_('registration password'), max_length=255)
administratos = models.ManyToManyField(User, verbose_name=_('administrators'))
activated_plugins = models.ManyToManyField(AcceptedTypes, verbose_name=_('activated plugins'))
account_name = models.CharField(_('account name'), max_length=255,
unique=True)
registration_password = models.CharField(_('registration password'),
max_length=255)
administratos = models.ManyToManyField(User,
verbose_name=_('administrators'))
activated_plugins = models.ManyToManyField(AcceptedTypes,
verbose_name=_('activated plugins'))
activated_plugins_hash = models.CharField(max_length=255)

def __unicode__(self):
Expand All @@ -31,6 +37,7 @@ class Meta:
verbose_name = _('Company')
verbose_name_plural = _('Companies')


class Computer(models.Model):
"""Represents one Computer, information is pulled from the client
registration request. Secure id is used to identify the computer,
Expand All @@ -42,7 +49,8 @@ class Computer(models.Model):
hostname = models.CharField(_('hostname'), max_length=255, unique=True)
# TODO: uniqness of secureid
secure_id = models.TextField(_('secure id'))
insecure_id = models.CharField(_('insecure id'), max_length=36, unique=True)
insecure_id = models.CharField(_('insecure id'), max_length=36,
unique=True)
client_accepted_types_hash = models.CharField(max_length=255)
confirmed = models.BooleanField(_('confirmed'), default=False)

Expand All @@ -59,6 +67,7 @@ class Meta:
verbose_name_plural = _('Computers')
unique_together = ('company', 'computer_title')


class Message(models.Model):
"""Message which is queued and should get send to the client"""
computer = models.ForeignKey(Computer, verbose_name=_('computer'))
Expand All @@ -67,5 +76,3 @@ class Message(models.Model):
class Meta:
verbose_name = _('Message')
verbose_name_plural = _('Messages')


24 changes: 13 additions & 11 deletions mountain/core/registration.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import uuid, string
import string
import uuid
from random import choice

from django.utils.simplejson import dumps

from taggit.utils import parse_tags

from mountain.core.models import Computer, Company, Message
from mountain.core.models import Computer, Company
from mountain.core.signals import message_available
from mountain.core.utils import MessageType, hash_types

CHARS = string.ascii_letters + string.digits + string.punctuation


def handle_registration(sender, computer, request_data, msg_data, **kwargs):
try:
company = Company.objects.get(account_name=msg_data['account_name'])
except Company.DoesNotExist:
return [{'type':'registration', 'info':'unknown-account'}]
return [{'type':'registration', 'info':'unknown-account'}]

if msg_data['registration_password'] != company.registration_password:
return [{'type':'registration', 'info':'unknown-account'}]

comp = Computer()
comp.hostname=msg_data['hostname']
comp.company=company
comp.computer_title=msg_data['computer_title']
comp.secure_id= ''.join([choice(CHARS) for i in range(1600)])
comp.insecure_id=str(uuid.uuid4())
comp.client_accepted_types_hash = hash_types(request_data['client-accepted-types']).encode('hex')
comp.hostname = msg_data['hostname']
comp.company = company
comp.computer_title = msg_data['computer_title']
comp.secure_id = ''.join([choice(CHARS) for i in range(1600)])
comp.insecure_id = str(uuid.uuid4())
comp.client_accepted_types_hash = \
hash_types(request_data['client-accepted-types']).encode('hex')
comp.save()
comp.tags.set(*parse_tags(msg_data['tags']))

return [{'type':'set-id', 'id':comp.secure_id,
'insecure-id':comp.insecure_id}]


message_available.connect(handle_registration, sender=MessageType("register"))
3 changes: 2 additions & 1 deletion mountain/core/signals.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import django.dispatch

message_available = django.dispatch.Signal(providing_args=['computer', 'request_data', 'msg_data'])
message_available = django.dispatch.Signal(providing_args=['computer',
'request_data', 'msg_data'])
13 changes: 8 additions & 5 deletions mountain/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from mountain.core.settings import SERVER_UUID
from mountain.core.models import AcceptedTypes


def render_messages(messages, computer=None, ping_answer=False):
"""Updates the answer with the server-uuid, pickles it and returns the
HttpReponse.
Expand All @@ -16,17 +17,19 @@ def render_messages(messages, computer=None, ping_answer=False):
ret.update({'server-uuid': SERVER_UUID})
if computer:
ret.update({
'client-accepted-types-hash': computer.client_accepted_types_hash.decode('hex'),
'next-expected-sequence': computer.next_client_sequence
})
'client-accepted-types-hash':
computer.client_accepted_types_hash.decode('hex'),
'next-expected-sequence': computer.next_client_sequence})
return HttpResponse(dumps(ret))


def MessageType(type, __instance_cache={}):
"""Use this to register your receiver function to a string.
"""
instance = __instance_cache.setdefault(type, object())
return instance


def hash_types(types):
"""The client only sends the hashed server types, we do the same,
compare them and only send new types if the types difer.
Expand All @@ -35,15 +38,15 @@ def hash_types(types):
m.update(";".join(types))
return m.digest()


def register_messagetype(type):
from django.db.models.signals import post_syncdb
from mountain.core import models

def install_type(sender, app, created_models, verbosity=0, **kwargs):
if verbosity>=1:
if verbosity >= 1:
obj, created = AcceptedTypes.objects.get_or_create(identifier=type)
if created:
print "Installed message type %s" % type

post_syncdb.connect(install_type, sender=models, weak=False)

15 changes: 9 additions & 6 deletions mountain/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

message_logger = logging.getLogger('mountain.messaging')


@csrf_exempt
def ping(request):
id = request.POST.get('insecure_id')
msg_count = Message.objects.filter(computer__insecure_id=id).count()
return render_messages(bool(msg_count), ping_answer=True)


@csrf_exempt
@transaction.commit_on_success
def message_system(request):
Expand All @@ -42,7 +44,8 @@ def message_system(request):
computer = Computer.objects.select_related('company')\
.get(secure_id=secure_id)
company = computer.company
if not all((computer.next_client_sequence, computer.next_server_sequence)):
if not all((computer.next_client_sequence,
computer.next_server_sequence)):
computer.next_client_sequence = data['sequence']
computer.next_server_sequence = data['next-expected-sequence']
computer.save()
Expand All @@ -52,13 +55,13 @@ def message_system(request):
# Special case registration, as we could get a request with nothing,
# and without accepting register, we'll never get a registration.
if computer is not None and computer.confirmed:
accepted_types = company.activated_plugins.values_list('identifier', flat=True).order_by('identifier')
accepted_types = company.activated_plugins.values_list('identifier',
flat=True).order_by('identifier')
accepted_types_hash = company.activated_plugins_hash.decode('hex')
else:
accepted_types = ['register']
accepted_types_hash = hash_types(['register'])


# Check if sequence numbers match
if computer is not None and computer.confirmed and \
((data['sequence'] != computer.next_client_sequence) or
Expand All @@ -67,7 +70,8 @@ def message_system(request):

# Determine whether we need to notify the client about new/delete types
if data.get('accepted-types') != accepted_types_hash:
return_msgs.append({'type':'accepted-types', 'types':list(accepted_types)})
return_msgs.append({'type': 'accepted-types',
'types': list(accepted_types)})

for msg in received_msgs:
if computer is None and msg['type'] != 'register':
Expand Down Expand Up @@ -95,7 +99,6 @@ def message_system(request):
next_client_sequence = \
F('next_client_sequence') + data['total-messages'],
next_server_sequence = \
F('next_server_sequence') + len(return_msgs)
)
F('next_server_sequence') + len(return_msgs))

return render_messages(return_msgs, computer=computer)
13 changes: 10 additions & 3 deletions mountain/monitor/admin.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
from django.contrib import admin

from mountain.monitor.models import ComputerInfo#, ProcessorInfo, HardwarePart
from mountain.monitor.models import ComputerInfo
#, ProcessorInfo, HardwarePart


class ProcessorInfoAdmin(admin.ModelAdmin):
list_display = ('model', 'vendor', 'processor_id', 'cache_size', 'computer')
list_display = ('model', 'vendor', 'processor_id',
'cache_size', 'computer')
list_filter = ('computer',)


class HardwarePartAdmin(admin.ModelAdmin):
list_display = ('product', 'vendor', 'computer')
list_filter = ('computer',)


class ComputerInfoAdmin(admin.ModelAdmin):
list_display = ('hostname', 'code_name', 'description', 'distributor_id', 'release', 'computer')
list_display = ('hostname', 'code_name', 'description',
'distributor_id', 'release', 'computer')


#admin.site.register(ProcessorInfo, ProcessorInfoAdmin)
#admin.site.register(HardwarePart, HardwarePartAdmin)
Expand Down
17 changes: 11 additions & 6 deletions mountain/monitor/computerinfo.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
from mountain.core.signals import message_available
from mountain.core.utils import MessageType, register_messagetype
from mountain.core.utils import MessageType
from mountain.monitor.models import ComputerInfo


def handle_computer_info(sender, computer, request_data, msg_data, **kwargs):
comp_info, created = ComputerInfo.objects.get_or_create(computer=computer)
for i in ['total-memory', 'total-swap', 'hostname']:
setattr(comp_info, i.replace('-','_'), msg_data[i])
setattr(comp_info, i.replace('-', '_'), msg_data[i])
computer.hostname = msg_data['hostname']
comp_info.save()
return []


def handle_distribution_info(sender, computer, request_data, msg_data, **kwargs):
dist_info, created = ComputerInfo.objects.get_or_create(computer=computer)
for i in ['code-name', 'description', 'distributor-id', 'release'] :
setattr(dist_info, i.replace('-','_'), msg_data[i])
for i in ['code-name', 'description', 'distributor-id', 'release']:
setattr(dist_info, i.replace('-', '_'), msg_data[i])
dist_info.save()
return []

message_available.connect(handle_computer_info, sender=MessageType('computer-info'))
message_available.connect(handle_distribution_info, sender=MessageType('distribution-info'))

message_available.connect(handle_computer_info,
sender=MessageType('computer-info'))
message_available.connect(handle_distribution_info,
sender=MessageType('distribution-info'))
3 changes: 2 additions & 1 deletion mountain/monitor/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db import models

from mountain.core.models import Computer

#
#class ProcessorInfo(models.Model):
# computer = models.ForeignKey(Computer)
# processor_id = models.SmallIntegerField()
Expand All @@ -24,6 +24,7 @@
# def __unicode__(self):
# return self.product


class ComputerInfo(models.Model):
computer = models.ForeignKey(Computer)
hostname = models.CharField(max_length=255)
Expand Down
10 changes: 7 additions & 3 deletions mountain/monitor/processorinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
from mountain.core.utils import MessageType
from mountain.monitor.models import ProcessorInfo


def handle_proc_info(sender, computer, request_data, msg_data, **kwargs):
ProcessorInfo.objects.filter(computer=computer).delete()
for p in msg_data['processors']:
p_info = ProcessorInfo(computer=computer, processor_id=p['processor-id'],
p_info = ProcessorInfo(computer=computer,
processor_id=p['processor-id'],
model=p['model'])
for i in ['cache-size', 'vendor']:
if p.get(i):
setattr(p_info, i.replace('-','_'), p.get(i))
setattr(p_info, i.replace('-', '_'), p.get(i))

p_info.save()

return []

message_available.connect(handle_proc_info, sender=MessageType('processor-info'))

message_available.connect(handle_proc_info,
sender=MessageType('processor-info'))
1 change: 1 addition & 0 deletions mountain/packages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
(3, 'locked'),
)


class PackageManager(models.Manager):
def hashes_in_bulk(self, id_list):
qs = self.model.objects.filter(hash__in=id_list)
Expand Down
Loading

0 comments on commit ad3acd9

Please sign in to comment.