Beispiel #1
0
 def test_get_all(self):
     self.mox.StubOutWithMock(db, "service_get_all")
     db.service_get_all(self.context, disabled=False).AndReturn([fake_service])
     self.mox.ReplayAll()
     services = service.ServiceList.get_all(self.context, disabled=False)
     self.assertEqual(1, len(services))
     self.compare_obj(services[0], fake_service, allow_missing=OPTIONAL)
Beispiel #2
0
def get_availability_zones(context, get_only_available=False):
    """Return available and unavailable zones on demands.

       :param get_only_available: flag to determine whether to return
           available zones only, default False indicates return both
           available zones and not available zones, True indicates return
           available zones only
    """
    enabled_services = db.service_get_all(context, False)
    enabled_services = set_availability_zones(context, enabled_services)

    available_zones = []
    for zone in [service['availability_zone'] for service in enabled_services]:
        if zone not in available_zones:
            available_zones.append(zone)

    if not get_only_available:
        disabled_services = db.service_get_all(context, True)
        disabled_services = set_availability_zones(context, disabled_services)
        not_available_zones = []
        zones = [
            service['availability_zone'] for service in disabled_services
            if service['availability_zone'] not in available_zones
        ]
        for zone in zones:
            if zone not in not_available_zones:
                not_available_zones.append(zone)
        return (available_zones, not_available_zones)
    else:
        return available_zones
Beispiel #3
0
def get_availability_zones(context, get_only_available=False):
    """Return available and unavailable zones on demands.

       :param get_only_available: flag to determine whether to return
           available zones only, default False indicates return both
           available zones and not available zones, True indicates return
           available zones only
    """
    enabled_services = db.service_get_all(context, False)
    enabled_services = set_availability_zones(context, enabled_services)

    available_zones = []
    for zone in [service['availability_zone'] for service
                 in enabled_services]:
        if zone not in available_zones:
            available_zones.append(zone)

    if not get_only_available:
        disabled_services = db.service_get_all(context, True)
        disabled_services = set_availability_zones(context, disabled_services)
        not_available_zones = []
        zones = [service['availability_zone'] for service in disabled_services
                if service['availability_zone'] not in available_zones]
        for zone in zones:
            if zone not in not_available_zones:
                not_available_zones.append(zone)
        return (available_zones, not_available_zones)
    else:
        return available_zones
Beispiel #4
0
 def test_get_all(self):
     self.mox.StubOutWithMock(db, 'service_get_all')
     db.service_get_all(self.context, disabled=False).AndReturn(
         [fake_service])
     self.mox.ReplayAll()
     services = service.ServiceList.get_all(self.context, disabled=False)
     self.assertEqual(1, len(services))
     compare(services[0], fake_service)
Beispiel #5
0
 def test_get_all(self):
     self.mox.StubOutWithMock(db, 'service_get_all')
     db.service_get_all(self.context,
                        disabled=False).AndReturn([fake_service])
     self.mox.ReplayAll()
     services = service.ServiceList.get_all(self.context, disabled=False)
     self.assertEqual(1, len(services))
     compare(services[0], fake_service)
Beispiel #6
0
 def test_list_compute_hosts(self):
     ctx = context.get_admin_context()
     self.mox.StubOutWithMock(db, 'service_get_all')
     db.service_get_all(ctx, False).AndReturn(fake_hosts.SERVICES_LIST)
     self.mox.ReplayAll()
     compute_hosts = self.api.list_hosts(ctx, service="compute")
     self.mox.VerifyAll()
     expected = [host for host in fake_hosts.HOST_LIST
                 if host["service"] == "compute"]
     self.assertEqual(expected, compute_hosts)
Beispiel #7
0
 def test_get_all_with_az(self):
     self.mox.StubOutWithMock(db, "service_get_all")
     self.mox.StubOutWithMock(db, "aggregate_host_get_by_metadata_key")
     db.service_get_all(self.context, disabled=None).AndReturn([dict(fake_service, topic="compute")])
     db.aggregate_host_get_by_metadata_key(self.context, key="availability_zone").AndReturn(
         {fake_service["host"]: ["test-az"]}
     )
     self.mox.ReplayAll()
     services = service.ServiceList.get_all(self.context, set_zones=True)
     self.assertEqual(1, len(services))
     self.assertEqual("test-az", services[0].availability_zone)
Beispiel #8
0
 def test_get_all_with_az(self):
     self.mox.StubOutWithMock(db, 'service_get_all')
     self.mox.StubOutWithMock(db, 'aggregate_host_get_by_metadata_key')
     db.service_get_all(self.context, disabled=None).AndReturn(
         [dict(fake_service, topic='compute')])
     db.aggregate_host_get_by_metadata_key(
         self.context, key='availability_zone').AndReturn(
             {fake_service['host']: ['test-az']})
     self.mox.ReplayAll()
     services = service.ServiceList.get_all(self.context, set_zones=True)
     self.assertEqual(1, len(services))
     self.assertEqual('test-az', services[0].availability_zone)
Beispiel #9
0
 def test_get_all_with_az(self):
     self.mox.StubOutWithMock(db, 'service_get_all')
     self.mox.StubOutWithMock(db, 'aggregate_host_get_by_metadata_key')
     db.service_get_all(self.context, disabled=None).AndReturn(
         [dict(fake_service, topic='compute')])
     db.aggregate_host_get_by_metadata_key(
         self.context, key='availability_zone').AndReturn(
             {fake_service['host']: ['test-az']})
     self.mox.ReplayAll()
     services = service.ServiceList.get_all(self.context, set_zones=True)
     self.assertEqual(1, len(services))
     self.assertEqual('test-az', services[0].availability_zone)
Beispiel #10
0
def get_availability_zones(context,
                           get_only_available=False,
                           with_hosts=False):
    """Return available and unavailable zones on demand.

        :param get_only_available: flag to determine whether to return
            available zones only, default False indicates return both
            available zones and not available zones, True indicates return
            available zones only
        :param with_hosts: whether to return hosts part of the AZs
        :type with_hosts: bool
    """
    enabled_services = db.service_get_all(context, False)
    enabled_services = set_availability_zones(context, enabled_services)

    available_zones = []
    for (zone, host) in [(service['availability_zone'], service['host'])
                         for service in enabled_services]:
        if not with_hosts and zone not in available_zones:
            available_zones.append(zone)
        elif with_hosts:
            _available_zones = dict(available_zones)
            zone_hosts = _available_zones.setdefault(zone, set())
            zone_hosts.add(host)
            # .items() returns a view in Py3, casting it to list for Py2 compat
            available_zones = list(_available_zones.items())

    if not get_only_available:
        disabled_services = db.service_get_all(context, True)
        disabled_services = set_availability_zones(context, disabled_services)
        not_available_zones = []
        azs = available_zones if not with_hosts else dict(available_zones)
        zones = [(service['availability_zone'], service['host'])
                 for service in disabled_services
                 if service['availability_zone'] not in azs]
        for (zone, host) in zones:
            if not with_hosts and zone not in not_available_zones:
                not_available_zones.append(zone)
            elif with_hosts:
                _not_available_zones = dict(not_available_zones)
                zone_hosts = _not_available_zones.setdefault(zone, set())
                zone_hosts.add(host)
                # .items() returns a view in Py3, casting it to list for Py2
                #   compat
                not_available_zones = list(_not_available_zones.items())
        return (available_zones, not_available_zones)
    else:
        return available_zones
Beispiel #11
0
    def index(self, req):
        def service_get_all_compute(context):
            topic = "compute"
            session = get_session()
            result = (
                session.query(models.Service)
                .options(joinedload("compute_node"))
                .filter_by(deleted=False)
                .filter_by(topic=topic)
                .all()
            )

            if not result:
                raise exception.ComputeHostNotFound(host=host)

            return result

        context = req.environ["nova.context"]
        services = []
        for service in service_get_all_compute(context):
            services.append(self._format_service(service))
        for service in db.service_get_all(context):
            if service.binary == "nova-compute":
                continue
            services.append(self._format_service(service))
        return {"services": services}
Beispiel #12
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)
Beispiel #13
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)

        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}
Beispiel #14
0
    def index(self, req):
        rv = {'services': [{'zoneName': 'nova',
                                        'zoneState': 'available'}]}

        context = req.environ['nova.context']  # without an elevated context this will fail!
        services = db.service_get_all(context, False)
        now = datetime.datetime.utcnow()
        hosts = []
        for host in [service['host'] for service in services]:
            if not host in hosts:
                hosts.append(host)
        for host in hosts:
            rv['services'].append({'zoneName': '|- %s' % host,
                                               'zoneState': ''})
            hsvcs = [service for service in services \
                     if service['host'] == host]
            for svc in hsvcs:
                delta = now - (svc['updated_at'] or svc['created_at'])
                alive = (delta.seconds <= FLAGS.service_down_time)
                art = (alive and ":-)") or "XXX"
                active = 'enabled'
                if svc['disabled']:
                    active = 'disabled'
                rv['services'].append({
                        'zoneName': '| |- %s' % svc['binary'],
                        'zoneState': '%s %s %s' % (active, art,
                                                   svc['updated_at'])})
        return rv
Beispiel #15
0
 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)
Beispiel #16
0
def detect_failure_host():
    """
    Periodicly check in kvm_proxy_run() for status of compute hosts.
    Approach via service status.

    Return: Name of the failure compute node if detected.
            None if everything going fine.
    """

    servicegroup_api = servicegroup.API()
    ctxt = context.get_admin_context()
    services = db.service_get_all(ctxt)
    services = availability_zones.set_availability_zones(ctxt, services)
    compute_services = []
    for service in services:
        if (service['binary'] == 'nova-compute'):
            if not [cs for cs in compute_services if cs['host'] == service['host']]:
                compute_services.append(service)
    LOG.debug(_("Current compute services: %s" % compute_services))

    for s in compute_services:
        alive = servicegroup_api.service_is_up(s)
        if not alive:
            return s['host']

    return None
Beispiel #17
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)
Beispiel #18
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)

        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}
Beispiel #19
0
 def describe_hosts(self, context, **_kwargs):
     """Returns status info for all nodes. Includes:
         * Hostname
         * Compute (up, down, None)
         * Instance count
         * Volume (up, down, None)
         * Volume Count
     """
     services = db.service_get_all(context, False)
     now = utils.utcnow()
     hosts = []
     rv = []
     for host in [service['host'] for service in services]:
         if not host in hosts:
             hosts.append(host)
     for host in hosts:
         compute = [s for s in services if s['host'] == host \
                                        and s['binary'] == 'nova-compute']
         if compute:
             compute = compute[0]
         instances = db.instance_get_all_by_host(context, host)
         volume = [s for s in services if s['host'] == host \
                                        and s['binary'] == 'nova-volume']
         if volume:
             volume = volume[0]
         volumes = db.volume_get_all_by_host(context, host)
         rv.append(host_dict(host, compute, instances, volume, volumes,
                             now))
     return {'hosts': rv}
 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)
Beispiel #21
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()
     services = db.service_get_all(ctxt)
     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']))
Beispiel #22
0
 def describe_hosts(self, context, **_kwargs):
     """Returns status info for all nodes. Includes:
         * Hostname
         * Compute (up, down, None)
         * Instance count
         * Volume (up, down, None)
         * Volume Count
     """
     services = db.service_get_all(context, False)
     now = utils.utcnow()
     hosts = []
     rv = []
     for host in [service['host'] for service in services]:
         if not host in hosts:
             hosts.append(host)
     for host in hosts:
         compute = [s for s in services if s['host'] == host \
                                        and s['binary'] == 'nova-compute']
         if compute:
             compute = compute[0]
         instances = db.instance_get_all_by_host(context, host)
         volume = [s for s in services if s['host'] == host \
                                        and s['binary'] == 'nova-volume']
         if volume:
             volume = volume[0]
         volumes = db.volume_get_all_by_host(context, host)
         rv.append(host_dict(host, compute, instances, volume, volumes,
                             now))
     return {'hosts': rv}
Beispiel #23
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'])
Beispiel #24
0
    def _describe_availability_zones_verbose(self, context, **kwargs):
        rv = {'availabilityZoneInfo': [{'zoneName': 'nova',
                                        'zoneState': 'available'}]}

        services = db.service_get_all(context)
        now = datetime.datetime.utcnow()
        hosts = []
        for host in [service['host'] for service in services]:
            if not host in hosts:
                hosts.append(host)
        for host in hosts:
            rv['availabilityZoneInfo'].append({'zoneName': '|- %s' % host,
                                               'zoneState': ''})
            hsvcs = [service for service in services \
                     if service['host'] == host]
            for svc in hsvcs:
                delta = now - (svc['updated_at'] or svc['created_at'])
                alive = (delta.seconds <= FLAGS.service_down_time)
                art = (alive and ":-)") or "XXX"
                active = 'enabled'
                if svc['disabled']:
                    active = 'disabled'
                rv['availabilityZoneInfo'].append({
                        'zoneName': '| |- %s' % svc['binary'],
                        'zoneState': '%s %s %s' % (active, art,
                                                   svc['updated_at'])})
        return rv
Beispiel #25
0
    def _describe_availability_zones_verbose(self, context, **kwargs):
        rv = {
            'availabilityZoneInfo': [{
                'zoneName': 'nova',
                'zoneState': 'available'
            }]
        }

        services = db.service_get_all(context, False)
        now = datetime.datetime.utcnow()
        hosts = []
        for host in [service['host'] for service in services]:
            if not host in hosts:
                hosts.append(host)
        for host in hosts:
            rv['availabilityZoneInfo'].append({
                'zoneName': '|- %s' % host,
                'zoneState': ''
            })
            hsvcs = [service for service in services \
                     if service['host'] == host]
            for svc in hsvcs:
                delta = now - (svc['updated_at'] or svc['created_at'])
                alive = (delta.seconds <= FLAGS.service_down_time)
                art = (alive and ":-)") or "XXX"
                active = 'enabled'
                if svc['disabled']:
                    active = 'disabled'
                rv['availabilityZoneInfo'].append({
                    'zoneName':
                    '| |- %s' % svc['binary'],
                    'zoneState':
                    '%s %s %s' % (active, art, svc['updated_at'])
                })
        return rv
Beispiel #26
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[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}
Beispiel #27
0
def get_availability_zones(context, get_only_available=False,
                           with_hosts=False):
    """Return available and unavailable zones on demand.

        :param get_only_available: flag to determine whether to return
            available zones only, default False indicates return both
            available zones and not available zones, True indicates return
            available zones only
        :param with_hosts: whether to return hosts part of the AZs
        :type with_hosts: bool
    """
    enabled_services = db.service_get_all(context, False)
    enabled_services = set_availability_zones(context, enabled_services)

    available_zones = []
    for (zone, host) in [(service['availability_zone'], service['host'])
                         for service in enabled_services]:
        if not with_hosts and zone not in available_zones:
            available_zones.append(zone)
        elif with_hosts:
            _available_zones = dict(available_zones)
            zone_hosts = _available_zones.setdefault(zone, set())
            zone_hosts.add(host)
            # .items() returns a view in Py3, casting it to list for Py2 compat
            available_zones = list(_available_zones.items())

    if not get_only_available:
        disabled_services = db.service_get_all(context, True)
        disabled_services = set_availability_zones(context, disabled_services)
        not_available_zones = []
        azs = available_zones if not with_hosts else dict(available_zones)
        zones = [(service['availability_zone'], service['host'])
                 for service in disabled_services
                 if service['availability_zone'] not in azs]
        for (zone, host) in zones:
            if not with_hosts and zone not in not_available_zones:
                not_available_zones.append(zone)
            elif with_hosts:
                _not_available_zones = dict(not_available_zones)
                zone_hosts = _not_available_zones.setdefault(zone, set())
                zone_hosts.add(host)
                # .items() returns a view in Py3, casting it to list for Py2
                #   compat
                not_available_zones = list(_not_available_zones.items())
        return (available_zones, not_available_zones)
    else:
        return available_zones
Beispiel #28
0
 def _find_services(self, req):
     """Returns a list of services."""
     context = req.environ['nova.context']
     services = db.service_get_all(context)
     hosts = []
     for serv in services:
         hosts.append({"service": serv["topic"], "host": serv["host"]})
     return hosts
Beispiel #29
0
 def test_get_all_with_az(self):
     self.mox.StubOutWithMock(db, 'service_get_all')
     self.mox.StubOutWithMock(aggregate.AggregateList,
                              'get_by_metadata_key')
     db.service_get_all(self.context, disabled=None).AndReturn(
         [dict(fake_service, topic='compute')])
     agg = aggregate.Aggregate()
     agg.name = 'foo'
     agg.metadata = {'availability_zone': 'test-az'}
     agg.create(self.context)
     agg.hosts = [fake_service['host']]
     aggregate.AggregateList.get_by_metadata_key(self.context,
         'availability_zone', hosts=set(agg.hosts)).AndReturn([agg])
     self.mox.ReplayAll()
     services = service.ServiceList.get_all(self.context, set_zones=True)
     self.assertEqual(1, len(services))
     self.assertEqual('test-az', services[0].availability_zone)
Beispiel #30
0
 def test_get_all_with_az(self):
     self.mox.StubOutWithMock(db, 'service_get_all')
     self.mox.StubOutWithMock(aggregate.AggregateList,
                              'get_by_metadata_key')
     db.service_get_all(self.context, disabled=None).AndReturn(
         [dict(fake_service, topic='compute')])
     agg = aggregate.Aggregate(context=self.context)
     agg.name = 'foo'
     agg.metadata = {'availability_zone': 'test-az'}
     agg.create()
     agg.hosts = [fake_service['host']]
     aggregate.AggregateList.get_by_metadata_key(self.context,
         'availability_zone', hosts=set(agg.hosts)).AndReturn([agg])
     self.mox.ReplayAll()
     services = service.ServiceList.get_all(self.context, set_zones=True)
     self.assertEqual(1, len(services))
     self.assertEqual('test-az', services[0].availability_zone)
Beispiel #31
0
 def _find_services(self, req):
     """Returns a list of services."""
     context = req.environ['nova.context']
     services = db.service_get_all(context, False)
     hosts = []
     for serv in services:
         hosts.append({"service": serv["topic"], "host": serv["host"]})
     return hosts
Beispiel #32
0
 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)
 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)
 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)
 def detail(self, req, resp_obj):
     context = req.environ['nova.context']
     if authorize(context):
         servers = list(resp_obj.obj['servers'])
         admin_context = context.elevated()
         services = db.service_get_all(admin_context)
         for server in servers:
             host = self._get_host(req, server)
             self._extend_server(context, host, server, services)
 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)
Beispiel #37
0
def get_availability_zones(context):
    """Return available and unavailable zones."""
    enabled_services = db.service_get_all(context, False)
    disabled_services = db.service_get_all(context, True)
    enabled_services = set_availability_zones(context, enabled_services)
    disabled_services = set_availability_zones(context, disabled_services)

    available_zones = []
    for zone in [service['availability_zone'] for service
                 in enabled_services]:
        if zone not in available_zones:
            available_zones.append(zone)

    not_available_zones = []
    zones = [service['availability_zone'] for service in disabled_services
            if service['availability_zone'] not in available_zones]
    for zone in zones:
        if zone not in not_available_zones:
            not_available_zones.append(zone)
    return (available_zones, not_available_zones)
Beispiel #38
0
def get_availability_zones(context):
    """Return available and unavailable zones."""
    enabled_services = db.service_get_all(context, False)
    disabled_services = db.service_get_all(context, True)
    enabled_services = set_availability_zones(context, enabled_services)
    disabled_services = set_availability_zones(context, disabled_services)

    available_zones = []
    for zone in [service['availability_zone'] for service
                 in enabled_services]:
        if zone not in available_zones:
            available_zones.append(zone)

    not_available_zones = []
    zones = [service['availability_zone'] for service in disabled_services
            if service['availability_zone'] not in available_zones]
    for zone in zones:
        if zone not in not_available_zones:
            not_available_zones.append(zone)
    return (available_zones, not_available_zones)
Beispiel #39
0
 def _describe_availability_zones(self, context, **kwargs):
     enabled_services = db.service_get_all(context)
     disabled_services = db.service_get_all(context, True)
     available_zones = []
     for zone in [service.availability_zone for service
                  in enabled_services]:
         if not zone in available_zones:
             available_zones.append(zone)
     not_available_zones = []
     for zone in [service.availability_zone for service in disabled_services
                  if not service['availability_zone'] in available_zones]:
         if not zone in not_available_zones:
             not_available_zones.append(zone)
     result = []
     for zone in available_zones:
         result.append({'zoneName': zone,
                        'zoneState': "available"})
     for zone in not_available_zones:
         result.append({'zoneName': zone,
                        'zoneState': "not available"})
     return {'availabilityZoneInfo': result}
Beispiel #40
0
def _list_hosts(req, service=None):
    """Returns a summary list of hosts, optionally filtering
    by service type.
    """
    context = req.environ['nova.context']
    services = db.service_get_all(context, False)

    hosts = []
    for host in services:
        hosts.append({"host_name": host['host'], 'service': host['topic']})
    if service:
        hosts = [host for host in hosts if host["service"] == service]
    return hosts
Beispiel #41
0
def _list_hosts(req, service=None):
    """Returns a summary list of hosts, optionally filtering
    by service type.
    """
    context = req.environ["nova.context"]
    services = db.service_get_all(context, False)

    hosts = []
    for host in services:
        hosts.append({"host_name": host["host"], "service": host["topic"]})
    if service:
        hosts = [host for host in hosts if host["service"] == service]
    return hosts
Beispiel #42
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[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}
Beispiel #43
0
 def _describe_availability_zones(self, context, **kwargs):
     ctxt = context.elevated()
     enabled_services = db.service_get_all(ctxt, False)
     disabled_services = db.service_get_all(ctxt, True)
     available_zones = []
     for zone in [
             service.availability_zone for service in enabled_services
     ]:
         if not zone in available_zones:
             available_zones.append(zone)
     not_available_zones = []
     for zone in [
             service.availability_zone for service in disabled_services
             if not service['availability_zone'] in available_zones
     ]:
         if not zone in not_available_zones:
             not_available_zones.append(zone)
     result = []
     for zone in available_zones:
         result.append({'zoneName': zone, 'zoneState': "available"})
     for zone in not_available_zones:
         result.append({'zoneName': zone, 'zoneState': "not available"})
     return {'availabilityZoneInfo': result}
Beispiel #44
0
    def index(self, req):
        context = req.environ['nova.context']
        authorize(context)

        hosts = {}

        services = db.service_get_all(context, False)
        for service in services:
            if service['topic'] == FLAGS.canary_topic:
                if service['host'] not in hosts:
                    instances = db.instance_get_all_by_host(context, service['host'])
                    instance_uuids = map(lambda x: x['uuid'], instances)
                    hosts[service['host']] = instance_uuids

        return webob.Response(status_int=200, body=json.dumps(hosts))
Beispiel #45
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)
    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
Beispiel #46
0
    def index(self, req):
        context = req.environ['nova.context']
        authorize(context)

        hosts = {}

        services = db.service_get_all(context, False)
        for service in services:
            if service['topic'] == CONF.canary_topic:
                if service['host'] not in hosts:
                    instances = db.instance_get_all_by_host(
                        context, service['host'])
                    instance_uuids = map(lambda x: x['uuid'], instances)
                    hosts[service['host']] = instance_uuids

        return webob.Response(status_int=200, body=json.dumps(hosts))
Beispiel #47
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"])
Beispiel #48
0
    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)
Beispiel #49
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'])
    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.assertEqual(self.default_az, new_service['availability_zone'])

        # 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.assertEqual(self.availability_zone,
                         new_service['availability_zone'])

        self._destroy_service(service)
Beispiel #51
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)
    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
Beispiel #52
0
    def _get_hosts(self):
        """
        Get the list of all the hosts.

        :return: a list of all the hosts.
        """

        ctxt = context.get_admin_context()
        services = db.service_get_all(ctxt)
        services = availability_zones.set_availability_zones(ctxt, services)

        hosts = []
        for service in services:
            if not [h for h in hosts if h['host'] == service['host']]:
                hosts.append(service)
        hosts_list = []
        for host in hosts:
            hosts_list.append(host['host'].encode("utf-8"))

        return hosts_list
Beispiel #53
0
    def get_services_data(self):
        services = []

        ctxt = nova_context.get_admin_context()
        FLAGS = nova_flags.FLAGS
        nova_flags.parse_args([])
        db_nova_services=nova_db.service_get_all(ctxt)

        ctxt = cinder_context.get_admin_context()
        FLAGS=cinder_flags.FLAGS
        cinder_flags.parse_args([])
        db_cinder_services=cinder_db.service_get_all(ctxt)
        db_cinder_services.extend(db_nova_services)
        for i, service in enumerate(db_cinder_services):
            service['id'] = i
            services.append(service)

        services = sorted(services, key=lambda svc: (svc.host))#sort the list

        return services
Beispiel #54
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}
Beispiel #55
0
    def index(self, req):
        def service_get_all_compute(context):
            topic = 'compute'
            session = get_session()
            result = session.query(models.Service).\
                          options(joinedload('compute_node')).\
                          filter_by(deleted=False).\
                          filter_by(topic=topic).\
                          all()

            if not result:
                raise exception.ComputeHostNotFound(host=host)

            return result

        context = req.environ['nova.context']
        services = []
        for service in service_get_all_compute(context):
            services.append(self._format_service(service))
        for service in db.service_get_all(context):
            if service.binary == 'nova-compute':
                continue
            services.append(self._format_service(service))
        return {'services': services}
Beispiel #56
0
def get_hosts():
    c = get_admin_context()
    return [service.host for service in db.service_get_all(c)]
Beispiel #57
0
 def index(self, req):
     context = req.environ['nova.context'].elevated()
     services = []
     for service in db.service_get_all(context):
         services.append(self._set_attr(service))
     return {'services': services}
Beispiel #58
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)
Beispiel #59
0
    def _get_audit_task_logs(self, context, begin=None, end=None, before=None):
        """Returns a full log for all instance usage audit tasks on all
           computes.

        :param begin: datetime beginning of audit period to get logs for,
            Defaults to the beginning of the most recently completed
            audit period prior to the 'before' date.
        :param end: datetime ending of audit period to get logs for,
            Defaults to the ending of the most recently completed
            audit period prior to the 'before' date.
        :param before: By default we look for the audit period most recently
            completed before this datetime. Has no effect if both begin and end
            are specified.
        """
        defbegin, defend = utils.last_completed_audit_period(before=before)
        if begin is None:
            begin = defbegin
        if end is None:
            end = defend
        task_logs = db.task_log_get_all(context, "instance_usage_audit", begin,
                                        end)
        # We do this this way to include disabled compute services,
        # which can have instances on them. (mdragon)
        services = [
            svc for svc in db.service_get_all(context)
            if svc['topic'] == 'compute'
        ]
        hosts = set(serv['host'] for serv in services)
        seen_hosts = set()
        done_hosts = set()
        running_hosts = set()
        total_errors = 0
        total_items = 0
        for tlog in task_logs:
            seen_hosts.add(tlog['host'])
            if tlog['state'] == "DONE":
                done_hosts.add(tlog['host'])
            if tlog['state'] == "RUNNING":
                running_hosts.add(tlog['host'])
            total_errors += tlog['errors']
            total_items += tlog['task_items']
        log = dict((tl['host'],
                    dict(state=tl['state'],
                         instances=tl['task_items'],
                         errors=tl['errors'],
                         message=tl['message'])) for tl in task_logs)
        missing_hosts = hosts - seen_hosts
        overall_status = "%s hosts done. %s errors." % (
            'ALL' if len(done_hosts) == len(hosts) else "%s of %s" %
            (len(done_hosts), len(hosts)), total_errors)
        return dict(period_beginning=str(begin),
                    period_ending=str(end),
                    num_hosts=len(hosts),
                    num_hosts_done=len(done_hosts),
                    num_hosts_running=len(running_hosts),
                    num_hosts_not_run=len(missing_hosts),
                    hosts_not_run=list(missing_hosts),
                    total_instances=total_items,
                    total_errors=total_errors,
                    overall_status=overall_status,
                    log=log)