def delete_from_voldrv(name, storagedriver_id): """ This method will delete a vmachine based on the name of the vmx given """ pmachine = PMachineList.get_by_storagedriver_id(storagedriver_id) if pmachine.hvtype not in ['VMWARE', 'KVM']: return hypervisor = Factory.get(pmachine) name = hypervisor.clean_vmachine_filename(name) if pmachine.hvtype == 'VMWARE': storagedriver = StorageDriverList.get_by_storagedriver_id( storagedriver_id) vpool = storagedriver.vpool else: vpool = None vm = VMachineList.get_by_devicename_and_vpool(name, vpool) if vm is not None: MessageController.fire(MessageController.Type.EVENT, { 'type': 'vmachine_deleted', 'metadata': { 'name': vm.name } }) vm.delete(abandon=['vdisks'])
def task_postrun_handler(sender=None, task_id=None, task=None, args=None, kwargs=None, **kwds): """ Hook for celery postrun event """ _ = sender, task, args, kwargs, kwds try: MessageController.fire(MessageController.Type.TASK_COMPLETE, task_id) except Exception as ex: loghandler.error('Caught error during postrun handler: {0}'.format(ex))
def task_postrun_handler(sender=None, task_id=None, task=None, args=None, kwargs=None, **kwds): """ Hook for celery postrun event """ _ = sender, task, args, kwargs, kwds try: MessageController.fire(MessageController.Type.TASK_COMPLETE, task_id) except Exception as ex: loghandler = LogHandler.get('celery', name='celery') loghandler.error('Caught error during postrun handler: {0}'.format(ex))
def task_postrun_handler(sender=None, task_id=None, task=None, args=None, kwargs=None, **kwds): """ Hook for celery postrun event """ _ = sender, task, args, kwargs, kwds MessageController.fire(MessageController.Type.TASK_COMPLETE, task_id)
def task_postrun_handler(sender=None, task_id=None, task=None, args=None, kwargs=None, **kwds): """ Hook for celery post-run event """ _ = sender, task, args, kwargs, kwds try: MessageController.fire(MessageController.Type.TASK_COMPLETE, task_id) except Exception: ovs_logger.exception('Caught error during post-run handler')
def _wait(subscriber_id, message_id): messages = [] last_message_id = 0 counter = 0 while len(messages) == 0: messages, last_message_id = MessageController.get_messages(subscriber_id, message_id) if len(messages) == 0: counter += 1 if counter >= 120: # 120 * 0.5 seconds = 60 seconds = 1 minute break gevent.sleep(.5) if len(messages) == 0: last_message_id = MessageController.last_message_id() MessageController.reset_subscriptions(subscriber_id) return messages, last_message_id
def subscribe(self, request, pk): """ Subscribes a subscriber to a set of types """ try: pk = int(pk) subscriptions = request.DATA cleaned_subscriptions = [] if not isinstance(subscriptions, list): raise TypeError for s in subscriptions: if str(s) in MessageController.Type.ALL: cleaned_subscriptions.append(str(s)) except (ValueError, TypeError): return Response(status=status.HTTP_400_BAD_REQUEST) MessageController.subscribe(pk, cleaned_subscriptions) return Response(cleaned_subscriptions, status=status.HTTP_200_OK)
def last(self, pk): """ Get the last messageid """ try: _ = int(pk) except (ValueError, TypeError): return Response(status=status.HTTP_400_BAD_REQUEST) return Response(MessageController.last_message_id(), status=status.HTTP_200_OK)
def retrieve(self, pk): """ Retrieves the subscriptions for a given subscriber """ try: pk = int(pk) except (ValueError, TypeError): return Response(status=status.HTTP_400_BAD_REQUEST) return Response(MessageController.subscriptions(pk), status=status.HTTP_200_OK)
def last(self, pk): """ Get the last messageid :param pk: Primary key of subscriber :type pk: int """ try: _ = int(pk) except (ValueError, TypeError): return Response(status=status.HTTP_400_BAD_REQUEST) return Response(MessageController.last_message_id(), status=status.HTTP_200_OK)
def delete_from_voldrv(name, storagedriver_id): """ This method will delete a vmachine based on the name of the vmx given """ pmachine = PMachineList.get_by_storagedriver_id(storagedriver_id) if pmachine.hvtype not in ['VMWARE', 'KVM']: return hypervisor = Factory.get(pmachine) name = hypervisor.clean_vmachine_filename(name) if pmachine.hvtype == 'VMWARE': storagedriver = StorageDriverList.get_by_storagedriver_id(storagedriver_id) vpool = storagedriver.vpool else: vpool = None vm = VMachineList.get_by_devicename_and_vpool(name, vpool) if vm is not None: MessageController.fire(MessageController.Type.EVENT, {'type': 'vmachine_deleted', 'metadata': {'name': vm.name}}) vm.delete(abandon=['vdisks'])
def wait(self, pk, message_id): """ Wait for messages to appear for a given subscriber """ try: pk = int(pk) message_id = int(message_id) except (ValueError, TypeError): return Response(status=status.HTTP_400_BAD_REQUEST) thread = gevent.spawn(MessagingViewSet._wait, pk, message_id) gevent.joinall([thread]) messages, last_message_id = thread.value return Response({'messages' : messages, 'last_message_id': last_message_id, 'subscriptions' : MessageController.subscriptions(pk)}, status=status.HTTP_200_OK)
def wait(self, pk, message_id): """ Wait for messages to appear for a given subscriber :param pk: Primary key of subscriber :type pk: int :param message_id: The last message_id that was already received :type message_id: str """ try: pk = int(pk) message_id = int(message_id) except (ValueError, TypeError): return Response(status=status.HTTP_400_BAD_REQUEST) thread = gevent.spawn(MessagingViewSet._wait, pk, message_id) gevent.joinall([thread]) result = thread.value if result is None: messages = [] last_message_id = message_id else: messages, last_message_id = thread.value return Response({'messages': messages, 'last_message_id': last_message_id, 'subscriptions': MessageController.subscriptions(pk)}, status=status.HTTP_200_OK)
def list(self): """ Provides a list of subscriptions """ return Response(MessageController.all_subscriptions(), status=status.HTTP_200_OK)
def update_vmachine_config(vmachine, vm_object, pmachine=None): """ Update a vMachine configuration with a given vMachine configuration """ try: vdisks_synced = 0 if vmachine.name is None: MessageController.fire( MessageController.Type.EVENT, { 'type': 'vmachine_created', 'metadata': { 'name': vm_object['name'] } }) elif vmachine.name != vm_object['name']: MessageController.fire( MessageController.Type.EVENT, { 'type': 'vmachine_renamed', 'metadata': { 'old_name': vmachine.name, 'new_name': vm_object['name'] } }) if pmachine is not None: vmachine.pmachine = pmachine vmachine.name = vm_object['name'] vmachine.hypervisor_id = vm_object['id'] vmachine.devicename = vm_object['backing']['filename'] vmachine.save() # Updating and linking disks storagedrivers = StorageDriverList.get_storagedrivers() datastores = dict([('{}:{}'.format(storagedriver.storage_ip, storagedriver.mountpoint), storagedriver) for storagedriver in storagedrivers]) vdisk_guids = [] for disk in vm_object['disks']: if disk['datastore'] in vm_object['datastores']: datastore = vm_object['datastores'][disk['datastore']] if datastore in datastores: vdisk = VDiskList.get_by_devicename_and_vpool( disk['filename'], datastores[datastore].vpool) if vdisk is None: # The disk couldn't be located, but is in our datastore. We might be in a recovery scenario vdisk = VDisk() vdisk.vpool = datastores[datastore].vpool vdisk.reload_client() vdisk.devicename = disk['filename'] vdisk.volume_id = vdisk.storagedriver_client.get_volume_id( str(disk['backingfilename'])) vdisk.size = vdisk.info['volume_size'] MDSServiceController.ensure_safety(vdisk) # Update the disk with information from the hypervisor if vdisk.vmachine is None: MessageController.fire( MessageController.Type.EVENT, { 'type': 'vdisk_attached', 'metadata': { 'vmachine_name': vmachine.name, 'vdisk_name': disk['name'] } }) vdisk.vmachine = vmachine vdisk.name = disk['name'] vdisk.order = disk['order'] vdisk.save() vdisk_guids.append(vdisk.guid) vdisks_synced += 1 for vdisk in vmachine.vdisks: if vdisk.guid not in vdisk_guids: MessageController.fire( MessageController.Type.EVENT, { 'type': 'vdisk_detached', 'metadata': { 'vmachine_name': vmachine.name, 'vdisk_name': vdisk.name } }) vdisk.vmachine = None vdisk.save() logger.info( 'Updating vMachine finished (name {}, {} vdisks (re)linked)'. format(vmachine.name, vdisks_synced)) except Exception as ex: logger.info('Error during vMachine update: {0}'.format(str(ex))) raise
def update_vmachine_config(vmachine, vm_object, pmachine=None): """ Update a vMachine configuration with a given vMachine configuration """ try: vdisks_synced = 0 if vmachine.name is None: MessageController.fire(MessageController.Type.EVENT, {'type': 'vmachine_created', 'metadata': {'name': vm_object['name']}}) elif vmachine.name != vm_object['name']: MessageController.fire(MessageController.Type.EVENT, {'type': 'vmachine_renamed', 'metadata': {'old_name': vmachine.name, 'new_name': vm_object['name']}}) if pmachine is not None: vmachine.pmachine = pmachine vmachine.name = vm_object['name'] vmachine.hypervisor_id = vm_object['id'] vmachine.devicename = vm_object['backing']['filename'] vmachine.save() # Updating and linking disks storagedrivers = StorageDriverList.get_storagedrivers() datastores = dict([('{}:{}'.format(storagedriver.storage_ip, storagedriver.mountpoint), storagedriver) for storagedriver in storagedrivers]) vdisk_guids = [] for disk in vm_object['disks']: if disk['datastore'] in vm_object['datastores']: datastore = vm_object['datastores'][disk['datastore']] if datastore in datastores: vdisk = VDiskList.get_by_devicename_and_vpool(disk['filename'], datastores[datastore].vpool) if vdisk is None: # The disk couldn't be located, but is in our datastore. We might be in a recovery scenario vdisk = VDisk() vdisk.vpool = datastores[datastore].vpool vdisk.reload_client() vdisk.devicename = disk['filename'] vdisk.volume_id = vdisk.storagedriver_client.get_volume_id(str(disk['backingfilename'])) vdisk.size = vdisk.info['volume_size'] # Update the disk with information from the hypervisor if vdisk.vmachine is None: MessageController.fire(MessageController.Type.EVENT, {'type': 'vdisk_attached', 'metadata': {'vmachine_name': vmachine.name, 'vdisk_name': disk['name']}}) vdisk.vmachine = vmachine vdisk.name = disk['name'] vdisk.order = disk['order'] vdisk.save() vdisk_guids.append(vdisk.guid) vdisks_synced += 1 for vdisk in vmachine.vdisks: if vdisk.guid not in vdisk_guids: MessageController.fire(MessageController.Type.EVENT, {'type': 'vdisk_detached', 'metadata': {'vmachine_name': vmachine.name, 'vdisk_name': vdisk.name}}) vdisk.vmachine = None vdisk.save() logger.info('Updating vMachine finished (name {}, {} vdisks (re)linked)'.format( vmachine.name, vdisks_synced )) except Exception as ex: logger.info('Error during vMachine update: {0}'.format(str(ex))) raise
def update_vmachine_config(vmachine, vm_object, pmachine=None): """ Update a vMachine configuration with a given vMachine configuration :param vmachine: Virtual Machine to update :param vm_object: New virtual machine info :param pmachine: Physical machine of the virtual machine """ try: vdisks_synced = 0 if vmachine.name is None: MessageController.fire(MessageController.Type.EVENT, {'type': 'vmachine_created', 'metadata': {'name': vm_object['name']}}) elif vmachine.name != vm_object['name']: MessageController.fire(MessageController.Type.EVENT, {'type': 'vmachine_renamed', 'metadata': {'old_name': vmachine.name, 'new_name': vm_object['name']}}) if pmachine is not None: vmachine.pmachine = pmachine vmachine.name = vm_object['name'] vmachine.hypervisor_id = vm_object['id'] vmachine.devicename = vm_object['backing']['filename'] vmachine.save() # Updating and linking disks storagedrivers = StorageDriverList.get_storagedrivers() datastores = dict([('{0}:{1}'.format(storagedriver.storage_ip, storagedriver.mountpoint), storagedriver) for storagedriver in storagedrivers]) vdisk_guids = [] mutex = volatile_mutex('{0}_{1}'.format(vmachine.name, vmachine.devicename)) for disk in vm_object['disks']: ensure_safety = False if disk['datastore'] in vm_object['datastores']: datastore = vm_object['datastores'][disk['datastore']] if datastore in datastores: try: mutex.acquire(wait=10) vdisk = VDiskList.get_by_devicename_and_vpool(disk['filename'], datastores[datastore].vpool) if vdisk is None: # The disk couldn't be located, but is in our datastore. We might be in a recovery scenario vdisk = VDisk() vdisk.vpool = datastores[datastore].vpool vdisk.reload_client() vdisk.devicename = disk['filename'] vdisk.volume_id = vdisk.storagedriver_client.get_volume_id(str(disk['backingfilename'])) vdisk.size = vdisk.info['volume_size'] vdisk.metadata = {'lba_size': vdisk.info['lba_size'], 'cluster_multiplier': vdisk.info['cluster_multiplier']} # Create the disk in a locked context, but don't execute long running-task in same context vdisk.save() ensure_safety = True finally: mutex.release() if ensure_safety: MDSServiceController.ensure_safety(vdisk) VDiskController.dtl_checkup(vdisk_guid=vdisk.guid) # Update the disk with information from the hypervisor if vdisk.vmachine is None: MessageController.fire(MessageController.Type.EVENT, {'type': 'vdisk_attached', 'metadata': {'vmachine_name': vmachine.name, 'vdisk_name': disk['name']}}) vdisk.vmachine = vmachine vdisk.name = disk['name'] vdisk.order = disk['order'] vdisk.save() vdisk_guids.append(vdisk.guid) vdisks_synced += 1 for vdisk in vmachine.vdisks: if vdisk.guid not in vdisk_guids: MessageController.fire(MessageController.Type.EVENT, {'type': 'vdisk_detached', 'metadata': {'vmachine_name': vmachine.name, 'vdisk_name': vdisk.name}}) vdisk.vmachine = None vdisk.save() VMachineController._logger.info('Updating vMachine finished (name {0}, {1} vdisks (re)linked)'.format( vmachine.name, vdisks_synced )) except Exception as ex: VMachineController._logger.info('Error during vMachine update: {0}'.format(str(ex))) raise