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

« back to all changes in this revision

Viewing changes to hooks/services.py

  • Committer: James Page
  • Date: 2016-02-04 11:42:36 UTC
  • Revision ID: james.page@ubuntu.com-20160204114236-btikuqecvdku2v2a
Initial changes to use local nova-api-metadata service with neutron-metadata-agent

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
from charmhelpers.contrib.openstack import utils
19
19
from charmhelpers.core import hookenv
20
20
from charmhelpers.core import host
 
21
from charmhelpers.core import unitdata
21
22
from charmhelpers.core.services import base
22
23
from charmhelpers.core.services import helpers
23
24
from midonet_helpers import puppet
28
29
TEMPLATES = 'templates/'
29
30
 
30
31
 
31
 
class Config(dict):
 
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):
32
41
    def __init__(self, *args):
33
42
        self['config'] = hookenv.config()
 
43
        self['unit'] = {
 
44
            'shared-secret': get_shared_secret()
 
45
        }
34
46
 
35
47
 
36
48
class OSTRenderer(base.ManagerCallback):
37
 
    def __init__(self, amqp_ctxt, release=None):
 
49
    def __init__(self, dest, ctxts, release=None):
38
50
        release = release or utils.os_release('neutron-common')
39
51
        self._configs = templating.OSConfigRenderer(templates_dir=TEMPLATES,
40
52
                                                    openstack_release=release)
41
 
        self._configs.register('/etc/neutron/neutron.conf', [amqp_ctxt])
 
53
        self._configs.register(dest, ctxts)
42
54
 
43
55
    def __call__(self, manager, service_name, event_name):
44
56
        self._configs.write_all()
45
57
 
46
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
 
65
 
 
66
 
47
67
def manage():
48
68
    nova = relations.NovaRelation()
49
 
    config = Config()
 
69
    charm = Charm()
50
70
    hookenv.status_set('maintenance', 'reconfiguring charm')
51
 
    ost_origin = config['config'].get('openstack-origin')
52
 
    midonet_origin = config['config'].get('midonet-origin')
 
71
    ost_origin = charm['config'].get('openstack-origin')
 
72
    midonet_origin = charm['config'].get('midonet-origin')
53
73
    if None in (ost_origin, midonet_origin):
54
74
        hookenv.status_set('maintenance', 'Lacking repository configuration')
55
75
        return
56
76
    ost_release = utils.get_os_codename_install_source(ost_origin)
57
77
    try:
58
78
        repo = repositories.config('%s/%s' % (ost_release, midonet_origin),
59
 
                                   config['config'].get('mem-username'),
60
 
                                   config['config'].get('mem-password'))
 
79
                                   charm['config'].get('mem-username'),
 
80
                                   charm['config'].get('mem-password'))
61
81
    except ValueError as ver:
62
82
        hookenv.status_set('maintenance',
63
83
                           'Wrong repository configuration: {}'.format(ver))
64
84
        return
65
85
 
66
86
    amqp = context.AMQPContext()
 
87
    network_service = MidonetNetworkServiceContext(rel_name='neutron_agents')
67
88
    midonet = common_relations.MidonetApiRelation()
68
89
 
69
90
    manager = base.ServiceManager([
82
103
                    target='/etc/neutron/dhcp_agent.ini'),
83
104
                puppet.InstallPackagesCallback(
84
105
                    ('python-neutron-plugin-midonet',)),
85
 
                OSTRenderer(amqp),
 
106
                OSTRenderer('/etc/neutron/neutron.conf',
 
107
                            [amqp]),
86
108
            ],
87
109
        },
88
110
        {
89
111
            'service': 'neutron-metadata-agent',
90
 
            'required_data': [config,
 
112
            'required_data': [charm,
91
113
                              nova],
92
114
            'data_ready': [
93
115
                helpers.render_template(
95
117
                    target='/etc/neutron/metadata_agent.ini'),
96
118
            ],
97
119
        },
 
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
        },
98
129
    ])
99
130
    manager.manage()
100
131
    if (host.service_running('neutron-dhcp-agent') and
101
 
            host.service_running('neutron-metadata-agent')):
102
 
        hookenv.status_set('active', 'dhcp and metadata agents up and running')
 
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')