def add_computehost(self, pool, host, stay_in=False): """Add a compute host to an aggregate. The `host` must exist otherwise raise an error and the `host` must be in the freepool. :param pool: Name or UUID of the pool to rattach the host :param host: Name (not UUID) of the host to associate :type host: str Return the related aggregate. Raise an aggregate exception if something wrong. """ agg = self.get_aggregate_from_name_or_id(pool) try: freepool_agg = self.get(self.freepool_name) except manager_exceptions.AggregateNotFound: raise manager_exceptions.NoFreePool() if freepool_agg.id != agg.id and not stay_in: if host not in freepool_agg.hosts: raise manager_exceptions.HostNotInFreePool( host=host, freepool_name=freepool_agg.name) LOG.info( "removing host '%(host)s' from aggregate freepool " "%(name)s", { 'host': host, 'name': freepool_agg.name }) try: self.remove_computehost(freepool_agg.id, host) except nova_exception.NotFound: raise manager_exceptions.HostNotFound(host=host) LOG.info("adding host '%(host)s' to aggregate %(id)s", { 'host': host, 'id': agg.id }) try: return self.nova.aggregates.add_host(agg.id, host) except nova_exception.NotFound: raise manager_exceptions.HostNotFound(host=host) except nova_exception.Conflict: raise manager_exceptions.AggregateAlreadyHasHost(pool=pool, host=host)
def remove_computehost(self, pool, hosts): """Remove compute host(s) from an aggregate.""" if not isinstance(hosts, list): hosts = [hosts] agg = self.get_aggregate_from_name_or_id(pool) try: freepool_agg = self.get(self.freepool_name) except manager_exceptions.AggregateNotFound: raise manager_exceptions.NoFreePool() hosts_failing_to_remove = [] hosts_failing_to_add = [] hosts_not_in_freepool = [] for host in hosts: if freepool_agg.id == agg.id: if host not in freepool_agg.hosts: hosts_not_in_freepool.append(host) continue try: self.nova.aggregates.remove_host(agg.id, host) except nova_exception.ClientException: hosts_failing_to_remove.append(host) if freepool_agg.id != agg.id and host not in freepool_agg.hosts: # NOTE(sbauza) : We don't want to put again the host in # freepool if the requested pool is the freepool... try: self.nova.aggregates.add_host(freepool_agg.id, host) except nova_exception.ClientException: hosts_failing_to_add.append(host) if hosts_failing_to_remove: raise manager_exceptions.CantRemoveHost( host=hosts_failing_to_remove, pool=agg) if hosts_failing_to_add: raise manager_exceptions.CantAddHost(host=hosts_failing_to_add, pool=freepool_agg) if hosts_not_in_freepool: raise manager_exceptions.HostNotInFreePool( host=hosts_not_in_freepool, freepool_name=freepool_agg.name)
def add_computehost(self, pool, hosts, stay_in=False): """Add compute host(s) to an aggregate. Each host must exist and be in the freepool, otherwise raise an error. :param pool: Name or UUID of the pool to rattach the host :param hosts: Names (not UUID) of hosts to associate :type host: str or list of str Return the related aggregate. Raise an aggregate exception if something wrong. """ if not isinstance(hosts, list): hosts = [hosts] added_hosts = [] removed_hosts = [] agg = self.get_aggregate_from_name_or_id(pool) try: freepool_agg = self.get(self.freepool_name) except manager_exceptions.AggregateNotFound: raise manager_exceptions.NoFreePool() try: for host in hosts: if freepool_agg.id != agg.id and not stay_in: if host not in freepool_agg.hosts: raise manager_exceptions.HostNotInFreePool( host=host, freepool_name=freepool_agg.name) LOG.info( "removing host '%(host)s' from freepool " "aggregate %(name)s", { 'host': host, 'name': freepool_agg.name }) try: self.remove_computehost(freepool_agg.id, host) removed_hosts.append(host) except nova_exception.NotFound: raise manager_exceptions.HostNotFound(host=host) LOG.info("adding host '%(host)s' to aggregate %(id)s", { 'host': host, 'id': agg.id }) try: self.nova.aggregates.add_host(agg.id, host) added_hosts.append(host) except nova_exception.NotFound: raise manager_exceptions.HostNotFound(host=host) except nova_exception.Conflict as e: raise manager_exceptions.AggregateAlreadyHasHost( pool=pool, host=host, nova_exception=str(e)) except Exception as e: if added_hosts: LOG.warn('Removing hosts added to aggregate %s: %s', agg.id, added_hosts) for host in added_hosts: self.nova.aggregates.remove_host(agg.id, host) if removed_hosts: LOG.warn('Adding hosts back to freepool: %s', removed_hosts) for host in removed_hosts: self.nova.aggregates.add_host(freepool_agg.id, host) raise e return self.get_aggregate_from_name_or_id(pool)