~ubuntu-branches/ubuntu/utopic/ironic/utopic

« back to all changes in this revision

Viewing changes to ironic/openstack/common/rpc/matchmaker_ring.py

  • Committer: Package Import Robot
  • Author(s): Adam Gandelman, Adam Gandelman, James Page
  • Date: 2014-09-30 10:44:08 UTC
  • mfrom: (1.2.1) (6.1.1 utopic-proposed)
  • Revision ID: package-import@ubuntu.com-20140930104408-k3z2yaikfvmpkptd
Tags: 2014.2~rc1-0ubuntu1
[ Adam Gandelman ]
* New upstream release.
* debian/patches/set_logdir.patch: Renamed to set_config_defaults.patch,
  also set default sqlite db connection.
* debian/control: Refreshed dependencies for Juno, wrap-and-sort.
* debian/ironic-common.install: Added ironic-nova-bm-migrate binary.
* debian/ironic-common.postinst: Create the default sqlite database if
  configured to use it and it does not exist.
* debian/pydist-overrides: Add pysendfile.
* debian/ironic_sudoers: Add rootwrap.conf (LP: #1185019).

[ James Page ]
* d/rules,control: Increase test verbosity using subunit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#    Copyright 2011-2013 Cloudscaling Group, Inc
2
 
#
3
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
4
 
#    not use this file except in compliance with the License. You may obtain
5
 
#    a copy of the License at
6
 
#
7
 
#         http://www.apache.org/licenses/LICENSE-2.0
8
 
#
9
 
#    Unless required by applicable law or agreed to in writing, software
10
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
 
#    License for the specific language governing permissions and limitations
13
 
#    under the License.
14
 
 
15
 
"""
16
 
The MatchMaker classes should except a Topic or Fanout exchange key and
17
 
return keys for direct exchanges, per (approximate) AMQP parlance.
18
 
"""
19
 
 
20
 
import itertools
21
 
import json
22
 
 
23
 
from oslo.config import cfg
24
 
 
25
 
from ironic.openstack.common.gettextutils import _
26
 
from ironic.openstack.common import log as logging
27
 
from ironic.openstack.common.rpc import matchmaker as mm
28
 
 
29
 
 
30
 
matchmaker_opts = [
31
 
    # Matchmaker ring file
32
 
    cfg.StrOpt('ringfile',
33
 
               deprecated_name='matchmaker_ringfile',
34
 
               deprecated_group='DEFAULT',
35
 
               default='/etc/oslo/matchmaker_ring.json',
36
 
               help='Matchmaker ring file (JSON)'),
37
 
]
38
 
 
39
 
CONF = cfg.CONF
40
 
CONF.register_opts(matchmaker_opts, 'matchmaker_ring')
41
 
LOG = logging.getLogger(__name__)
42
 
 
43
 
 
44
 
class RingExchange(mm.Exchange):
45
 
    """Match Maker where hosts are loaded from a static JSON formatted file.
46
 
 
47
 
    __init__ takes optional ring dictionary argument, otherwise
48
 
    loads the ringfile from CONF.mathcmaker_ringfile.
49
 
    """
50
 
    def __init__(self, ring=None):
51
 
        super(RingExchange, self).__init__()
52
 
 
53
 
        if ring:
54
 
            self.ring = ring
55
 
        else:
56
 
            with open(CONF.matchmaker_ring.ringfile, 'r') as fh:
57
 
                self.ring = json.load(fh)
58
 
 
59
 
        self.ring0 = {}
60
 
        for k in self.ring.keys():
61
 
            self.ring0[k] = itertools.cycle(self.ring[k])
62
 
 
63
 
    def _ring_has(self, key):
64
 
        return key in self.ring0
65
 
 
66
 
 
67
 
class RoundRobinRingExchange(RingExchange):
68
 
    """A Topic Exchange based on a hashmap."""
69
 
    def __init__(self, ring=None):
70
 
        super(RoundRobinRingExchange, self).__init__(ring)
71
 
 
72
 
    def run(self, key):
73
 
        if not self._ring_has(key):
74
 
            LOG.warn(
75
 
                _("No key defining hosts for topic '%s', "
76
 
                  "see ringfile") % (key, )
77
 
            )
78
 
            return []
79
 
        host = next(self.ring0[key])
80
 
        return [(key + '.' + host, host)]
81
 
 
82
 
 
83
 
class FanoutRingExchange(RingExchange):
84
 
    """Fanout Exchange based on a hashmap."""
85
 
    def __init__(self, ring=None):
86
 
        super(FanoutRingExchange, self).__init__(ring)
87
 
 
88
 
    def run(self, key):
89
 
        # Assume starts with "fanout~", strip it for lookup.
90
 
        nkey = key.split('fanout~')[1:][0]
91
 
        if not self._ring_has(nkey):
92
 
            LOG.warn(
93
 
                _("No key defining hosts for topic '%s', "
94
 
                  "see ringfile") % (nkey, )
95
 
            )
96
 
            return []
97
 
        return map(lambda x: (key + '.' + x, x), self.ring[nkey])
98
 
 
99
 
 
100
 
class MatchMakerRing(mm.MatchMakerBase):
101
 
    """Match Maker where hosts are loaded from a static hashmap."""
102
 
    def __init__(self, ring=None):
103
 
        super(MatchMakerRing, self).__init__()
104
 
        self.add_binding(mm.FanoutBinding(), FanoutRingExchange(ring))
105
 
        self.add_binding(mm.DirectBinding(), mm.DirectExchange())
106
 
        self.add_binding(mm.TopicBinding(), RoundRobinRingExchange(ring))