Skip to content

Commit 334f50c

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 9c1655d commit 334f50c

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
@@ -2585,9 +2585,8 @@ def _start_persistent_http_server(volume_name):
25852585
# Use a timeout call because XAPI may be unusable on startup
25862586
# or if the host has been ejected. So in this case the call can
25872587
# block indefinitely.
2588-
apisession = util.timeout(
2589-
5, util.APISession, "SM-LinstorSR-http")
2590-
session = apisession.session
2588+
api_session = util.timeout(5, util.APISession, "SM-ha-linstor-http-server")
2589+
session = api_session.session
25912590
host_ip = util.get_this_host_address(session)
25922591
except:
25932592
# Fallback using the XHA file if session not available.
@@ -2596,6 +2595,8 @@ def _start_persistent_http_server(volume_name):
25962595
raise Exception(
25972596
'Cannot start persistent HTTP server: no XAPI session, nor XHA config file'
25982597
)
2598+
finally:
2599+
api_session.logout()
25992600

26002601
arguments = [
26012602
'http-disk-server',
@@ -2671,9 +2672,8 @@ def _start_persistent_nbd_server(self, volume_name):
26712672
device_size = 256 * 1024 * 1024
26722673

26732674
try:
2674-
apisession = util.timeout(
2675-
5, util.APISession, "SM-LinstorSR-nbd")
2676-
session = apisession.session
2675+
api_session = util.timeout(5, util.APISession, "SM-ha-linstor-nbd-server")
2676+
session = api_session.session
26772677
ips = util.get_host_addresses(session)
26782678
except Exception as e:
26792679
_, ips = get_ips_from_xha_config_file()
@@ -2682,6 +2682,8 @@ def _start_persistent_nbd_server(self, volume_name):
26822682
'Cannot start persistent NBD server: no XAPI session, nor XHA config file ({})'.format(e)
26832683
)
26842684
ips = ips.values()
2685+
finally:
2686+
api_session.logout()
26852687

26862688
arguments = [
26872689
'nbd-http-server',

drivers/cleanup.py

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

302302
def __init__(self, session, srUuid):
303303
self.session = session
304-
self.apisession = None
304+
self._api_session = None
305305
if self.session is None:
306-
self.apisession = util.APISession("SM-cleanup-XAPI")
307-
self.session = self.apisession.session
306+
self._api_session = util.APISession("SM-cleanup-XAPI")
307+
self.session = self._api_session.session
308308
self._srRef = self.session.xenapi.SR.get_by_uuid(srUuid)
309309
self.srRecord = self.session.xenapi.SR.get_record(self._srRef)
310310
self.hostUuid = util.get_this_host()
@@ -313,8 +313,8 @@ def __init__(self, session, srUuid):
313313
self.task_progress = {"coalescable": 0, "done": 0}
314314

315315
def __del__(self):
316-
if self.apisession:
317-
self.apisession.logout()
316+
if self._api_session:
317+
self._api_session.logout()
318318

319319
@property
320320
def srRef(self):
@@ -2121,7 +2121,7 @@ def msg_cleared(self, xapi_session, msg_ref):
21212121
return msg is None
21222122

21232123
def check_no_space_candidates(self):
2124-
with util.APISession("SM-cleanup-SR-check_no_space_candidates") as xapi_session:
2124+
with util.APISession("GC-check_no_space") as xapi_session:
21252125
msg_id = self.xapi.srRecord["sm_config"].get(VDI.DB_GC_NO_SPACE)
21262126
if self.no_space_candidates:
21272127
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
@@ -221,7 +221,7 @@ def from_session(cls, session):
221221

222222
@classmethod
223223
def from_cli(cls):
224-
with util.APISession("SM-lcache-CacheFileSR") as session:
224+
with util.APISession("SM-local-cache") as session:
225225
return cls.from_session(session)
226226

227227
def statvfs(self):

drivers/linstorcowutil.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,8 @@ def get_local_cowutil(self, vdi_type):
678678
return instance
679679

680680
def _init_executor_thread(self):
681-
apisession = util.APISession("SM-linstorvhdutil")
682-
session = apisession.session
681+
api_session = util.APISession("SM-linstorvhdutil")
682+
session = api_session.session
683683
try:
684684
linstor = LinstorVolumeManager(
685685
self._uri,
@@ -692,7 +692,7 @@ def _init_executor_thread(self):
692692
except:
693693
self._executor_data.clear()
694694
raise
695-
self._loads.append(apisession)
695+
self._loads.append(api_session)
696696

697697
def _cleanup(self):
698698
for load in self._loads:

drivers/linstorvolumemanager.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def get_all_volume_openers(resource_name, volume) -> LinstorVolumeOpeners:
101101
volume = str(volume)
102102
openers = {}
103103

104-
with util.APISession("SM-linstorvolumemanager-get_all_volume_openers") as session:
104+
with util.APISession("SM-get-linstor-volume-openers") as session:
105105
hosts = session.xenapi.host.get_all_records()
106106
for host_ref, host_record in hosts.items():
107107
node_name = host_record['hostname']
@@ -190,8 +190,7 @@ def get_controller_node_name():
190190
if res:
191191
return res.groups()[0]
192192

193-
apisession = util.timeout(5, util.APISession, "SM-linstorvolumemanager-get_controller_node_name")
194-
session = apisession.session
193+
session = util.timeout(5, util.APISession, "SM-get-linstor-controller-node-name").session
195194

196195
for host_ref, host_record in session.xenapi.host.get_all_records().items():
197196
node_name = host_record['hostname']
@@ -214,8 +213,7 @@ def get_controller_node_name():
214213
def demote_drbd_resource(node_name, resource_name):
215214
PLUGIN_CMD = 'demoteDrbdResource'
216215

217-
apisession = util.timeout(5, util.APISession, "SM-linstorvolumemanager-demote_drbd_resource")
218-
session = apisession.session
216+
session = util.timeout(5, util.APISession, "SM-demote-drbd-resource").session
219217

220218
for host_ref, host_record in session.xenapi.host.get_all_records().items():
221219
if host_record['hostname'] != node_name:
@@ -1418,8 +1416,7 @@ def destroy(self):
14181416
# It needs to be done locally by each host so we go through the linstor-manager plugin.
14191417
# If we don't do this sometimes, the destroy will fail when trying to destroy the resource groups with:
14201418
# "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."
1421-
apisession = util.timeout(5, util.APISession, "SM-linstorvolumemanager-destroy")
1422-
session = apisession.session
1419+
session = util.timeout(5, util.APISession, "SM-linstor-destroy").session
14231420
for host_ref in session.xenapi.host.get_all():
14241421
try:
14251422
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
@@ -469,9 +469,10 @@ def ioretry_stat(path, maxretry=IORETRY_MAX):
469469

470470
def sr_get_capability(sr_uuid, session=None):
471471
result = []
472+
api_session = None
472473
if session is None:
473-
apisession = APISession("SM-util-sr_get_capability")
474-
session = apisession.session
474+
api_session = APISession("SM-sr-get-capability")
475+
session = api_session.session
475476
sr_ref = session.xenapi.SR.get_by_uuid(sr_uuid)
476477
sm_type = session.xenapi.SR.get_record(sr_ref)['type']
477478
sm_rec = session.xenapi.SM.get_all_records_where(
@@ -480,7 +481,8 @@ def sr_get_capability(sr_uuid, session=None):
480481
# SM expects at least one entry of any SR type
481482
if len(sm_rec) > 0:
482483
result = list(sm_rec.values())[0]['capabilities']
483-
484+
if api_session:
485+
session.logout()
484486
return result
485487

486488
def sr_get_driver_info(driver_info):
@@ -773,19 +775,17 @@ def getrootdevID():
773775
class APISession(contextlib.AbstractContextManager):
774776
def __init__(self, originator="SM"):
775777
self.originator = originator
776-
self.session = self.login()
778+
# First acquire a valid session
779+
self.session = self._login()
777780
SMlog("APISession [{}] login".format(self.originator), priority=LOG_DEBUG)
778-
atexit.register(self.atexit)
781+
atexit.register(self._atexit)
779782

780-
def login(self):
781-
# First acquire a valid session
783+
def _login(self):
782784
session = XenAPI.xapi_local()
783785
try:
784786
session.xenapi.login_with_password('root', '', '', self.originator)
785787
except Exception as exc:
786-
msg = f"APISession [{self.originator}] Unable to open local XAPI session"
787-
SMlog(msg, priority=LOG_ERR)
788-
raise xs_errors.XenError(msg) from exc
788+
raise xs_errors.XenError(f"APISession [{self.originator}] Unable to open local XAPI session") from exc
789789
return session
790790

791791
def _logout(self, log):
@@ -798,14 +798,14 @@ def _logout(self, log):
798798
self.session = None
799799

800800
def logout(self, log="logout"):
801-
atexit.unregister(self.atexit)
801+
atexit.unregister(self._atexit)
802802
self._logout(log=log)
803803

804804
def __del__(self):
805805
if self.session:
806806
self.logout(log="logout del")
807807

808-
def atexit(self):
808+
def _atexit(self):
809809
self._logout(log="logout atexit")
810810

811811
@override

tests/test_cleanup.py

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

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

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

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

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

0 commit comments

Comments
 (0)