Пример #1
0
 def _update_credit(self, principal, credit, balance_limit):
     profile = traverse1('/home/%s' % principal.id)
     profile.credit = credit
     profile.balance_limit = balance_limit
     log.debug('Updated credit of %s: %s, %s', profile, profile.credit,
               profile.balance_limit)
     return profile
Пример #2
0
 def run():
     model = traverse1(path)
     if model is None:
         log.msg('Model is not found while performing %s for %s: %s' % (action, event, path))
         return
     d = action(model).execute(DetachedProtocol(), object())
     d.addErrback(log.err)
Пример #3
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
                }))
Пример #4
0
 def run():
     model = traverse1(path)
     if model is None:
         log.msg('Model is not found while performing %s for %s: %s' %
                 (action, event, path))
         return
     d = action(model).execute(DetachedProtocol(), object())
     d.addErrback(log.err)
Пример #5
0
def mv_compute_model(context_path, target_path):
    try:
        vm = traverse1(context_path)
        destination = traverse1(target_path)

        if vm is None or vm.__parent__ is None or destination is None:
            log.msg('Source or destination not found: %s (%s) -> %s (%s)' %
                    (context_path, vm, target_path, destination), system='deploy')

        if vm.__parent__.__parent__ != destination.__parent__:
            destination.add(vm)
            log.msg('Model moved.', system='deploy')
        else:
            log.msg('Model NOT moved: VM already in destination', system='deploy')
    except IndexError:
        log.msg('Model NOT moved: destination compute or vms do not exist', system='deploy',
                logLevel=WARNING)
    except KeyError:
        log.msg('Model NOT moved: already moved by sync?', system='deploy')
Пример #6
0
 def _get_profile_and_need_update(self, principal):
     credit_check_cooldown = get_config().getstring('auth', 'billing_timeout', 60)
     try:
         profile = traverse1('/home/%s' % principal.id)
         timeout = (datetime.strptime(profile.credit_timestamp, '%Y-%m-%dT%H:%M:%S.%f') +
                    timedelta(seconds=credit_check_cooldown))
         log.debug('Next update for "%s": %s', principal.id, timeout)
         return (profile, profile.uid, timeout < datetime.now())
     except Exception as e:
         log.error('%s', e)
         raise
Пример #7
0
    def import_data(self, args):
        with open(args.filename, 'r') as f:
            serialized = f.read()
            data = self.deserialize_action_map.get(args.format)(serialized)

        for path, recursive in self.traverse_paths:
            container = traverse1(path)
            pdata = data.get(path)
            if container and pdata:
                self.write('Importing %s (%s)...\n' % (path, 'recursive' if recursive else 'non-recursive'))
                self.traverse_level_set(pdata, container, args.attributes,
                                        recursive=recursive, maxlevel=args.max_depth)
Пример #8
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)
Пример #9
0
 def _get_profile_and_need_update(self, principal):
     credit_check_cooldown = get_config().getstring('auth',
                                                    'billing_timeout', 60)
     try:
         profile = traverse1('/home/%s' % principal.id)
         timeout = (datetime.strptime(profile.credit_timestamp,
                                      '%Y-%m-%dT%H:%M:%S.%f') +
                    timedelta(seconds=credit_check_cooldown))
         log.debug('Next update for "%s": %s', principal.id, timeout)
         return (profile, profile.uid, timeout < datetime.now())
     except Exception as e:
         log.error('%s', e)
         raise
Пример #10
0
def mv_compute_model(context_path, target_path):
    try:
        vm = traverse1(context_path)
        destination = traverse1(target_path)

        if vm is None or vm.__parent__ is None or destination is None:
            log.msg('Source or destination not found: %s (%s) -> %s (%s)' %
                    (context_path, vm, target_path, destination),
                    system='deploy')

        if vm.__parent__.__parent__ != destination.__parent__:
            destination.add(vm)
            log.msg('Model moved.', system='deploy')
        else:
            log.msg('Model NOT moved: VM already in destination',
                    system='deploy')
    except IndexError:
        log.msg('Model NOT moved: destination compute or vms do not exist',
                system='deploy',
                logLevel=WARNING)
    except KeyError:
        log.msg('Model NOT moved: already moved by sync?', system='deploy')
Пример #11
0
def preload_acl_line(path, permspec, filename='-', lineno='-'):
    obj = traverse1(path[1:])

    if obj is None:
        log.warning('No such object: \'%s\'; file: \'%s\' line: %s', path,
                    filename, lineno)
        return

    if obj.__transient__:
        log.warning(
            "Transient object %s always inherits permissions from its parent",
            path)
        return

    if permspec in ('inherit', 'noinherit'):
        obj.inherit_permissions = (permspec == 'inherit')
        return

    auth = getUtility(IAuthentication, context=None)
    interaction = new_interaction(auth.getPrincipal('root'))
    with interaction:
        prinrole = IPrincipalRoleManager(obj)
        action_map = {
            'allow': prinrole.assignRoleToPrincipal,
            'deny': prinrole.removeRoleFromPrincipal,
            'unset': prinrole.unsetRoleForPrincipal
        }

        parsedspec = permspec.strip().split(':', 3)
        if len(parsedspec) < 4:
            log.error(
                'Format error: not all fields are specified: \'%s\' on line %s',
                filename, lineno)
            return

        permtype, kind, principal, perms = parsedspec

        if not perms:
            log.warning(
                'No permissions specified for object: \'%s\'; file: \'%s\' line: %s',
                path, filename, lineno)
            return

        for perm in perms.strip().split(','):
            if perm not in Role.nick_to_role:
                raise NoSuchPermission(perm)
            role = Role.nick_to_role[perm].id
            log.info('%s \'%s\' on %s (%s) to \'%s\'', permtype, perm, path,
                     obj, principal)
            action_map[permtype](role, principal)
Пример #12
0
    def export_data(self, args):
        data = {}
        for path, recursive in self.traverse_paths:
            container = traverse1(path)
            if container:
                self.write('Exporting %s (%s)...\n' % (path, 'recursive' if recursive else 'non-recursive'))
                data[path] = self.traverse_level_get(container, args.attributes,
                                                 recursive=recursive, maxlevel=args.max_depth,
                                                 add_full_paths=args.full_path)

        serialized = self.serialize_action_map.get(args.format)(data)

        with open(args.filename, 'w') as f:
            f.write(serialized)
Пример #13
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}))
Пример #14
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)
Пример #15
0
    def import_data(self, args):
        with open(args.filename, 'r') as f:
            serialized = f.read()
            data = self.deserialize_action_map.get(args.format)(serialized)

        for path, recursive in self.traverse_paths:
            container = traverse1(path)
            pdata = data.get(path)
            if container and pdata:
                self.write(
                    'Importing %s (%s)...\n' %
                    (path, 'recursive' if recursive else 'non-recursive'))
                self.traverse_level_set(pdata,
                                        container,
                                        args.attributes,
                                        recursive=recursive,
                                        maxlevel=args.max_depth)
Пример #16
0
    def export_data(self, args):
        data = {}
        for path, recursive in self.traverse_paths:
            container = traverse1(path)
            if container:
                self.write(
                    'Exporting %s (%s)...\n' %
                    (path, 'recursive' if recursive else 'non-recursive'))
                data[path] = self.traverse_level_get(
                    container,
                    args.attributes,
                    recursive=recursive,
                    maxlevel=args.max_depth,
                    add_full_paths=args.full_path)

        serialized = self.serialize_action_map.get(args.format)(data)

        with open(args.filename, 'w') as f:
            f.write(serialized)
Пример #17
0
def preload_acl_line(path, permspec, filename='-', lineno='-'):
    obj = traverse1(path[1:])

    if obj is None:
        log.warning('No such object: \'%s\'; file: \'%s\' line: %s', path, filename, lineno)
        return

    if obj.__transient__:
        log.warning("Transient object %s always inherits permissions from its parent", path)
        return

    if permspec in ('inherit', 'noinherit'):
        obj.inherit_permissions = (permspec == 'inherit')
        return

    auth = getUtility(IAuthentication, context=None)
    interaction = new_interaction(auth.getPrincipal('root'))
    with interaction:
        prinrole = IPrincipalRoleManager(obj)
        action_map = {'allow': prinrole.assignRoleToPrincipal,
                      'deny': prinrole.removeRoleFromPrincipal,
                      'unset': prinrole.unsetRoleForPrincipal}

        parsedspec = permspec.strip().split(':', 3)
        if len(parsedspec) < 4:
            log.error('Format error: not all fields are specified: \'%s\' on line %s', filename, lineno)
            return

        permtype, kind, principal, perms = parsedspec

        if not perms:
            log.warning('No permissions specified for object: \'%s\'; file: \'%s\' line: %s',
                        path, filename, lineno)
            return

        for perm in perms.strip().split(','):
            if perm not in Role.nick_to_role:
                raise NoSuchPermission(perm)
            role = Role.nick_to_role[perm].id
            log.info('%s \'%s\' on %s (%s) to \'%s\'', permtype, perm, path, obj, principal)
            action_map[permtype](role, principal)
Пример #18
0
 def _update_credit(self, principal, credit, balance_limit):
     profile = traverse1('/home/%s' % principal.id)
     profile.credit = credit
     profile.balance_limit = balance_limit
     log.debug('Updated credit of %s: %s, %s', profile, profile.credit, profile.balance_limit)
     return profile