Commit 986afb27 authored by Sebastien Gougeaud's avatar Sebastien Gougeaud Committed by Thomas Leibovici
Browse files

refactor: move dss "drive lock" code to the admin module



Now that admin operations require to notify the daemon if it is online,
we will try to remove all dss-related code from the CLI to add it in the
admin module and let the CLI only be a command line interpreter.

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

Tested-by: default avatarJenkins s8open_nr <s8open_nr@ccc.ocre.cea.fr>
Reviewed-by: default avatarQuentin Bouget <quentin.bouget@cea.fr>
Reviewed-by: default avatarPatrice LUCAS <patrice.lucas@cea.fr>
Reviewed-by: default avatarThomas Leibovici <thomas.leibovici@cea.fr>
parent 0b4e6a63
......@@ -183,8 +183,9 @@ static int _get_device_by_id(struct admin_handle *adm, struct pho_id *dev_id,
return 0;
}
static int _device_unlock(struct admin_handle *adm, struct pho_id *dev_ids,
int num_dev, bool is_forced)
static int _device_update_adm_status(struct admin_handle *adm,
struct pho_id *dev_ids, int num_dev,
enum rsc_adm_status status, bool is_forced)
{
struct dev_info *devices;
int avail_devices = 0;
......@@ -219,17 +220,23 @@ static int _device_unlock(struct admin_handle *adm, struct pho_id *dev_ids,
* the update of a single database field will be implemented
*/
/* if (strcmp(dev_res->lock.lock, "") && !is_forced) {
* pho_error(-EBUSY, "Device '%s' is in use by '%s'", dev_ids[i].name,
* dev_res->lock.lock);
* dss_res_free(dev_res, 1);
* continue;
/* if (strcmp(dev_res->lock.lock, "")) {
* if (!is_forced) {
* pho_error(-EBUSY, "Device '%s' is in use by '%s'",
* dev_ids[i].name, dev_res->lock.lock);
* dss_res_free(dev_res, 1);
* continue;
* } else if (status == PHO_RSC_ADM_ST_LOCKED) {
* pho_warn("Device '%s' is in use. Administrative locking will "
* "not be effective immediately", dev_res->rsc.id.name);
* }
* }
*/
if (dev_res->rsc.adm_status == PHO_RSC_ADM_ST_UNLOCKED)
pho_warn("Device '%s' is already unlocked", dev_ids[i].name);
if (dev_res->rsc.adm_status == status)
pho_warn("Device '%s' is already in the desired state",
dev_ids[i].name);
dev_res->rsc.adm_status = PHO_RSC_ADM_ST_UNLOCKED;
dev_res->rsc.adm_status = status;
dev_info_cpy(devices + i, dev_res);
dss_res_free(dev_res, 1);
......@@ -347,16 +354,17 @@ int phobos_admin_device_add(struct admin_handle *adm, enum rsc_family family,
return 0;
}
/**
* TODO: admin_device_lock will have the responsability to update the device
* state, to then remove this part of code from the CLI.
*/
int phobos_admin_device_lock(struct admin_handle *adm, struct pho_id *dev_ids,
int num_dev)
int num_dev, bool is_forced)
{
int rc = 0;
int rc;
int i;
rc = _device_update_adm_status(adm, dev_ids, num_dev,
PHO_RSC_ADM_ST_LOCKED, is_forced);
if (rc)
return rc;
if (!adm->daemon_is_online)
return 0;
......@@ -379,7 +387,8 @@ int phobos_admin_device_unlock(struct admin_handle *adm, struct pho_id *dev_ids,
int rc;
int i;
rc = _device_unlock(adm, dev_ids, num_dev, is_forced);
rc = _device_update_adm_status(adm, dev_ids, num_dev,
PHO_RSC_ADM_ST_UNLOCKED, is_forced);
if (rc)
return rc;
......
......@@ -625,50 +625,17 @@ class DeviceOptHandler(BaseResourceOptHandler):
def exec_lock(self):
"""Device lock"""
serials = self.params.get('res')
devices = []
for serial in serials:
device = self.filter(serial)
if not device:
self.logger.error("Device '%s' not found", serial)
sys.exit(os.EX_DATAERR)
assert len(device) == 1
try:
self.client.devices.lock([device[0]])
except EnvironmentError as err:
self.logger.error("Failed to lock device '%s': %s", serial,
env_error_format(err))
continue
device = self.filter(serial)
assert len(device) == 1
device[0].rsc.adm_status = PHO_RSC_ADM_ST_LOCKED
devices.append(device[0])
if len(devices) != len(serials):
self.logger.error("At least one device is in use, use --force")
sys.exit(os.EX_DATAERR)
names = self.params.get('res')
try:
self.client.devices.update(devices)
with AdminClient(lrs_required=False) as adm:
adm.device_lock(self.family, names, self.params.get('force'))
except EnvironmentError as err:
self.logger.error("Failed to lock device(s): %s",
env_error_format(err))
sys.exit(os.EX_DATAERR)
finally:
self.client.devices.unlock(devices)
try:
with AdminClient(lrs_required=False) as adm:
adm.device_lock(self.family, serials)
except EnvironmentError as err:
self.logger.error("Failed to notify: %s", env_error_format(err))
sys.exit(os.EX_DATAERR)
self.logger.info("%d device(s) locked", len(devices))
self.logger.info("%d device(s) locked", len(names))
def exec_unlock(self):
"""Device unlock"""
......
......@@ -100,16 +100,16 @@ class Client(object):
raise EnvironmentError(rc, "Cannot add device '%s' to the LRS"
% dev_name)
def device_lock(self, dev_family, dev_names):
"""Inform the daemon that devices are locked."""
def device_lock(self, dev_family, dev_names, is_forced):
"""Wrapper for the device lock command."""
c_id = Id * len(dev_names)
dev_ids = [Id(dev_family, name) for name in dev_names]
rc = LIBPHOBOS_ADMIN.phobos_admin_device_lock(byref(self.handle),
c_id(*dev_ids),
len(dev_ids))
len(dev_ids), is_forced)
if rc:
raise EnvironmentError(rc, "Cannot notify the daemon")
raise EnvironmentError(rc, "Error during device lock")
def device_unlock(self, dev_family, dev_names, is_forced):
"""Wrapper for the device unlock command."""
......
......@@ -77,17 +77,19 @@ int phobos_admin_device_add(struct admin_handle *adm, enum rsc_family family,
const char *name);
/**
* Inform the LRS devices state has changed to 'locked'.
* Update the administrative state of the given devices to 'locked' and
* inform the LRS devices state has changed.
*
* \param[in] adm Admin module handler.
* \param[in] dev_ids Device IDs to lock.
* \param[in] num_dev Number of device to lock.
* \param[in] is_forced true if forced lock is requested.
*
* \return 0 on success,
* -errno on failure.
*/
int phobos_admin_device_lock(struct admin_handle *adm, struct pho_id *dev_ids,
int num_dev);
int num_dev, bool is_forced);
/**
* Update the administrative state of the given devices to 'unlocked' and
......
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