def discover_hosts(self): # this function is called when no node-discovery driver is specified; # discover the default host set in config for this driver if not self.address: raise error.OSFError('Cloud has no nodes. Specify address in ' 'cloud management driver or add node ' 'discovery driver') if not self.cached_hosts: LOG.info('Discovering host name and MAC address for %s', self.address) host = node_collection.Host(ip=self.address) mac = None if self.iface: cmd = 'cat /sys/class/net/{}/address'.format(self.iface) res = self.execute_on_cloud([host], {'command': cmd}) mac = res[0].payload['stdout'] res = self.execute_on_cloud([host], {'command': 'hostname'}) hostname = res[0].payload['stdout'] # update my hosts self.cached_hosts = [ node_collection.Host(ip=self.address, mac=mac, fqdn=hostname) ] return self.cached_hosts
def setUp(self): super(IPMIDriverTestCase, self).setUp() self.params = { 'mac_to_bmc': { '00:00:00:00:00:00': { 'address': '55.55.55.55', 'username': '******', 'password': '******' } }, 'fqdn_to_bmc': { 'node2.com': { 'address': '55.55.55.56', 'username': '******', 'password': '******' } } } self.driver = ipmi.IPMIDriver(self.params) self.host = node_collection.Host(ip='10.0.0.2', mac='00:00:00:00:00:00', fqdn='node1.com') self.host2 = node_collection.Host(ip='10.0.0.3', mac='00:00:00:00:00:01', fqdn='node2.com')
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)
def setUp(self): super(DevStackManagementTestCase, self).setUp() self.conf = {'address': '10.0.0.2', 'auth': {'username': '******'}} self.host = node_collection.Host('10.0.0.2') self.discoverd_host = node_collection.Host(ip='10.0.0.2', mac='09:7b:74:90:63:c1', fqdn='')
def setUp(self): super(DevStackSystemdServiceTestCase, self).setUp() self.conf = {'address': '10.0.0.2', 'username': '******'} self.host = node_collection.Host('10.0.0.2') self.discoverd_host = node_collection.Host(ip='10.0.0.2', mac='09:7b:74:90:63:c1', fqdn='')
def test_nodes(self, mock_connect): cloud_management_mock = mock.create_autospec( cloud_management.CloudManagement) mock_connect.return_value = cloud_management_mock cloud_management_mock.get_nodes.return_value.hosts = [ node_collection.Host(ip='10.0.0.2', mac='09:7b:74:90:63:c1', fqdn='node1.local'), node_collection.Host(ip='10.0.0.3', mac='09:7b:74:90:63:c2', fqdn='node2.local') ] with self.runner.isolated_filesystem(): with open('my.yaml', 'w') as f: f.write('foo') myconf = os.path.abspath(f.name) result = self.runner.invoke(main.main, ['nodes', '-c', myconf]) self.assertEqual(0, result.exit_code) self.assertEqual( '- fqdn: node1.local\n' ' ip: 10.0.0.2\n' ' mac: 09:7b:74:90:63:c1\n' '- fqdn: node2.local\n' ' ip: 10.0.0.3\n' ' mac: 09:7b:74:90:63:c2\n', result.output)
def test_verify_slaves(self, mock_ansible_runner): self.conf['slaves'] = ['10.0.0.3', '10.0.0.4'] ansible_runner_inst = mock_ansible_runner.return_value ansible_runner_inst.execute.side_effect = [ [fakes.FakeAnsibleResult(payload={'stdout': 'mac1'}, host='10.0.0.2'), fakes.FakeAnsibleResult(payload={'stdout': 'mac2'}, host='10.0.0.3'), fakes.FakeAnsibleResult(payload={'stdout': 'mac3'}, host='10.0.0.4')], [fakes.FakeAnsibleResult(payload={'stdout': ''}, host='10.0.0.2'), fakes.FakeAnsibleResult(payload={'stdout': ''}, host='10.0.0.3'), fakes.FakeAnsibleResult(payload={'stdout': ''}, host='10.0.0.4')], ] devstack_management = devstack.DevStackManagement(self.conf) devstack_management.verify() hosts = [ node_collection.Host('10.0.0.2'), node_collection.Host('10.0.0.3'), node_collection.Host('10.0.0.4') ] ansible_runner_inst.execute.assert_has_calls([ mock.call(hosts, {'command': 'cat /sys/class/net/eth0/address'}), ])
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]"' } })
def setUp(self): super(FuelServiceTestCase, self).setUp() self.conf = {'address': 'fuel.local', 'username': '******'} self.fake_ansible_result = fakes.FakeAnsibleResult( payload={ 'stdout': '[{"ip": "10.0.0.2", "mac": "02", "fqdn": "node-2"},' ' {"ip": "10.0.0.3", "mac": "03", "fqdn": "node-3"}]' }) self.master_host = node_collection.Host('fuel.local') self.hosts = [ node_collection.Host(ip='10.0.0.2', mac='02', fqdn='node-2'), node_collection.Host(ip='10.0.0.3', mac='03', fqdn='node-3'), ]
def test_discover_hosts(self): conf = [ {'ip': '10.0.0.11', 'mac': '01', 'fqdn': 'node-1'}, {'ip': '10.0.0.12', 'mac': '02', 'fqdn': 'node-2'}, ] expected_hosts = [ node_collection.Host(ip='10.0.0.11', mac='01', fqdn='node-1'), node_collection.Host(ip='10.0.0.12', mac='02', fqdn='node-2'), ] node_list_discover = node_list.NodeListDiscover(conf) hosts = node_list_discover.discover_hosts() self.assertEqual(expected_hosts, hosts)
def setUp(self): super(TCPCloudManagementTestCase, self).setUp() self.fake_ansible_result = fakes.FakeAnsibleResult( payload={ 'stdout': 'cmp01.mk20.local:\n' ' eth1:\n' ' hwaddr: 09:7b:74:90:63:c2\n' ' inet:\n' ' - address: 10.0.0.2\n' ' eth2:\n' ' hwaddr: 00:00:00:00:00:02\n' ' inet:\n' ' - address: 192.168.1.2\n' 'cmp02.mk20.local:\n' ' eth1:\n' ' hwaddr: 00:00:00:00:00:03\n' ' inet:\n' ' - address: 192.168.1.3\n' ' eth2:\n' ' hwaddr: 09:7b:74:90:63:c3\n' ' inet:\n' ' - address: 10.0.0.3\n' }) self.fake_node_ip_result = fakes.FakeAnsibleResult( payload={ 'stdout': 'cmp01.mk20.local:\n' ' 10.0.0.2\n' 'cmp02.mk20.local:\n' ' 10.0.0.3\n' }) self.tcp_conf = { 'address': 'tcp.local', 'username': '******', } self.get_nodes_cmd = ( "salt -E '^(?!cfg|mon)' network.interfaces --out=yaml") self.get_ips_cmd = ("salt -E '^(?!cfg|mon)' " "pillar.get _param:single_address --out=yaml") self.master_host = node_collection.Host('tcp.local') self.hosts = [ node_collection.Host(ip='10.0.0.2', mac='09:7b:74:90:63:c2', fqdn='cmp01.mk20.local'), node_collection.Host(ip='10.0.0.3', mac='09:7b:74:90:63:c3', fqdn='cmp02.mk20.local'), ]
def test_get_service_nodes(self, service_name, service_cls, mock_ansible_runner): ansible_runner_inst = mock_ansible_runner.return_value ansible_runner_inst.execute.side_effect = [ [self.fake_ansible_result], [ fakes.FakeAnsibleResult(payload={'stdout': ''}, status=executor.STATUS_FAILED, host='10.0.0.2'), fakes.FakeAnsibleResult(payload={'stdout': ''}, host='10.0.0.3') ] ] fuel_managment = fuel.FuelManagement({ 'address': 'fuel.local', 'username': '******', }) service = fuel_managment.get_service(service_name) self.assertIsInstance(service, service_cls) nodes = service.get_nodes() cmd = 'bash -c "ps ax | grep \'{}\'"'.format(service_cls.GREP) ansible_runner_inst.execute.assert_has_calls([ mock.call(['fuel.local'], {'command': 'fuel node --json'}), mock.call(['10.0.0.2', '10.0.0.3'], {'command': cmd}, []), ]) hosts = [ node_collection.Host(ip='10.0.0.3', mac='03', fqdn='node-3'), ] self.assertEqual(nodes.hosts, hosts)
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()
def test_get_nodes_from_discover_driver(self, mock_ansible_runner): ansible_runner_inst = mock_ansible_runner.return_value hosts = [ node_collection.Host(ip='10.0.2.2', mac='09:7b:74:90:63:c2', fqdn='mynode1.local'), node_collection.Host(ip='10.0.2.3', mac='09:7b:74:90:63:c3', fqdn='mynode2.local'), ] node_discover_driver = mock.Mock() node_discover_driver.discover_hosts.return_value = hosts tcp_managment = saltcloud.SaltCloudManagement(self.tcp_conf) tcp_managment.set_node_discover(node_discover_driver) nodes = tcp_managment.get_nodes() self.assertFalse(ansible_runner_inst.execute.called) self.assertEqual(hosts, nodes.hosts)
def test_get_service_nodes(self, service_name, service_cls, mock_ansible_runner): ansible_runner_inst = mock_ansible_runner.return_value ansible_runner_inst.execute.side_effect = [ [fakes.FakeAnsibleResult(payload={'stdout': '09:7b:74:90:63:c1'})], [fakes.FakeAnsibleResult(payload={'stdout': ''}, host='10.0.0.2')] ] devstack_management = devstack.DevStackManagement(self.conf) service = devstack_management.get_service(service_name) self.assertIsInstance(service, service_cls) nodes = service.get_nodes() cmd = 'bash -c "ps ax | grep \'{}\'"'.format(service_cls.GREP) ansible_runner_inst.execute.assert_has_calls([ mock.call( ['10.0.0.2'], {'command': 'cat /sys/class/net/eth0/address'}), mock.call(['10.0.0.2'], {'command': cmd}, []) ]) self.assertEqual( [node_collection.Host(ip='10.0.0.2', mac='09:7b:74:90:63:c1', fqdn='')], nodes.hosts)
def test_get_service_nodes(self, service_name, service_cls, mock_ansible_runner): ansible_runner_inst = mock_ansible_runner.return_value ansible_runner_inst.execute.side_effect = [ [self.fake_ansible_result], [ fakes.FakeAnsibleResult(payload={'stdout': ''}, status=executor.STATUS_FAILED, host='10.0.0.2'), fakes.FakeAnsibleResult(payload={'stdout': ''}, host='10.0.0.3') ] ] tcp_managment = tcpcloud.TCPCloudManagement(self.tcp_conf) service = tcp_managment.get_service(service_name) self.assertIsInstance(service, service_cls) nodes = service.get_nodes() cmd = 'bash -c "ps ax | grep \'{}\'"'.format(service_cls.GREP) ansible_runner_inst.execute.assert_has_calls([ mock.call(['tcp.local'], {'command': self.get_nodes_cmd}), mock.call(['10.0.0.2', '10.0.0.3'], {'command': cmd}, []), ]) hosts = [ node_collection.Host(ip='10.0.0.3', mac='09:7b:74:90:63:c3', fqdn='cmp02.mk20.local'), ] self.assertEqual(nodes.hosts, hosts)
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()
def discover_hosts(self): if not self.cached_cloud_hosts: interfaces = self._run_salt("network.interfaces") ips = self._run_salt(self.get_ips_cmd) for fqdn, ip in ips.items(): node_ifaces = interfaces[fqdn] mac = None for iface_name, net_data in node_ifaces.items(): iface_ips = [data['address'] for data in net_data.get('inet', [])] if ip in iface_ips: mac = net_data['hwaddr'] break else: raise error.CloudManagementError( "Can't find ip {} on node {} with node_ifaces:\n{}" "".format(ip, fqdn, yaml.dump(node_ifaces))) host = node_collection.Host(ip=ip, mac=mac, fqdn=fqdn) self.cached_cloud_hosts.append(host) self.cached_cloud_hosts = sorted(self.cached_cloud_hosts) return self.cached_cloud_hosts
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': {} })
def setUp(self): super(LibvirtDriverTestCase, self).setUp() self.params = {'connection_uri': 'fake_connection_uri'} self.driver = libvirt.LibvirtDriver(self.params) self.host = node_collection.Host(ip='10.0.0.2', mac='00:00:00:00:00:00', fqdn='node1.com')
def test_get_nodes(self, mock_ansible_runner): ansible_runner_inst = mock_ansible_runner.return_value ansible_runner_inst.execute.side_effect = [[self.fake_ansible_result]] fuel_managment = fuel.FuelManagement({ 'address': 'fuel.local', 'username': '******', }) nodes = fuel_managment.get_nodes() ansible_runner_inst.execute.assert_has_calls([ mock.call(['fuel.local'], {'command': 'fuel node --json'}), ]) hosts = [ node_collection.Host(ip='10.0.0.2', mac='02', fqdn='node-2'), node_collection.Host(ip='10.0.0.3', mac='03', fqdn='node-3'), ] self.assertEqual(nodes.hosts, hosts)
def test_get_nodes(self, mock_ansible_runner): ansible_runner_inst = mock_ansible_runner.return_value ansible_runner_inst.execute.side_effect = [[self.fake_ansible_result]] tcp_managment = tcpcloud.TCPCloudManagement(self.tcp_conf) nodes = tcp_managment.get_nodes() ansible_runner_inst.execute.assert_has_calls([ mock.call(['tcp.local'], {'command': self.get_nodes_cmd}), ]) hosts = [ node_collection.Host(ip='10.0.0.2', mac='09:7b:74:90:63:c2', fqdn='cmp01.mk20.local'), node_collection.Host(ip='10.0.0.3', mac='09:7b:74:90:63:c3', fqdn='cmp02.mk20.local'), ] self.assertEqual(nodes.hosts, hosts)
def setUp(self): super(PowerManagerTestCase, self).setUp() self.dummy_driver1 = mock.Mock(spec=power_management.PowerDriver) self.dummy_driver1.supports.side_effect = lambda host: 'c1' in host.mac self.dummy_driver2 = mock.Mock(spec=power_management.PowerDriver) self.dummy_driver2.supports.side_effect = lambda host: 'c2' in host.mac self.dummy_drivers = [self.dummy_driver1, self.dummy_driver2] self.hosts = [ node_collection.Host(ip='10.0.0.2', mac='09:7b:74:90:63:c1', fqdn='node1.com'), node_collection.Host(ip='10.0.0.3', mac='09:7b:74:90:63:c2', fqdn='node2.com'), ] self.pm = power_management.PowerManager() self.pm.add_driver(self.dummy_driver1) self.pm.add_driver(self.dummy_driver2)
def test__find_domain_by_host_domain_not_found(self, mock__get_connection): host = node_collection.Host(ip='10.0.0.2') domain1 = mock.MagicMock() domain1.XMLDesc.return_value = '52:54:00:ab:64:42' domain2 = mock.MagicMock() domain2.XMLDesc.return_value = '52:54:00:f9:b8:f9' self.driver.conn.listAllDomains.return_value = [domain1, domain2] self.assertRaises(error.PowerManagementError, self.driver._find_domain_by_host, host)
def test__find_domain_by_host_mac(self, mock__get_connection): host = node_collection.Host(ip='10.0.0.2', mac=':54:00:f9:b8:f9') domain1 = mock.MagicMock() domain1.XMLDesc.return_value = '52:54:00:ab:64:42' domain2 = mock.MagicMock() domain2.XMLDesc.return_value = '52:54:00:f9:b8:f9' self.driver.conn.listAllDomains.return_value = [domain1, domain2] domain = self.driver._find_domain_by_host(host) self.assertEqual(domain, domain2)
def test_get_nodes_fqdns(self, mock_ansible_runner): ansible_runner_inst = mock_ansible_runner.return_value ansible_runner_inst.execute.side_effect = [[self.fake_ansible_result]] fuel_managment = fuel.FuelManagement(self.conf) nodes = fuel_managment.get_nodes(fqdns=['node-3']) hosts = [ node_collection.Host(ip='10.0.0.3', mac='03', fqdn='node-3'), ] self.assertEqual(nodes.hosts, hosts)
def setUp(self): super(DevStackNodeTestCase, self).setUp() self.mock_cloud_management = mock.Mock( spec=devstack.DevStackCloudManagement) self.host = node_collection.Host( ip='10.0.0.2', mac='09:7b:74:90:63:c1', fqdn='') self.node_collection = devstack.DevStackNodeCollection( cloud_management=self.mock_cloud_management, hosts=[copy.deepcopy(self.host)])
def get_nodes(self, fqdns=None): if self.host is None: task = {'command': 'cat /sys/class/net/eth0/address'} mac = self.execute_on_cloud([self.address], task)[0].payload['stdout'] # TODO(astudenov): support fqdn self.host = node_collection.Host(ip=self.address, mac=mac, fqdn='') return self.NODE_CLS(cloud_management=self, power_management=self.power_management, hosts=[self.host])
def discover_hosts(self): if not self.cached_cloud_hosts: task = {'command': 'fuel node --json'} result = self._execute_on_master_node(task) for r in json.loads(result[0].payload['stdout']): host = node_collection.Host(ip=r['ip'], mac=r['mac'], fqdn=r['fqdn']) self.cached_cloud_hosts.append(host) return self.cached_cloud_hosts
def test_get_nodes_fqdns(self, mock_ansible_runner): ansible_runner_inst = mock_ansible_runner.return_value ansible_runner_inst.execute.side_effect = [[self.fake_ansible_result]] tcp_managment = tcpcloud.TCPCloudManagement(self.tcp_conf) nodes = tcp_managment.get_nodes(fqdns=['cmp02.mk20.local']) hosts = [ node_collection.Host(ip='10.0.0.3', mac='09:7b:74:90:63:c3', fqdn='cmp02.mk20.local'), ] self.assertEqual(nodes.hosts, hosts)