def delete_virtual_compute(model, event): if not ICompute.providedBy(model.__parent__.__parent__): return if IDeployed.providedBy(model): log.msg('Deleting compute %s which is in IDeployed state, shutting down and ' 'undeploying first' % model.hostname, system='compute-backend') yield DestroyComputeAction(model).execute(DetachedProtocol(), object()) yield UndeployAction(model).execute(DetachedProtocol(), object()) else: log.msg('Deleting compute %s which is already in IUndeployed state' % model.hostname, system='compute-backend') owner = (yield db.get(model, '__owner__')) ulog = UserLogger(subject=model, owner=owner) ulog.log('Deleted %s' % model) @db.transact def deallocate_ip(): ippools = db.get_root()['oms_root']['ippools'] ip = netaddr.IPAddress(model.ipv4_address.split('/')[0]) if ippools.free(ip): ulog.log('Deallocated IP: %s', ip) yield deallocate_ip()
def handle_ownership_change(model, event): if model.__parent__ is None: log.msg( 'Ownership change event handler suppressed: model is not attached to a parent yet', system='ownership-change-event') return msg = 'Compute "%s" owner changed from "%s" to "%s"' % ( model, event.oldowner, event.nextowner) oldowner = getUtility(IAuthentication).getPrincipal(event.oldowner) newowner = getUtility(IAuthentication).getPrincipal(event.nextowner) oldulog = UserLogger(subject=model, owner=oldowner) newulog = UserLogger(subject=model, owner=newowner) log.msg(msg, system='ownership-change-event') oldulog.log(msg) newulog.log(msg) try: submitter = IVirtualizationContainerSubmitter(model.__parent__) blocking_yield( submitter.submit(ISetOwner, model.__name__, model.__owner__)) except Exception: log.err(system='ownership-change-event') raise curtransaction = transaction.get() curtransaction.addAfterCommitHook(update_statistics_dbhook, args=([event.oldowner, event.nextowner], ))
def delete_virtual_compute(model, event): if not ICompute.providedBy(model.__parent__.__parent__): return if IDeployed.providedBy(model): log.msg( 'Deleting compute %s which is in IDeployed state, shutting down and ' 'undeploying first' % model.hostname, system='compute-backend') yield DestroyComputeAction(model).execute(DetachedProtocol(), object()) yield UndeployAction(model).execute(DetachedProtocol(), object()) else: log.msg('Deleting compute %s which is already in IUndeployed state' % model.hostname, system='compute-backend') owner = (yield db.get(model, '__owner__')) ulog = UserLogger(subject=model, owner=owner) ulog.log('Deleted %s' % model) @db.transact def deallocate_ip(): ippools = db.get_root()['oms_root']['ippools'] ip = netaddr.IPAddress(model.ipv4_address.split('/')[0]) if ippools.free(ip): ulog.log('Deallocated IP: %s', ip) yield deallocate_ip()
def add_log_event(self, cmd, msg, *args, **kwargs): self._action_log(cmd, msg) owner = yield db.get(self.context, '__owner__') ulog = UserLogger( principal=cmd.protocol.interaction.participations[0].principal, subject=self.context, owner=owner) ulog.log(msg, *args, **kwargs)
def allocate_ip_address(): ippools = db.get_root()['oms_root']['ippools'] ip = ippools.allocate() if ip is not None: self._action_log(cmd, 'Allocated IP: %s for %s' % (ip, self.context), system='deploy') ulog = UserLogger(principal=cmd.protocol.interaction.participations[0].principal, subject=self.context, owner=self.context.__owner__) ulog.log('Allocated IP for %s: %s' % (self.context, ip)) return ip else: raise Exception('Could not allocate IP for the new compute: pools exhausted or undefined')
def finalize_vm(): ippools = db.get_root()['oms_root']['ippools'] ip = netaddr.IPAddress(self.context.ipv4_address.split('/')[0]) if ippools.free(ip): ulog = UserLogger(principal=cmd.protocol.interaction.participations[0].principal, subject=self.context, owner=self.context.__owner__) ulog.log('Deallocated IP: %s', ip) vm = traverse1(canonical_path(self.context)) if vm is not None: noLongerProvides(vm, IDeployed) alsoProvides(vm, IUndeployed)
def finalize_vm(): ippools = db.get_root()['oms_root']['ippools'] ip = netaddr.IPAddress(self.context.ipv4_address.split('/')[0]) log.msg('Attempting to deallocate IP %s from the pools' % ip, system='undeploy-action') if ippools.free(ip): ulog = UserLogger(principal=cmd.protocol.interaction. participations[0].principal, subject=self.context, owner=self.context.__owner__) ulog.log('Deallocated IP: %s', ip) log.msg('Deallocated IP %s' % ip, system='ippool') vm = traverse1(canonical_path(self.context)) if vm is not None: noLongerProvides(vm, IDeployed) alsoProvides(vm, IUndeployed)
def allocate_virtual_compute_from_hangar(model, event): if not IVirtualizationContainer.providedBy(model.__parent__): return if IDeployed.providedBy(model): return auto_allocate = get_config().getboolean('vms', 'auto_allocate', True) if not auto_allocate: return if IHangar.providedBy(model.__parent__.__parent__): action = AllocateAction msg = 'Allocated compute %s' elif ICompute.providedBy(model.__parent__.__parent__): action = DeployAction msg = 'Deployed compute %s' else: return try: path = canonical_path(model) owner = model.__owner__ ul = UserLogger(subject=model, owner=owner) log.msg('Attempting %s for %s (%s, %s)' % (action.__name__, model, path, owner), system='create-event') d = task.deferLater(reactor, 2.0, virtual_compute_action, action, path, event) d.addCallback(lambda r: ul.log(msg % path)) if not get_config().getboolean('stats', 'only_report_on_sync', True): d.addCallback(lambda r: defer.maybeDeferred(getUtility(IUserStatisticsProvider).update, owner)) d.addErrback(log.err) except Exception: log.err(system='create-event')
def allocate_ip_address(): ippools = db.get_root()['oms_root']['ippools'] ip = ippools.allocate() if ip is not None: self._action_log(cmd, 'Allocated IP: %s for %s' % (ip, self.context), system='deploy') ulog = UserLogger(principal=cmd.protocol.interaction. participations[0].principal, subject=self.context, owner=self.context.__owner__) ulog.log('Allocated IP for %s: %s' % (self.context, ip)) return ip else: raise Exception( 'Could not allocate IP for the new compute: pools exhausted or undefined' )
def handle_compute_state_change_request(compute, event): if not event.modified.get('state', None): return original = event.original['state'] modified = event.modified['state'] if original == modified: return owner = compute.__owner__ ulog = UserLogger() ulog.log('Changed state of %s (%s): %s -> %s' % (compute, owner, original, modified)) log.msg('Changed state of %s (%s): %s -> %s' % (compute, owner, original, modified), system='state-change') curtransaction = transaction.get() curtransaction.addAfterCommitHook(update_statistics_dbhook, args=([owner],))
def handle_ownership_change(model, event): msg = 'Compute "%s" owner changed from "%s" to "%s"' % (model, event.oldowner, event.nextoner) oldowner = getUtility(IAuthentication).getPrincipal(event.oldowner) newowner = getUtility(IAuthentication).getPrincipal(event.nextowner) oldulog = UserLogger(subject=model, owner=oldowner) newulog = UserLogger(subject=model, owner=newowner) log.msg(msg, system='ownership-change-event') oldulog.log(msg) newulog.log(msg) try: submitter = IVirtualizationContainerSubmitter(model.__parent__) blocking_yield(submitter.submit(ISetOwner, model.__name__, model.__owner__)) except Exception: log.err(system='ownership-change-event') raise curtransaction = transaction.get() curtransaction.addAfterCommitHook(update_statistics_dbhook, args=([event.oldowner, event.nextowner],))
def handle_compute_state_change_request(compute, event): if not event.modified.get('state', None): return original = event.original['state'] modified = event.modified['state'] if original == modified: return owner = compute.__owner__ ulog = UserLogger() ulog.log('Changed state of %s (%s): %s -> %s' % (compute, owner, original, modified)) log.msg('Changed state of %s (%s): %s -> %s' % (compute, owner, original, modified), system='state-change') curtransaction = transaction.get() curtransaction.addAfterCommitHook(update_statistics_dbhook, args=([owner], ))
def allocate_virtual_compute_from_hangar(model, event): if not IVirtualizationContainer.providedBy(model.__parent__): return if IDeployed.providedBy(model): return auto_allocate = get_config().getboolean('vms', 'auto_allocate', True) if not auto_allocate: return if IHangar.providedBy(model.__parent__.__parent__): action = AllocateAction msg = 'Allocated compute %s' elif ICompute.providedBy(model.__parent__.__parent__): action = DeployAction msg = 'Deployed compute %s' else: return try: path = canonical_path(model) owner = model.__owner__ ul = UserLogger(subject=model, owner=owner) log.msg('Attempting %s for %s (%s, %s)' % (action.__name__, model, path, owner), system='create-event') d = task.deferLater(reactor, 2.0, virtual_compute_action, action, path, event) d.addCallback(lambda r: ul.log(msg % path)) if not get_config().getboolean('stats', 'only_report_on_sync', True): d.addCallback(lambda r: defer.maybeDeferred( getUtility(IUserStatisticsProvider).update, owner)) d.addErrback(log.err) except Exception: log.err(system='create-event')
def add_log_event(self, principal, msg, *args, **kwargs): owner = self.context.__owner__ ulog = UserLogger(principal=principal, subject=self.context, owner=owner) ulog.log(msg, *args, **kwargs)
def __init__(self, proto, chainedProtocol, avatar, width, height): TerminalSessionTransport.__init__(self, proto, chainedProtocol, avatar, width, height) chainedProtocol.terminalProtocol.logged_in(avatar.principal) logger = UserLogger(principal=avatar.principal) logger.log('User logged in')
def add_log_event(self, cmd, msg, *args, **kwargs): self._action_log(cmd, msg) owner = yield db.get(self.context, '__owner__') ulog = UserLogger(principal=cmd.protocol.interaction.participations[0].principal, subject=self.context, owner=owner) ulog.log(msg, *args, **kwargs)