Skip to content

Commit

Permalink
feat(LinstorSR): is now compatible with python 3
Browse files Browse the repository at this point in the history
Signed-off-by: Ronan Abhamon <[email protected]>
  • Loading branch information
Wescoeur committed Jun 30, 2023
1 parent 0ccd692 commit ea591fb
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 40 deletions.
15 changes: 7 additions & 8 deletions drivers/LinstorSR.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = \
Expand Down Expand Up @@ -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)
]
Expand Down
4 changes: 2 additions & 2 deletions drivers/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions drivers/linstor-manager
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright (C) 2020 Vates SAS - [email protected]
#
Expand Down Expand Up @@ -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)))

Expand Down
13 changes: 5 additions & 8 deletions drivers/linstorvhdutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
30 changes: 15 additions & 15 deletions drivers/linstorvolumemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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<id> 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)
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -2530,7 +2530,7 @@ def _request_database_path(cls, lin, activate=False):
.format(DATABASE_PATH)
)
# Contains a path of the /dev/drbd<id> form.
return resources[0].volumes[0].device_path
return resource.volumes[0].device_path

@classmethod
def _create_database_volume(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion drivers/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion scripts/fork-log-daemon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import select
import signal
Expand Down
2 changes: 1 addition & 1 deletion scripts/linstor-kv-tool
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright (C) 2022 Vates SAS
#
Expand Down
2 changes: 1 addition & 1 deletion scripts/safe-umount
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3

import argparse
import subprocess
Expand Down

0 comments on commit ea591fb

Please sign in to comment.