Skip to content

Commit 305d84f

Browse files
committed
Make login reentrant.
Also fix some strings. Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
1 parent 334f50c commit 305d84f

3 files changed

Lines changed: 17 additions & 12 deletions

File tree

drivers/coalesce-leaf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#
33
# Copyright (C) Citrix Systems Inc.
44
#
5-
# This program is free software; you can redistribute it and/or modify
6-
# it under the terms of the GNU Lesser General Public License as published
5+
# This program is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU Lesser General Public License as published
77
# by the Free Software Foundation; version 2.1 only.
88
#
99
# This program is distributed in the hope that it will be useful,

drivers/mpathcount.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,8 @@ def _run_command(command, timeout):
220220
util.SMlog(f"XAPI health check failed: {stderr}")
221221
return returncode == 0
222222

223-
if __name__ == '__main__':
224-
try:
225-
session = util.APISession("SM-mpathcount").session
226-
except xs_errors.XenError:
227-
sys.exit(-1)
228-
223+
def main(session):
224+
global mpath_enabled
229225
localhost = session.xenapi.host.get_by_uuid(get_localhost_uuid())
230226
check_xapi_is_enabled()
231227
# Check whether multipathing is enabled (either for root dev or SRs)
@@ -282,4 +278,10 @@ def add(key, val):
282278

283279
util.SMlog("MPATH: Update done")
284280

281+
if __name__ == '__main__':
282+
try:
283+
with util.APISession("SM-mpathcount") as api_session:
284+
main(api_session)
285+
except xs_errors.XenError:
286+
sys.exit(-1)
285287
sys.exit(0)

drivers/util.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,19 +773,23 @@ def getrootdevID():
773773

774774

775775
class APISession(contextlib.AbstractContextManager):
776+
session=None
777+
776778
def __init__(self, originator="SM"):
777779
self.originator = originator
778780
# First acquire a valid session
779781
self.session = self._login()
780-
SMlog("APISession [{}] login".format(self.originator), priority=LOG_DEBUG)
781782
atexit.register(self._atexit)
782783

783784
def _login(self):
785+
if self.session:
786+
return self.session
784787
session = XenAPI.xapi_local()
785788
try:
786789
session.xenapi.login_with_password('root', '', '', self.originator)
787790
except Exception as exc:
788791
raise xs_errors.XenError(f"APISession [{self.originator}] Unable to open local XAPI session") from exc
792+
SMlog("APISession [{}] login".format(self.originator), priority=LOG_DEBUG)
789793
return session
790794

791795
def _logout(self, log):
@@ -810,8 +814,7 @@ def _atexit(self):
810814

811815
@override
812816
def __enter__(self):
813-
if not self.session:
814-
raise SyntaxError("Session is already closed: wrong usage of context.")
817+
self.session = self._login()
815818
return self.session
816819

817820
@override
@@ -1488,7 +1491,7 @@ def is_active(self, name):
14881491
return os.path.exists("/tmp/fist_%s" % name)
14891492

14901493
def mark_sr(self, name, sruuid, started):
1491-
with APISession("SM-util-FistPoint-mark_sr") as session:
1494+
with APISession("SM-sr-fist-point") as session:
14921495
sr = session.xenapi.SR.get_by_uuid(sruuid)
14931496
if started:
14941497
session.xenapi.SR.add_to_other_config(sr, name, "active")

0 commit comments

Comments
 (0)