~gandelman-a/charms/precise/nova-compute/unused_func

« back to all changes in this revision

Viewing changes to hooks/nova_compute_hooks.py

  • Committer: James Page
  • Date: 2013-10-15 12:04:13 UTC
  • mfrom: (46.1.83 nova-compute)
  • Revision ID: james.page@canonical.com-20131015120413-grclbw2ot5gbgp5r
Update of all Havana / Saucy / python-redux work:

* Full python rewrite using new OpenStack charm-helpers.

* Test coverage

* Havana support

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import sys
 
4
 
 
5
from charmhelpers.core.hookenv import (
 
6
    Hooks,
 
7
    config,
 
8
    log,
 
9
    relation_ids,
 
10
    relation_get,
 
11
    relation_set,
 
12
    service_name,
 
13
    unit_get,
 
14
    UnregisteredHookError,
 
15
)
 
16
 
 
17
from charmhelpers.core.host import (
 
18
    restart_on_change,
 
19
)
 
20
 
 
21
from charmhelpers.fetch import (
 
22
    apt_install,
 
23
    apt_update,
 
24
    filter_installed_packages,
 
25
)
 
26
 
 
27
from charmhelpers.contrib.openstack.utils import (
 
28
    configure_installation_source,
 
29
    openstack_upgrade_available,
 
30
)
 
31
 
 
32
from charmhelpers.contrib.storage.linux.ceph import ensure_ceph_keyring
 
33
from charmhelpers.contrib.openstack.neutron import neutron_plugin_attribute
 
34
from charmhelpers.payload.execd import execd_preinstall
 
35
 
 
36
from nova_compute_utils import (
 
37
    create_libvirt_secret,
 
38
    determine_packages,
 
39
    import_authorized_keys,
 
40
    import_keystone_ca_cert,
 
41
    initialize_ssh_keys,
 
42
    migration_enabled,
 
43
    network_manager,
 
44
    neutron_plugin,
 
45
    do_openstack_upgrade,
 
46
    public_ssh_key,
 
47
    restart_map,
 
48
    register_configs,
 
49
    NOVA_CONF,
 
50
    QUANTUM_CONF, NEUTRON_CONF,
 
51
    CEPH_CONF, CEPH_SECRET
 
52
)
 
53
 
 
54
from nova_compute_context import CEPH_SECRET_UUID
 
55
 
 
56
hooks = Hooks()
 
57
CONFIGS = register_configs()
 
58
 
 
59
 
 
60
@hooks.hook()
 
61
def install():
 
62
    execd_preinstall()
 
63
    configure_installation_source(config('openstack-origin'))
 
64
    apt_update()
 
65
    apt_install(determine_packages(), fatal=True)
 
66
 
 
67
 
 
68
@hooks.hook('config-changed')
 
69
@restart_on_change(restart_map())
 
70
def config_changed():
 
71
    if openstack_upgrade_available('nova-common'):
 
72
        do_openstack_upgrade(CONFIGS)
 
73
 
 
74
    if migration_enabled() and config('migration-auth-type') == 'ssh':
 
75
        # Check-in with nova-c-c and register new ssh key, if it has just been
 
76
        # generated.
 
77
        initialize_ssh_keys()
 
78
        [compute_joined(rid) for rid in relation_ids('cloud-compute')]
 
79
 
 
80
    CONFIGS.write_all()
 
81
 
 
82
 
 
83
@hooks.hook('amqp-relation-joined')
 
84
@restart_on_change(restart_map())
 
85
def amqp_joined():
 
86
    relation_set(username=config('rabbit-user'), vhost=config('rabbit-vhost'))
 
87
 
 
88
 
 
89
@hooks.hook('amqp-relation-changed')
 
90
@restart_on_change(restart_map())
 
91
def amqp_changed():
 
92
    if 'amqp' not in CONFIGS.complete_contexts():
 
93
        log('amqp relation incomplete. Peer not ready?')
 
94
        return
 
95
    CONFIGS.write(NOVA_CONF)
 
96
    if network_manager() == 'quantum':
 
97
        CONFIGS.write(QUANTUM_CONF)
 
98
    if network_manager() == 'neutron':
 
99
        CONFIGS.write(NEUTRON_CONF)
 
100
 
 
101
 
 
102
@hooks.hook('shared-db-relation-joined')
 
103
def db_joined(rid=None):
 
104
    relation_set(relation_id=rid,
 
105
                 nova_database=config('database'),
 
106
                 nova_username=config('database-user'),
 
107
                 nova_hostname=unit_get('private-address'))
 
108
    if network_manager() in ['quantum', 'neutron']:
 
109
        # XXX: Renaming relations from quantum_* to neutron_* here.
 
110
        relation_set(relation_id=rid,
 
111
                     neutron_database=config('neutron-database'),
 
112
                     neutron_username=config('neutron-database-user'),
 
113
                     neutron_hostname=unit_get('private-address'))
 
114
 
 
115
 
 
116
@hooks.hook('shared-db-relation-changed')
 
117
@restart_on_change(restart_map())
 
118
def db_changed():
 
119
    if 'shared-db' not in CONFIGS.complete_contexts():
 
120
        log('shared-db relation incomplete. Peer not ready?')
 
121
        return
 
122
    CONFIGS.write(NOVA_CONF)
 
123
    nm = network_manager()
 
124
    if nm in ['quantum', 'neutron']:
 
125
        plugin = neutron_plugin()
 
126
        CONFIGS.write(neutron_plugin_attribute(plugin, 'config', nm))
 
127
 
 
128
 
 
129
@hooks.hook('image-service-relation-changed')
 
130
@restart_on_change(restart_map())
 
131
def image_service_changed():
 
132
    if 'image-service' not in CONFIGS.complete_contexts():
 
133
        log('image-service relation incomplete. Peer not ready?')
 
134
        return
 
135
    CONFIGS.write(NOVA_CONF)
 
136
 
 
137
 
 
138
@hooks.hook('cloud-compute-relation-joined')
 
139
def compute_joined(rid=None):
 
140
    if not migration_enabled():
 
141
        return
 
142
    auth_type = config('migration-auth-type')
 
143
    settings = {
 
144
        'migration_auth_type': auth_type
 
145
    }
 
146
    if auth_type == 'ssh':
 
147
        settings['ssh_public_key'] = public_ssh_key()
 
148
    relation_set(relation_id=rid, **settings)
 
149
 
 
150
 
 
151
@hooks.hook('cloud-compute-relation-changed')
 
152
@restart_on_change(restart_map())
 
153
def compute_changed():
 
154
    # rewriting all configs to pick up possible net or vol manager
 
155
    # config advertised from controller.
 
156
    CONFIGS.write_all()
 
157
    import_authorized_keys()
 
158
    import_keystone_ca_cert()
 
159
    if network_manager() in ['quantum', 'neutron']:
 
160
        # in case we already have a database relation, need to request
 
161
        # access to the additional neutron database.
 
162
        [db_joined(rid) for rid in relation_ids('shared-db')]
 
163
 
 
164
 
 
165
@hooks.hook('ceph-relation-joined')
 
166
@restart_on_change(restart_map())
 
167
def ceph_joined():
 
168
    apt_install(filter_installed_packages(['ceph-common']), fatal=True)
 
169
 
 
170
 
 
171
@hooks.hook('ceph-relation-changed')
 
172
@restart_on_change(restart_map())
 
173
def ceph_changed():
 
174
    if 'ceph' not in CONFIGS.complete_contexts():
 
175
        log('ceph relation incomplete. Peer not ready?')
 
176
        return
 
177
    svc = service_name()
 
178
    if not ensure_ceph_keyring(service=svc):
 
179
        log('Could not create ceph keyring: peer not ready?')
 
180
        return
 
181
    CONFIGS.write(CEPH_CONF)
 
182
    CONFIGS.write(CEPH_SECRET)
 
183
    CONFIGS.write(NOVA_CONF)
 
184
 
 
185
    # With some refactoring, this can move into NovaComputeCephContext
 
186
    # and allow easily extended to support other compute flavors.
 
187
    if config('virt-type') in ['kvm', 'qemu', 'lxc']:
 
188
        create_libvirt_secret(secret_file=CEPH_SECRET,
 
189
                              secret_uuid=CEPH_SECRET_UUID,
 
190
                              key=relation_get('key'))
 
191
 
 
192
 
 
193
@hooks.hook('amqp-relation-broken',
 
194
            'ceph-relation-broken',
 
195
            'image-service-relation-broken',
 
196
            'shared-db-relation-broken')
 
197
@restart_on_change(restart_map())
 
198
def relation_broken():
 
199
    CONFIGS.write_all()
 
200
 
 
201
 
 
202
def main():
 
203
    try:
 
204
        hooks.execute(sys.argv)
 
205
    except UnregisteredHookError as e:
 
206
        log('Unknown hook {} - skipping.'.format(e))
 
207
 
 
208
 
 
209
if __name__ == '__main__':
 
210
    main()