~celebdor/charms/trusty/neutron-agents-midonet/trunk

« back to all changes in this revision

Viewing changes to hooks/services.py

  • Committer: Antoni Segura Puimedon
  • Date: 2016-01-28 00:56:30 UTC
  • Revision ID: toni@midokura.com-20160128005630-z9ijncumfh90grxf
make amqp relation joined executable

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
15
#
 
16
import functools
 
17
 
16
18
from charmhelpers.contrib.openstack import context
17
19
from charmhelpers.contrib.openstack import templating
18
20
from charmhelpers.contrib.openstack import utils
19
21
from charmhelpers.core import hookenv
20
22
from charmhelpers.core import host
21
 
from charmhelpers.core import unitdata
22
23
from charmhelpers.core.services import base
23
24
from charmhelpers.core.services import helpers
24
25
from midonet_helpers import puppet
29
30
TEMPLATES = 'templates/'
30
31
 
31
32
 
32
 
def get_shared_secret():
33
 
    db = unitdata.kv()
34
 
    if not db.get('shared-secret'):
35
 
        db.set('shared-secret', host.pwgen(32))
36
 
        db.flush()
37
 
    return db.get('shared-secret')
38
 
 
39
 
 
40
 
class Charm(dict):
 
33
class Config(dict):
41
34
    def __init__(self, *args):
42
35
        self['config'] = hookenv.config()
43
 
        self['unit'] = {
44
 
            'shared-secret': get_shared_secret()
45
 
        }
46
 
 
47
 
 
48
 
class OSTRenderer(base.ManagerCallback):
49
 
    def __init__(self, dest, ctxts, release=None):
50
 
        release = release or utils.os_release('neutron-common')
51
 
        self._configs = templating.OSConfigRenderer(templates_dir=TEMPLATES,
52
 
                                                    openstack_release=release)
53
 
        self._configs.register(dest, ctxts)
54
 
 
55
 
    def __call__(self, manager, service_name, event_name):
56
 
        self._configs.write_all()
57
 
 
58
 
 
59
 
class MidonetNetworkServiceContext(context.NetworkServiceContext):
60
 
 
61
 
    def __call__(self):
62
 
        ctxt = super(MidonetNetworkServiceContext, self).__call__()
63
 
        ctxt['shared_secret'] = get_shared_secret()
64
 
        return ctxt
 
36
 
 
37
 
 
38
def register_ost_configs(amqp_ctxt, release=None):
 
39
    release = release or utils.os_release('neutron-common')
 
40
    configs = templating.OSConfigRenderer(templates_dir=TEMPLATES,
 
41
                                          openstack_release=release)
 
42
    configs.register('/etc/neutron/neutron.conf', [amqp_ctxt])
 
43
    return configs
65
44
 
66
45
 
67
46
def manage():
68
47
    nova = relations.NovaRelation()
69
 
    charm = Charm()
 
48
    config = Config()
70
49
    hookenv.status_set('maintenance', 'reconfiguring charm')
71
 
    ost_origin = charm['config'].get('openstack-origin')
72
 
    midonet_origin = charm['config'].get('midonet-origin')
 
50
    ost_origin = config['config'].get('openstack-origin')
 
51
    midonet_origin = config['config'].get('midonet-origin')
73
52
    if None in (ost_origin, midonet_origin):
74
53
        hookenv.status_set('maintenance', 'Lacking repository configuration')
75
54
        return
76
55
    ost_release = utils.get_os_codename_install_source(ost_origin)
77
56
    try:
78
57
        repo = repositories.config('%s/%s' % (ost_release, midonet_origin),
79
 
                                   charm['config'].get('mem-username'),
80
 
                                   charm['config'].get('mem-password'))
 
58
                                   config['config'].get('mem-username'),
 
59
                                   config['config'].get('mem-password'))
81
60
    except ValueError as ver:
82
61
        hookenv.status_set('maintenance',
83
62
                           'Wrong repository configuration: {}'.format(ver))
84
63
        return
85
64
 
86
65
    amqp = context.AMQPContext()
87
 
    network_service = MidonetNetworkServiceContext(rel_name='neutron_agents')
 
66
    ost_cfgs = register_ost_configs(amqp)
88
67
    midonet = common_relations.MidonetApiRelation()
89
68
 
90
69
    manager = base.ServiceManager([
99
78
                    source='common.yaml',
100
79
                    target='/var/lib/hiera/neutron_agents/common.yaml'),
101
80
                helpers.render_template(
 
81
                    source='neutron.conf',
 
82
                    target='/etc/neutron/neutron.conf'),
 
83
                helpers.render_template(
102
84
                    source='dhcp_agent.ini',
103
85
                    target='/etc/neutron/dhcp_agent.ini'),
104
86
                puppet.InstallPackagesCallback(
105
87
                    ('python-neutron-plugin-midonet',)),
106
 
                OSTRenderer('/etc/neutron/neutron.conf',
107
 
                            [amqp]),
 
88
                functools.partial(ost_cfgs.write_all),
108
89
            ],
109
90
        },
110
91
        {
111
92
            'service': 'neutron-metadata-agent',
112
 
            'required_data': [charm,
 
93
            'required_data': [config,
113
94
                              nova],
114
95
            'data_ready': [
115
96
                helpers.render_template(
117
98
                    target='/etc/neutron/metadata_agent.ini'),
118
99
            ],
119
100
        },
120
 
        {
121
 
            'service': 'nova-api-metadata',
122
 
            'required_data': [network_service(),
123
 
                              amqp()],
124
 
            'data_ready': [
125
 
                OSTRenderer('/etc/nova/nova.conf',
126
 
                            [network_service, amqp]),
127
 
            ],
128
 
        },
129
101
    ])
130
102
    manager.manage()
131
103
    if (host.service_running('neutron-dhcp-agent') and
132
 
            host.service_running('neutron-metadata-agent') and
133
 
            host.service_running('nova-api-metadata')):
134
 
        hookenv.status_set('active', 'All nova and neutron agents up and running')
 
104
            host.service_running('neutron-metadata-agent')):
 
105
        hookenv.status_set('active', 'dhcp and metadata agents up and running')