Пример #1
0
 def lock_keys(self):
     if IVirtualCompute.providedBy(self.context):
         return (canonical_path(self.context),
                 canonical_path(self.context.__parent__),
                 canonical_path(self.context.__parent__.__parent__)) + self._additional_keys
     else:
         return (canonical_path(self.context),) + self._additional_keys
Пример #2
0
 def lock_keys(self):
     if IVirtualCompute.providedBy(self.context):
         return (canonical_path(
             self.context), canonical_path(self.context.__parent__),
                 canonical_path(self.context.__parent__.__parent__)
                 ) + self._additional_keys
     else:
         return (canonical_path(self.context), ) + self._additional_keys
Пример #3
0
def model_deleted(model, event):
    from opennode.oms.model.traversal import canonical_path
    timestamp = int(time.time() * 1000)

    parent = event.container
    if IStream.providedBy(parent) or queryAdapter(parent, IStream):
        IStream(parent).add((timestamp, dict(event='remove', name=model.__name__,
                                             url=canonical_path(parent))))

    if IStream.providedBy(model) or queryAdapter(model, IStream):
        IStream(model).add((timestamp, dict(event='delete', name=model.__name__,
                                            url=canonical_path(model))))
Пример #4
0
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')
Пример #5
0
    def _do_cat(self, obj, attrs=[], add_full_paths=False):
        items = model_to_dict(obj).items()

        data = dict((key, value) for key, value in items
                    if obj and (key in attrs or not attrs))

        data['__name__'] = obj.__name__

        if 'owner' in attrs or not attrs:
            data['owner'] = obj.__owner__

        cls = type(removeSecurityProxy(obj))
        data['__classname__'] = cls.__name__
        data['__module__'] = cls.__module__

        if 'permissions' in attrs or not attrs:
            data['permissions'] = self._do_cat_acl(obj)

        if 'tags' in data:
            data['tags'] = list(data['tags'])

        if len(data.keys()) == 0:
            return

        if add_full_paths:
            data.update({'path': canonical_path(obj)})

        if not attrs and IIncomplete.providedBy(obj):
            data.update({'incomplete': True})

        return data
Пример #6
0
    def _do_cat(self, obj, attrs=[], add_full_paths=False):
        items = model_to_dict(obj).items()

        data = dict((key, value) for key, value in items if obj and (key in attrs or not attrs))

        data['__name__'] = obj.__name__

        if 'owner' in attrs or not attrs:
            data['owner'] = obj.__owner__

        cls = type(removeSecurityProxy(obj))
        data['__classname__'] = cls.__name__
        data['__module__'] = cls.__module__

        if 'permissions' in attrs or not attrs:
            data['permissions'] = self._do_cat_acl(obj)

        if 'tags' in data:
            data['tags'] = list(data['tags'])

        if len(data.keys()) == 0:
            return

        if add_full_paths:
            data.update({'path': canonical_path(obj)})

        if not attrs and IIncomplete.providedBy(obj):
            data.update({'incomplete': True})

        return data
Пример #7
0
 def get_symlink_nicknames(item):
     for method in (lambda item: [canonical_path(item)],
                    lambda item: getattr(follow_symlinks(item), 'nicknames', [])):
         try:
             for n in method(item):
                 yield n
         except Unauthorized:
             log.err(system='security')
Пример #8
0
def model_created(model, event):
    from opennode.oms.model.traversal import canonical_path

    timestamp = int(time.time() * 1000)

    parent = event.container
    if IStream.providedBy(parent) or queryAdapter(parent, IStream):
        IStream(parent).add((timestamp, dict(event="add", name=model.__name__, url=canonical_path(model))))
Пример #9
0
            def add_deployed_model_remove_from_hangar(c, target):
                path = canonical_path(target)
                target = traverse1(path)

                cpath = canonical_path(c)
                c = traverse1(cpath)
                if c is None:
                    raise Exception('Compute not found: "%s"' % cpath)

                new_compute = Compute(unicode(hostname), u'inactive')
                new_compute.__name__ = name
                new_compute.__owner__ = owner_obj
                new_compute.template = unicode(template)
                new_compute._ipv4_address = unicode(ipaddr)
                new_compute.mac_address = getattr(c, 'mac_address', None)
                new_compute.memory = getattr(c, 'memory', 0)
                new_compute.diskspace = getattr(c, 'diskspace', {u'total': 0})
                new_compute.num_cores = getattr(c, 'num_cores', 0)
                new_compute.license_activated = getattr(
                    c, 'license_activated', True)

                alsoProvides(new_compute, IVirtualCompute)
                alsoProvides(new_compute, IDeployed)
                noLongerProvides(new_compute, IManageable)
                target.add(new_compute)

                container = c.__parent__
                del container[name]

                timestamp = int(time.time() * 1000)
                IStream(new_compute).add((timestamp, {
                    'event': 'change',
                    'name': 'features',
                    'value': new_compute.features,
                    'old_value': self.context.features
                }))
                IStream(new_compute).add((timestamp, {
                    'event':
                    'change',
                    'name':
                    'ipv4_address',
                    'value':
                    new_compute._ipv4_address,
                    'old_value':
                    self.context._ipv4_address
                }))
Пример #10
0
 def get_symlink_nicknames(item):
     for method in (lambda item: [canonical_path(item)],
                    lambda item: getattr(follow_symlinks(item),
                                         'nicknames', [])):
         try:
             for n in method(item):
                 yield n
         except Unauthorized:
             log.err(system='security')
Пример #11
0
    def _execute(self, cmd, args):
        self._full = getattr(args, 'full', False)

        log.msg('Executing SyncAction on %s (%s) (Full: %s)' %
                (self.context, canonical_path(self.context), self._full),
                system='sync-action')

        if any_stack_installed(self.context):
            try:
                yield self.sync_agent_version(self._full)
            except OperationRemoteError:
                log.err(system='sync-action')

            yield self.sync_hw(self._full)

            try:
                yield self.ensure_vms(self._full)
            except Exception:
                log.err(system='sync-action')

            if self._full:
                try:
                    yield SyncTemplatesAction(self.context)._execute(
                        DetachedProtocol(), object())
                except Exception:
                    log.err(system='sync-action')
        else:
            log.msg('No stacks installed on %s: %s' %
                    (self.context, self.context.features))

        try:
            default = yield self.default_console()
            yield self.sync_consoles()

            if IVirtualCompute.providedBy(self.context):
                yield self._sync_virtual()

            yield self._create_default_console(default)
        except Exception:
            log.err(system='sync-action')

        @db.transact
        def set_additional_keys():
            vms = follow_symlinks(self.context['vms'])
            if vms:
                self._additional_keys = (canonical_path(vms), )

        yield set_additional_keys()

        dl = yield self.reacquire()
        if dl is not None:
            return

        yield self.sync_vms()
Пример #12
0
        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)
Пример #13
0
    def _execute(self, cmd, args):
        self._full = getattr(args, 'full', False)

        log.msg('Executing SyncAction on %s (%s) (Full: %s)' % (self.context,
                                                                canonical_path(self.context),
                                                                self._full), system='sync-action')

        if any_stack_installed(self.context):
            try:
                yield self.sync_agent_version(self._full)
            except OperationRemoteError:
                log.err(system='sync-action')

            yield self.sync_hw(self._full)

            try:
                yield self.ensure_vms(self._full)
            except Exception:
                log.err(system='sync-action')

            if self._full:
                try:
                    yield SyncTemplatesAction(self.context)._execute(DetachedProtocol(), object())
                except Exception:
                    log.err(system='sync-action')
        else:
            log.msg('No stacks installed on %s: %s' % (self.context, self.context.features))

        try:
            default = yield self.default_console()
            yield self.sync_consoles()

            if IVirtualCompute.providedBy(self.context):
                yield self._sync_virtual()

            yield self._create_default_console(default)
        except Exception:
            log.err(system='sync-action')

        @db.transact
        def set_additional_keys():
            vms = follow_symlinks(self.context['vms'])
            if vms:
                self._additional_keys = (canonical_path(vms),)

        yield set_additional_keys()

        dl = yield self.reacquire()
        if dl is not None:
            return

        yield self.sync_vms()
Пример #14
0
 def fake_data():
     from opennode.oms.model.traversal import canonical_path
     r = canonical_path(self.context)
     if r.endswith('cpu_usage'):
         return random.random()
     elif r.endswith('memory_usage'):
         return random.randint(0, 100)
     elif r.endswith('network_usage'):
         return random.randint(0, 100)
     elif r.endswith('diskspace_usage'):
         return random.random() * 0.5 + 600  # useful
     else:
         raise Exception('cannot fake')
Пример #15
0
        def fake_data():
            from opennode.oms.model.traversal import canonical_path

            r = canonical_path(self.context)
            if r.endswith("cpu_usage"):
                return random.random()
            elif r.endswith("memory_usage"):
                return random.randint(0, 100)
            elif r.endswith("network_usage"):
                return random.randint(0, 100)
            elif r.endswith("diskspace_usage"):
                return random.random() * 0.5 + 600  # useful
            else:
                raise Exception("cannot fake")
Пример #16
0
    def _do_print_acl(self, obj, verbose, recursive, seen):
        prinrole = IPrincipalRoleManager(obj)
        auth = getUtility(IAuthentication, context=None)

        user_allow = collections.defaultdict(list)
        user_deny = collections.defaultdict(list)
        users = set()
        for role, principal, setting in prinrole.getPrincipalsAndRoles():
            users.add(principal)
            if setting.getName() == 'Allow':
                user_allow[principal].append(role)
            else:
                user_deny[principal].append(role)

        for principal in users:

            def formatted_perms(perms):
                prin = auth.getPrincipal(principal)
                typ = 'group' if isinstance(prin, Group) else 'user'
                if verbose:

                    def grants(i):
                        return ','.join(
                            '@%s' % i[0] for i in
                            rolePermissionManager.getPermissionsForRole(i)
                            if i[0] != 'oms.nothing')

                    return (typ, principal, ''.join(
                        '%s{%s}' %
                        (Role.role_to_nick.get(i, '(%s)' % i), grants(i))
                        for i in sorted(perms)))
                else:
                    return (typ, principal, ''.join(
                        Role.role_to_nick.get(i, '(%s)' % i)
                        for i in sorted(perms)))

            if principal in user_allow:
                self.write("%s:%s:+%s\n" %
                           formatted_perms(user_allow[principal]))
            if principal in user_deny:
                self.write("%s:%s:-%s\n" %
                           formatted_perms(user_deny[principal]))

        if recursive and IContainer.providedBy(follow_symlinks(obj)):
            for sobj in follow_symlinks(obj).listcontent():
                if follow_symlinks(sobj) not in seen:
                    seen.append(sobj)
                    self.write('%s:\n' % canonical_path(sobj))
                    self._do_print_acl(sobj, verbose, recursive, seen)
Пример #17
0
            def add_deployed_model_remove_from_hangar(c, target):
                path = canonical_path(target)
                target = traverse1(path)

                cpath = canonical_path(c)
                c = traverse1(cpath)
                if c is None:
                    raise Exception('Compute not found: "%s"' % cpath)

                new_compute = Compute(unicode(hostname), u'inactive')
                new_compute.__name__ = name
                new_compute.__owner__ = owner_obj
                new_compute.template = unicode(template)
                new_compute._ipv4_address = unicode(ipaddr)
                new_compute.mac_address = getattr(c, 'mac_address', None)
                new_compute.memory = getattr(c, 'memory', 0)
                new_compute.diskspace = getattr(c, 'diskspace', {u'total': 0})
                new_compute.num_cores = getattr(c, 'num_cores', 0)

                alsoProvides(new_compute, IVirtualCompute)
                alsoProvides(new_compute, IDeployed)
                noLongerProvides(new_compute, IManageable)
                target.add(new_compute)

                container = c.__parent__
                del container[name]

                timestamp = int(time.time() * 1000)
                IStream(new_compute).add((timestamp, {'event': 'change',
                                                      'name': 'features',
                                                      'value': new_compute.features,
                                                      'old_value': self.context.features}))
                IStream(new_compute).add((timestamp, {'event': 'change',
                                                      'name': 'ipv4_address',
                                                      'value': new_compute._ipv4_address,
                                                      'old_value': self.context._ipv4_address}))
Пример #18
0
 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)
Пример #19
0
    def arguments(self, parser, args, rest):
        parser.declare_argument('keywords', {})

        model_or_obj, args_required = ((creatable_models.get(args.type),
                                        True) if self.context.name == 'mk' else
                                       (self.context.traverse(args.path),
                                        False))

        schema_fields = get_schema_fields(model_or_obj,
                                          marker=getattr(
                                              self.context.current_obj,
                                              '__contains__', None))

        for name, field, schema in schema_fields:
            if field.readonly:
                continue

            field = field.bind(model_or_obj)

            choices = ([i.value.encode('utf-8') for i in field.vocabulary]
                       if isinstance(field, zope.schema.Choice) else None)

            type = (int if isinstance(field, zope.schema.Int) else None)

            kwargs = {}
            if isinstance(field, Path):
                kwargs['is_path'] = True

                base_path = '.'
                if field.relative_to == Path.PARENT:
                    if self.context.name == 'mk':
                        base_path = self.context.protocol._cwd()
                    else:
                        base_path = canonical_path(model_or_obj.__parent__)

                kwargs['base_path'] = os.path.join(base_path, field.base_path)

            parser.add_argument('=%s' % name,
                                required=(args_required and field.required),
                                type=type,
                                action=GroupDictAction,
                                group='keywords',
                                help=field.title.encode('utf8'),
                                choices=choices,
                                **kwargs)

        return parser
Пример #20
0
    def _do_print_acl(self, obj, verbose, recursive, seen):
        prinrole = IPrincipalRoleManager(obj)
        auth = getUtility(IAuthentication, context=None)

        user_allow = collections.defaultdict(list)
        user_deny = collections.defaultdict(list)
        users = set()
        for role, principal, setting in prinrole.getPrincipalsAndRoles():
            users.add(principal)
            if setting.getName() == 'Allow':
                user_allow[principal].append(role)
            else:
                user_deny[principal].append(role)

        for principal in users:
            def formatted_perms(perms):
                prin = auth.getPrincipal(principal)
                typ = 'group' if isinstance(prin, Group) else 'user'
                if verbose:
                    def grants(i):
                        return ','.join('@%s' % i[0] for i in rolePermissionManager.getPermissionsForRole(i)
                                        if i[0] != 'oms.nothing')
                    return (typ, principal, ''.join('%s{%s}' %
                                                    (Role.role_to_nick.get(i, '(%s)' % i), grants(i))
                                                    for i in sorted(perms)))
                else:
                    return (typ, principal, ''.join(Role.role_to_nick.get(i, '(%s)' % i)
                                                    for i in sorted(perms)))

            if principal in user_allow:
                self.write("%s:%s:+%s\n" % formatted_perms(user_allow[principal]))
            if principal in user_deny:
                self.write("%s:%s:-%s\n" % formatted_perms(user_deny[principal]))

        if recursive and IContainer.providedBy(follow_symlinks(obj)):
            for sobj in follow_symlinks(obj).listcontent():
                if follow_symlinks(sobj) not in seen:
                    seen.append(sobj)
                    self.write('%s:\n' % canonical_path(sobj))
                    self._do_print_acl(sobj, verbose, recursive, seen)
Пример #21
0
    def try_index(self, searcher, model, event):
        path = canonical_path(model)
        op = 'un' if IModelDeletedEvent.providedBy(event) else ''

        log.msg("%sindexing %s %s" % (op, path, type(event).__name__), system="indexer")

        objs, unresolved_path = traverse_path(db.get_root()['oms_root'], path)
        if unresolved_path and not IModelDeletedEvent.providedBy(event):
            return False

        obj = objs[-1]

        try:
            if IModelDeletedEvent.providedBy(event):
                searcher.unindex_object(obj)
            else:
                searcher._index_object(obj)
        except NotYet:
            return False

        log.msg("%sindexed %s %s" % (op, path, type(event).__name__), system="indexer")
        return True
Пример #22
0
    def arguments(self, parser, args, rest):
        parser.declare_argument('keywords', {})

        model_or_obj, args_required = ((creatable_models.get(args.type), True)
                                       if self.context.name == 'mk' else
                                       (self.context.traverse(args.path), False))

        schema_fields = get_schema_fields(model_or_obj,
                                          marker=getattr(self.context.current_obj, '__contains__', None))

        for name, field, schema in schema_fields:
            if field.readonly:
                continue

            field = field.bind(model_or_obj)

            choices = ([i.value.encode('utf-8') for i in field.vocabulary]
                       if isinstance(field, zope.schema.Choice) else None)

            type = (int if isinstance(field, zope.schema.Int) else None)

            kwargs = {}
            if isinstance(field, Path):
                kwargs['is_path'] = True

                base_path = '.'
                if field.relative_to == Path.PARENT:
                    if self.context.name == 'mk':
                        base_path = self.context.protocol._cwd()
                    else:
                        base_path = canonical_path(model_or_obj.__parent__)

                kwargs['base_path'] = os.path.join(base_path, field.base_path)

            parser.add_argument('=%s' % name, required=(args_required and field.required),
                                type=type, action=GroupDictAction, group='keywords',
                                help=field.title.encode('utf8'), choices=choices, **kwargs)

        return parser
Пример #23
0
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')
Пример #24
0
    def _execute(self, cmd, args):
        # XXX for some strange reason args is object()
        if type(args) == argparse.Namespace:
            self._full = args.full
        log.msg('Executing SyncAction on %s (%s)' % (self.context, canonical_path(self.context)),
                 system='sync-action')

        if any_stack_installed(self.context):
            yield self.sync_agent_version(self._full)
            yield self.sync_hw(self._full)
            yield self.ensure_vms(self._full)
            if self._full:
                yield SyncTemplatesAction(self.context)._execute(DetachedProtocol(), object())
        else:
            log.msg('No stacks installed on %s: %s' % (self.context, self.context.features))

        default = yield self.default_console()
        yield self.sync_consoles()

        if IVirtualCompute.providedBy(self.context):
            yield self._sync_virtual()

        yield self._create_default_console(default)

        @db.transact
        def set_additional_keys():
            vms = follow_symlinks(self.context['vms'])
            if vms:
                self._additional_keys = (canonical_path(vms),)

        yield set_additional_keys()
        dl = yield self.reacquire()
        if dl is not None:
            return

        yield self.sync_vms()
Пример #25
0
 def lock_keys(self):
     return (canonical_path(self.context),
             canonical_path(self.context.__parent__),
             canonical_path(self.context.__parent__.__parent__)) + self._additional_keys
Пример #26
0
 def set_additional_keys():
     self._additional_keys = [
         canonical_path(best),
         canonical_path(bestvmscontainer)
     ]
Пример #27
0
 def set_additional_keys():
     self._additional_keys = [
         canonical_path(destination_vms),
         canonical_path(destination)
     ]
Пример #28
0
    def data(self):
        from opennode.oms.model.traversal import canonical_path

        path = canonical_path(self.context)
        return self.transient_store[path]
Пример #29
0
 def lock_keys(self):
     return [
         canonical_path(self.context),
         canonical_path(self.context.__parent__),
         canonical_path(self.context.__parent__.__parent__)
     ]
Пример #30
0
 def set_additional_keys():
     vms = follow_symlinks(self.context['vms'])
     if vms:
         self._additional_keys = (canonical_path(vms),)
Пример #31
0
 def set_additional_keys():
     self._additional_keys = [canonical_path(destination_vms), canonical_path(destination)]
Пример #32
0
 def set_additional_keys():
     self._additional_keys = [canonical_path(best), canonical_path(bestvmscontainer)]
Пример #33
0
 def data(self):
     from opennode.oms.model.traversal import canonical_path
     path = canonical_path(self.context)
     return self.transient_store[path]
Пример #34
0
 def lock_keys(self):
     return [canonical_path(self.context),
             canonical_path(self.context.__parent__),
             canonical_path(self.context.__parent__.__parent__)]
Пример #35
0
 def lock_keys(self):
     """ Returns list of object-related 'hashes' to lock against. Overload in derived classes
     to add more objects """
     return (canonical_path(self.context),
             canonical_path(self.context.__parent__),)
Пример #36
0
 def set_additional_keys():
     vms = follow_symlinks(self.context['vms'])
     if vms:
         self._additional_keys = (canonical_path(vms), )