Skip to content

Commit 8173b33

Browse files
committed
Make login reentrant.
Also fix some strings. Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
1 parent 9cd74da commit 8173b33

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
@@ -705,19 +705,23 @@ def getrootdevID():
705705

706706

707707
class APISession(contextlib.AbstractContextManager):
708+
session=None
709+
708710
def __init__(self, originator="SM"):
709711
self.originator = originator
710712
# First acquire a valid session
711713
self.session = self._login()
712-
SMlog("APISession [{}] login".format(self.originator), priority=LOG_DEBUG)
713714
atexit.register(self._atexit)
714715

715716
def _login(self):
717+
if self.session:
718+
return self.session
716719
session = XenAPI.xapi_local()
717720
try:
718721
session.xenapi.login_with_password('root', '', '', self.originator)
719722
except Exception as exc:
720723
raise xs_errors.XenError(f"APISession [{self.originator}] Unable to open local XAPI session") from exc
724+
SMlog("APISession [{}] login".format(self.originator), priority=LOG_DEBUG)
721725
return session
722726

723727
def _logout(self, log):
@@ -742,8 +746,7 @@ def _atexit(self):
742746

743747
@override
744748
def __enter__(self):
745-
if not self.session:
746-
raise SyntaxError("Session is already closed: wrong usage of context.")
749+
self.session = self._login()
747750
return self.session
748751

749752
@override
@@ -1384,7 +1387,7 @@ def is_active(self, name):
13841387
return os.path.exists("/tmp/fist_%s" % name)
13851388

13861389
def mark_sr(self, name, sruuid, started):
1387-
with APISession("SM-util-FistPoint-mark_sr") as session:
1390+
with APISession("SM-sr-fist-point") as session:
13881391
sr = session.xenapi.SR.get_by_uuid(sruuid)
13891392
if started:
13901393
session.xenapi.SR.add_to_other_config(sr, name, "active")

0 commit comments

Comments
 (0)