Skip to content

Commit c4d891b

Browse files
committed
Cosmetic, consistency, readability, and one better logout handling.
Reworked some APISession originator for better consistency. Removed unuseful `session` intermediate variables. Reworked restevdis to avoid a huge indentation caused by APISession, by using a function. Adds a try/finally for session.logout() in util.py#sr_get_capability(). Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
1 parent d1d6cb5 commit c4d891b

6 files changed

Lines changed: 65 additions & 60 deletions

File tree

drivers/blktap2.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -882,18 +882,18 @@ def abort_linstor_gc(drbd_path: str) -> bool:
882882

883883
openers = get_all_volume_openers(volume_name, "0")
884884

885-
api_session = util.timeout(5, util.APISession, "blktap-abort_linstor_gc")
886-
session = api_session.session
885+
api_session = util.timeout(5, util.APISession, "SM-blktap2-abort_linstor_gc")
887886
try:
888-
srs = util.get_linstor_srs_uuid(session)
887+
srs = util.get_linstor_srs_uuid(api_session.session)
889888
pbd_ref = util.find_pbd_ref_from_dconf_value(
890-
session, srs, "group-name", group_name, LinstorVolumeManager.build_group_name
889+
api_session.session, srs, "group-name", group_name,
890+
LinstorVolumeManager.build_group_name,
891891
)
892892
if pbd_ref:
893-
pbd_rec = session.xenapi.PBD.get_record(pbd_ref)
893+
pbd_rec = api_session.session.xenapi.PBD.get_record(pbd_ref)
894894

895895
sr_ref = pbd_rec["SR"]
896-
sr_uuid = session.xenapi.SR.get_uuid(sr_ref)
896+
sr_uuid = api_session.session.xenapi.SR.get_uuid(sr_ref)
897897

898898
import cleanup # pylint: disable=C0415
899899
if cleanup.LinstorSR.abort_gc_from_openers_sr(sr_uuid, openers):

drivers/cleanup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def __init__(self, session, srUuid):
303303
self.session = session
304304
self._api_session = None
305305
if self.session is None:
306-
self._api_session = util.APISession("SM-cleanup-XAPI")
306+
self._api_session = util.APISession("SM-GC")
307307
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)
@@ -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("GC-check_no_space") as xapi_session:
2124+
with util.APISession("SM-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):
@@ -3928,7 +3928,7 @@ def _abort_gc_from_openers(uuid: str, is_vdi_uuid: bool, openers: "LinstorVolume
39283928
if node_name == hostname:
39293929
continue
39303930

3931-
with util.timeout(5), util.APISession("GC-coalescing") as session:
3931+
with util.timeout(5), util.APISession("SM-GC-coalescing") as session:
39323932
sr_uuid = util.get_sr_uuid_from_vdi_uuid(session, uuid) if is_vdi_uuid else uuid
39333933
util.SMlog(f"LINSTOR volume is coalescing on `{sr_uuid}`. We're going to interrupt the GC...")
39343934
return util.strtobool(session.xenapi.host.call_plugin(

drivers/linstorcowutil.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,6 @@ def get_local_cowutil(self, vdi_type):
679679

680680
def _init_executor_thread(self):
681681
api_session = util.APISession("SM-linstorvhdutil")
682-
session = api_session.session
683682
try:
684683
linstor = LinstorVolumeManager(
685684
self._uri,
@@ -688,7 +687,7 @@ def _init_executor_thread(self):
688687
logger=util.SMlog
689688
)
690689
self._executor_data.linstor = linstor
691-
self._executor_data.session = session
690+
self._executor_data.session = api_session.session
692691
except:
693692
self._executor_data.clear()
694693
raise

drivers/mpathcount.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ def add(key, val):
282282
try:
283283
with util.APISession("SM-mpathcount") as api_session:
284284
main(api_session)
285-
except xs_errors.XenError:
285+
except xs_errors.XenError as e:
286+
util.SMlog(f"MPATH: Failure `{e}`")
286287
sys.exit(-1)
287288
sys.exit(0)

drivers/resetvdis.py

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -139,43 +139,46 @@ def usage():
139139
sys.exit(1)
140140

141141

142+
def main(session):
143+
mode = sys.argv[1]
144+
if mode == "all":
145+
if len(sys.argv) not in [4, 5]:
146+
usage()
147+
host_uuid = sys.argv[2]
148+
sr_uuid = sys.argv[3]
149+
is_master = False
150+
if len(sys.argv) == 5:
151+
if sys.argv[4] == "--master":
152+
is_master = True
153+
else:
154+
usage()
155+
reset_sr(session, host_uuid, sr_uuid, is_master)
156+
elif mode == "single":
157+
vdi_uuid = sys.argv[2]
158+
force = False
159+
if len(sys.argv) == 4 and sys.argv[3] == "--force":
160+
force = True
161+
reset_vdi(session, vdi_uuid, force)
162+
elif len(sys.argv) in [3, 4]:
163+
# backwards compatibility: the arguments for the "all" case used to be
164+
# just host_uuid, sr_uuid, [is_master] (i.e., no "all" string, since it
165+
# was the only mode available). To avoid having to change XAPI, accept
166+
# the old format here as well.
167+
host_uuid = sys.argv[1]
168+
sr_uuid = sys.argv[2]
169+
is_master = False
170+
if len(sys.argv) == 4:
171+
if sys.argv[3] == "--master":
172+
is_master = True
173+
else:
174+
usage()
175+
reset_sr(session, host_uuid, sr_uuid, is_master)
176+
else:
177+
usage()
178+
142179
if __name__ == '__main__':
143180
if len(sys.argv) not in [3, 4, 5]:
144181
usage()
145182

146183
with util.APISession("SM-resetvdis") as session:
147-
mode = sys.argv[1]
148-
if mode == "all":
149-
if len(sys.argv) not in [4, 5]:
150-
usage()
151-
host_uuid = sys.argv[2]
152-
sr_uuid = sys.argv[3]
153-
is_master = False
154-
if len(sys.argv) == 5:
155-
if sys.argv[4] == "--master":
156-
is_master = True
157-
else:
158-
usage()
159-
reset_sr(session, host_uuid, sr_uuid, is_master)
160-
elif mode == "single":
161-
vdi_uuid = sys.argv[2]
162-
force = False
163-
if len(sys.argv) == 4 and sys.argv[3] == "--force":
164-
force = True
165-
reset_vdi(session, vdi_uuid, force)
166-
elif len(sys.argv) in [3, 4]:
167-
# backwards compatibility: the arguments for the "all" case used to be
168-
# just host_uuid, sr_uuid, [is_master] (i.e., no "all" string, since it
169-
# was the only mode available). To avoid having to change XAPI, accept
170-
# the old format here as well.
171-
host_uuid = sys.argv[1]
172-
sr_uuid = sys.argv[2]
173-
is_master = False
174-
if len(sys.argv) == 4:
175-
if sys.argv[3] == "--master":
176-
is_master = True
177-
else:
178-
usage()
179-
reset_sr(session, host_uuid, sr_uuid, is_master)
180-
else:
181-
usage()
184+
main(session)

drivers/util.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -469,19 +469,21 @@ def ioretry_stat(path, maxretry=IORETRY_MAX):
469469
def sr_get_capability(sr_uuid, session=None):
470470
result = []
471471
api_session = None
472-
if session is None:
473-
api_session = APISession("SM-sr-get-capability")
474-
session = api_session.session
475-
sr_ref = session.xenapi.SR.get_by_uuid(sr_uuid)
476-
sm_type = session.xenapi.SR.get_record(sr_ref)['type']
477-
sm_rec = session.xenapi.SM.get_all_records_where(
478-
"field \"type\" = \"%s\"" % sm_type)
479-
480-
# SM expects at least one entry of any SR type
481-
if len(sm_rec) > 0:
482-
result = list(sm_rec.values())[0]['capabilities']
483-
if api_session:
484-
session.logout()
472+
try:
473+
if session is None:
474+
api_session = APISession("SM-sr-get-capability")
475+
session = api_session.session
476+
sr_ref = session.xenapi.SR.get_by_uuid(sr_uuid)
477+
sm_type = session.xenapi.SR.get_record(sr_ref)['type']
478+
sm_rec = session.xenapi.SM.get_all_records_where(
479+
"field \"type\" = \"%s\"" % sm_type)
480+
481+
# SM expects at least one entry of any SR type
482+
if len(sm_rec) > 0:
483+
result = list(sm_rec.values())[0]['capabilities']
484+
finally:
485+
if api_session:
486+
session.logout()
485487
return result
486488

487489
def sr_get_driver_info(driver_info):

0 commit comments

Comments
 (0)