- def delete_flavor(self, flavor=None, name=None):
- try:
- if not flavor:
- flavor = self.find_flavor(name)
- flavor.delete()
- return True
- except Exception:
- return False
-
- def normalize_az_host(self, az, host):
- if not az:
- az = self.config.availability_zone
- return az + ':' + host
-
- def auto_fill_az(self, host_list, host):
- '''
- no az provided, if there is a host list we can auto-fill the az
- else we use the configured az if available
- else we return an error
- '''
- if host_list:
- for hyp in host_list:
- if hyp.host == host:
- return self.normalize_az_host(hyp.zone, host)
- # no match on host
- LOG.error('Passed host name does not exist: ' + host)
- return None
- if self.config.availability_zone:
- return self.normalize_az_host(None, host)
- LOG.error('--hypervisor passed without an az and no az configured')
- return None
-
- def sanitize_az_host(self, host_list, az_host):
- '''
- host_list: list of hosts as retrieved from openstack (can be empty)
- az_host: either a host or a az:host string
- if a host, will check host is in the list, find the corresponding az and
- return az:host
- if az:host is passed will check the host is in the list and az matches
- if host_list is empty, will return the configured az if there is no
- az passed
- '''
- if ':' in az_host:
- # no host_list, return as is (no check)
- if not host_list:
- return az_host
- # if there is a host_list, extract and verify the az and host
- az_host_list = az_host.split(':')
- zone = az_host_list[0]
- host = az_host_list[1]
- for hyp in host_list:
- if hyp.host == host:
- if hyp.zone == zone:
- # matches
- return az_host
- # else continue - another zone with same host name?
- # no match
- LOG.error('No match for availability zone and host ' + az_host)
- return None
- else:
- return self.auto_fill_az(host_list, az_host)
-
- #
- # Return a list of 0, 1 or 2 az:host
- #
- # The list is computed as follows:
- # The list of all hosts is retrieved first from openstack
- # if this fails, checks and az auto-fill are disabled
- #
- # If the user provides a list of hypervisors (--hypervisor)
- # that list is checked and returned
- #
- # If the user provides a configured az name (config.availability_zone)
- # up to the first 2 hosts from the list that match the az are returned
- #
- # If the user did not configure an az name
- # up to the first 2 hosts from the list are returned
- # Possible return values:
- # [ az ]
- # [ az:hyp ]
- # [ az1:hyp1, az2:hyp2 ]
- # [] if an error occurred (error message printed to console)
- #
- def get_az_host_list(self):
- avail_list = []
- host_list = []
-
- try:
- host_list = self.novaclient.services.list()
- except novaclient.exceptions.Forbidden:
- LOG.warning('Operation Forbidden: could not retrieve list of hosts'
- ' (likely no permission)')
-
- for host in host_list:
- # this host must be a compute node
- if host.binary != 'nova-compute' or host.state != 'up':
- continue
- candidate = None
- if self.config.availability_zone:
- if host.zone == self.config.availability_zone:
- candidate = self.normalize_az_host(None, host.host)
- else:
- candidate = self.normalize_az_host(host.zone, host.host)
- if candidate:
- avail_list.append(candidate)
- # pick first 2 matches at most
- if len(avail_list) == 2:
- break
-
- # if empty we insert the configured az
- if not avail_list:
-
- if not self.config.availability_zone:
- LOG.error('Availability_zone must be configured')
- elif host_list:
- LOG.error('No host matching the selection for availability zone: ' +
- self.config.availability_zone)
- avail_list = []
- else:
- avail_list = [self.config.availability_zone]
- return avail_list
-
- def get_enabled_az_host_list(self, required_count=1):
- """
- Check which hypervisors are enabled and on which compute nodes they are running.
- Pick required count of hosts.