def initialize_disks(node_guid, disks): """ Initializes 1 or multiple disks :param node_guid: Guid of the node to which the disks belong :type node_guid: str :param disks: Disks to initialize (key: device_alias, value: amount of ASDs to deploy) :type disks: dict :return: Dict of all failures with as key the Diskname, and as value the error :rtype: dict """ node = AlbaNode(node_guid) try: available_disks = node.client.get_disks() except (requests.ConnectionError, requests.Timeout): AlbaNodeController._logger.exception('Could not connect to node {0} to validate disks'.format(node.guid)) raise failures = {} added_disks = [] for device_alias, amount in disks.iteritems(): device_id = device_alias.split('/')[-1] AlbaNodeController._logger.debug('Initializing disk {0} at node {1}'.format(device_alias, node.ip)) if device_id not in available_disks or available_disks[device_id]['available'] is False: AlbaNodeController._logger.exception('Disk {0} not available on node {1}'.format(device_alias, node.ip)) failures[device_alias] = 'Disk unavailable' else: add_disk_result = node.client.add_disk(disk_id=device_id) # Verify if an AlbaDisk with found aliases already exists (eg: When initialize individual and initialize all run at the same time) exists = False aliases = add_disk_result['aliases'] for alba_disk in node.disks: if set(alba_disk.aliases).intersection(set(aliases)): exists = True break if exists is True: continue disk = AlbaDisk() disk.aliases = aliases disk.alba_node = node disk.save() if add_disk_result['_success'] is False: failures[device_alias] = add_disk_result['_error'] disk.delete() else: device_id = disk.aliases[0].split('/')[-1] for _ in xrange(amount): add_asd_result = node.client.add_asd(disk_id=device_id) if add_asd_result['_success'] is False: failures[device_alias] = add_asd_result['_error'] added_disks.extend(add_disk_result['aliases']) if node.storagerouter is not None: DiskController.sync_with_reality(storagerouter_guid=node.storagerouter_guid) for disk in node.storagerouter.disks: if set(disk.aliases).intersection(set(added_disks)): partition = disk.partitions[0] if DiskPartition.ROLES.BACKEND not in partition.roles: partition.roles.append(DiskPartition.ROLES.BACKEND) partition.save() return failures
def ovs_3769_validation_test(): """ Create an albanode with an asd statistics part set to empty dictionary Assert code does not raise """ an = AlbaNode() an.password = '******' an.node_id = 'ovs3769an' an.port = 1234 an.ip = '127.0.0.1' an.username = '******' an.save() bet = GeneralBackend.get_backendtype_by_code('alba') be = Backend() be.backend_type = bet be.name = 'ovs3769be' be.save() abe = AlbaBackend() abe.backend = be abe.save() ad = AlbaDisk() ad.name = 'ovs3769ad' ad.alba_node = an ad.save() asd = AlbaASD() asd.alba_backend = abe asd.asd_id = 'ovs3769asd' asd.alba_disk = ad asd.save() try: abe.statistics except KeyError, ex: logger.error('Regression OVS-3769 - asd statistics raises a KeyError: {0}'.format(str(ex)))
def initialize_disks(node_guid, disks): """ Initializes a disk :param node_guid: Guid of the node which disks need to be initialized :type node_guid: str :param disks: Disks to initialize :type disks: dict :return: Dict of all failures with as key the Diskname, and as value the error :rtype: dict """ node = AlbaNode(node_guid) try: available_disks = node.client.get_disks() except (requests.ConnectionError, requests.Timeout): AlbaNodeController._logger.exception('Could not connect to node {0} to validate disks'.format(node.guid)) raise failures = {} added_disks = [] for disk_id, amount in disks.iteritems(): AlbaNodeController._logger.debug('Initializing disk {0} at node {1}'.format(disk_id, node.ip)) if disk_id not in available_disks or available_disks[disk_id]['available'] is False: AlbaNodeController._logger.exception('Disk {0} not available on node {1}'.format(disk_id, node.ip)) failures[disk_id] = 'Disk unavailable' else: disk = AlbaDisk() disk.name = disk_id disk.alba_node = node disk.save() result = node.client.add_disk(disk_id) if result['_success'] is False: failures[disk_id] = result['_error'] disk.delete() else: device = result['device'] for _ in xrange(amount): result = node.client.add_asd(disk_id) if result['_success'] is False: failures[disk_id] = result['_error'] added_disks.append(device) if node.storagerouter is not None: DiskController.sync_with_reality(node.storagerouter_guid) for disk in node.storagerouter.disks: if disk.path in added_disks: partition = disk.partitions[0] partition.roles.append(DiskPartition.ROLES.BACKEND) partition.save() return failures
def build_service_structure(structure, previous_structure=None): """ Builds a service structure Example: structure = Helper.build_service_structure({ 'alba_backends': [1], 'alba_nodes': [1] }) """ if previous_structure is None: previous_structure = {} backend_types = previous_structure.get('backend_types', {}) service_types = previous_structure.get('service_types', {}) alba_backends = previous_structure.get('alba_backends', {}) alba_nodes = previous_structure.get('alba_nodes', {}) alba_disks = previous_structure.get('alba_disks', {}) alba_osds = previous_structure.get('alba_osds', {}) if 1 not in backend_types: backend_type = BackendType() backend_type.code = 'alba' backend_type.name = 'ALBA' backend_type.save() backend_types[1] = backend_type if 1 not in service_types: service_type = ServiceType() service_type.name = 'AlbaManager' service_type.save() service_types[1] = service_type for ab_id in structure.get('alba_backends', ()): if ab_id not in alba_backends: backend = Backend() backend.name = 'backend_{0}'.format(ab_id) backend.backend_type = backend_types[1] backend.save() alba_backend = AlbaBackend() alba_backend.backend = backend alba_backend.scaling = AlbaBackend.SCALINGS.LOCAL alba_backend.save() alba_backends[ab_id] = alba_backend service = Service() service.name = 'backend_{0}_abm'.format(ab_id) service.type = service_types[1] service.ports = [] service.save() abm_service = ABMService() abm_service.service = service abm_service.alba_backend = alba_backend abm_service.save() for an_id in structure.get('alba_nodes', []): if an_id not in alba_nodes: alba_node = AlbaNode() alba_node.ip = '10.1.0.{0}'.format(an_id) alba_node.port = 8500 alba_node.username = str(an_id) alba_node.password = str(an_id) alba_node.node_id = 'node_{0}'.format(an_id) alba_node.save() alba_nodes[an_id] = alba_node for ad_id, an_id in structure.get('alba_disks', ()): if ad_id not in alba_disks: alba_disk = AlbaDisk() alba_disk.aliases = ['/dev/alba_disk_{0}'.format(ad_id)] alba_disk.alba_node = alba_nodes[an_id] alba_disk.save() alba_disks[ad_id] = alba_disk for ao_id, ad_id, ab_id in structure.get('alba_osds', ()): if ao_id not in alba_osds: osd = AlbaOSD() osd.osd_id = 'alba_osd_{0}'.format(ao_id) osd.osd_type = AlbaOSD.OSD_TYPES.ASD osd.alba_backend = alba_backends[ab_id] osd.alba_disk = alba_disks[ad_id] osd.save() alba_osds[ao_id] = osd return {'backend_types': backend_types, 'service_types': service_types, 'alba_backends': alba_backends, 'alba_nodes': alba_nodes, 'alba_disks': alba_disks, 'alba_osds': alba_osds}
def test_asd_statistics(self): """ Validates whether the ASD statistics work as expected. * Add keys that were not passed in * Collapse certain keys * Calculate correct per-second, average, total, min and max values """ expected_0 = {'statistics': {'max': 0, 'n_ps': 0, 'min': 0, 'avg': 0, 'n': 0}, 'range': {'max': 0, 'n_ps': 0, 'min': 0, 'avg': 0, 'n': 0}, 'range_entries': {'max': 0, 'n_ps': 0, 'min': 0, 'avg': 0, 'n': 0}, 'multi_get': {'max': 10, 'n_ps': 0, 'min': 1, 'avg': 13, 'n': 5}, 'apply': {'max': 5, 'n_ps': 0, 'min': 5, 'avg': 5, 'n': 1}, 'timestamp': None} expected_1 = {'statistics': {'max': 0, 'n_ps': 0, 'min': 0, 'avg': 0, 'n': 0}, 'range': {'max': 0, 'n_ps': 0, 'min': 0, 'avg': 0, 'n': 0}, 'range_entries': {'max': 0, 'n_ps': 0, 'min': 0, 'avg': 0, 'n': 0}, 'multi_get': {'max': 10, 'n_ps': 1, 'min': 1, 'avg': 12.5, 'n': 10}, 'apply': {'max': 5, 'n_ps': 0, 'min': 5, 'avg': 5, 'n': 1}, 'timestamp': None} base_time = time.time() backend_type = BackendType() backend_type.code = 'alba' backend_type.name = 'ALBA' backend_type.save() backend = Backend() backend.name = 'foobar' backend.backend_type = backend_type backend.save() alba_backend = AlbaBackend() alba_backend.backend = backend alba_backend.save() alba_node = AlbaNode() alba_node.ip = '127.0.0.1' alba_node.port = 8500 alba_node.username = '******' alba_node.password = '******' alba_node.node_id = 'foobar' alba_node.save() alba_disk = AlbaDisk() alba_disk.name = 'foo' alba_disk.alba_node = alba_node alba_disk.save() asd = AlbaASD() asd.asd_id = 'foo' asd.alba_backend = alba_backend asd.alba_disk = alba_disk asd.save() service_type = ServiceType() service_type.name = 'AlbaManager' service_type.save() service = Service() service.name = 'foobar' service.type = service_type service.ports = [] service.save() abm_service = ABMService() abm_service.service = service abm_service.alba_backend = alba_backend abm_service.save() asdmanager_client = ASDManagerClient('') asdmanager_client._results['get_disks'] = [] AlbaCLI._run_results['asd-multistatistics'] = {'foo': {'success': True, 'result': {'Apply': {'n': 1, 'avg': 5, 'min': 5, 'max': 5}, 'MultiGet': {'n': 2, 'avg': 10, 'min': 5, 'max': 10}, 'MultiGet2': {'n': 3, 'avg': 15, 'min': 1, 'max': 5}}}} statistics = asd._statistics(AlbaASD._dynamics[0]) expected_0['timestamp'] = base_time self.assertDictEqual(statistics, expected_0, 'The first statistics should be as expected: {0} vs {1}'.format(statistics, expected_0)) time.sleep(5) asdmanager_client._results['get_disks'] = [] AlbaCLI._run_results['asd-multistatistics'] = {'foo': {'success': True, 'result': {'Apply': {'n': 1, 'avg': 5, 'min': 5, 'max': 5}, 'MultiGet': {'n': 5, 'avg': 10, 'min': 5, 'max': 10}, 'MultiGet2': {'n': 5, 'avg': 15, 'min': 1, 'max': 5}}}} statistics = asd._statistics(AlbaASD._dynamics[0]) expected_1['timestamp'] = base_time + 5 self.assertDictEqual(statistics, expected_1, 'The second statistics should be as expected: {0} vs {1}'.format(statistics, expected_1))