Example #1
0
    def on_end(self, resource_id):
        """Remove the hosts from the pool."""
        reservations = db_api.reservation_get_all_by_values(
            resource_id=resource_id)
        for reservation in reservations:
            if reservation['status'] not in ['completed', 'deleted']:
                allocations = db_api.host_allocation_get_all_by_values(
                    reservation_id=reservation['id'])
                pool = rp.ReservationPool()
                for allocation in allocations:
                    db_api.host_allocation_destroy(allocation['id'])
                    hyp = self.nova.hypervisors.get(
                            self._get_hypervisor_from_name_or_id(
                            allocation['compute_host_id'])
                    )
                    if hyp.__dict__['running_vms'] > 0:
                        hyp = self.nova.hypervisors.search(hyp.__dict__['hypervisor_hostname'], servers=True)
                        for server in hyp[0].__dict__['servers']:
                            s = self.nova.servers.get(server['uuid'])
                            s.delete()
                pool.delete(reservation['resource_id'])

            db_api.reservation_update(reservation['id'],
                                      {'status': 'completed'})
            host_reservation = db_api.host_reservation_get_by_reservation_id(
                reservation['id'])
            db_api.host_reservation_update(host_reservation['id'],
                                           {'status': 'completed'})
 def on_start(self, resource_id):
     """Add the hosts in the pool."""
     reservations = db_api.reservation_get_all_by_values(
         resource_id=resource_id)
     for reservation in reservations:
         pool = rp.ReservationPool()
         for allocation in db_api.host_allocation_get_all_by_values(
                 reservation_id=reservation['id']):
             host = db_api.host_get(allocation['compute_host_id'])
             pool.add_computehost(reservation['resource_id'],
                                  host['service_name'])
Example #3
0
 def on_start(self, resource_id):
     """Add the hosts in the pool."""
     reservations = db_api.reservation_get_all_by_values(
         resource_id=resource_id)
     for reservation in reservations:
         pool = rp.ReservationPool()
         for allocation in db_api.host_allocation_get_all_by_values(
                 reservation_id=reservation['id']):
             host = db_api.host_get(allocation['compute_host_id'])
             pool.add_computehost(reservation['resource_id'],
                                  host['service_name'])
 def on_end(self, resource_id):
     """Remove the hosts from the pool."""
     reservations = db_api.reservation_get_all_by_values(
         resource_id=resource_id)
     for reservation in reservations:
         db_api.reservation_update(reservation['id'],
                                   {'status': 'completed'})
         host_reservation = db_api.host_reservation_get_by_reservation_id(
             reservation['id'])
         db_api.host_reservation_update(host_reservation['id'],
                                        {'status': 'completed'})
         allocations = db_api.host_allocation_get_all_by_values(
             reservation_id=reservation['id'])
         pool = rp.ReservationPool()
         for allocation in allocations:
             db_api.host_allocation_destroy(allocation['id'])
             if self.nova.hypervisors.get(
                     self._get_hypervisor_from_name_or_id(
                     allocation['compute_host_id'])
             ).__dict__['running_vms'] == 0:
                 pool.delete(reservation['resource_id'])
Example #5
0
 def on_end(self, resource_id):
     """Remove the hosts from the pool."""
     reservations = db_api.reservation_get_all_by_values(
         resource_id=resource_id)
     for reservation in reservations:
         db_api.reservation_update(reservation['id'],
                                   {'status': 'completed'})
         host_reservation = db_api.host_reservation_get_by_reservation_id(
             reservation['id'])
         db_api.host_reservation_update(host_reservation['id'],
                                        {'status': 'completed'})
         allocations = db_api.host_allocation_get_all_by_values(
             reservation_id=reservation['id'])
         pool = rp.ReservationPool()
         for allocation in allocations:
             db_api.host_allocation_destroy(allocation['id'])
             if self.nova.hypervisors.get(
                     self._get_hypervisor_from_name_or_id(
                         allocation['compute_host_id'])
             ).__dict__['running_vms'] == 0:
                 pool.delete(reservation['resource_id'])
    def _matching_hosts(self, hypervisor_properties, resource_properties,
                        count_range, start_date, end_date):
        """Return the matching hosts (preferably not allocated)

        """
        count_range = count_range.split('-')
        min_host = count_range[0]
        max_host = count_range[1]
        allocated_host_ids = []
        not_allocated_host_ids = []
        filter_array = []
        # TODO(frossigneux) support "or" operator
        if hypervisor_properties:
            filter_array = self._convert_requirements(
                hypervisor_properties)
        if resource_properties:
            filter_array += self._convert_requirements(
                resource_properties)
        for host in db_api.host_get_all_by_queries(filter_array):
            if not db_api.host_allocation_get_all_by_values(
                    compute_host_id=host['id']):
                not_allocated_host_ids.append(host['id'])
            elif db_utils.get_free_periods(
                host['id'],
                start_date,
                end_date,
                end_date - start_date,
            ) == [
                (start_date, end_date),
            ]:
                allocated_host_ids.append(host['id'])
        if len(not_allocated_host_ids) >= int(min_host):
            return not_allocated_host_ids[:int(max_host)]
        all_host_ids = allocated_host_ids + not_allocated_host_ids
        if len(all_host_ids) >= int(min_host):
            return all_host_ids[:int(max_host)]
        else:
            return []
Example #7
0
    def _matching_hosts(self, hypervisor_properties, resource_properties,
                        count_range, start_date, end_date):
        """Return the matching hosts (preferably not allocated)

        """
        count_range = count_range.split('-')
        min_host = count_range[0]
        max_host = count_range[1]
        allocated_host_ids = []
        not_allocated_host_ids = []
        filter_array = []
        # TODO(frossigneux) support "or" operator
        if hypervisor_properties:
            filter_array = self._convert_requirements(
                hypervisor_properties)
        if resource_properties:
            filter_array += self._convert_requirements(
                resource_properties)
        for host in db_api.host_get_all_by_queries(filter_array):
            if not db_api.host_allocation_get_all_by_values(
                    compute_host_id=host['id']):
                not_allocated_host_ids.append(host['id'])
            elif db_utils.get_free_periods(
                host['id'],
                start_date,
                end_date,
                end_date - start_date,
            ) == [
                (start_date, end_date),
            ]:
                allocated_host_ids.append(host['id'])
        if len(not_allocated_host_ids) >= int(min_host):
            return not_allocated_host_ids[:int(max_host)]
        all_host_ids = allocated_host_ids + not_allocated_host_ids
        if len(all_host_ids) >= int(min_host):
            return all_host_ids[:int(max_host)]
        else:
            return []
 def update_reservation(self, reservation_id, values):
     """Update reservation."""
     reservation = db_api.reservation_get(reservation_id)
     lease = db_api.lease_get(reservation['lease_id'])
     pool = rp.ReservationPool()
     hosts_in_pool = pool.get_computehosts(
         reservation['resource_id'])
     if (values['start_date'] < lease['start_date'] or
             values['end_date'] > lease['end_date']):
         allocations = []
         for allocation in db_api.host_allocation_get_all_by_values(
                 reservation_id=reservation_id):
             full_periods = db_utils.get_full_periods(
                 allocation['compute_host_id'],
                 values['start_date'],
                 values['end_date'],
                 datetime.timedelta(seconds=1))
             if lease['start_date'] < values['start_date']:
                 max_start = values['start_date']
             else:
                 max_start = lease['start_date']
             if lease['end_date'] < values['end_date']:
                 min_end = lease['end_date']
             else:
                 min_end = values['end_date']
             if not (len(full_periods) == 0 or
                     (len(full_periods) == 1 and
                      full_periods[0][0] == max_start and
                      full_periods[0][1] == min_end)):
                 allocations.append(allocation)
                 if (hosts_in_pool and
                         self.nova.hypervisors.get(
                             self._get_hypervisor_from_name_or_id(
                                 allocation['compute_host_id'])
                         ).__dict__['running_vms'] > 0):
                     raise manager_ex.NotEnoughHostsAvailable()
         if allocations:
             host_reservation = (
                 db_api.host_reservation_get_by_reservation_id(
                     reservation_id))
             host_ids = self._matching_hosts(
                 host_reservation['hypervisor_properties'],
                 host_reservation['resource_properties'],
                 str(len(allocations)) + '-' + str(len(allocations)),
                 values['start_date'],
                 values['end_date'])
             if not host_ids:
                 raise manager_ex.NotEnoughHostsAvailable()
             if hosts_in_pool:
                 old_hosts = [allocation['compute_host_id']
                              for allocation in allocations]
                 pool.remove_computehost(reservation['resource_id'],
                                         old_hosts)
             for allocation in allocations:
                 db_api.host_allocation_destroy(allocation['id'])
             for host_id in host_ids:
                 db_api.host_allocation_create(
                     {'compute_host_id': host_id,
                      'reservation_id': reservation_id})
                 if hosts_in_pool:
                     host = db_api.host_get(host_id)
                     pool.add_computehost(reservation['resource_id'],
                                          host['service_name'])
Example #9
0
 def update_reservation(self, reservation_id, values):
     """Update reservation."""
     reservation = db_api.reservation_get(reservation_id)
     lease = db_api.lease_get(reservation['lease_id'])
     pool = rp.ReservationPool()
     hosts_in_pool = pool.get_computehosts(
         reservation['resource_id'])
     if (values['start_date'] < lease['start_date'] or
             values['end_date'] > lease['end_date']):
         allocations = []
         for allocation in db_api.host_allocation_get_all_by_values(
                 reservation_id=reservation_id):
             full_periods = db_utils.get_full_periods(
                 allocation['compute_host_id'],
                 values['start_date'],
                 values['end_date'],
                 datetime.timedelta(seconds=1))
             if lease['start_date'] < values['start_date']:
                 max_start = values['start_date']
             else:
                 max_start = lease['start_date']
             if lease['end_date'] < values['end_date']:
                 min_end = lease['end_date']
             else:
                 min_end = values['end_date']
             if not (len(full_periods) == 0 or
                     (len(full_periods) == 1 and
                      full_periods[0][0] == max_start and
                      full_periods[0][1] == min_end)):
                 allocations.append(allocation)
                 if (hosts_in_pool and
                         self.nova.hypervisors.get(
                             self._get_hypervisor_from_name_or_id(
                                 allocation['compute_host_id'])
                         ).__dict__['running_vms'] > 0):
                     raise manager_ex.NotEnoughHostsAvailable()
         if allocations:
             host_reservation = (
                 db_api.host_reservation_get_by_reservation_id(
                     reservation_id))
             host_ids = self._matching_hosts(
                 host_reservation['hypervisor_properties'],
                 host_reservation['resource_properties'],
                 str(len(allocations)) + '-' + str(len(allocations)),
                 values['start_date'],
                 values['end_date'])
             if not host_ids:
                 raise manager_ex.NotEnoughHostsAvailable()
             if hosts_in_pool:
                 old_hosts = [allocation['compute_host_id']
                              for allocation in allocations]
                 pool.remove_computehost(reservation['resource_id'],
                                         old_hosts)
             for allocation in allocations:
                 db_api.host_allocation_destroy(allocation['id'])
             for host_id in host_ids:
                 db_api.host_allocation_create(
                     {'compute_host_id': host_id,
                      'reservation_id': reservation_id})
                 if hosts_in_pool:
                     host = db_api.host_get(host_id)
                     pool.add_computehost(reservation['resource_id'],
                                          host['service_name'])