Commit 86f28ce3 authored by Sebastien Gougeaud's avatar Sebastien Gougeaud Committed by Thomas Leibovici
Browse files

cli: add context manager methods to AdminClient



This will allow the embedding of initializer/finalizer functions with
the 'with' statement.

Change-Id: I0dc87b60b37548859bae661f8db4c8dc3a3b88e0
Signed-off-by: default avatarSebastien Gougeaud <sebastien.gougeaud@cea.fr>
Reviewed-on: https://gerrit.ccc.ocre.cea.fr/gerrit/c/phobos/+/6877

Tested-by: default avatarJenkins s8open_nr <s8open_nr@ccc.ocre.cea.fr>
Reviewed-by: default avatarQuentin Bouget <quentin.bouget@cea.fr>
Reviewed-by: default avatarThomas Leibovici <thomas.leibovici@cea.fr>
parent 15cbe254
......@@ -609,22 +609,16 @@ class DeviceOptHandler(BaseResourceOptHandler):
resources = self.params.get('res')
keep_locked = not self.params.get('unlock')
adm = AdminClient()
try:
adm.init(lrs_required=False)
for path in resources:
# TODO: this will be dropped when adding to DSS will be done by
# adm.device_add() below
dev_family, serial = self.client.devices.add(self.family, path,
with AdminClient(lrs_required=False) as adm:
for path in resources:
# TODO: this will be dropped when adding to DSS will be
# done by adm.device_add() below
_, serial = self.client.devices.add(self.family, path,
locked=keep_locked)
adm.device_add(dev_family, serial)
adm.fini()
adm.device_add(self.family, serial)
except EnvironmentError as err:
self.logger.error("Cannot add device: %s",
env_error_format(err))
adm.fini()
self.logger.error("Cannot add device: %s", env_error_format(err))
sys.exit(os.EX_DATAERR)
self.logger.info("Added %d device(s) successfully", len(resources))
......@@ -782,21 +776,17 @@ class MediaOptHandler(BaseResourceOptHandler):
media_list = NodeSet.fromlist(self.params.get('res'))
fs_type = self.params.get('fs')
unlock = self.params.get('unlock')
adm = AdminClient()
try:
adm.init(lrs_required=True)
if unlock:
self.logger.debug("Post-unlock enabled")
for name in media_list:
self.logger.info("Formatting media '%s'", name)
adm.fs_format(name, fs_type, unlock=unlock)
try:
with AdminClient(lrs_required=True) as adm:
if unlock:
self.logger.debug("Post-unlock enabled")
for name in media_list:
self.logger.info("Formatting media '%s'", name)
adm.fs_format(name, fs_type, unlock=unlock)
except EnvironmentError as err:
# XXX add an option to exit on first error
self.logger.error("fs_format: %s", env_error_format(err))
finally:
adm.fini()
def exec_list(self):
"""List media and display results."""
......@@ -908,25 +898,21 @@ class DirOptHandler(MediaOptHandler):
resources = self.params.get('res')
keep_locked = not self.params.get('unlock')
tags = self.params.get('tags', [])
adm = AdminClient()
try:
adm.init(lrs_required=False)
for path in resources:
# Remove any trailing slash
path = path.rstrip('/')
self.client.media.add(self.family, 'POSIX', None, path,
locked=keep_locked, tags=tags)
# Add device unlocked and rely on media locking
_, serial = self.client.devices.add(self.family, path,
locked=False)
adm.device_add(self.family, serial)
self.logger.debug("Added directory '%s'", path)
adm.fini()
with AdminClient(lrs_required=False) as adm:
for path in resources:
# Remove any trailing slash
path = path.rstrip('/')
self.client.media.add(self.family, 'POSIX', None, path,
locked=keep_locked, tags=tags)
# Add device unlocked and rely on media locking
_, serial = self.client.devices.add(self.family, path,
locked=False)
adm.device_add(self.family, serial)
self.logger.debug("Added directory '%s'", path)
except EnvironmentError as err:
self.logger.error("Cannot add directory: %s", env_error_format(err))
sys.exit(os.EX_DATAERR)
......
......@@ -44,10 +44,18 @@ class AdminHandle(Structure):
class Client(object):
"""Wrapper on the phobos admin client"""
def __init__(self):
def __init__(self, lrs_required=True):
super(Client, self).__init__()
self.lrs_required = lrs_required
self.handle = None
def __enter__(self):
self.init(self.lrs_required)
return self
def __exit__(self, exc_type, exc_value, traceback):
self.fini()
def init(self, lrs_required):
if self.handle is not None:
self.fini()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment