Ejemplo n.º 1
0
def main(args):
    if not (args.groups or args.users):
        display_usage_for_a_user(getpass.getuser(),
                                 kilobytes=args.kilobytes,
                                 megabytes=args.megabytes)
    elif args.groups and (not args.users):
        groups = args.groups.split(',')
        policy = mutil.load_policy(mutil.CURRENT_POLICY_PATH)
        if set(groups) - set(policy.keys()):
            raise Exception('group(s) {} not present in the policy'.format(
                ', '.join(set(groups) - set(policy.keys()))))
        users = {user for group in groups for user in policy[group]['users']}
        for group in groups:
            display_usage_for_a_group(policy,
                                      group,
                                      kilobytes=args.kilobytes,
                                      megabytes=args.megabytes)
        if len(groups) > 1:
            display_total_usage_for_users(users,
                                          kilobytes=args.kilobytes,
                                          megabytes=args.megabytes)
    elif (not args.groups) and args.users:
        users = args.users.split(',')
        for user in users:
            display_usage_for_a_user(user,
                                     kilobytes=args.kilobytes,
                                     megabytes=args.megabytes)
        if len(users) > 1:
            display_total_usage_for_users(users,
                                          kilobytes=args.kilobytes,
                                          megabytes=args.megabytes)
    elif args.groups and args.users:
        groups = args.groups.split(',')
        users = args.users.split(',')
        policy = mutil.load_policy(mutil.CURRENT_POLICY_PATH)
        if set(groups) - set(policy.keys()):
            raise Exception('group(s) {} not present in the policy'.format(
                ', '.join(set(groups) - set(policy.keys()))))
        group_users = {
            user
            for group in groups for user in policy[group]['users']
        }
        total_users = (
            {user
             for group in groups for user in policy[group]['users']}
            | set(users))
        for group in groups:
            display_usage_for_a_group(policy,
                                      group,
                                      kilobytes=args.kilobytes,
                                      megabytes=args.megabytes)
        for user in sorted(set(users) - set(group_users)):
            display_usage_for_a_user(user,
                                     kilobytes=args.kilobytes,
                                     megabytes=args.megabytes)
        display_total_usage_for_users(total_users,
                                      kilobytes=args.kilobytes,
                                      megabytes=args.megabytes)
Ejemplo n.º 2
0
def update_policy(policy_path, shared_memory, enact=False):
    policy = mutil.load_policy(policy_path)
    if policy['shared'] == shared_memory:
        return

    shared_memory_overage = shared_memory - policy['shared']

    if policy['free']['memory_limit'] - shared_memory_overage >= 16:
        policy['free']['memory_limit'] -= shared_memory_overage
        policy['shared'] = shared_memory
        mutil.dump_policy(policy, policy_path)
        if enact:
            mutil.enact_policy(policy)
            mutil.enact_policy(policy)
        return

    policy['free']['memory_limit'] = 16
    policy['shared'] = shared_memory
    for group in itertools.islice(
            itertools.cycle(mutil.get_group_set(policy) - {'free'}),
            shared_memory_overage - policy['free']['memory_limit'] + 16):
        policy[group]['memory_limit'] -= 1
    mutil.dump_policy(policy, policy_path)
    if enact:
        mutil.enact_policy(policy)
        mutil.enact_policy(policy)
Ejemplo n.º 3
0
def main(args):
    mutil.validate_group(args.group)
    policy = mutil.load_policy(mutil.PROPOSED_POLICY_PATH)
    user = getpass.getuser()
    users = mutil.get_user_set(policy)
    mutil.validate_user(user, users)
    if not (args.group or args.users):
        if user in policy['free']['users']:
            raise Exception('You have no reserved memory to free.')
        mutil.remove_user_from_current_group(user, policy)
        policy['free']['users'].append(user)
    elif args.group and (not args.users):
        try:
            for user in policy[args.group]['users']:
                policy['free']['users'].append(user)
            del policy[args.group]
        except KeyError:
            raise Exception('The specified group does not exist.')
    elif (not args.group) and args.users:
        users = mutil.specified_users(args.users, users)
        for user in users:
            mutil.remove_user_from_current_group(user, policy)
            policy['free']['users'].append(user)
    elif args.group and args.users:
        users = mutil.specified_users(args.users, users)
        for user in users:
            mutil.remove_user_from_current_group(user, policy)
            policy['free']['users'].append(user)
    policy['free']['memory_limit'] = mutil.available_memory(policy)
    policy['shared'] = mutil.shared_memory_in_gigabytes()
    mutil.dump_policy(policy, mutil.PROPOSED_POLICY_PATH)
Ejemplo n.º 4
0
def main():
    mutil.require_super_user()
    shutil.copy(mutil.PREVIOUS_POLICY_PATH, mutil.CURRENT_POLICY_PATH)
    policy = mutil.load_policy(mutil.CURRENT_POLICY_PATH)
    cgconfig, cgrules = mutil.generate_config_files(policy)
    with open(mutil.CGCONFIG_PATH, 'w') as f:
        f.write(cgconfig)
    with open(mutil.CGRULES_PATH, 'w') as f:
        f.write(cgrules)
    mutil.restart_daemons()
Ejemplo n.º 5
0
def main(args):
    new_users = set(args.users.split(','))
    policy = mutil.load_policy(mutil.PROPOSED_POLICY_PATH)
    existing_users = mutil.get_user_set(policy)
    for user in new_users.intersection(existing_users):
        print('{} is already in the memory policy.'.format(user))
    policy['free']['users'].extend(list(new_users - existing_users))
    mutil.dump_policy(policy, mutil.PROPOSED_POLICY_PATH)
    for user in new_users - existing_users:
        print('{} has been inducted into the memory policy.'.format(user))
Ejemplo n.º 6
0
def main(args):
    draft = load_draft(args.draft)
    draft_users = mutil.get_user_set(draft)
    total_reserved_memory = mutil.reserved_memory(draft)
    proposed_policy = mutil.load_policy(mutil.PROPOSED_POLICY_PATH)
    proposed_policy_users = mutil.get_user_set(proposed_policy)
    mutil.validate_draft_policy(draft, total_reserved_memory, draft_users,
                                proposed_policy_users)
    draft['free'] = mutil.infer_free_group(draft, total_reserved_memory,
                                           draft_users, proposed_policy_users)
    draft['shared'] = mutil.shared_memory_in_gigabytes()
    mutil.dump_policy(draft, mutil.PROPOSED_POLICY_PATH)
Ejemplo n.º 7
0
def main(args):
    policy = mutil.load_policy(mutil.CURRENT_POLICY_PATH)
    mutil.print_policy(policy,
                       fancy=not args.flat,
                       dark_background=mutil.check_dark_background_config())
Ejemplo n.º 8
0
def main():
    policy = mutil.load_policy(mutil.PREVIOUS_POLICY_PATH)
    mutil.print_policy(policy)
Ejemplo n.º 9
0
def main(args):
    mutil.require_super_user()
    shutil.move(mutil.CURRENT_POLICY_PATH, mutil.PREVIOUS_POLICY_PATH)
    shutil.copy(mutil.PROPOSED_POLICY_PATH, mutil.CURRENT_POLICY_PATH)
    policy = mutil.load_policy(mutil.CURRENT_POLICY_PATH)
    mutil.enact_policy(policy, no_daemon=args.no_daemon)
Ejemplo n.º 10
0
def main():
    policy = mutil.load_policy(mutil.PROPOSED_POLICY_PATH)
    mutil.print_policy(policy)