Skip to content

Commit 9cd74da

Browse files
committed
Name consistency, less intermediate variables, some more explicit logout calls, privatise some APISession methods.
Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
1 parent 119cae3 commit 9cd74da

11 files changed

Lines changed: 46 additions & 47 deletions

drivers/LinstorSR.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,9 +2587,8 @@ def _start_persistent_http_server(volume_name):
25872587
# Use a timeout call because XAPI may be unusable on startup
25882588
# or if the host has been ejected. So in this case the call can
25892589
# block indefinitely.
2590-
apisession = util.timeout_call(
2591-
5, util.APISession, "SM-LinstorSR-http")
2592-
session = apisession.session
2590+
api_session = util.timeout_call(5, util.APISession, "SM-ha-linstor-http-server")
2591+
session = api_session.session
25932592
host_ip = util.get_this_host_address(session)
25942593
except:
25952594
# Fallback using the XHA file if session not available.
@@ -2598,6 +2597,8 @@ def _start_persistent_http_server(volume_name):
25982597
raise Exception(
25992598
'Cannot start persistent HTTP server: no XAPI session, nor XHA config file'
26002599
)
2600+
finally:
2601+
api_session.logout()
26012602

26022603
arguments = [
26032604
'http-disk-server',
@@ -2673,9 +2674,8 @@ def _start_persistent_nbd_server(self, volume_name):
26732674
device_size = 256 * 1024 * 1024
26742675

26752676
try:
2676-
apisession = util.timeout_call(
2677-
5, util.APISession, "SM-LinstorSR-nbd")
2678-
session = apisession.session
2677+
api_session = util.timeout_call(5, util.APISession, "SM-ha-linstor-nbd-server")
2678+
session = api_session.session
26792679
ips = util.get_host_addresses(session)
26802680
except Exception as e:
26812681
_, ips = get_ips_from_xha_config_file()
@@ -2684,6 +2684,8 @@ def _start_persistent_nbd_server(self, volume_name):
26842684
'Cannot start persistent NBD server: no XAPI session, nor XHA config file ({})'.format(e)
26852685
)
26862686
ips = ips.values()
2687+
finally:
2688+
api_session.logout()
26872689

26882690
arguments = [
26892691
'nbd-http-server',

drivers/cleanup.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,10 @@ class LookupError(util.SMException):
297297

298298
def __init__(self, session, srUuid):
299299
self.session = session
300-
self.apisession = None
300+
self._api_session = None
301301
if self.session is None:
302-
self.apisession = util.APISession("SM-cleanup-XAPI")
303-
self.session = self.apisession.session
302+
self._api_session = util.APISession("SM-cleanup-XAPI")
303+
self.session = self._api_session.session
304304
self._srRef = self.session.xenapi.SR.get_by_uuid(srUuid)
305305
self.srRecord = self.session.xenapi.SR.get_record(self._srRef)
306306
self.hostUuid = util.get_this_host()
@@ -309,8 +309,8 @@ def __init__(self, session, srUuid):
309309
self.task_progress = {"coalescable": 0, "done": 0}
310310

311311
def __del__(self):
312-
if self.apisession:
313-
self.apisession.logout()
312+
if self._api_session:
313+
self._api_session.logout()
314314

315315
@property
316316
def srRef(self):
@@ -1993,7 +1993,7 @@ def msg_cleared(self, xapi_session, msg_ref):
19931993
return msg is None
19941994

19951995
def check_no_space_candidates(self):
1996-
with util.APISession("SM-cleanup-SR-check_no_space_candidates") as xapi_session:
1996+
with util.APISession("GC-check_no_space") as xapi_session:
19971997
msg_id = self.xapi.srRecord["sm_config"].get(VDI.DB_GC_NO_SPACE)
19981998
if self.no_space_candidates:
19991999
if msg_id is None or self.msg_cleared(xapi_session, msg_id):

drivers/coalesce-leaf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
# it under the terms of the GNU Lesser General Public License as published
77
# by the Free Software Foundation; version 2.1 only.
88
#
9-
# This program is distributed in the hope that it will be useful,
10-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1212
# GNU Lesser General Public License for more details.
1313
#
1414
# You should have received a copy of the GNU Lesser General Public License

drivers/lcache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def from_session(cls, session):
219219

220220
@classmethod
221221
def from_cli(cls):
222-
with util.APISession("SM-lcache-CacheFileSR") as session:
222+
with util.APISession("SM-local-cache") as session:
223223
return cls.from_session(session)
224224

225225
def statvfs(self):

drivers/linstorvhdutil.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,8 @@ def local_vhdutil(self):
634634
return self._executor_data.vhdutil
635635

636636
def _init_executor_thread(self):
637-
apisession = util.APISession("SM-linstorvhdutil")
638-
session = apisession.session
637+
api_session = util.APISession("SM-linstorvhdutil")
638+
session = api_session.session
639639
try:
640640
linstor = LinstorVolumeManager(
641641
self._uri,
@@ -649,7 +649,7 @@ def _init_executor_thread(self):
649649
except:
650650
self._executor_data.clear()
651651
raise
652-
self._loads.append(apisession)
652+
self._loads.append(api_session)
653653

654654
def _cleanup(self):
655655
for load in self._loads:

drivers/linstorvolumemanager.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def get_all_volume_openers(resource_name, volume):
9898
volume = str(volume)
9999
openers = {}
100100

101-
with util.APISession("SM-linstorvolumemanager-get_all_volume_openers") as session:
101+
with util.APISession("SM-get-linstor-volume-openers") as session:
102102
hosts = session.xenapi.host.get_all_records()
103103
for host_ref, host_record in hosts.items():
104104
node_name = host_record['hostname']
@@ -187,8 +187,7 @@ def get_controller_node_name():
187187
if res:
188188
return res.groups()[0]
189189

190-
apisession = util.timeout_call(5, util.APISession, "SM-linstorvolumemanager-get_controller_node_name")
191-
session = apisession.session
190+
session = util.timeout_call(5, util.APISession, "SM-get-linstor-controller-node-name").session
192191

193192
for host_ref, host_record in session.xenapi.host.get_all_records().items():
194193
node_name = host_record['hostname']
@@ -211,8 +210,7 @@ def get_controller_node_name():
211210
def demote_drbd_resource(node_name, resource_name):
212211
PLUGIN_CMD = 'demoteDrbdResource'
213212

214-
apisession = util.timeout_call(5, util.APISession, "SM-linstorvolumemanager-demote_drbd_resource")
215-
session = apisession.session
213+
session = util.timeout_call(5, util.APISession, "SM-demote-drbd-resource").session
216214

217215
for host_ref, host_record in session.xenapi.host.get_all_records().items():
218216
if host_record['hostname'] != node_name:
@@ -1404,8 +1402,7 @@ def destroy(self):
14041402
# It needs to be done locally by each host so we go through the linstor-manager plugin.
14051403
# If we don't do this sometimes, the destroy will fail when trying to destroy the resource groups with:
14061404
# "linstor-manager:destroy error: Failed to destroy SP `xcp-sr-linstor_group_thin_device` on node `r620-s2`: The specified storage pool 'xcp-sr-linstor_group_thin_device' on node 'r620-s2' can not be deleted as volumes / snapshot-volumes are still using it."
1407-
apisession = util.timeout_call(5, util.APISession, "SM-linstorvolumemanager-destroy")
1408-
session = apisession.session
1405+
session = util.timeout_call(5, util.APISession, "SM-linstor-destroy").session
14091406
for host_ref in session.xenapi.host.get_all():
14101407
try:
14111408
response = session.xenapi.host.call_plugin(

drivers/mpathcount.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ def _run_command(command, timeout):
222222

223223
if __name__ == '__main__':
224224
try:
225-
apisession = util.APISession("SM-mpathcount")
226-
session = apisession.session
225+
session = util.APISession("SM-mpathcount").session
227226
except xs_errors.XenError:
228227
sys.exit(-1)
229228

drivers/sr_health_check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def main():
3333
"""
3434
For all locally plugged SRs check that they are healthy
3535
"""
36-
with util.APISession("SM-sr_health_check") as session:
36+
with util.APISession("SM-sr-health-check") as session:
3737
localhost = util.get_localhost_ref(session)
3838
if not check_xapi_is_enabled(session, localhost):
3939
# Xapi not enabled, skip and let the next timer trigger this

drivers/util.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,10 @@ def ioretry_stat(path, maxretry=IORETRY_MAX):
401401

402402
def sr_get_capability(sr_uuid, session=None):
403403
result = []
404+
api_session = None
404405
if session is None:
405-
apisession = APISession("SM-util-sr_get_capability")
406-
session = apisession.session
406+
api_session = APISession("SM-sr-get-capability")
407+
session = api_session.session
407408
sr_ref = session.xenapi.SR.get_by_uuid(sr_uuid)
408409
sm_type = session.xenapi.SR.get_record(sr_ref)['type']
409410
sm_rec = session.xenapi.SM.get_all_records_where(
@@ -412,7 +413,8 @@ def sr_get_capability(sr_uuid, session=None):
412413
# SM expects at least one entry of any SR type
413414
if len(sm_rec) > 0:
414415
result = list(sm_rec.values())[0]['capabilities']
415-
416+
if api_session:
417+
session.logout()
416418
return result
417419

418420
def sr_get_driver_info(driver_info):
@@ -705,19 +707,17 @@ def getrootdevID():
705707
class APISession(contextlib.AbstractContextManager):
706708
def __init__(self, originator="SM"):
707709
self.originator = originator
708-
self.session = self.login()
710+
# First acquire a valid session
711+
self.session = self._login()
709712
SMlog("APISession [{}] login".format(self.originator), priority=LOG_DEBUG)
710-
atexit.register(self.atexit)
713+
atexit.register(self._atexit)
711714

712-
def login(self):
713-
# First acquire a valid session
715+
def _login(self):
714716
session = XenAPI.xapi_local()
715717
try:
716718
session.xenapi.login_with_password('root', '', '', self.originator)
717719
except Exception as exc:
718-
msg = f"APISession [{self.originator}] Unable to open local XAPI session"
719-
SMlog(msg, priority=LOG_ERR)
720-
raise xs_errors.XenError(msg) from exc
720+
raise xs_errors.XenError(f"APISession [{self.originator}] Unable to open local XAPI session") from exc
721721
return session
722722

723723
def _logout(self, log):
@@ -730,14 +730,14 @@ def _logout(self, log):
730730
self.session = None
731731

732732
def logout(self, log="logout"):
733-
atexit.unregister(self.atexit)
733+
atexit.unregister(self._atexit)
734734
self._logout(log=log)
735735

736736
def __del__(self):
737737
if self.session:
738738
self.logout(log="logout del")
739739

740-
def atexit(self):
740+
def _atexit(self):
741741
self._logout(log="logout atexit")
742742

743743
@override

tests/test_cleanup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def acquireNoblock(self):
5959
class IrrelevantLock(object):
6060
pass
6161

62+
6263
def create_cleanup_sr(xapi, uuid=None):
6364
return cleanup.SR(uuid=uuid, xapi=xapi, createLock=False, force=False)
6465

@@ -84,9 +85,9 @@ def setUp(self) -> None:
8485
self.xapi_mock.isMaster.return_value = True
8586
self.mock_xapi_session = mock.MagicMock(name="MockSession")
8687

87-
util_apisession_patcher = mock.patch('cleanup.util.APISession.login')
88-
self.mock_util_apisession_patcher = util_apisession_patcher.start()
89-
self.mock_util_apisession_patcher.return_value = self.mock_xapi_session
88+
util_api_session_patcher = mock.patch('cleanup.util.APISession._login')
89+
self.mock_util_api_session_patcher = util_api_session_patcher.start()
90+
self.mock_util_api_session_patcher.return_value = self.mock_xapi_session
9091

9192
self.addCleanup(mock.patch.stopall)
9293

0 commit comments

Comments
 (0)