def test_ipa_run_does_not_record_erroring_commands( monkeypatch, subprocess_run_failure): mock_original_command(monkeypatch, 'command 1') with pytest.raises(IpaRunError): ipa_utils.ipa_run('group-add', ['flintstones']) with open(CONFIG.DIRECTORY_RECORD) as record: lines = record.read() assert lines.strip() == ''
def remove_member(group_name, users): _validate_blacklist_groups(group_name, users) user_options = ['--users={}'.format(user) for user in users] ipa_command = 'group-remove-member' args = [group_name] + user_options try: ipa_utils.ipa_run(ipa_command, args, error_in_stdout=True) utils.display_success() utils.run_post_command_script('POST_MEMBER_REMOVE_SCRIPT', builtins.list(users)) except IpaRunError: _diagnose_member_command_error(group_name, users, add_command=False)
def add_member(hostgroup_name, hosts): _validate_blacklist_hostgroups(hostgroup_name, hosts) host_options = ['--hosts={}'.format(host) for host in hosts] ipa_command = 'hostgroup-add-member' args = [hostgroup_name] + host_options try: ipa_utils.ipa_run(ipa_command, args, error_in_stdout=True) utils.display_success() except IpaRunError: _diagnose_member_command_error(hostgroup_name, hosts, add_command=True)
def test_ipa_run_can_not_record_commands(monkeypatch): mock_original_command(monkeypatch, 'command 1') ipa_utils.ipa_run('group-add', ['flintstones']) ipa_utils.ipa_run( 'user-add', ['fred', '--first', 'Fred', '--last', 'Flintstone'], record=False # Don't record in this case. ) with open(CONFIG.DIRECTORY_RECORD) as record: lines = record.read() assert lines.strip() == 'command 1'
def remove_member(): click.echo('Please enter the name of the group you wish to remove from:') group = click.prompt(' Group name') _validate_blacklist_groups(group) click.echo( 'Please enter the user(s) you wish to remove from %s, separated' ' by spaces:' % group ) users = click.prompt(' User(s)').split() user_options = ['--users={}'.format(user) for user in users] args = [group] + user_options try: ipa_utils.ipa_run('group-remove-member', args, error_in_stdout=True) utils.display_success() utils.run_post_command_script('POST_MEMBER_REMOVE_SCRIPT', users) except IpaRunError: _diagnose_member_command_error(group, users, add_command=False)
def _modify_ip(argument, new_ip, ip_only): all_dns_zones = _all_dns_zones() # to prevent possible ambiguity here I've elected to reject any host that isn't fully qualified arg_zone = None for zone in all_dns_zones: zone_without_trailing_dot = re.sub(r'\.$', '', zone['Zone name'][0]) # create regex string with end of line char at the end zone_at_end = re.escape(zone_without_trailing_dot) + r'$' if re.search(zone_at_end, argument, re.IGNORECASE): arg_zone = zone_without_trailing_dot zone_with_leading_dot = r'.' + re.escape(arg_zone) arg_host_name = re.sub(zone_with_leading_dot, '', argument) if arg_zone == None: error = "Host " + argument + " not fully qualified" raise click.ClickException(error) else: args = [arg_zone, arg_host_name, '--a-rec=' + new_ip] ipa_utils.ipa_run('dnsrecord-mod', args, record=True) if ip_only: utils.display_success()
def ipa_wrapper(**validated_params): # This method is called by both Click as a callback and manually for the simple commands. # Using a defaultdict allows for handling these params in the same way regardless of # how this method is called. validated_params = defaultdict(lambda: None, validated_params) # Get argument if present; the other params are options. argument = validated_params.pop(argument_name) # At somepoint during processing of the options, seemingly in click.Option, the '-' in ip-address # is being replaced with an underscore # I couldn't work out why so this is a messy fix for the time being if 'ip_address' in validated_params: validated_params['ip-address'] = validated_params['ip_address'] del validated_params['ip_address'] options = validated_params args = _build_ipa_args( argument, options=options, transform_options_callback=transform_options_callback) # if the command is modify host & there's only one item in the args list it's neccesary not to call # the ipa command as a) the option to modify it wouldn't do anything anyway and b) it would result # in a spurious error message if the --ip-address option has been removed in transform_options_callback if not (ipa_command == "host-mod" and len(args) == 1): # want to check if attempting user-add with creation script but that script isn't usable # before completing the user-add if (ipa_command == "user-add" and utils.get_user_config('POST_CREATE_SCRIPT') \ and (not utils.detect_user_config \ or not os.access(utils.get_user_config('POST_CREATE_SCRIPT'), os.X_OK))\ ): raise click.ClickException( "User create script unavailable - you need permissions to execute '{}' or alter your user config." .format(utils.get_user_config('POST_CREATE_SCRIPT'))) result = ipa_utils.ipa_run(ipa_command, args) utils.display_success() handle_result_callback(argument, options, result)
def test_ipa_run_logs_commands_by_default(monkeypatch): mock_original_command(monkeypatch, 'command 1') ipa_utils.ipa_run('group-add', ['flintstones']) mock_original_command(monkeypatch, 'command 2') ipa_utils.ipa_run( 'user-add', ['fred', '--first', 'Fred', '--last', 'Flintstone'] ) mock_original_command(monkeypatch, 'command 3') ipa_utils.ipa_run( 'user-add', ['barney', '--first', 'Barney', '--last', 'Rubble'] ) with open(CONFIG.DIRECTORY_RECORD) as record: lines = record.readlines() assert lines[0].strip() == 'command 1' assert lines[1].strip() == 'command 2' assert lines[2].strip() == 'command 3'