~ionutbalutoiu/charms/trusty/neutron-api/next

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/context.py

  • Committer: David Ames
  • Date: 2015-09-28 17:45:40 UTC
  • mfrom: (145 trunk)
  • mto: This revision was merged to the branch mainline in revision 146.
  • Revision ID: david.ames@canonical.com-20150928174540-wx0t0d3uwgmlsotb
PullĀ inĀ upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
194
194
class OSContextGenerator(object):
195
195
    """Base class for all context generators."""
196
196
    interfaces = []
 
197
    related = False
 
198
    complete = False
 
199
    missing_data = []
197
200
 
198
201
    def __call__(self):
199
202
        raise NotImplementedError
200
203
 
 
204
    def context_complete(self, ctxt):
 
205
        """Check for missing data for the required context data.
 
206
        Set self.missing_data if it exists and return False.
 
207
        Set self.complete if no missing data and return True.
 
208
        """
 
209
        # Fresh start
 
210
        self.complete = False
 
211
        self.missing_data = []
 
212
        for k, v in six.iteritems(ctxt):
 
213
            if v is None or v == '':
 
214
                if k not in self.missing_data:
 
215
                    self.missing_data.append(k)
 
216
 
 
217
        if self.missing_data:
 
218
            self.complete = False
 
219
            log('Missing required data: %s' % ' '.join(self.missing_data), level=INFO)
 
220
        else:
 
221
            self.complete = True
 
222
        return self.complete
 
223
 
 
224
    def get_related(self):
 
225
        """Check if any of the context interfaces have relation ids.
 
226
        Set self.related and return True if one of the interfaces
 
227
        has relation ids.
 
228
        """
 
229
        # Fresh start
 
230
        self.related = False
 
231
        try:
 
232
            for interface in self.interfaces:
 
233
                if relation_ids(interface):
 
234
                    self.related = True
 
235
            return self.related
 
236
        except AttributeError as e:
 
237
            log("{} {}"
 
238
                "".format(self, e), 'INFO')
 
239
            return self.related
 
240
 
201
241
 
202
242
class SharedDBContext(OSContextGenerator):
203
243
    interfaces = ['shared-db']
213
253
        self.database = database
214
254
        self.user = user
215
255
        self.ssl_dir = ssl_dir
 
256
        self.rel_name = self.interfaces[0]
216
257
 
217
258
    def __call__(self):
218
259
        self.database = self.database or config('database')
246
287
            password_setting = self.relation_prefix + '_password'
247
288
 
248
289
        for rid in relation_ids(self.interfaces[0]):
 
290
            self.related = True
249
291
            for unit in related_units(rid):
250
292
                rdata = relation_get(rid=rid, unit=unit)
251
293
                host = rdata.get('db_host')
257
299
                    'database_password': rdata.get(password_setting),
258
300
                    'database_type': 'mysql'
259
301
                }
260
 
                if context_complete(ctxt):
 
302
                if self.context_complete(ctxt):
261
303
                    db_ssl(rdata, ctxt, self.ssl_dir)
262
304
                    return ctxt
263
305
        return {}
278
320
 
279
321
        ctxt = {}
280
322
        for rid in relation_ids(self.interfaces[0]):
 
323
            self.related = True
281
324
            for unit in related_units(rid):
282
325
                rel_host = relation_get('host', rid=rid, unit=unit)
283
326
                rel_user = relation_get('user', rid=rid, unit=unit)
287
330
                        'database_user': rel_user,
288
331
                        'database_password': rel_passwd,
289
332
                        'database_type': 'postgresql'}
290
 
                if context_complete(ctxt):
 
333
                if self.context_complete(ctxt):
291
334
                    return ctxt
292
335
 
293
336
        return {}
348
391
            ctxt['signing_dir'] = cachedir
349
392
 
350
393
        for rid in relation_ids(self.rel_name):
 
394
            self.related = True
351
395
            for unit in related_units(rid):
352
396
                rdata = relation_get(rid=rid, unit=unit)
353
397
                serv_host = rdata.get('service_host')
366
410
                             'service_protocol': svc_protocol,
367
411
                             'auth_protocol': auth_protocol})
368
412
 
369
 
                if context_complete(ctxt):
 
413
                if self.context_complete(ctxt):
370
414
                    # NOTE(jamespage) this is required for >= icehouse
371
415
                    # so a missing value just indicates keystone needs
372
416
                    # upgrading
405
449
        ctxt = {}
406
450
        for rid in relation_ids(self.rel_name):
407
451
            ha_vip_only = False
 
452
            self.related = True
408
453
            for unit in related_units(rid):
409
454
                if relation_get('clustered', rid=rid, unit=unit):
410
455
                    ctxt['clustered'] = True
437
482
                ha_vip_only = relation_get('ha-vip-only',
438
483
                                           rid=rid, unit=unit) is not None
439
484
 
440
 
                if context_complete(ctxt):
 
485
                if self.context_complete(ctxt):
441
486
                    if 'rabbit_ssl_ca' in ctxt:
442
487
                        if not self.ssl_dir:
443
488
                            log("Charm not setup for ssl support but ssl ca "
469
514
            ctxt['oslo_messaging_flags'] = config_flags_parser(
470
515
                oslo_messaging_flags)
471
516
 
472
 
        if not context_complete(ctxt):
 
517
        if not self.complete:
473
518
            return {}
474
519
 
475
520
        return ctxt
485
530
 
486
531
        log('Generating template context for ceph', level=DEBUG)
487
532
        mon_hosts = []
488
 
        auth = None
489
 
        key = None
490
 
        use_syslog = str(config('use-syslog')).lower()
 
533
        ctxt = {
 
534
            'use_syslog': str(config('use-syslog')).lower()
 
535
        }
491
536
        for rid in relation_ids('ceph'):
492
537
            for unit in related_units(rid):
493
 
                auth = relation_get('auth', rid=rid, unit=unit)
494
 
                key = relation_get('key', rid=rid, unit=unit)
 
538
                if not ctxt.get('auth'):
 
539
                    ctxt['auth'] = relation_get('auth', rid=rid, unit=unit)
 
540
                if not ctxt.get('key'):
 
541
                    ctxt['key'] = relation_get('key', rid=rid, unit=unit)
495
542
                ceph_pub_addr = relation_get('ceph-public-address', rid=rid,
496
543
                                             unit=unit)
497
544
                unit_priv_addr = relation_get('private-address', rid=rid,
500
547
                ceph_addr = format_ipv6_addr(ceph_addr) or ceph_addr
501
548
                mon_hosts.append(ceph_addr)
502
549
 
503
 
        ctxt = {'mon_hosts': ' '.join(sorted(mon_hosts)),
504
 
                'auth': auth,
505
 
                'key': key,
506
 
                'use_syslog': use_syslog}
 
550
        ctxt['mon_hosts'] = ' '.join(sorted(mon_hosts))
507
551
 
508
552
        if not os.path.isdir('/etc/ceph'):
509
553
            os.mkdir('/etc/ceph')
510
554
 
511
 
        if not context_complete(ctxt):
 
555
        if not self.context_complete(ctxt):
512
556
            return {}
513
557
 
514
558
        ensure_packages(['ceph-common'])
1367
1411
                    'auth_protocol':
1368
1412
                    rdata.get('auth_protocol') or 'http',
1369
1413
                }
1370
 
                if context_complete(ctxt):
 
1414
                if self.context_complete(ctxt):
1371
1415
                    return ctxt
1372
1416
        return {}