~heut2008/charms/trusty/ceph-radosgw/auth_protocol

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Marco Ceppi
  • Date: 2014-01-29 09:32:33 UTC
  • mfrom: (14.1.5 ceph-radosgw)
  • Revision ID: marco@ceppi.net-20140129093233-o7eva2xwuyp140p9
[james-page] Fixes for compatibility with apache 2.4
[james-page] General refresh to use charm-helpers inline with other ceph charms

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
import os
15
15
import ceph
16
16
 
17
 
import utils
 
17
from charmhelpers.core.hookenv import (
 
18
    relation_get,
 
19
    relation_ids,
 
20
    related_units,
 
21
    config,
 
22
    unit_get,
 
23
    open_port,
 
24
    relation_set,
 
25
    log,
 
26
    Hooks, UnregisteredHookError,
 
27
)
 
28
from charmhelpers.fetch import (
 
29
    apt_update,
 
30
    apt_install,
 
31
    add_source,
 
32
)
 
33
from utils import (
 
34
    render_template,
 
35
    get_host_ip,
 
36
    enable_pocket,
 
37
    is_apache_24
 
38
)
 
39
 
 
40
from charmhelpers.payload.execd import execd_preinstall
 
41
from socket import gethostname as get_unit_hostname
 
42
 
 
43
hooks = Hooks()
18
44
 
19
45
 
20
46
def install_www_scripts():
22
48
        shutil.copy(x, '/var/www/')
23
49
 
24
50
 
25
 
NSS_DIR='/var/lib/ceph/nss'
26
 
 
27
 
 
 
51
NSS_DIR = '/var/lib/ceph/nss'
 
52
 
 
53
 
 
54
@hooks.hook('install')
28
55
def install():
29
 
    utils.juju_log('INFO', 'Begin install hook.')
30
 
    utils.enable_pocket('multiverse')
31
 
    utils.configure_source()
32
 
    utils.install('radosgw',
33
 
                  'libapache2-mod-fastcgi',
34
 
                  'apache2',
35
 
                  'ntp')
 
56
    execd_preinstall()
 
57
    enable_pocket('multiverse')
 
58
    add_source(config('source'), config('key'))
 
59
    apt_update(fatal=True)
 
60
    apt_install(['radosgw',
 
61
                 'libapache2-mod-fastcgi',
 
62
                 'apache2',
 
63
                 'ntp'], fatal=True)
36
64
    os.makedirs(NSS_DIR)
37
 
    utils.juju_log('INFO', 'End install hook.')
38
65
 
39
66
 
40
67
def emit_cephconf():
45
72
    cephcontext = {
46
73
        'auth_supported': get_auth() or 'none',
47
74
        'mon_hosts': ' '.join(get_mon_hosts()),
48
 
        'hostname': utils.get_unit_hostname(),
 
75
        'hostname': get_unit_hostname(),
49
76
        'version': ceph.get_ceph_version('radosgw')
50
 
        }
51
 
    
52
 
    # Check to ensure that correct version of ceph is 
 
77
    }
 
78
 
 
79
    # Check to ensure that correct version of ceph is
53
80
    # in use
54
 
    if ceph.get_ceph_version('radosgw') >= "0.55":    
 
81
    if ceph.get_ceph_version('radosgw') >= "0.55":
55
82
        # Add keystone configuration if found
56
83
        ks_conf = get_keystone_conf()
57
84
        if ks_conf:
58
85
            cephcontext.update(ks_conf)
59
86
 
60
87
    with open('/etc/ceph/ceph.conf', 'w') as cephconf:
61
 
        cephconf.write(utils.render_template('ceph.conf', cephcontext))
 
88
        cephconf.write(render_template('ceph.conf', cephcontext))
62
89
 
63
90
 
64
91
def emit_apacheconf():
65
92
    apachecontext = {
66
 
        "hostname": utils.unit_get('private-address')
67
 
        }
68
 
    with open('/etc/apache2/sites-available/rgw', 'w') as apacheconf:
69
 
        apacheconf.write(utils.render_template('rgw', apachecontext))
 
93
        "hostname": unit_get('private-address')
 
94
    }
 
95
    site_conf = '/etc/apache2/sites-available/rgw'
 
96
    if is_apache_24():
 
97
        site_conf = '/etc/apache2/sites-available/rgw.conf'
 
98
    with open(site_conf, 'w') as apacheconf:
 
99
        apacheconf.write(render_template('rgw', apachecontext))
70
100
 
71
101
 
72
102
def apache_sites():
73
 
    utils.juju_log('INFO', 'Begin apache_sites.')
74
 
    subprocess.check_call(['a2dissite', 'default'])
 
103
    if is_apache_24():
 
104
        subprocess.check_call(['a2dissite', '000-default'])
 
105
    else:
 
106
        subprocess.check_call(['a2dissite', 'default'])
75
107
    subprocess.check_call(['a2ensite', 'rgw'])
76
 
    utils.juju_log('INFO', 'End apache_sites.')
77
108
 
78
109
 
79
110
def apache_modules():
80
 
    utils.juju_log('INFO', 'Begin apache_sites.')
81
111
    subprocess.check_call(['a2enmod', 'fastcgi'])
82
112
    subprocess.check_call(['a2enmod', 'rewrite'])
83
 
    utils.juju_log('INFO', 'End apache_sites.')
84
113
 
85
114
 
86
115
def apache_reload():
87
116
    subprocess.call(['service', 'apache2', 'reload'])
88
117
 
89
118
 
 
119
@hooks.hook('upgrade-charm',
 
120
            'config-changed')
90
121
def config_changed():
91
 
    utils.juju_log('INFO', 'Begin config-changed hook.')
92
122
    emit_cephconf()
93
123
    emit_apacheconf()
94
124
    install_www_scripts()
95
125
    apache_sites()
96
126
    apache_modules()
97
127
    apache_reload()
98
 
    utils.juju_log('INFO', 'End config-changed hook.')
99
128
 
100
129
 
101
130
def get_mon_hosts():
102
131
    hosts = []
103
 
    for relid in utils.relation_ids('mon'):
104
 
        for unit in utils.relation_list(relid):
 
132
    for relid in relation_ids('mon'):
 
133
        for unit in related_units(relid):
105
134
            hosts.append(
106
 
                '{}:6789'.format(utils.get_host_ip(
107
 
                                    utils.relation_get('private-address',
108
 
                                                       unit, relid)))
109
 
                )
 
135
                '{}:6789'.format(get_host_ip(
 
136
                    relation_get('private-address',
 
137
                                 unit, relid)))
 
138
            )
110
139
 
111
140
    hosts.sort()
112
141
    return hosts
117
146
 
118
147
 
119
148
def get_conf(name):
120
 
    for relid in utils.relation_ids('mon'):
121
 
        for unit in utils.relation_list(relid):
122
 
            conf = utils.relation_get(name,
123
 
                                      unit, relid)
 
149
    for relid in relation_ids('mon'):
 
150
        for unit in related_units(relid):
 
151
            conf = relation_get(name,
 
152
                                unit, relid)
124
153
            if conf:
125
154
                return conf
126
155
    return None
127
156
 
 
157
 
128
158
def get_keystone_conf():
129
 
    for relid in utils.relation_ids('identity-service'):
130
 
        for unit in utils.relation_list(relid):
 
159
    for relid in relation_ids('identity-service'):
 
160
        for unit in related_units(relid):
131
161
            ks_auth = {
132
162
                'auth_type': 'keystone',
133
163
                'auth_protocol': 'http',
134
 
                'auth_host': utils.relation_get('auth_host', unit, relid),
135
 
                'auth_port': utils.relation_get('auth_port', unit, relid),
136
 
                'admin_token': utils.relation_get('admin_token', unit, relid),
137
 
                'user_roles': utils.config_get('operator-roles'),
138
 
                'cache_size': utils.config_get('cache-size'),
139
 
                'revocation_check_interval': utils.config_get('revocation-check-interval')
 
164
                'auth_host': relation_get('auth_host', unit, relid),
 
165
                'auth_port': relation_get('auth_port', unit, relid),
 
166
                'admin_token': relation_get('admin_token', unit, relid),
 
167
                'user_roles': config('operator-roles'),
 
168
                'cache_size': config('cache-size'),
 
169
                'revocation_check_interval':
 
170
                config('revocation-check-interval')
140
171
            }
141
172
            if None not in ks_auth.itervalues():
142
173
                return ks_auth
143
174
    return None
144
175
 
145
176
 
 
177
@hooks.hook('mon-relation-departed',
 
178
            'mon-relation-changed')
146
179
def mon_relation():
147
 
    utils.juju_log('INFO', 'Begin mon-relation hook.')
148
180
    emit_cephconf()
149
 
    key = utils.relation_get('radosgw_key')
 
181
    key = relation_get('radosgw_key')
150
182
    if key:
151
183
        ceph.import_radosgw_key(key)
152
184
        restart()  # TODO figure out a better way todo this
153
 
    utils.juju_log('INFO', 'End mon-relation hook.')
154
 
 
155
 
 
 
185
 
 
186
 
 
187
@hooks.hook('gateway-relation-joined')
156
188
def gateway_relation():
157
 
    utils.juju_log('INFO', 'Begin gateway-relation hook.')
158
 
    utils.relation_set(hostname=utils.unit_get('private-address'),
159
 
                       port=80)
160
 
    utils.juju_log('INFO', 'Begin gateway-relation hook.')
161
 
 
162
 
 
163
 
def upgrade_charm():
164
 
    utils.juju_log('INFO', 'Begin upgrade-charm hook.')
165
 
    utils.juju_log('INFO', 'End upgrade-charm hook.')
 
189
    relation_set(hostname=unit_get('private-address'),
 
190
                 port=80)
166
191
 
167
192
 
168
193
def start():
169
194
    subprocess.call(['service', 'radosgw', 'start'])
170
 
    utils.expose(port=80)
 
195
    open_port(port=80)
171
196
 
172
197
 
173
198
def stop():
174
199
    subprocess.call(['service', 'radosgw', 'stop'])
175
 
    utils.expose(port=80)
 
200
    open_port(port=80)
176
201
 
177
202
 
178
203
def restart():
179
204
    subprocess.call(['service', 'radosgw', 'restart'])
180
 
    utils.expose(port=80)
181
 
 
182
 
 
 
205
    open_port(port=80)
 
206
 
 
207
 
 
208
@hooks.hook('identity-service-relation-joined',
 
209
            'identity-service-relation-changed')
183
210
def identity_joined(relid=None):
184
211
    if ceph.get_ceph_version('radosgw') < "0.55":
185
 
        utils.juju_log('ERROR',
186
 
                       'Integration with keystone requires ceph >= 0.55')
 
212
        log('Integration with keystone requires ceph >= 0.55')
187
213
        sys.exit(1)
188
214
 
189
 
    hostname = utils.unit_get('private-address')
 
215
    hostname = unit_get('private-address')
190
216
    admin_url = 'http://{}:80/swift'.format(hostname)
191
217
    internal_url = public_url = '{}/v1'.format(admin_url)
192
 
    utils.relation_set(service='swift',
193
 
                       region=utils.config_get('region'),
194
 
                       public_url=public_url, internal_url=internal_url,
195
 
                       admin_url=admin_url,
196
 
                       requested_roles=utils.config_get('operator-roles'),
197
 
                       rid=relid)
 
218
    relation_set(service='swift',
 
219
                 region=config('region'),
 
220
                 public_url=public_url, internal_url=internal_url,
 
221
                 admin_url=admin_url,
 
222
                 requested_roles=config('operator-roles'),
 
223
                 rid=relid)
198
224
 
199
225
 
200
226
def identity_changed():
201
227
    emit_cephconf()
202
 
    restart() 
203
 
 
204
 
 
205
 
utils.do_hooks({
206
 
        'install': install,
207
 
        'config-changed': config_changed,
208
 
        'mon-relation-departed': mon_relation,
209
 
        'mon-relation-changed': mon_relation,
210
 
        'gateway-relation-joined': gateway_relation,
211
 
        'upgrade-charm': config_changed,  # same function ATM
212
 
        'identity-service-relation-joined': identity_joined,
213
 
        'identity-service-relation-changed': identity_changed
214
 
        })
215
 
 
216
 
sys.exit(0)
 
228
    restart()
 
229
 
 
230
 
 
231
if __name__ == '__main__':
 
232
    try:
 
233
        hooks.execute(sys.argv)
 
234
    except UnregisteredHookError as e:
 
235
        log('Unknown hook {} - skipping.'.format(e))