def test_set_availability_zone_unicode_key(self):
     """Test set availability zone cache key is unicode."""
     service = self._create_service_with_topic('network', self.host)
     services = db.service_get_all(self.context)
     az.set_availability_zones(self.context, services)
     self.assertIsInstance(services[0]['host'], six.text_type)
     cached_key = az._make_cache_key(services[0]['host'])
     self.assertIsInstance(cached_key, str)
     self._destroy_service(service)
Exemple #2
0
    def service_get_all(self, context, filters=None, set_zones=False):
        if filters is None:
            filters = {}
        if "availability_zone" in filters:
            zone_filter = filters.pop("availability_zone")
            set_zones = True
        else:
            zone_filter = None
        services = self.cells_rpcapi.service_get_all(context, filters=filters)
        if set_zones:
            services = availability_zones.set_availability_zones(context, services)
            if zone_filter is not None:
                services = [s for s in services if s["availability_zone"] == zone_filter]
        # NOTE(johannes): Cells adds the cell path as a prefix to the id
        # to uniquely identify the service amongst all cells. Unfortunately
        # the object model makes the id an integer. Use a proxy here to
        # work around this particular problem.

        # Split out the cell path first
        cell_paths = []
        for service in services:
            cell_path, id = cells_utils.split_cell_and_item(service["id"])
            service["id"] = id
            cell_paths.append(cell_path)

        # NOTE(danms): Currently cells does not support objects as
        # return values, so just convert the db-formatted service objects
        # to new-world objects here
        services = obj_base.obj_make_list(context, objects.ServiceList(), objects.Service, services)

        # Now wrap it in the proxy with the original cell_path
        services = [ServiceProxy(s, c) for s, c in zip(services, cell_paths)]
        return services
    def _describe_availability_zones_verbose(self, context, **kwargs):
        ctxt = context.elevated()
        available_zones, not_available_zones = availability_zones.get_availability_zones(ctxt)

        # Available services
        enabled_services = db.service_get_all(context, False)
        enabled_services = availability_zones.set_availability_zones(context, enabled_services)
        zone_hosts = {}
        host_services = {}
        for service in enabled_services:
            zone_hosts.setdefault(service["availability_zone"], [])
            if service["host"] not in zone_hosts[service["availability_zone"]]:
                zone_hosts[service["availability_zone"]].append(service["host"])

            host_services.setdefault(service["availability_zone"] + service["host"], [])
            host_services[service["availability_zone"] + service["host"]].append(service)

        result = []
        for zone in available_zones:
            hosts = {}
            for host in zone_hosts[zone]:
                hosts[host] = {}
                for service in host_services[zone + host]:
                    alive = self.servicegroup_api.service_is_up(service)
                    hosts[host][service["binary"]] = {
                        "available": alive,
                        "active": True != service["disabled"],
                        "updated_at": service["updated_at"],
                    }
            result.append({"zoneName": zone, "zoneState": {"available": True}, "hosts": hosts})

        for zone in not_available_zones:
            result.append({"zoneName": zone, "zoneState": {"available": False}, "hosts": None})
        return {"availabilityZoneInfo": result}
Exemple #4
0
 def get_all(cls, context, disabled=None, set_zones=False):
     db_services = db.service_get_all(context, disabled=disabled)
     if set_zones:
         db_services = availability_zones.set_availability_zones(
             context, db_services)
     return base.obj_make_list(context, cls(context), objects.Service,
                               db_services)
Exemple #5
0
 def list(self, host=None, service=None):
     """
     Show a list of all running services. Filter by host & service name.
     """
     servicegroup_api = servicegroup.API()
     ctxt = context.get_admin_context()
     now = timeutils.utcnow()
     services = db.service_get_all(ctxt)
     services = availability_zones.set_availability_zones(ctxt, services)
     if host:
         services = [s for s in services if s['host'] == host]
     if service:
         services = [s for s in services if s['binary'] == service]
     print_format = "%-16s %-36s %-16s %-10s %-5s %-10s"
     print print_format % (
                 _('Binary'),
                 _('Host'),
                 _('Zone'),
                 _('Status'),
                 _('State'),
                 _('Updated_At'))
     for svc in services:
         alive = servicegroup_api.service_is_up(svc)
         art = (alive and ":-)") or "XXX"
         active = 'enabled'
         if svc['disabled']:
             active = 'disabled'
         print print_format % (svc['binary'], svc['host'],
                               svc['availability_zone'], active, art,
                               svc['updated_at'])
Exemple #6
0
    def service_get_all(self, context, filters=None, set_zones=False):
        if filters is None:
            filters = {}
        if "availability_zone" in filters:
            zone_filter = filters.pop("availability_zone")
            set_zones = True
        else:
            zone_filter = None
        services = self.cells_rpcapi.service_get_all(context, filters=filters)
        if set_zones:
            # TODO(sbauza): set_availability_zones returns flat dicts,
            # we should rather modify the RPC API to amend service_get_all by
            # adding a set_zones argument
            services = availability_zones.set_availability_zones(context, services)
            if zone_filter is not None:
                services = [s for s in services if s["availability_zone"] == zone_filter]

            # NOTE(sbauza): As services is a list of flat dicts, we need to
            # rehydrate the corresponding ServiceProxy objects
            cell_paths = []
            for service in services:
                cell_path, id = cells_utils.split_cell_and_item(service["id"])
                cell_path, host = cells_utils.split_cell_and_item(service["host"])
                service["id"] = id
                service["host"] = host
                cell_paths.append(cell_path)
            services = obj_base.obj_make_list(context, objects.ServiceList(), objects.Service, services)
            services = [cells_utils.ServiceProxy(s, c) for s, c in zip(services, cell_paths)]

        return services
 def test_set_availability_zone_not_compute_service(self):
     """Test not compute service get right availability zone."""
     service = self._create_service_with_topic('network', self.host)
     services = db.service_get_all(self.context)
     new_service = az.set_availability_zones(self.context, services)[0]
     self.assertEqual(self.default_in_az, new_service['availability_zone'])
     self._destroy_service(service)
Exemple #8
0
    def index(self, req):
        """
        Return a list of all running services. Filter by host & service name.
        """
        context = req.environ['nova.context']
        authorize(context)
        now = timeutils.utcnow()
        services = db.service_get_all(context)
        services = availability_zones.set_availability_zones(context, services)

        host = ''
        if 'host' in req.GET:
            host = req.GET['host']
        service = ''
        if 'service' in req.GET:
            service = req.GET['service']
        if host:
            services = [s for s in services if s['host'] == host]
        if service:
            services = [s for s in services if s['binary'] == service]

        svcs = []
        for svc in services:
            delta = now - (svc['updated_at'] or svc['created_at'])
            alive = abs(utils.total_seconds(delta)) <= CONF.service_down_time
            art = (alive and "up") or "down"
            active = 'enabled'
            if svc['disabled']:
                active = 'disabled'
            svcs.append({"binary": svc['binary'], 'host': svc['host'],
                         'zone': svc['availability_zone'],
                         'status': active, 'state': art,
                         'updated_at': svc['updated_at']})
        return {'services': svcs}
Exemple #9
0
    def _get_service_detail(self, svc, additional_fields, req):
        alive = self.servicegroup_api.service_is_up(svc)
        state = (alive and "up") or "down"
        active = 'enabled'
        if svc['disabled']:
            active = 'disabled'
        updated_time = self.servicegroup_api.get_updated_time(svc)

        uuid_for_id = api_version_request.is_supported(
            req, min_version=UUID_FOR_ID_MIN_VERSION)

        if 'availability_zone' not in svc:
            # The service wasn't loaded with the AZ so we need to do it here.
            # Yes this looks weird, but set_availability_zones makes a copy of
            # the list passed in and mutates the objects within it, so we have
            # to pull it back out from the resulting copied list.
            svc.availability_zone = (
                availability_zones.set_availability_zones(
                    req.environ['nova.context'],
                    [svc])[0]['availability_zone'])

        service_detail = {'binary': svc['binary'],
                          'host': svc['host'],
                          'id': svc['uuid' if uuid_for_id else 'id'],
                          'zone': svc['availability_zone'],
                          'status': active,
                          'state': state,
                          'updated_at': updated_time,
                          'disabled_reason': svc['disabled_reason']}

        for field in additional_fields:
            service_detail[field] = svc[field]

        return service_detail
    def test_set_availability_zone_compute_service(self):
        """Test for compute service get right availability zone."""
        service = self._create_service_with_topic("compute", self.host)
        services = db.service_get_all(self.context)

        # The service is not add into aggregate, so confirm it is default
        # availability zone.
        new_service = az.set_availability_zones(self.context, services)[0]
        self.assertEquals(new_service["availability_zone"], self.default_az)

        # The service is added into aggregate, confirm return the aggregate
        # availability zone.
        self._add_to_aggregate(service, self.agg)
        new_service = az.set_availability_zones(self.context, services)[0]
        self.assertEquals(new_service["availability_zone"], self.availability_zone)

        self._destroy_service(service)
 def test_set_availability_zone_unicode_key(self):
     """Test set availability zone cache key is unicode."""
     service = self._create_service_with_topic('network', self.host)
     services = db.service_get_all(self.context)
     new_service = az.set_availability_zones(self.context, services)[0]
     self.assertEqual(type(services[0]['host']), unicode)
     cached_key = az._make_cache_key(services[0]['host'])
     self.assertEqual(type(cached_key), str)
     self._destroy_service(service)
Exemple #12
0
 def service_get_all(self, context, filters=None, set_zones=False):
     if filters is None:
         filters = {}
     if "availability_zone" in filters:
         zone_filter = filters.pop("availability_zone")
         set_zones = True
     else:
         zone_filter = None
     services = self.cells_rpcapi.service_get_all(context, filters=filters)
     if set_zones:
         services = availability_zones.set_availability_zones(context, services)
         if zone_filter is not None:
             services = [s for s in services if s["availability_zone"] == zone_filter]
     return services
Exemple #13
0
    def list(self, zone=None):
        """Show a list of all physical hosts. Filter by zone.
        args: [zone]"""
        print "%-25s\t%-15s" % (_("host"), _("zone"))
        ctxt = context.get_admin_context()
        services = db.service_get_all(ctxt)
        services = availability_zones.set_availability_zones(ctxt, services)
        if zone:
            services = [s for s in services if s["availability_zone"] == zone]
        hosts = []
        for srv in services:
            if not [h for h in hosts if h["host"] == srv["host"]]:
                hosts.append(srv)

        for h in hosts:
            print "%-25s\t%-15s" % (h["host"], h["availability_zone"])
Exemple #14
0
def _list_hosts(req):
    """Returns a summary list of hosts, optionally filtering
    by service type.
    """
    context = req.environ['nova.context']
    services = db.service_get_all(context, False)
    services = availability_zones.set_availability_zones(context, services)
    zone = ''
    if 'zone' in req.GET:
        zone = req.GET['zone']
    if zone:
        services = [s for s in services if s['availability_zone'] == zone]
    hosts = []
    for host in services:
        hosts.append({"host_name": host['host'], 'service': host['topic'],
                      'zone': host['availability_zone']})
    return hosts
Exemple #15
0
    def service_get_all(self, context, filters=None, set_zones=False,
                        all_cells=False):
        """Get all services.

        Note that this is the cellsv1 variant, which means we ignore the
        "all_cells" parameter.
        """
        if filters is None:
            filters = {}
        if 'availability_zone' in filters:
            zone_filter = filters.pop('availability_zone')
            set_zones = True
        else:
            zone_filter = None
        services = self.cells_rpcapi.service_get_all(context,
                                                     filters=filters)
        if set_zones:
            # TODO(sbauza): set_availability_zones returns flat dicts,
            # we should rather modify the RPC API to amend service_get_all by
            # adding a set_zones argument
            services = availability_zones.set_availability_zones(context,
                                                                 services)
            if zone_filter is not None:
                services = [s for s in services
                            if s['availability_zone'] == zone_filter]

            # NOTE(sbauza): As services is a list of flat dicts, we need to
            # rehydrate the corresponding ServiceProxy objects
            cell_paths = []
            for service in services:
                cell_path, id = cells_utils.split_cell_and_item(service['id'])
                cell_path, host = cells_utils.split_cell_and_item(
                    service['host'])
                service['id'] = id
                service['host'] = host
                cell_paths.append(cell_path)
            services = obj_base.obj_make_list(context,
                                              objects.ServiceList(),
                                              objects.Service,
                                              services)
            services = [cells_utils.ServiceProxy(s, c)
                        for s, c in zip(services, cell_paths)]

        return services
Exemple #16
0
    def _get_service_detail(self, svc, additional_fields, req,
                            cell_down_support=False):
        # NOTE(tssurya): The below logic returns a minimal service construct
        # consisting of only the host, binary and status fields for the compute
        # services in the down cell.
        if (cell_down_support and 'uuid' not in svc):
            return {'binary': svc.binary,
                    'host': svc.host,
                    'status': "UNKNOWN"}

        alive = self.servicegroup_api.service_is_up(svc)
        state = (alive and "up") or "down"
        active = 'enabled'
        if svc['disabled']:
            active = 'disabled'
        updated_time = self.servicegroup_api.get_updated_time(svc)

        uuid_for_id = api_version_request.is_supported(
            req, min_version=UUID_FOR_ID_MIN_VERSION)

        if 'availability_zone' not in svc:
            # The service wasn't loaded with the AZ so we need to do it here.
            # Yes this looks weird, but set_availability_zones makes a copy of
            # the list passed in and mutates the objects within it, so we have
            # to pull it back out from the resulting copied list.
            svc.availability_zone = (
                availability_zones.set_availability_zones(
                    req.environ['nova.context'],
                    [svc])[0]['availability_zone'])

        service_detail = {'binary': svc['binary'],
                          'host': svc['host'],
                          'id': svc['uuid' if uuid_for_id else 'id'],
                          'zone': svc['availability_zone'],
                          'status': active,
                          'state': state,
                          'updated_at': updated_time,
                          'disabled_reason': svc['disabled_reason']}

        for field in additional_fields:
            service_detail[field] = svc[field]

        return service_detail
Exemple #17
0
    def _describe_availability_zones_verbose(self, context, **kwargs):
        ctxt = context.elevated()
        available_zones, not_available_zones = \
            availability_zones.get_availability_zones(ctxt)

        # Available services
        enabled_services = db.service_get_all(context, False)
        enabled_services = availability_zones.set_availability_zones(context,
                enabled_services)
        zone_hosts = {}
        host_services = {}
        for service in enabled_services:
            zone_hosts.setdefault(service['availability_zone'], [])
            if service['host'] not in zone_hosts[service['availability_zone']]:
                zone_hosts[service['availability_zone']].append(
                    service['host'])

            host_services.setdefault(service['availability_zone'] +
                    service['host'], [])
            host_services[service['availability_zone'] + service['host']].\
                    append(service)

        result = []
        for zone in available_zones:
            hosts = {}
            for host in zone_hosts.get(zone, []):
                hosts[host] = {}
                for service in host_services[zone + host]:
                    alive = self.servicegroup_api.service_is_up(service)
                    hosts[host][service['binary']] = {'available': alive,
                                      'active': True != service['disabled'],
                                      'updated_at': service['updated_at']}
            result.append({'zone_name': zone,
                           'zone_state': {'available': True},
                           "hosts": hosts})

        for zone in not_available_zones:
            result.append({'zone_name': zone,
                           'zone_state': {'available': False},
                           "hosts": None})
        return {'availability_zone_info': result}
Exemple #18
0
 def list(self, host=None, service=None):
     """
     Show a list of all running services. Filter by host & service name.
     """
     servicegroup_api = servicegroup.API()
     ctxt = context.get_admin_context()
     now = timeutils.utcnow()
     services = db.service_get_all(ctxt)
     services = availability_zones.set_availability_zones(ctxt, services)
     if host:
         services = [s for s in services if s["host"] == host]
     if service:
         services = [s for s in services if s["binary"] == service]
     print_format = "%-16s %-36s %-16s %-10s %-5s %-10s"
     print print_format % (_("Binary"), _("Host"), _("Zone"), _("Status"), _("State"), _("Updated_At"))
     for svc in services:
         alive = servicegroup_api.service_is_up(svc)
         art = (alive and ":-)") or "XXX"
         active = "enabled"
         if svc["disabled"]:
             active = "disabled"
         print print_format % (svc["binary"], svc["host"], svc["availability_zone"], active, art, svc["updated_at"])
Exemple #19
0
 def service_get_all(self, context, filters=None, set_zones=False):
     if filters is None:
         filters = {}
     if 'availability_zone' in filters:
         zone_filter = filters.pop('availability_zone')
         set_zones = True
     else:
         zone_filter = None
     services = self.cells_rpcapi.service_get_all(context,
                                                  filters=filters)
     if set_zones:
         services = availability_zones.set_availability_zones(context,
                                                              services)
         if zone_filter is not None:
             services = [s for s in services
                         if s['availability_zone'] == zone_filter]
     # NOTE(danms): Currently cells does not support objects as
     # return values, so just convert the db-formatted service objects
     # to new-world objects here
     return obj_base.obj_make_list(context,
                                   service_obj.ServiceList(),
                                   service_obj.Service,
                                   services)
Exemple #20
0
 def service_get_all(context, filters=None, set_zones=False):
     if set_zones or 'availability_zone' in filters:
         return availability_zones.set_availability_zones(context,
                                                          services)
     return services
Exemple #21
0
 def get_all(cls, context, disabled=None, set_zones=False):
     db_services = db.service_get_all(context, disabled=disabled)
     if set_zones:
         db_services = availability_zones.set_availability_zones(
             context, db_services)
     return _make_list(context, ServiceList(), Service, db_services)
Exemple #22
0
 def get_all(cls, context, disabled=None, set_zones=False):
     db_services = db.service_get_all(context, disabled=disabled)
     if set_zones:
         db_services = availability_zones.set_availability_zones(
             context, db_services)
     return base.obj_make_list(context, ServiceList(), Service, db_services)
Exemple #23
0
 def service_get_all(context, filters=None, set_zones=False):
     if set_zones or 'availability_zone' in filters:
         return availability_zones.set_availability_zones(context,
                                                          services)
     return services
Exemple #24
0
def fake_host_api_service_get_all(context, filters=None, set_zones=False):
    if set_zones or 'availability_zone' in filters:
        return availability_zones.set_availability_zones(context,
                                                         fake_services_list)
Exemple #25
0
def fake_host_api_service_get_all(context, filters=None, set_zones=False):
    if set_zones or 'availability_zone' in filters:
        return availability_zones.set_availability_zones(
            context, fake_services_list)