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'}] 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.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"))
from mountain.core.signals import message_available 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'], model=p['model']) for i in ['cache-size', 'vendor']: if 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'))
def handle_packages(sender, computer, request_data, msg_data, **kwargs): for status in ('installed', 'available', 'available-upgrades', 'locked'): ids = [] remove_ids = [] for remove in (False, True): s = status if not remove else 'not-%s' % status for id_or_range in msg_data.get(s, []): ids_ = ids if not remove else remove_ids if isinstance(id_or_range, tuple): ids_.extend(range(id_or_range[0], id_or_range[1] + 1)) else: ids_.append(id_or_range) for id in ids: PackageStatus.objects.create(computer=computer, package_id=id, status=STATUS_TO_ID[status]) if remove_ids: PackageStatus.objects.delete(computer=computer, package_id__in=remove_ids) return [] message_available.connect(handle_unknown_hashes, sender=MessageType('unknown-package-hashes')) message_available.connect(handle_add_packages, sender=MessageType('add-packages')) message_available.connect(handle_packages, sender=MessageType('packages'))
from mountain.core.signals import message_available 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]) 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]) dist_info.save() return [] message_available.connect(handle_computer_info, sender=MessageType('computer-info')) message_available.connect(handle_distribution_info, sender=MessageType('distribution-info'))