Пример #1
0
    def __init__(self, cloud_management_params):
        super(FuelManagement, self).__init__()
        self.node_discover = self  # supports discovering

        self.master_node_address = cloud_management_params['address']
        self._master_host = node_collection.Host(ip=self.master_node_address)
        self.username = cloud_management_params['username']
        self.private_key_file = cloud_management_params.get('private_key_file')
        self.slave_direct_ssh = cloud_management_params.get(
            'slave_direct_ssh', False)
        self.serial = cloud_management_params.get('serial')

        self.master_node_executor = executor.AnsibleRunner(
            remote_user=self.username, private_key_file=self.private_key_file)

        jump_host = self.master_node_address
        if self.slave_direct_ssh:
            jump_host = None

        self.cloud_executor = executor.AnsibleRunner(
            remote_user=self.username,
            private_key_file=self.private_key_file,
            jump_host=jump_host,
            serial=self.serial)

        self.cached_cloud_hosts = list()
Пример #2
0
    def __init__(self, cloud_management_params):
        super(SaltCloudManagement, self).__init__()
        self.node_discover = self  # supports discovering

        self.master_node_address = cloud_management_params['address']
        self._master_host = node_collection.Host(ip=self.master_node_address)
        self.slave_direct_ssh = cloud_management_params.get(
            'slave_direct_ssh', False)
        use_jump = not self.slave_direct_ssh
        self.get_ips_cmd = cloud_management_params.get(
            'get_ips_cmd', 'pillar.get _param:single_address')
        self.serial = cloud_management_params.get('serial')

        self.master_node_executor = executor.AnsibleRunner(
            auth=cloud_management_params.get('auth'))

        slave_auth = cloud_management_params.get('slave_auth') or {}
        if use_jump:
            slave_auth['jump'] = {}
            jump = slave_auth['jump']
            jump['host'] = self.master_node_address
            if not jump.get('username'):
                jump['username'] = (
                    slave_auth.get('username')
                    or cloud_management_params['auth']['username'])

        self.cloud_executor = executor.AnsibleRunner(auth=slave_auth,
                                                     serial=self.serial)

        # get all nodes except salt master (that has cfg* hostname) by default
        self.slave_name_regexp = cloud_management_params.get(
            'slave_name_regexp', '^(?!cfg|mon)')

        self.cached_cloud_hosts = list()
Пример #3
0
 def test___init__options(self, auth, default_host_vars, mock_options, _):
     runner = executor.AnsibleRunner(auth=auth)
     module_path = executor.make_module_path_option()
     mock_options.assert_called_once_with(module_path=module_path,
                                          connection='smart',
                                          forks=100)
     self.assertEqual(default_host_vars, runner.default_host_vars)
Пример #4
0
    def test_execute_status_failed(self, mock_run_playbook):
        my_hosts = [
            node_collection.Host('0.0.0.0'),
            node_collection.Host('255.255.255.255')
        ]
        my_tasks = 'my_task'
        my_statuses = {
            executor.STATUS_OK, executor.STATUS_FAILED,
            executor.STATUS_SKIPPED, executor.STATUS_UNREACHABLE
        }
        r0 = executor.AnsibleExecutionRecord(host='0.0.0.0',
                                             status=executor.STATUS_OK,
                                             task={},
                                             payload={})
        r1 = executor.AnsibleExecutionRecord(
            host='255.255.255.255',
            status=executor.STATUS_UNREACHABLE,
            task={},
            payload={})

        mock_run_playbook.return_value = [r0, r1]
        ex = executor.AnsibleRunner()

        err = self.assertRaises(executor.AnsibleExecutionException, ex.execute,
                                my_hosts, my_tasks, my_statuses)
        self.assertEqual(type(err), executor.AnsibleExecutionException)
Пример #5
0
 def test___init__options(self, config, options_args, passwords,
                          mock_options):
     runner = executor.AnsibleRunner(**config)
     module_path = executor.make_module_path_option()
     mock_options.assert_called_once_with(module_path=module_path,
                                          **options_args)
     self.assertEqual(passwords, runner.passwords)
Пример #6
0
    def test__run_play(self, mock_dataloader, mock_vmanager, mock_inventory,
                       mock_play_load, mock_taskqm):
        mock_play_load.return_value = 'my_load'
        variable_manager = mock_vmanager.return_value
        host_inst = mock_inventory.return_value.get_host.return_value
        host_vars = {
            '0.0.0.0': {
                'ansible_user': '******',
                'ansible_ssh_pass': '******',
                'ansible_become': True,
                'ansible_ssh_private_key_file': None,
                'ansible_ssh_common_args': '-o Option=yes',
            }
        }
        ex = executor.AnsibleRunner()
        ex._run_play({'hosts': ['0.0.0.0']}, host_vars)

        mock_taskqm.assert_called_once()
        self.assertEqual(mock_taskqm.mock_calls[1], mock.call().run('my_load'))
        self.assertEqual(mock_taskqm.mock_calls[2], mock.call().cleanup())

        variable_manager.set_host_variable.assert_has_calls((
            mock.call(host_inst, 'ansible_user', 'foo'),
            mock.call(host_inst, 'ansible_ssh_pass', 'bar'),
            mock.call(host_inst, 'ansible_become', True),
            mock.call(host_inst, 'ansible_ssh_common_args', '-o Option=yes'),
        ),
                                                            any_order=True)
Пример #7
0
    def __init__(self, cloud_management_params):
        super(TCPCloudManagement, self).__init__()

        self.master_node_address = cloud_management_params['address']
        self.username = cloud_management_params['username']
        self.private_key_file = cloud_management_params.get('private_key_file')

        self.master_node_executor = executor.AnsibleRunner(
            remote_user=self.username, private_key_file=self.private_key_file)

        self.cloud_executor = executor.AnsibleRunner(
            remote_user=self.username, private_key_file=self.private_key_file,
            jump_host=self.master_node_address)

        self.cached_cloud_hosts = list()
        self.fqdn_to_hosts = dict()
Пример #8
0
 def test___init__options_jump_host(self, mock_options):
     executor.AnsibleRunner(
         remote_user='******',
         jump_host='jhost.com',
         private_key_file='/path/my.key',
     )
     module_path = executor.resolve_relative_path(
         'os_faults/ansible/modules')
     mock_options.assert_called_once_with(
         become=None,
         become_method='sudo',
         become_user='******',
         check=False,
         connection='smart',
         forks=100,
         module_path=module_path,
         password=None,
         private_key_file='/path/my.key',
         remote_user='******',
         scp_extra_args=None,
         sftp_extra_args=None,
         ssh_common_args=('-o UserKnownHostsFile=/dev/null '
                          '-o StrictHostKeyChecking=no '
                          '-o ProxyCommand='
                          '"ssh -i /path/my.key '
                          '-W %h:%p '
                          '-o UserKnownHostsFile=/dev/null '
                          '-o StrictHostKeyChecking=no '
                          '*****@*****.**"'),
         ssh_extra_args=None,
         verbosity=100)
Пример #9
0
 def test___init__options(self, config, options_args, passwords,
                          mock_options):
     runner = executor.AnsibleRunner(**config)
     module_path = executor.resolve_relative_path(
         'os_faults/ansible/modules')
     mock_options.assert_called_once_with(module_path=module_path,
                                          **options_args)
     self.assertEqual(passwords, runner.passwords)
Пример #10
0
 def test_execute(self, mock_run_playbook):
     my_hosts = ['0.0.0.0', '255.255.255.255']
     my_tasks = 'my_task'
     ex = executor.AnsibleRunner()
     ex.execute(my_hosts, my_tasks)
     mock_run_playbook.assert_called_once_with(
         [{'tasks': ['my_task'],
           'hosts': ['0.0.0.0', '255.255.255.255']}])
Пример #11
0
    def test_run_playbook(self, mock_run_play):
        ex = executor.AnsibleRunner()
        my_playbook = [{'gather_facts': 'yes'}, {'gather_facts': 'no'}]
        ex.run_playbook(my_playbook)

        self.assertEqual(my_playbook, [{'gather_facts': 'no'},
                                       {'gather_facts': 'no'}])
        self.assertEqual(mock_run_play.call_count, 2)
Пример #12
0
 def test_execute_with_host_vars(self, mock_run_playbook, _):
     my_hosts = [
         node_collection.Host('0.0.0.0',
                              auth={
                                  'username': '******',
                                  'password': '******',
                                  'become_username': '******'
                              }),
         node_collection.Host(
             '255.255.255.255',
             auth={'jump': {
                 'host': '192.168.1.100',
                 'username': '******'
             }})
     ]
     my_tasks = 'my_task'
     ex = executor.AnsibleRunner()
     ex.execute(my_hosts, my_tasks)
     mock_run_playbook.assert_called_once_with(
         [{
             'tasks': ['my_task'],
             'hosts': ['0.0.0.0', '255.255.255.255'],
             'serial': 10
         }], {
             '0.0.0.0': {
                 'ansible_user': '******',
                 'ansible_ssh_pass': '******',
                 'ansible_become_method': None,
                 'ansible_become_user': '******',
                 'ansible_become_pass': None,
                 'ansible_ssh_private_key_file': None,
                 'ansible_ssh_common_args': executor.SSH_COMMON_ARGS,
             },
             '255.255.255.255': {
                 'ansible_user':
                 None,
                 'ansible_ssh_pass':
                 None,
                 'ansible_become_method':
                 None,
                 'ansible_become_user':
                 None,
                 'ansible_become_pass':
                 None,
                 'ansible_ssh_private_key_file':
                 None,
                 'ansible_ssh_common_args':
                 '-o UserKnownHostsFile=/dev/null '
                 '-o StrictHostKeyChecking=no '
                 '-o ConnectTimeout=60 '
                 '-o ProxyCommand="'
                 'ssh  -W %h:%p '
                 '-o UserKnownHostsFile=/dev/null '
                 '-o StrictHostKeyChecking=no '
                 '-o ConnectTimeout=60 '
                 '[email protected]"'
             }
         })
Пример #13
0
    def test__run_play(self, mock_dataloader, mock_vmanager, mock_inventory,
                       mock_play_load, mock_taskqm):
        mock_play_load.return_value = 'my_load'
        ex = executor.AnsibleRunner()
        ex._run_play({'hosts': ['0.0.0.0']})

        mock_taskqm.assert_called_once()
        self.assertEqual(mock_taskqm.mock_calls[1], mock.call().run('my_load'))
        self.assertEqual(mock_taskqm.mock_calls[2], mock.call().cleanup())
Пример #14
0
    def __init__(self, cloud_management_params):
        super(DevStackCloudManagement, self).__init__()
        self.node_discover = self  # supports discovering

        address = cloud_management_params['address']
        auth = cloud_management_params['auth']
        self.iface = cloud_management_params.get('iface', 'eth0')

        self.cloud_executor = executor.AnsibleRunner(auth=auth)

        self.hosts = [node_collection.Host(ip=address)]
        self.nodes = None
Пример #15
0
    def __init__(self, cloud_management_params):
        super(DevStackManagement, self).__init__()

        self.address = cloud_management_params['address']
        self.username = cloud_management_params['username']
        self.private_key_file = cloud_management_params.get('private_key_file')

        self.cloud_executor = executor.AnsibleRunner(
            remote_user=self.username,
            private_key_file=self.private_key_file,
            become=False)
        self.host = None
Пример #16
0
    def __init__(self, cloud_management_params):
        super(TCPCloudManagement, self).__init__()
        self.node_discover = self  # supports discovering

        self.master_node_address = cloud_management_params['address']
        self._master_host = node_collection.Host(ip=self.master_node_address)
        self.username = cloud_management_params['username']
        self.slave_username = cloud_management_params.get(
            'slave_username', self.username)
        self.private_key_file = cloud_management_params.get('private_key_file')
        self.slave_direct_ssh = cloud_management_params.get(
            'slave_direct_ssh', False)
        use_jump = not self.slave_direct_ssh
        self.get_ips_cmd = cloud_management_params.get(
            'get_ips_cmd', 'pillar.get _param:single_address')
        self.serial = cloud_management_params.get('serial')

        password = cloud_management_params.get('password')
        self.master_node_executor = executor.AnsibleRunner(
            remote_user=self.username,
            password=password,
            private_key_file=self.private_key_file,
            become=cloud_management_params.get('master_sudo'))

        self.cloud_executor = executor.AnsibleRunner(
            remote_user=self.slave_username,
            password=cloud_management_params.get('slave_password', password),
            private_key_file=self.private_key_file,
            jump_host=self.master_node_address if use_jump else None,
            jump_user=self.username if use_jump else None,
            become=cloud_management_params.get('slave_sudo'),
            serial=self.serial)

        # get all nodes except salt master (that has cfg* hostname) by default
        self.slave_name_regexp = cloud_management_params.get(
            'slave_name_regexp', '^(?!cfg|mon)')

        self.cached_cloud_hosts = list()
Пример #17
0
    def test__run_play_no_host_vars(self, mock_dataloader, mock_vmanager,
                                    mock_inventory, mock_play_load,
                                    mock_taskqm):
        mock_play_load.return_value = 'my_load'
        variable_manager = mock_vmanager.return_value
        host_vars = {}
        ex = executor.AnsibleRunner()
        ex._run_play({'hosts': ['0.0.0.0']}, host_vars)

        mock_taskqm.assert_called_once()
        self.assertEqual(mock_taskqm.mock_calls[1], mock.call().run('my_load'))
        self.assertEqual(mock_taskqm.mock_calls[2], mock.call().cleanup())

        self.assertEqual(0, variable_manager.set_host_variable.call_count)
Пример #18
0
 def test_execute_with_serial(self, mock_run_playbook):
     my_hosts = [
         node_collection.Host('0.0.0.0'),
         node_collection.Host('255.255.255.255')
     ]
     my_tasks = 'my_task'
     ex = executor.AnsibleRunner(serial=50)
     ex.execute(my_hosts, my_tasks)
     mock_run_playbook.assert_called_once_with(
         [{
             'tasks': ['my_task'],
             'hosts': ['0.0.0.0', '255.255.255.255'],
             'serial': 50
         }], {
             '0.0.0.0': {},
             '255.255.255.255': {}
         })
Пример #19
0
    def test_execute_status_unreachable(self, mock_run_playbook):
        my_hosts = ['0.0.0.0', '255.255.255.255']
        my_tasks = 'my_task'
        my_statuses = {executor.STATUS_FAILED,
                       executor.STATUS_SKIPPED, executor.STATUS_UNREACHABLE}
        r0 = executor.AnsibleExecutionRecord(
            host=my_hosts[0], status=executor.STATUS_OK, task={}, payload={})
        r1 = executor.AnsibleExecutionRecord(
            host=my_hosts[1], status=executor.STATUS_UNREACHABLE,
            task={}, payload={})

        mock_run_playbook.return_value = [r0, r1]
        ex = executor.AnsibleRunner()

        err = self.assertRaises(executor.AnsibleExecutionException,
                                ex.execute, my_hosts, my_tasks, my_statuses)
        self.assertEqual(type(err), executor.AnsibleExecutionUnreachable)
Пример #20
0
    def test_execute_stdout_is_more_than_stdout_limit(
            self, mock_run_playbook, mock_deepcopy):
        result = mock.Mock()
        result.payload = {'stdout': 'a' * (executor.STDOUT_LIMIT + 1),
                          'stdout_lines': 'a' * (executor.STDOUT_LIMIT + 1)}
        mock_run_playbook.return_value = [result]

        mock_deepcopy.return_value = [result]
        log_result = mock_deepcopy.return_value[0]

        my_hosts = ['0.0.0.0', '255.255.255.255']
        my_tasks = 'my_task'
        ex = executor.AnsibleRunner()
        ex.execute(my_hosts, my_tasks)

        self.assertEqual('a' * executor.STDOUT_LIMIT + '... <cut>',
                         log_result.payload['stdout'])
Пример #21
0
    def test_execute_payload_without_stdout(self, mock_run_playbook,
                                            mock_debug):
        task = {'task': 'foo'}
        host = '0.0.0.0'
        result = executor.AnsibleExecutionRecord(
            host=host, status=executor.STATUS_OK,
            task=task, payload={'foo': 'bar'})
        mock_run_playbook.return_value = [result]

        ex = executor.AnsibleRunner()
        ex.execute([host], task)

        mock_debug.assert_has_calls((
            mock.call('Executing task: %s on hosts: %s', task, [host]),
            mock.call('Execution completed with 1 result(s):'),
            mock.call(result),
        ))
Пример #22
0
 def test___init__options(self, mock_options):
     executor.AnsibleRunner()
     module_path = executor.resolve_relative_path(
         'os_faults/ansible/modules')
     mock_options.assert_called_once_with(
         become=None,
         become_method='sudo',
         become_user='******',
         check=False,
         connection='smart',
         forks=100,
         module_path=module_path,
         password=None,
         private_key_file=None,
         remote_user='******',
         scp_extra_args=None,
         sftp_extra_args=None,
         ssh_common_args=executor.SSH_COMMON_ARGS,
         ssh_extra_args=None,
         verbosity=100)
Пример #23
0
    def __init__(self, cloud_management_params):
        super(DevStackManagement, self).__init__()
        self.node_discover = self  # supports discovering

        self.address = cloud_management_params['address']
        self.username = cloud_management_params['username']
        self.private_key_file = cloud_management_params.get('private_key_file')
        self.slaves = cloud_management_params.get('slaves', [])
        self.iface = cloud_management_params.get('iface', 'eth0')
        self.serial = cloud_management_params.get('serial')

        self.cloud_executor = executor.AnsibleRunner(
            remote_user=self.username, private_key_file=self.private_key_file,
            password=cloud_management_params.get('password'),
            become=False, serial=self.serial)

        self.hosts = [node_collection.Host(ip=self.address)]
        if self.slaves:
            self.hosts.extend([node_collection.Host(ip=h)
                               for h in self.slaves])
        self.nodes = None
Пример #24
0
    def __init__(self, cloud_management_params):
        super(UniversalCloudManagement, self).__init__()
        self.node_discover = self  # by default can discover itself

        self.address = cloud_management_params.get('address')
        self.iface = cloud_management_params.get('iface')
        serial = cloud_management_params.get('serial')

        auth = cloud_management_params.get('auth') or {}
        jump = auth.get('jump') or {}

        self.cloud_executor = executor.AnsibleRunner(
            remote_user=auth.get('username'),
            password=auth.get('password'),
            private_key_file=auth.get('private_key_file'),
            become=auth.get('become'),
            become_password=auth.get('become_password'),
            jump_host=jump.get('host'),
            jump_user=jump.get('user'),
            serial=serial,
        )

        self.cached_hosts = None  # cache for node discovery
Пример #25
0
    def __init__(self, cloud_management_params):
        super(UniversalCloudManagement, self).__init__()

        self.cloud_executor = executor.AnsibleRunner()
Пример #26
0
 def test___init__jump_host(self):
     host = 'my_host'
     ssh_common_args = executor.SSH_COMMON_ARGS
     ar = executor.AnsibleRunner(jump_host=host)
     self.assertLess(len(ssh_common_args), len(ar.options.ssh_common_args))