From 8c31a86cb03e4fdf0cf06dae2472d8079408da73 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Fri, 30 Jun 2023 12:41:43 +0200 Subject: [PATCH] feat(LinstorSR): is now compatible with python 3 Signed-off-by: Ronan Abhamon --- drivers/LinstorSR.py | 15 +++++++-------- drivers/cleanup.py | 4 ++-- drivers/linstor-manager | 6 +++--- drivers/linstorvhdutil.py | 13 +++++-------- drivers/linstorvolumemanager.py | 30 +++++++++++++++--------------- drivers/util.py | 2 +- scripts/fork-log-daemon | 2 +- scripts/linstor-kv-tool | 2 +- scripts/safe-umount | 2 +- 9 files changed, 36 insertions(+), 40 deletions(-) diff --git a/drivers/LinstorSR.py b/drivers/LinstorSR.py index ca5015e31..063a4e373 100755 --- a/drivers/LinstorSR.py +++ b/drivers/LinstorSR.py @@ -934,7 +934,7 @@ def _update_drbd_reactor_on_all_hosts( secondary_hosts = [] hosts = self.session.xenapi.host.get_all_records() - for host_ref, host_rec in hosts.iteritems(): + for host_ref, host_rec in hosts.items(): hostname = host_rec['hostname'] if controller_node_name == hostname: controller_host = host_ref @@ -1045,7 +1045,7 @@ def _update_physical_size(self): # We use the size of the smallest disk, this is an approximation that # ensures the displayed physical size is reachable by the user. (min_physical_size, pool_count) = self._linstor.get_min_physical_size() - self.physical_size = min_physical_size * pool_count / \ + self.physical_size = min_physical_size * pool_count // \ self._linstor.redundancy self.physical_utilisation = self._linstor.allocated_volume_size @@ -1497,17 +1497,16 @@ def _undo_clone(self, volume_names, vdi_uuid, base_uuid, snap_uuid): # -------------------------------------------------------------------------- def _create_linstor_cache(self): - # TODO: use a nonlocal with python3. - class context: - reconnect = False + reconnect = False def create_cache(): + nonlocal reconnect try: - if context.reconnect: + if reconnect: self._reconnect() return self._linstor.get_volumes_with_info() except Exception as e: - context.reconnect = True + reconnect = True raise e self._all_volume_metadata_cache = \ @@ -2649,7 +2648,7 @@ def _start_persistent_nbd_server(self, volume_name): '--nbd-name', volume_name, '--urls', - ','.join(map(lambda ip: 'http://' + ip + ':' + port, ips)), + ','.join(['http://' + ip + ':' + port for ip in ips]), '--device-size', str(device_size) ] diff --git a/drivers/cleanup.py b/drivers/cleanup.py index 1fda9f69a..1655dae1e 100755 --- a/drivers/cleanup.py +++ b/drivers/cleanup.py @@ -933,7 +933,7 @@ def _coalesceVHD(self, timeOut): util.SMlog('Coalesce failed on %s, attempting repair on ' \ 'parent %s' % (self.uuid, parent)) self.repair(parent) - except Exception, e: + except Exception as e: util.SMlog('(error ignored) Failed to repair parent %s ' \ 'after failed coalesce on %s, err: %s' % (parent, self.path, e)) @@ -3193,7 +3193,7 @@ def _finishInterruptedCoalesceLeaf(self, childUuid, parentUuid): def _checkSlaves(self, vdi): try: all_openers = self._linstor.get_volume_openers(vdi.uuid) - for openers in all_openers.itervalues(): + for openers in all_openers.values(): for opener in openers.values(): if opener['process-name'] != 'tapdisk': raise util.SMException( diff --git a/drivers/linstor-manager b/drivers/linstor-manager index 9e96aacac..4916613fc 100755 --- a/drivers/linstor-manager +++ b/drivers/linstor-manager @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Copyright (C) 2020 Vates SAS - ronan.abhamon@vates.fr # @@ -622,10 +622,10 @@ def add_host(session, args): for pbd_ref, pbd in pbds: device_config = pbd['device_config'] - hosts = filter( + hosts = list(filter( lambda host: len(host.strip()), device_config.get('hosts', []).split(',') - ) + )) hosts.append(node_name) hosts = ','.join(list(set(hosts))) diff --git a/drivers/linstorvhdutil.py b/drivers/linstorvhdutil.py index 83d7f8be5..4cf4f86d5 100644 --- a/drivers/linstorvhdutil.py +++ b/drivers/linstorvhdutil.py @@ -25,9 +25,6 @@ MANAGER_PLUGIN = 'linstor-manager' -# EMEDIUMTYPE constant (124) is not available in python2. -EMEDIUMTYPE = 124 - def call_vhd_util_on_host(session, host_ref, method, device_path, args): try: @@ -105,7 +102,7 @@ def wrapper(*args, **kwargs): 'groupName': self._linstor.group_name } remote_args.update(**kwargs) - remote_args = {str(key): str(value) for key, value in remote_args.iteritems()} + remote_args = {str(key): str(value) for key, value in remote_args.items()} try: def remote_call(): @@ -331,7 +328,7 @@ def local_call(): try: return local_method(device_path, *args, **kwargs) except util.CommandException as e: - if e.code == errno.EROFS or e.code == EMEDIUMTYPE: + if e.code == errno.EROFS or e.code == errno.EMEDIUMTYPE: raise ErofsLinstorCallException(e) # Break retry calls. if e.code == errno.ENOENT: raise NoPathLinstorCallException(e) @@ -380,7 +377,7 @@ def _call_vhd_util(self, local_method, remote_method, device_path, use_parent, * 'groupName': self._linstor.group_name } remote_args.update(**kwargs) - remote_args = {str(key): str(value) for key, value in remote_args.iteritems()} + remote_args = {str(key): str(value) for key, value in remote_args.items()} volume_uuid = self._linstor.get_volume_uuid_from_device_path( device_path @@ -403,12 +400,12 @@ def remote_call(): ) no_host_found = True - for hostname, openers in all_openers.iteritems(): + for hostname, openers in all_openers.items(): if not openers: continue try: - host_ref = next(ref for ref, rec in hosts.iteritems() if rec['hostname'] == hostname) + host_ref = next(ref for ref, rec in hosts.items() if rec['hostname'] == hostname) except StopIteration: continue diff --git a/drivers/linstorvolumemanager.py b/drivers/linstorvolumemanager.py index a9f39e03c..91de3a905 100755 --- a/drivers/linstorvolumemanager.py +++ b/drivers/linstorvolumemanager.py @@ -526,8 +526,8 @@ def allocated_volume_size(self): current[volume.number] = max(current_size, current.get(volume.number) or 0) total_size = 0 - for volumes in sizes.itervalues(): - for size in volumes.itervalues(): + for volumes in sizes.values(): + for size in volumes.values(): total_size += size return total_size * 1024 @@ -1638,8 +1638,8 @@ def _create_sr( lin = cls._create_linstor_instance(uri, keep_uri_unmodified=True) - node_names = ips.keys() - for node_name, ip in ips.iteritems(): + node_names = list(ips.keys()) + for node_name, ip in ips.items(): while True: # Try to create node. result = lin.node_create( @@ -2204,13 +2204,13 @@ def _find_device_path(self, volume_uuid, volume_name): def _request_device_path(self, volume_uuid, volume_name, activate=False): node_name = socket.gethostname() - resources = filter( + resource = next(filter( lambda resource: resource.node_name == node_name and resource.name == volume_name, self._get_resource_cache().resources - ) + ), None) - if not resources: + if not resource: if activate: self._mark_resource_cache_as_dirty() self._activate_device_path( @@ -2222,7 +2222,7 @@ def _request_device_path(self, volume_uuid, volume_name, activate=False): .format(volume_uuid) ) # Contains a path of the /dev/drbd form. - return resources[0].volumes[0].device_path + return resource.volumes[0].device_path def _destroy_resource(self, resource_name, force=False): result = self._linstor.resource_dfn_delete(resource_name) @@ -2240,7 +2240,7 @@ def _destroy_resource(self, resource_name, force=False): # If force is used, ensure there is no opener. all_openers = get_all_volume_openers(resource_name, '0') - for openers in all_openers.itervalues(): + for openers in all_openers.values(): if openers: self._mark_resource_cache_as_dirty() raise LinstorVolumeManagerError( @@ -2506,18 +2506,18 @@ def _request_database_path(cls, lin, activate=False): node_name = socket.gethostname() try: - resources = filter( + resource = next(filter( lambda resource: resource.node_name == node_name and resource.name == DATABASE_VOLUME_NAME, lin.resource_list_raise().resources - ) + ), None) except Exception as e: raise LinstorVolumeManagerError( 'Unable to get resources during database creation: {}' .format(e) ) - if not resources: + if not resource: if activate: cls._activate_device_path( lin, node_name, DATABASE_VOLUME_NAME @@ -2530,7 +2530,7 @@ def _request_database_path(cls, lin, activate=False): .format(DATABASE_PATH) ) # Contains a path of the /dev/drbd form. - return resources[0].volumes[0].device_path + return resource.volumes[0].device_path @classmethod def _create_database_volume( @@ -2565,7 +2565,7 @@ def _create_database_volume( ) # Ensure we have a correct list of storage pools. - nodes_with_pool = map(lambda pool: pool.node_name, pools.storage_pools) + nodes_with_pool = [pool.node_name for pool in pools.storage_pools] assert nodes_with_pool # We must have at least one storage pool! for node_name in nodes_with_pool: assert node_name in node_names @@ -2815,7 +2815,7 @@ def _check_volume_creation_errors(cls, result, volume_uuid, group_name): def _move_files(cls, src_dir, dest_dir, force=False): def listdir(dir): ignored = ['lost+found'] - return filter(lambda file: file not in ignored, os.listdir(dir)) + return [file for file in os.listdir(dir) if file not in ignored] try: if not force: diff --git a/drivers/util.py b/drivers/util.py index 376aaf703..3cf7fa79d 100755 --- a/drivers/util.py +++ b/drivers/util.py @@ -751,7 +751,7 @@ def get_this_host_address(session): def get_host_addresses(session): addresses = [] hosts = session.xenapi.host.get_all_records() - for record in hosts.itervalues(): + for record in hosts.values(): addresses.append(record['address']) return addresses diff --git a/scripts/fork-log-daemon b/scripts/fork-log-daemon index 665a60baf..986de63ff 100755 --- a/scripts/fork-log-daemon +++ b/scripts/fork-log-daemon @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import select import signal diff --git a/scripts/linstor-kv-tool b/scripts/linstor-kv-tool index c9070270c..9d746569f 100755 --- a/scripts/linstor-kv-tool +++ b/scripts/linstor-kv-tool @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Copyright (C) 2022 Vates SAS # diff --git a/scripts/safe-umount b/scripts/safe-umount index 9c1dcc400..3c64a3f31 100755 --- a/scripts/safe-umount +++ b/scripts/safe-umount @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 import argparse import subprocess