def test_users_sync(mocker):
    """
  Given a list of user dictionaries, `users.sync(...)` should create missing users, remove extra non-system users,
  and update existing users. A list of existing usernames can be passsed in or default users will be used.
  """
    existing_user_names = ['kehinde', 'bob']
    users_info = [
        user_dict, {
            'name': 'jose',
            'groups': ['wheel'],
            'password': password
        }
    ]
    mocker.patch('subprocess.call')
    users.sync(users_info, existing_user_names)

    subprocess.call.assert_has_calls([
        mocker.call(['usermod', '-p', password, '-G', 'wheel,dev', 'kehinde']),
        mocker.call([
            'useradd',
            '-p',
            password,
            '-G',
            'wheel',
            'jose',
        ]),
        mocker.call(['userdel', '-r', 'bob'])
    ])
Example #2
0
def test_users_sync(mocker):
    """
    Given a list of user dictionaries users.synch(..)should
    create the missing users
    remove extra non-system users
    update existing users
    """
    existing_user_names = ['kevin', 'bob']
    users_info = [
        user_dict, {
            'name': 'jose',
            'groups': ['wheel'],
            'password': password
        }
    ]
    mocker.patch('subprocess.call')
    users.sync(users_info, existing_user_names)
    subprocess.call.assert_has_calls([
        mocker.call([
            'usermod',
            '-p',
            password,
            '-G',
            'wheel,dev',
            'kevin',
        ]),
        mocker.call(['useradd', '-p', password, '-G', 'wheel', 'jose']),
        mocker.call([
            'userdel',
            '-r',
            'bob',
        ]),
    ])
Example #3
0
def test_users_sync(mocker):
    """
    users.sync should take a list of user dicts and apply the desired state on
    the current system. A list of user objects may be passed in or a default is used.
    """
    user_list = [
        {
            'name': 'bob',
            'groups': ['wheel'],
            'password': password,
        },
        {
            'name': 'kevin',
            'groups': ['dev', 'wheel'],
            'password': password,
        },
    ]

    existing_users = ['jose', 'kevin']

    mocker.patch('subprocess.run')
    users.sync(user_list, existing_users)
    subprocess.run.assert_has_calls([
        mocker.call(
            ['useradd', '-m', '-U', '-G', 'wheel', '-p', password, 'bob'],
            check=True),
        mocker.call(['usermod', '-G', 'dev,wheel', '-p', password, 'kevin'],
                    check=True),
        mocker.call(['userdel', '-r', 'jose'], check=True)
    ])
Example #4
0
def main():
    args = create_parser().parse_args()

    if args.export:
        inventory.dump(args.path)
    else:
        users_info = inventory.load(args.path)
        users.sync(users_info)
Example #5
0
File: cli.py Project: mrattray/hr
def main():
    from hr import json, users
    
    args = create_parser().parse_args()
    
    if args.export:
        json.export(args.path)
    else:
         users.sync(json.load(args.path))
Example #6
0
def main():
    from hr import users, inventory

    args = create_parser().parse_args()
    if args.export:
        inventory.dump(args.inventory)
    else:
        user_list = inventory.load(args.inventory)
        users.sync(user_list)
Example #7
0
def main():
    from hr import inventory, users

    args = create_parser().parse_args()

    if args.export:
        inventory.dump(args.path)
    else:
        load_data = inventory.load(args.path)
        users.sync(load_data)
Example #8
0
def main():
    from hr import inventory, users

    args = create_parser().parse_args()

    if args.export:
        print(f"Creating system state inventory file to {args.inventory}")
        inventory.export(args.inventory)
    else:
        print(f"Syncing system to desired state using: {args.inventory}")
        users_data = inventory.load(args.inventory)
        users.sync(users_data)
Example #9
0
File: cli.py Project: MacOG/hr
def main():
    from hr import inventory, users

    args = create_parser().parse_args()
    if os.getuid() == 0:
        if args.export:
            inventory.dump(args.filename)
        else:
            users_info = inventory.load(args.filename)
            users.sync(users_info)
    else:
        if args.export == True:
            print(
                f"Must be run as root! Try using:\n `sudo hr --export {args.filename}"
            )
            return sys.exit(1)
        else:
            print(
                f"Must be run as root! Try using:\n `sudo hr {args.filename}")