Skip to content

Commit

Permalink
'disabled' field to use in host plugin and admins can create lease wh…
Browse files Browse the repository at this point in the history
…en host is non reservable

When disabled key is present in extra capabilities handle it by setting it or unsetting it based on the value for the key

When disabled field is set to True for a host, the host also changes its reservable status to False.

Disabled key can only be set by an admin

An admin can create a lease on a host that is non reservable
  • Loading branch information
AnishReddyRavula committed Nov 17, 2023
1 parent 8f52fe8 commit e62ed0a
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions blazar/plugins/oshosts/host_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ def create_computehost(self, host_values):
# Do not use nova's primary key for this host.
# Instead, generate a new one.
del host_details['id']
if 'disabled' in host_values:
self.handle_disabled_key(host_id, host_values['disabled'])
del host_values['disabled']
# NOTE(sbauza): Only last duplicate name for same extra capability
# will be stored
to_store = set(host_values.keys()) - set(host_details.keys())
Expand Down Expand Up @@ -420,7 +423,9 @@ def update_computehost(self, host_id, values):
# nothing to update
if not values:
return self.get_computehost(host_id)

if 'disabled' in values:
self.handle_disabled_key(host_id, values['disabled'])
del values['disabled']
cant_update_extra_capability = []
cant_delete_extra_capability = []
previous_capabilities = self._get_extra_capabilities(host_id)
Expand Down Expand Up @@ -515,6 +520,24 @@ def delete_computehost(self, host_id):
# they have to rerun
raise manager_ex.CantDeleteHost(host=host_id, msg=str(e))

def handle_disabled_key(self, host_id, value):
# only admin can set/unset 'disabled' flag
if self._is_admin():
host_details = self.get_computehost(host_id)
new_disabled_flag = False if value is None else True
self.set_disabled(host_details, new_disabled_flag)

def set_disabled(self, resource, is_disabled):
host_update_values = {"disabled": is_disabled}
# if a host is set as disabled, then it should not be reservable
if is_disabled:
host_update_values['reservable'] = False
db_api.host_update(resource["id"], host_update_values)
LOG.warn(
'%s %s.', resource["hypervisor_hostname"],
"is set disabled" if is_disabled else "is not disabled"
)

def list_allocations(self, query, detail=False):
hosts_id_list = [h['id'] for h in db_api.host_list()]
options = self.get_query_options(query, QUERY_TYPE_ALLOCATION)
Expand Down Expand Up @@ -635,6 +658,11 @@ def allocation_candidates(self, values):

return host_ids

def _is_admin(self):
ctx = context.current()
ctx_dict = ctx.dict()
return ctx_dict.get('is_admin', False)

def _matching_hosts(self, hypervisor_properties, resource_properties,
count_range, start_date, end_date, project_id):
"""Return the matching hosts (preferably not allocated)
Expand All @@ -658,7 +686,12 @@ def _matching_hosts(self, hypervisor_properties, resource_properties,
if resource_properties:
filter_array += plugins_utils.convert_requirements(
resource_properties)
for host in db_api.reservable_host_get_all_by_queries(filter_array):
# admin can create a lease for host with 'reservable' False
if self._is_admin():
hosts = db_api.host_get_all_by_queries(filter_array)
else:
hosts = db_api.reservable_host_get_all_by_queries(filter_array)
for host in hosts:
if not self.is_project_allowed(project_id, resource_properties):
continue
if not db_api.host_allocation_get_all_by_values(
Expand Down Expand Up @@ -905,6 +938,9 @@ def notification_callback(self, event_type, payload):
return reservation_flags

def set_reservable(self, resource, is_reservable):
if resource['disabled']:
LOG.warn(f"{resource['hypervisor_hostname']} is disabled - cannot set reservable")
return
db_api.host_update(resource["id"], {"reservable": is_reservable})
LOG.warn('%s %s.', resource["hypervisor_hostname"],
"recovered" if is_reservable else "failed")
Expand Down

0 comments on commit e62ed0a

Please sign in to comment.