~hopem/charms/trusty/ceph-radosgw/lp1520236

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: David Ames
  • Date: 2015-10-12 14:47:18 UTC
  • mfrom: (45.2.7 ceph-radosgw)
  • Revision ID: david.ames@canonical.com-20151012144718-ssne1hj0xu95irp8
[gnuoy, r=thedac] Workload status

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
from charmhelpers.core.hookenv import (
17
17
    relation_get,
18
18
    relation_ids,
19
 
    related_units,
20
19
    config,
21
20
    unit_get,
22
21
    open_port,
23
22
    relation_set,
24
23
    log, ERROR,
25
24
    Hooks, UnregisteredHookError,
 
25
    status_set,
26
26
)
27
27
from charmhelpers.fetch import (
28
28
    apt_update,
32
32
)
33
33
from charmhelpers.core.host import (
34
34
    lsb_release,
35
 
    restart_on_change
 
35
    restart_on_change,
36
36
)
37
37
from utils import (
38
38
    render_template,
39
 
    get_host_ip,
40
39
    enable_pocket,
41
40
    is_apache_24,
42
41
    CEPHRG_HA_RES,
43
42
    register_configs,
 
43
    REQUIRED_INTERFACES,
 
44
    check_optional_relations,
44
45
)
45
46
 
46
47
from charmhelpers.payload.execd import execd_preinstall
47
48
from charmhelpers.core.host import cmp_pkgrevno
48
 
from socket import gethostname as get_unit_hostname
49
49
 
50
50
from charmhelpers.contrib.network.ip import (
51
51
    get_iface_for_address,
55
55
    canonical_url,
56
56
    PUBLIC, INTERNAL, ADMIN,
57
57
)
58
 
 
 
58
from charmhelpers.contrib.openstack.utils import (
 
59
    set_os_workload_status,
 
60
)
59
61
hooks = Hooks()
60
62
CONFIGS = register_configs()
61
63
 
96
98
 
97
99
 
98
100
def install_packages():
 
101
    status_set('maintenance', 'Installing apt packages')
99
102
    add_source(config('source'), config('key'))
100
103
    if (config('use-ceph-optimised-packages') and
101
104
            not config('use-embedded-webserver')):
111
114
 
112
115
@hooks.hook('install.real')
113
116
def install():
 
117
    status_set('maintenance', 'Executing pre-install')
114
118
    execd_preinstall()
115
119
    enable_pocket('multiverse')
116
120
    install_packages()
117
121
    os.makedirs(NSS_DIR)
118
 
 
119
 
 
120
 
def emit_cephconf():
121
 
    # Ensure ceph directory actually exists
122
122
    if not os.path.exists('/etc/ceph'):
123
123
        os.makedirs('/etc/ceph')
124
124
 
125
 
    cephcontext = {
126
 
        'auth_supported': get_auth() or 'none',
127
 
        'mon_hosts': ' '.join(get_mon_hosts()),
128
 
        'hostname': get_unit_hostname(),
129
 
        'old_auth': cmp_pkgrevno('radosgw', "0.51") < 0,
130
 
        'use_syslog': str(config('use-syslog')).lower(),
131
 
        'embedded_webserver': config('use-embedded-webserver'),
132
 
    }
133
 
 
134
 
    # Check to ensure that correct version of ceph is
135
 
    # in use
136
 
    if cmp_pkgrevno('radosgw', '0.55') >= 0:
137
 
        # Add keystone configuration if found
138
 
        ks_conf = get_keystone_conf()
139
 
        if ks_conf:
140
 
            cephcontext.update(ks_conf)
141
 
 
142
 
    with open('/etc/ceph/ceph.conf', 'w') as cephconf:
143
 
        cephconf.write(render_template('ceph.conf', cephcontext))
144
 
 
145
125
 
146
126
def emit_apacheconf():
147
127
    apachecontext = {
181
161
                    '/etc/haproxy/haproxy.cfg': ['haproxy']})
182
162
def config_changed():
183
163
    install_packages()
184
 
    emit_cephconf()
185
164
    CONFIGS.write_all()
186
165
    if not config('use-embedded-webserver'):
 
166
        status_set('maintenance', 'configuring apache')
187
167
        emit_apacheconf()
188
168
        install_www_scripts()
189
169
        apache_sites()
194
174
        identity_joined(relid=r_id)
195
175
 
196
176
 
197
 
def get_mon_hosts():
198
 
    hosts = []
199
 
    for relid in relation_ids('mon'):
200
 
        for unit in related_units(relid):
201
 
            host_ip = get_host_ip(relation_get('ceph-public-address',
202
 
                                               unit, relid))
203
 
            hosts.append('{}:6789'.format(host_ip))
204
 
 
205
 
    hosts.sort()
206
 
    return hosts
207
 
 
208
 
 
209
 
def get_auth():
210
 
    return get_conf('auth')
211
 
 
212
 
 
213
 
def get_conf(name):
214
 
    for relid in relation_ids('mon'):
215
 
        for unit in related_units(relid):
216
 
            conf = relation_get(name,
217
 
                                unit, relid)
218
 
            if conf:
219
 
                return conf
220
 
    return None
221
 
 
222
 
 
223
 
def get_keystone_conf():
224
 
    for relid in relation_ids('identity-service'):
225
 
        for unit in related_units(relid):
226
 
            ks_auth = {
227
 
                'auth_type': 'keystone',
228
 
                'auth_protocol':
229
 
                relation_get('auth_protocol', unit, relid) or "http",
230
 
                'auth_host': relation_get('auth_host', unit, relid),
231
 
                'auth_port': relation_get('auth_port', unit, relid),
232
 
                'admin_token': relation_get('admin_token', unit, relid),
233
 
                'user_roles': config('operator-roles'),
234
 
                'cache_size': config('cache-size'),
235
 
                'revocation_check_interval':
236
 
                config('revocation-check-interval')
237
 
            }
238
 
            if None not in ks_auth.itervalues():
239
 
                return ks_auth
240
 
    return None
241
 
 
242
 
 
243
177
@hooks.hook('mon-relation-departed',
244
178
            'mon-relation-changed')
245
179
@restart_on_change({'/etc/ceph/ceph.conf': ['radosgw']})
246
180
def mon_relation():
247
 
    emit_cephconf()
 
181
    CONFIGS.write_all()
248
182
    key = relation_get('radosgw_key')
249
183
    if key:
250
184
        ceph.import_radosgw_key(key)
295
229
@hooks.hook('identity-service-relation-changed')
296
230
@restart_on_change({'/etc/ceph/ceph.conf': ['radosgw']})
297
231
def identity_changed():
298
 
    emit_cephconf()
 
232
    CONFIGS.write_all()
299
233
    restart()
300
234
 
301
235
 
377
311
        hooks.execute(sys.argv)
378
312
    except UnregisteredHookError as e:
379
313
        log('Unknown hook {} - skipping.'.format(e))
 
314
    set_os_workload_status(CONFIGS, REQUIRED_INTERFACES,
 
315
                           charm_func=check_optional_relations)